aboutsummaryrefslogtreecommitdiff
path: root/lib/ssh/daemon.ex
diff options
context:
space:
mode:
authorElizabeth Hunt <elizabeth.hunt@simponic.xyz>2023-10-01 20:41:48 -0600
committerElizabeth Hunt <elizabeth.hunt@simponic.xyz>2023-10-01 20:41:48 -0600
commitffa3d553f693494c218a0244c946ba83dfbf7cd1 (patch)
tree092a824dad118c37960ab67da98cdbc445a35d2b /lib/ssh/daemon.ex
downloadtronglessh-main.tar.gz
tronglessh-main.zip
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