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
|
import paramiko
import time
class Connection(object):
def __init__(self, client, connection_id, publisher, keyfile):
self.transport = paramiko.Transport(client)
key = None
with open(keyfile) as f:
header = f.readline()
if header == "-----BEGIN DSA PRIVATE KEY-----\n":
key = paramiko.DSSKey(filename=keyfile)
elif header == "-----BEGIN RSA PRIVATE KEY-----\n":
key = paramiko.RSAKey(filename=keyfile)
if key is None:
raise Exception("%s doesn't appear to be an SSH keyfile" % keyfile)
self.transport.add_server_key(key)
self.connection_id = connection_id
self.publisher = publisher
self.initialized = False
self.watching_id = None
def run(self):
self.transport.start_server(server=Server())
self.chan = self.transport.accept(None)
while True:
self.initialized = False
self.watching_id = None
self.watching_id = self.select_stream()
if self.watching_id is None:
break
self.publisher.notify("new_viewer", self.watching_id)
while True:
c = self.chan.recv(1)
if c == b'q':
self.publisher.notify("viewer_disconnect", self.watching_id)
break
self.chan.close()
def select_stream(self):
key_code = ord('a')
keymap = {}
streamers = self.publisher.request_all("get_streamers")
for streamer in streamers:
key = chr(key_code)
streamer["key"] = key
keymap[key] = streamer["id"]
key_code += 1
self._display_streamer_screen(streamers)
c = self.chan.recv(1).decode('utf-8')
if c in keymap:
self.chan.send("\033[2J\033[H")
return keymap[c]
elif c == 'q':
self.chan.send("\r\n")
return None
else:
return self.select_stream()
def msg_new_data(self, connection_id, prev_buf, data):
if self.watching_id != connection_id:
return
if not self.initialized:
self.chan.send(prev_buf)
self.initialized = True
self.chan.send(data)
def _display_streamer_screen(self, streamers):
self.chan.send("\033[2J\033[HWelcome to Termcast!")
self.chan.send(
"\033[3H %-20s %-15s %-15s" % (
"User", "Terminal size", "Idle time",
)
)
row = 4
for streamer in streamers:
key = streamer["key"]
name = streamer["name"].decode('utf-8')
size = "(%dx%d)" % (streamer["cols"], streamer["rows"])
idle = streamer["idle"]
self.chan.send(
"\033[%dH%s) %-20s %-15s %-15s" % (
row, key, name, size, idle
)
)
row += 1
self.chan.send("\033[%dHChoose a stream: " % (row + 1))
class Server(paramiko.ServerInterface):
def check_channel_request(self, kind, chanid):
return paramiko.OPEN_SUCCEEDED
def check_channel_pty_request(self, channel, term, width, height, pixelwidth, pixelheight, modes):
return True
def check_channel_shell_request(self, channel):
return True
def check_auth_password(self, username, password):
if password == "blah":
return paramiko.AUTH_SUCCESSFUL
return paramiko.AUTH_FAILED
def get_allowed_auths(self, username):
return "password"
|