Add a simple echo server
This commit is contained in:
parent
b18899ffaf
commit
fed5d8a9e7
@ -2,7 +2,6 @@ defmodule Chessh.Auth.PasswordAuthenticator do
|
|||||||
alias Chessh.Player
|
alias Chessh.Player
|
||||||
alias Chessh.Repo
|
alias Chessh.Repo
|
||||||
use Sshd.PasswordAuthenticator
|
use Sshd.PasswordAuthenticator
|
||||||
require Logger
|
|
||||||
|
|
||||||
def authenticate(username, password) do
|
def authenticate(username, password) do
|
||||||
case Repo.get_by(Player, username: String.Chars.to_string(username)) do
|
case Repo.get_by(Player, username: String.Chars.to_string(username)) do
|
||||||
|
@ -14,15 +14,15 @@ defmodule Chessh.Player do
|
|||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
|
||||||
def registration_changeset(user, attrs, opts \\ []) do
|
def registration_changeset(player, attrs, opts \\ []) do
|
||||||
user
|
player
|
||||||
|> cast(attrs, [:username, :password])
|
|> cast(attrs, [:username, :password])
|
||||||
|> validate_username()
|
|> validate_username()
|
||||||
|> validate_password(opts)
|
|> validate_password(opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
def password_changeset(user, attrs, opts \\ []) do
|
def password_changeset(player, attrs, opts \\ []) do
|
||||||
user
|
player
|
||||||
|> cast(attrs, [:password])
|
|> cast(attrs, [:password])
|
||||||
|> validate_confirmation(:password, message: "does not match password")
|
|> validate_confirmation(:password, message: "does not match password")
|
||||||
|> validate_password(opts)
|
|> validate_password(opts)
|
||||||
|
@ -1,18 +1,51 @@
|
|||||||
defmodule Chessh.Shell do
|
defmodule Chessh.Shell do
|
||||||
use Sshd.ShellHandler
|
use Sshd.ShellHandler
|
||||||
|
|
||||||
def on_shell(_username, _public_key, _ip, _port) do
|
def on_shell(_username, _pubkey, _ip, _port) do
|
||||||
:ok =
|
IO.puts("Looks like you're on #{inspect(self())}")
|
||||||
IO.puts(
|
loop()
|
||||||
"Interactive example SSH shell - type exit ENTER to quit and it is running on #{inspect(self())}"
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def on_connect(_username, _ip, _port, _method) do
|
def on_connect(username, ip, port, method) do
|
||||||
Logger.debug("Connection established")
|
Logger.debug(fn ->
|
||||||
|
"""
|
||||||
|
Incoming SSH shell #{inspect(self())} requested for #{username} from #{inspect(ip)}:#{inspect(port)} using #{inspect(method)}
|
||||||
|
"""
|
||||||
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
def on_disconnect(_username, _ip, _port) do
|
def on_disconnect(username, ip, port) do
|
||||||
Logger.debug("Connection disestablished")
|
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
|
||||||
end
|
end
|
||||||
|
@ -2,14 +2,14 @@ defmodule Chessh.Repo.Migrations.CreatePlayer do
|
|||||||
use Ecto.Migration
|
use Ecto.Migration
|
||||||
|
|
||||||
def change do
|
def change do
|
||||||
execute "CREATE EXTENSION IF NOT EXISTS citext", ""
|
execute("CREATE EXTENSION IF NOT EXISTS citext", "")
|
||||||
|
|
||||||
create table(:players) do
|
create table(:players) do
|
||||||
add :username, :citext, null: false
|
add(:username, :citext, null: false)
|
||||||
add :hashed_password, :string, null: false
|
add(:hashed_password, :string, null: false)
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
|
||||||
create unique_index(:players, [:username])
|
create(unique_index(:players, [:username]))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -3,12 +3,14 @@ defmodule Chessh.Repo.Migrations.AddKeys do
|
|||||||
|
|
||||||
def change do
|
def change do
|
||||||
create table(:keys) do
|
create table(:keys) do
|
||||||
add :key, :string, null: false
|
add(:key, :text, null: false)
|
||||||
add :name, :string, null: false
|
add(:name, :string, null: false)
|
||||||
|
|
||||||
add :player_id, references(:players)
|
add(:player_id, references(:players))
|
||||||
|
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
create(unique_index(:keys, [:player_id, :key]))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user