diff options
| author | Simponic <loganhunt@simponic.xyz> | 2022-12-20 01:06:18 -0700 |
|---|---|---|
| committer | Simponic <loganhunt@simponic.xyz> | 2022-12-21 17:21:36 -0700 |
| commit | fed5d8a9e7b9500d317f2d8664cfe8ef4138154e (patch) | |
| tree | f3d084146c2d410a4c1c8d95398599ceb1c8094f /lib/chessh/shell.ex | |
| parent | b18899ffaffad71a2b2f8783d97bcab876ab22a1 (diff) | |
| download | chessh-fed5d8a9e7b9500d317f2d8664cfe8ef4138154e.tar.gz chessh-fed5d8a9e7b9500d317f2d8664cfe8ef4138154e.zip | |
Add a simple echo server
Diffstat (limited to 'lib/chessh/shell.ex')
| -rw-r--r-- | lib/chessh/shell.ex | 51 |
1 files changed, 42 insertions, 9 deletions
diff --git a/lib/chessh/shell.ex b/lib/chessh/shell.ex index f9f475d..cf65ad4 100644 --- a/lib/chessh/shell.ex +++ b/lib/chessh/shell.ex @@ -1,18 +1,51 @@ defmodule Chessh.Shell do use Sshd.ShellHandler - def on_shell(_username, _public_key, _ip, _port) do - :ok = - IO.puts( - "Interactive example SSH shell - type exit ENTER to quit and it is running on #{inspect(self())}" - ) + def on_shell(_username, _pubkey, _ip, _port) do + IO.puts("Looks like you're on #{inspect(self())}") + loop() end - def on_connect(_username, _ip, _port, _method) do - Logger.debug("Connection established") + def on_connect(username, ip, port, method) do + Logger.debug(fn -> + """ + Incoming SSH shell #{inspect(self())} requested for #{username} from #{inspect(ip)}:#{inspect(port)} using #{inspect(method)} + """ + end) end - def on_disconnect(_username, _ip, _port) do - Logger.debug("Connection disestablished") + def on_disconnect(username, ip, port) do + Logger.debug(fn -> + "Disconnecting SSH shell for #{username} from #{inspect(ip)}:#{inspect(port)}" + end) + end + + defp loop() do + self_pid = self() + IO.write([IO.ANSI.home(), IO.ANSI.clear()]) + IO.puts("#{inspect(:io.columns())}") + IO.puts("#{inspect(:io.rows())}") + input = spawn(fn -> io_get(self_pid) end) + wait_input(input) + end + + defp wait_input(input) do + receive do + {:hello, message} -> + IO.puts(message) + loop() + + {:input, ^input, x} -> + IO.puts(x) + loop() + + x -> + Logger.debug(inspect(x)) + loop() + end + end + + defp io_get(pid) do + send(pid, {:input, self(), IO.gets("")}) end end |
