aboutsummaryrefslogtreecommitdiffstats
path: root/ssh.py
blob: 0758d499fb8fedcac5abcd03ea8ac08e2dd9eeb3 (plain) (blame)
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
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 = 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':
                    break

        self.chan.close()

    def select_stream(self):
        self.chan.send("\033[2J\033[HTermcast")
        row = 3
        key_code = ord('a')
        keymap = {}
        for streamer in self.publisher.request_all("get_streamers"):
            key = chr(key_code)
            keymap[key] = streamer["id"]
            self.chan.send("\033[%dH%s) %s" % (row, key, streamer["name"].decode('utf-8')))
            row += 1
            key_code += 1

        self.chan.send("\033[%dHChoose a stream: " % (row + 1))

        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':
            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)

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"