aboutsummaryrefslogtreecommitdiff
path: root/lib/ssh/daemon.ex
diff options
context:
space:
mode:
Diffstat (limited to 'lib/ssh/daemon.ex')
-rw-r--r--lib/ssh/daemon.ex43
1 files changed, 43 insertions, 0 deletions
diff --git a/lib/ssh/daemon.ex b/lib/ssh/daemon.ex
new file mode 100644
index 0000000..34cd4b9
--- /dev/null
+++ b/lib/ssh/daemon.ex
@@ -0,0 +1,43 @@
+defmodule TrongleSSH.SSH.Daemon do
+ alias TrongleSSH.SSH.ConnectionCLIManager
+ use GenServer
+ require Logger
+
+ defmodule State do
+ defstruct daemon_pid: nil
+ end
+
+ def start_link(_) do
+ GenServer.start_link(__MODULE__, %State{})
+ end
+
+ def init(%State{} = state) do
+ send(self(), :start)
+
+ {:ok, state}
+ end
+
+ def handle_info(:start, %State{} = state) do
+ port = Application.fetch_env!(:tronglessh, :ssh_port)
+ key_dir = String.to_charlist(Application.fetch_env!(:tronglessh, :key_dir))
+
+ case :ssh.daemon(
+ port,
+ system_dir: key_dir,
+ ssh_cli: {ConnectionCLIManager, [%ConnectionCLIManager.State{}]},
+ no_auth_needed: true,
+ id_string: :random,
+ parallel_login: true,
+ subsystems: []
+ ) do
+ {:ok, daemon_pid} ->
+ Logger.info("SSH server started on port #{port}, on #{inspect(daemon_pid)}")
+ {:noreply, %State{state | daemon_pid: daemon_pid}}
+
+ {:error, err} ->
+ raise inspect(err)
+ end
+ end
+
+ def handle_info(_, state), do: {:ok, state}
+end