aboutsummaryrefslogtreecommitdiff
path: root/lib/ssh/daemon.ex
blob: 34cd4b92d931223c396b9a264a6a2881b8b1fdec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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