diff options
author | Simponic <loganhunt@simponic.xyz> | 2022-12-31 02:29:38 -0700 |
---|---|---|
committer | Simponic <loganhunt@simponic.xyz> | 2022-12-31 02:31:58 -0700 |
commit | 58d0b1a89c461467c9ea6229f9a6b3d5ed573da5 (patch) | |
tree | f0e91ce7bccb93e755357395f1ed7a7a18d6257b /lib/chessh/ssh | |
parent | 3308036c0859dcca2d80d80396a19a3cca269e78 (diff) | |
download | chessh-58d0b1a89c461467c9ea6229f9a6b3d5ed573da5.tar.gz chessh-58d0b1a89c461467c9ea6229f9a6b3d5ed573da5.zip |
A simple stalling TUI! Also, ensure sessions are counted correctly. Next up, some way of pub-sub across multiple nodes
Diffstat (limited to 'lib/chessh/ssh')
-rw-r--r-- | lib/chessh/ssh/cli.ex | 26 | ||||
-rw-r--r-- | lib/chessh/ssh/client.ex | 16 | ||||
-rw-r--r-- | lib/chessh/ssh/daemon.ex | 19 | ||||
-rw-r--r-- | lib/chessh/ssh/tui.ex | 163 |
4 files changed, 180 insertions, 44 deletions
diff --git a/lib/chessh/ssh/cli.ex b/lib/chessh/ssh/cli.ex deleted file mode 100644 index 71d789b..0000000 --- a/lib/chessh/ssh/cli.ex +++ /dev/null @@ -1,26 +0,0 @@ -defmodule Chessh.SSH.Cli do - @behaviour :ssh_server_channel - - def init(_args) do - {:ok, %{}} - end - - def handle_msg(_message, state) do - {:ok, state} - end - - def handle_ssh_msg( - {:ssh_cm, _connection_handler, {:exit_signal, channel_id, _signal, _err, _lang}}, - state - ) do - {:stop, channel_id, state} - end - - def handle_ssh_msg(_message, state) do - {:ok, state} - end - - def terminate(_reason, _state) do - :ok - end -end diff --git a/lib/chessh/ssh/client.ex b/lib/chessh/ssh/client.ex new file mode 100644 index 0000000..35893f1 --- /dev/null +++ b/lib/chessh/ssh/client.ex @@ -0,0 +1,16 @@ +defmodule Chessh.SSH.Client do + alias Chessh.SSH.Client + require Logger + + use GenServer + + # TODO: tui_state_stack is like [:menu, :player_settings, :change_password] or [:menu, {:game, game_id}, {:game_chat, game_id}] + + defstruct [:tui_pid, :width, :height, :player_id, :tui_state_stack] + + @impl true + def init([tui_pid, width, height] = args) do + Logger.debug("#{inspect(args)}") + {:ok, %Client{tui_pid: tui_pid, width: width, height: height}} + end +end diff --git a/lib/chessh/ssh/daemon.ex b/lib/chessh/ssh/daemon.ex index b341833..24ad259 100644 --- a/lib/chessh/ssh/daemon.ex +++ b/lib/chessh/ssh/daemon.ex @@ -61,7 +61,7 @@ defmodule Chessh.SSH.Daemon do system_dir: key_dir, pwdfun: &pwd_authenticate/4, key_cb: Chessh.SSH.ServerKey, - ssh_cli: {Chessh.SSH.Cli, []}, + ssh_cli: {Chessh.SSH.Tui, []}, # connectfun: &on_connect/3, disconnectfun: &on_disconnect/1, id_string: :random, @@ -82,23 +82,6 @@ defmodule Chessh.SSH.Daemon do def handle_info(_, state), do: {:noreply, state} - # defp on_connect(username, _inet, _method) do - # Logger.debug("#{inspect(self())} connected and is authenticated as #{username}") - # - # case Repo.get_by(Player, username: String.Chars.to_string(username)) do - # nil -> - # nil - # - # player -> - # Repo.insert(%PlayerSession{ - # login: DateTime.utc_now(), - # node_id: System.fetch_env!("NODE_ID"), - # player: player, - # process: pid_to_str(self()) - # }) - # end - # end - defp on_disconnect(_reason) do Logger.debug("#{inspect(self())} disconnected") diff --git a/lib/chessh/ssh/tui.ex b/lib/chessh/ssh/tui.ex new file mode 100644 index 0000000..78360a9 --- /dev/null +++ b/lib/chessh/ssh/tui.ex @@ -0,0 +1,163 @@ +defmodule Chessh.SSH.Tui do + require Logger + + @behaviour :ssh_server_channel + + def init(opts) do + Logger.debug("#{inspect(opts)}") + + {:ok, + %{ + channel: nil, + cm: nil, + pty: %{term: nil, width: nil, height: nil, pixwidth: nil, pixheight: nil, modes: nil}, + shell: false, + client_pid: nil + }} + end + + @spec handle_msg(any, any) :: + :ok + | {:ok, atom | %{:channel => any, :cm => any, optional(any) => any}} + | {:stop, any, %{:channel => any, :client_pid => any, optional(any) => any}} + def handle_msg({:ssh_channel_up, channel_id, connection_handler}, state) do + Logger.debug( + "SSH CHANNEL UP #{inspect(connection_handler)} #{inspect(:ssh.connection_info(connection_handler))}" + ) + + {:ok, %{state | channel: channel_id, cm: connection_handler}} + end + + def handle_msg({:EXIT, client_pid, _reason}, %{client_pid: client_pid} = state) do + {:stop, state.channel, state} + end + + ### commands we expect from the client ### + def handle_msg({:send_data, data}, state) do + Logger.debug("DATA SENT #{inspect(data)}") + :ssh_connection.send(state.cm, state.channel, data) + {:ok, state} + end + + ### catch all for what we haven't seen ### + def handle_msg(msg, term) do + Logger.debug("Unknown msg #{inspect(msg)}, #{inspect(term)}") + end + + def handle_ssh_msg( + {:ssh_cm, _connection_handler, {:data, _channel_id, _type, data}}, + state + ) do + Logger.debug("DATA #{inspect(data)}") + send(state.client_pid, {:data, data}) + {:ok, state} + end + + def handle_ssh_msg( + {:ssh_cm, connection_handler, + {:pty, channel_id, want_reply?, {term, width, height, pixwidth, pixheight, modes} = _pty}}, + state + ) do + :ssh_connection.reply_request(connection_handler, want_reply?, :success, channel_id) + + {:ok, + %{ + state + | pty: %{ + term: term, + width: width, + height: height, + pixwidth: pixwidth, + pixheight: pixheight, + modes: modes + } + }} + end + + def handle_ssh_msg( + {:ssh_cm, connection_handler, {:env, channel_id, want_reply?, var, value}}, + state + ) do + :ssh_connection.reply_request(connection_handler, want_reply?, :failure, channel_id) + Logger.debug("ENV #{var} = #{value}") + {:ok, state} + end + + def handle_ssh_msg( + {:ssh_cm, _connection_handler, + {:window_change, _channel_id, width, height, pixwidth, pixheight}}, + state + ) do + Logger.debug("WINDOW CHANGE") + # SSHnakes.Client.resize(state.client_pid, width, height) + + {:ok, + %{ + state + | pty: %{ + state.pty + | width: width, + height: height, + pixwidth: pixwidth, + pixheight: pixheight + } + }} + end + + def handle_ssh_msg( + {:ssh_cm, connection_handler, {:shell, channel_id, want_reply?}}, + state + ) do + :ssh_connection.reply_request(connection_handler, want_reply?, :success, channel_id) + + {:ok, client_pid} = + GenServer.start_link(Chessh.SSH.Client, [self(), state.pty.width, state.pty.height]) + + {:ok, %{state | client_pid: client_pid, shell: true}} + end + + def handle_ssh_msg( + {:ssh_cm, connection_handler, {:exec, channel_id, want_reply?, cmd}}, + state + ) do + :ssh_connection.reply_request(connection_handler, want_reply?, :success, channel_id) + Logger.debug("EXEC #{cmd}") + {:ok, state} + end + + def handle_ssh_msg( + {:ssh_cm, _connection_handler, {:eof, _channel_id}}, + state + ) do + Logger.debug("EOF") + {:ok, state} + end + + def handle_ssh_msg( + {:ssh_cm, _connection_handler, {:signal, _channel_id, signal}}, + state + ) do + Logger.debug("SIGNAL #{signal}") + {:ok, state} + end + + def handle_ssh_msg( + {:ssh_cm, _connection_handler, {:exit_signal, channel_id, signal, err, lang}}, + state + ) do + Logger.debug("EXIT SIGNAL #{signal} #{err} #{lang}") + {:stop, channel_id, state} + end + + def handle_ssh_msg( + {:ssh_cm, _connection_handler, {:exit_STATUS, channel_id, status}}, + state + ) do + Logger.debug("EXIT STATUS #{status}") + {:stop, channel_id, state} + end + + def terminate(_reason, _state) do + :ok + end +end |