diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/chessh/auth/password.ex | 1 | ||||
-rw-r--r-- | lib/chessh/schema/player.ex | 8 | ||||
-rw-r--r-- | lib/chessh/shell.ex | 51 |
3 files changed, 46 insertions, 14 deletions
diff --git a/lib/chessh/auth/password.ex b/lib/chessh/auth/password.ex index 48a4d62..e2c26d6 100644 --- a/lib/chessh/auth/password.ex +++ b/lib/chessh/auth/password.ex @@ -2,7 +2,6 @@ defmodule Chessh.Auth.PasswordAuthenticator do alias Chessh.Player alias Chessh.Repo use Sshd.PasswordAuthenticator - require Logger def authenticate(username, password) do case Repo.get_by(Player, username: String.Chars.to_string(username)) do diff --git a/lib/chessh/schema/player.ex b/lib/chessh/schema/player.ex index 5b7b904..4b6a324 100644 --- a/lib/chessh/schema/player.ex +++ b/lib/chessh/schema/player.ex @@ -14,15 +14,15 @@ defmodule Chessh.Player do timestamps() end - def registration_changeset(user, attrs, opts \\ []) do - user + def registration_changeset(player, attrs, opts \\ []) do + player |> cast(attrs, [:username, :password]) |> validate_username() |> validate_password(opts) end - def password_changeset(user, attrs, opts \\ []) do - user + def password_changeset(player, attrs, opts \\ []) do + player |> cast(attrs, [:password]) |> validate_confirmation(:password, message: "does not match password") |> validate_password(opts) 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 |