aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--server.py120
-rw-r--r--ssh.py47
-rw-r--r--termcast.py56
3 files changed, 134 insertions, 89 deletions
diff --git a/server.py b/server.py
index f09e2b0..3df5c92 100644
--- a/server.py
+++ b/server.py
@@ -1,59 +1,24 @@
-import paramiko
-import re
import socket
-import time
import threading
-import vt100
+import uuid
-class Connection(object):
- def __init__(self):
- self.buf = b''
- self.vt = vt100.vt100()
-
- def process(self, data):
- self.buf += data
- self.vt.process(data)
-
- def get_term(self):
- term = ''
- for i in range(0, 24):
- for j in range(0, 80):
- term += self.vt.cell(i, j).contents.contents()
- term += "\n"
-
- return term[:-1]
-
-class SSHServer(paramiko.ServerInterface):
- def check_channel_request(self, kind, chanid):
- return paramiko.OPEN_SUCCEEDED
+import ssh
+import termcast
- 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"
-
-class TermcastServer(object):
+class Server(object):
def __init__(self):
- self.sessions = {}
+ self.termcast_connections = {}
+ self.ssh_connections = {}
def listen(self):
ssh_sock = self._open_socket(2200)
- tc_sock = self._open_socket(2201)
+ termcast_sock = self._open_socket(2201)
threading.Thread(
target=lambda: self.wait_for_ssh_connection(ssh_sock)
).start()
threading.Thread(
- target=lambda: self.wait_for_tc_connection(tc_sock)
+ target=lambda: self.wait_for_termcast_connection(termcast_sock)
).start()
def wait_for_ssh_connection(self, sock):
@@ -62,57 +27,27 @@ class TermcastServer(object):
lambda client: self.handle_ssh_connection(client)
)
- def wait_for_tc_connection(self, sock):
+ def wait_for_termcast_connection(self, sock):
self._wait_for_connection(
sock,
- lambda client: self.handle_tc_connection(client)
+ lambda client: self.handle_termcast_connection(client)
)
def handle_ssh_connection(self, client):
- t = paramiko.Transport(client)
- t.add_server_key(paramiko.RSAKey(filename='test_rsa.key'))
- t.start_server(server=SSHServer())
- chan = t.accept(None)
-
- if b'doy' in self.sessions:
- chan.send(self.sessions[b'doy'].get_term())
- else:
- chan.send("no data for doy\r\n")
-
- time.sleep(5)
- chan.close()
-
- def handle_tc_connection(self, client):
- buf = b''
- while len(buf) < 1024 and b"\n" not in buf:
- buf += client.recv(1024)
-
- pos = buf.find(b"\n")
- if pos == -1:
- print("no authentication found")
- return
-
- auth = buf[:pos]
- buf = buf[pos+1:]
-
- auth_re = re.compile(b'^hello ([^ ]+) ([^ ]+)$')
- m = auth_re.match(auth)
- if m is None:
- print("no authentication found (%s)" % auth)
- return
-
- print(b"got auth: " + auth)
- conn = Connection()
- self.sessions[m.group(1)] = conn
- client.send(b"hello, " + m.group(1) + b"\n")
+ self._handle_connection(
+ client,
+ self.ssh_connections,
+ self.termcast_connections,
+ lambda client, connection_id: ssh.Connection(client, connection_id)
+ )
- conn.process(buf)
- while True:
- buf = client.recv(1024)
- if len(buf) > 0:
- conn.process(buf)
- else:
- return
+ def handle_termcast_connection(self, client):
+ self._handle_connection(
+ client,
+ self.termcast_connections,
+ self.ssh_connections,
+ lambda client, connection_id: termcast.Connection(client, connection_id)
+ )
def _wait_for_connection(self, sock, cb):
while True:
@@ -126,6 +61,13 @@ class TermcastServer(object):
threading.Thread(target=cb, args=(client,)).start()
+ def _handle_connection(self, client, connection_store, other_store, cb):
+ connection_id = uuid.uuid4().hex
+ connection = cb(client, connection_id)
+ connection_store[connection_id] = connection
+ connection.run(other_store)
+ del connection_store[connection_id]
+
def _open_socket(self, port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
@@ -133,5 +75,5 @@ class TermcastServer(object):
return sock
if __name__ == '__main__':
- server = TermcastServer()
+ server = Server()
server.listen()
diff --git a/ssh.py b/ssh.py
new file mode 100644
index 0000000..69accae
--- /dev/null
+++ b/ssh.py
@@ -0,0 +1,47 @@
+import paramiko
+import time
+
+class Handler(object):
+ def __init__(self, sock, connections):
+ self.sock = sock
+ self.connections = connections
+ self.which
+
+ def show(self):
+ pass
+
+class Connection(object):
+ def __init__(self, client, connection_id):
+ self.transport = paramiko.Transport(client)
+ self.transport.add_server_key(paramiko.RSAKey(filename='test_rsa.key'))
+
+ def run(self, termcast_connections):
+ self.transport.start_server(server=Server())
+ chan = self.transport.accept(None)
+
+ if len(termcast_connections) > 0:
+ chan.send(termcast_connections.values().__iter__().__next__().handler.get_term())
+ else:
+ chan.send("no data for doy\r\n")
+
+ time.sleep(5)
+ chan.close()
+
+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"
+
diff --git a/termcast.py b/termcast.py
new file mode 100644
index 0000000..171980d
--- /dev/null
+++ b/termcast.py
@@ -0,0 +1,56 @@
+import re
+
+import vt100
+
+class Handler(object):
+ def __init__(self):
+ self.buf = b''
+ self.vt = vt100.vt100()
+
+ def process(self, data):
+ self.buf += data
+ self.vt.process(data)
+
+ def get_term(self):
+ term = ''
+ for i in range(0, 24):
+ for j in range(0, 80):
+ term += self.vt.cell(i, j).contents.contents()
+ term += "\n"
+
+ return term[:-1]
+
+class Connection(object):
+ def __init__(self, client, connection_id):
+ self.client = client
+
+ def run(self, ssh_connections):
+ buf = b''
+ while len(buf) < 1024 and b"\n" not in buf:
+ buf += self.client.recv(1024)
+
+ pos = buf.find(b"\n")
+ if pos == -1:
+ print("no authentication found")
+ return
+
+ auth = buf[:pos]
+ buf = buf[pos+1:]
+
+ auth_re = re.compile(b'^hello ([^ ]+) ([^ ]+)$')
+ m = auth_re.match(auth)
+ if m is None:
+ print("no authentication found (%s)" % auth)
+ return
+
+ print(b"got auth: " + auth)
+ self.handler = Handler()
+ self.client.send(b"hello, " + m.group(1) + b"\n")
+
+ self.handler.process(buf)
+ while True:
+ buf = self.client.recv(1024)
+ if len(buf) > 0:
+ self.handler.process(buf)
+ else:
+ return