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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
defmodule Chessh.SSH.Tui do
alias Chessh.{Repo, PlayerSession, Utils, Player}
alias Chessh.SSH.Client
alias IO.ANSI
require Logger
@behaviour :ssh_server_channel
@session_closed_message [
ANSI.clear(),
["This session has been closed"]
]
defmodule State do
defstruct channel_id: nil,
width: nil,
height: nil,
client_pid: nil,
connection_ref: nil,
player_session: nil
end
def init([%State{} = init_state]) do
:syn.add_node_to_scopes([:player_sessions])
{:ok, init_state}
end
def handle_msg({:ssh_channel_up, channel_id, connection_ref}, %State{} = state) do
Logger.debug("SSH channel up #{inspect(:ssh.connection_info(connection_ref))}")
connected_player =
:ssh.connection_info(connection_ref)
|> Keyword.fetch!(:user)
|> String.Chars.to_string()
case Repo.get_by(Player, username: connected_player) do
nil ->
Logger.error("Killing channel #{channel_id} - auth'd user does not exist")
{:stop, channel_id, state}
player ->
case Repo.get_by(PlayerSession,
node_id: System.fetch_env!("NODE_ID"),
process: Utils.pid_to_str(connection_ref),
player_id: player.id
) do
nil ->
Logger.error("Killing channel #{channel_id} - session does not exist")
{:stop, channel_id, state}
session ->
Logger.debug("Subscribing to session #{session.id}")
:syn.join(:player_sessions, {:session, session.id}, self())
{:ok,
%State{
state
| channel_id: channel_id,
connection_ref: connection_ref,
player_session: session
}}
end
end
end
def handle_msg(
{:EXIT, client_pid, _reason},
%State{client_pid: client_pid, channel_id: channel_id} = state
) do
send(client_pid, :quit)
{:stop, channel_id, state}
end
def handle_msg(
{:send_data, data},
%State{connection_ref: connection_ref, channel_id: channel_id} = state
) do
:ssh_connection.send(connection_ref, channel_id, data)
{:ok, state}
end
def handle_msg(
:session_closed,
%State{connection_ref: connection_ref, channel_id: channel_id} = state
) do
:ssh_connection.send(connection_ref, channel_id, @session_closed_message)
{:stop, channel_id, state}
end
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{} = state
) do
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, _opts}}},
%State{} = state
) do
Logger.debug("#{inspect(state.player_session)} has requested a PTY")
:ssh_connection.reply_request(connection_handler, want_reply?, :success, channel_id)
{:ok,
%{
state
| width: width,
height: height
}}
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{client_pid: client_pid} = state
) do
send(client_pid, {:resize, {width, height}})
{:ok,
%State{
state
| width: width,
height: height
}}
end
def handle_ssh_msg(
{:ssh_cm, connection_handler, {:shell, channel_id, want_reply?}},
%{width: width, height: height, player_session: player_session} = state
) do
:ssh_connection.reply_request(connection_handler, want_reply?, :success, channel_id)
{:ok, client_pid} =
GenServer.start_link(Client, [
%Client.State{
tui_pid: self(),
width: width,
player_session: player_session,
height: height
}
])
send(client_pid, :refresh)
{:ok, %State{state | client_pid: client_pid}}
end
def handle_ssh_msg(
msg,
%State{channel_id: channel_id} = state
) do
Logger.debug("UNKOWN MESSAGE #{inspect(msg)}")
{:stop, channel_id, state}
end
def terminate(_reason, _state) do
:ok
end
end
|