aboutsummaryrefslogtreecommitdiffstats
path: root/termcast_server/__init__.py
blob: de2af21b85a668cbfaaa3549fff535ba68dc1c96 (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
94
95
96
97
98
99
import signal
import socket
import sys
import threading
import traceback
import uuid

from . import pubsub
from . import ssh
from . import termcast
from . import web

class Server(object):
    def __init__(self, keyfile, pemfile):
        self.publisher = pubsub.Publisher()
        self.keyfile = keyfile
        self.pemfile = pemfile

    def listen(self):
        ssh_sock = self._open_socket(2200)
        termcast_sock = self._open_socket(2201)
        web_sock = self._open_socket(2202)

        threading.Thread(
            target=lambda: self.wait_for_ssh_connection(ssh_sock)
        ).start()
        threading.Thread(
            target=lambda: self.wait_for_termcast_connection(termcast_sock)
        ).start()
        threading.Thread(
            target=lambda: self.wait_for_web_connection(web_sock)
        ).start()

    def wait_for_ssh_connection(self, sock):
        self._wait_for_connection(
            sock,
            lambda client: self.handle_ssh_connection(client)
        )

    def wait_for_termcast_connection(self, sock):
        self._wait_for_connection(
            sock,
            lambda client: self.handle_termcast_connection(client)
        )

    def wait_for_web_connection(self, sock):
        sock.setblocking(0)
        sock.listen(100)
        web.start_server(sock, self.publisher, self.pemfile)

    def handle_ssh_connection(self, client):
        self._handle_connection(
            client,
            lambda client, connection_id: ssh.Connection(
                client, connection_id, self.publisher, self.keyfile
            )
        )

    def handle_termcast_connection(self, client):
        self._handle_connection(
            client,
            lambda client, connection_id: termcast.Connection(
                client, connection_id, self.publisher, self.pemfile
            )
        )

    def _wait_for_connection(self, sock, cb):
        while True:
            try:
                sock.listen(100)
                client, addr = sock.accept()
            except Exception as e:
                print(traceback.format_exc())
                print('*** Listen/accept failed: ' + str(e))
                continue

            threading.Thread(target=cb, args=(client,)).start()

    def _handle_connection(self, client, cb):
        connection_id = uuid.uuid4().hex
        connection = cb(client, connection_id)
        self.publisher.subscribe(connection)
        try:
            connection.run()
        except Exception as e:
            print(traceback.format_exc())
            print('Running connection failed: ' + str(e))
        self.publisher.unsubscribe(connection)
        client.close()

    def _open_socket(self, port):
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.bind(('', port))
        return sock

def main():
    server = Server(sys.argv[1], sys.argv[2])
    server.listen()