diff --git a/daemon/dist.py b/daemon/dist.py new file mode 100644 index 0000000..2bd526e --- /dev/null +++ b/daemon/dist.py @@ -0,0 +1 @@ +from dist import core diff --git a/daemon/dist/__init__.py b/daemon/dist/__init__.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/daemon/dist/__init__.py @@ -0,0 +1 @@ + diff --git a/daemon/dist/__init__.pyc b/daemon/dist/__init__.pyc new file mode 100644 index 0000000..c71f3b1 Binary files /dev/null and b/daemon/dist/__init__.pyc differ diff --git a/daemon/dist/core.py b/daemon/dist/core.py new file mode 100755 index 0000000..467e604 --- /dev/null +++ b/daemon/dist/core.py @@ -0,0 +1,95 @@ +#!/usr/bin/python + +################################ +# Configuration starts here + +cert_path = '/home/sven/ssl/cert' +key_path = '/home/sven/ssl/private' + +# Configuration ends here +################################ + +import socket, ssl, select +from shared.core import * + +def remove_from_list(ls, val): + return [value for value in ls if value is not val] + +client_list = [] +client_map = {} +select_inputs = [] +select_outputs = [] + +class Client: + buff = "" + channel_map = {} + + def __init__(self, connstream): + self.stream = connstream + self.channel_map[0] = Channel(self, EchoHandler()) + + def process_data(self, data): + self.buff += data + stack = self.buff.split(EOC) + self.buff = stack.pop() + + for chunk in stack: + self.process_chunk(chunk) + + def process_chunk(self, chunk): + if len(chunk) > 2: + channel_identifier = chunk[:2] + data = chunk[2:] + + channel_numeric = to_numeric(channel_identifier) + + if channel_numeric in self.channel_map: + self.channel_map[channel_numeric].process_chunk(data) + else: + print "WARNING: Received data on non-existent channel %d" % channel_numeric + + + +bindsocket = socket.socket() +bindsocket.bind(('0.0.0.0', 9151)) +bindsocket.listen(5) + +select_inputs = [ bindsocket ] + +while select_inputs: + readable, writable, error = select.select(select_inputs, select_outputs, select_inputs) + + for sock in readable: + try: + if sock is bindsocket: + newsocket, fromaddr = bindsocket.accept() + connstream = ssl.wrap_socket(newsocket, + server_side=True, + certfile=cert_path, + keyfile=key_path, + ssl_version=ssl.PROTOCOL_TLSv1) + + new_client = Client(connstream) + + select_inputs.append(connstream) + client_map[connstream.fileno()] = new_client + client_list.append(new_client) + else: + data = sock.recv(1024) + + if data: + cur_client = client_map[sock.fileno()] + cur_client.process_data(data) + else: + select_inputs = remove_from_list(select_inputs, sock) + print "NOTICE: Client disconnected" + except ssl.SSLError, err: + if err.args[0] == ssl.SSL_ERROR_WANT_READ: + select.select([sock], [], []) + elif err.args[0] == ssl.SSL_ERROR_WANT_WRITE: + select.select([], [sock], []) + else: + raise + + +print "Server socket closed, exiting..." diff --git a/daemon/dist/core.pyc b/daemon/dist/core.pyc new file mode 100644 index 0000000..6514ea5 Binary files /dev/null and b/daemon/dist/core.pyc differ diff --git a/daemon/master.py b/daemon/master.py new file mode 100644 index 0000000..53268a9 --- /dev/null +++ b/daemon/master.py @@ -0,0 +1 @@ +from master import core diff --git a/daemon/master/__init__.py b/daemon/master/__init__.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/daemon/master/__init__.py @@ -0,0 +1 @@ + diff --git a/daemon/master/core.py b/daemon/master/core.py new file mode 100644 index 0000000..013e4b7 --- /dev/null +++ b/daemon/master/core.py @@ -0,0 +1 @@ +#!/usr/bin/python diff --git a/daemon/node.py b/daemon/node.py new file mode 100644 index 0000000..0bdcf3c --- /dev/null +++ b/daemon/node.py @@ -0,0 +1 @@ +from node import core diff --git a/daemon/node/__init__.py b/daemon/node/__init__.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/daemon/node/__init__.py @@ -0,0 +1 @@ + diff --git a/daemon/node/__init__.pyc b/daemon/node/__init__.pyc new file mode 100644 index 0000000..a04d05a Binary files /dev/null and b/daemon/node/__init__.pyc differ diff --git a/daemon/node/core.py b/daemon/node/core.py new file mode 100755 index 0000000..afee0ea --- /dev/null +++ b/daemon/node/core.py @@ -0,0 +1,22 @@ +#!/usr/bin/python + +################################ +# Configuration starts here + +allowed_certs = '/home/sven/ssl/allowed' + +# Configuration ends here +################################ + +import socket, ssl, time, math +from shared.core import * + +s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +ssl_sock = ssl.wrap_socket(s, cert_reqs=ssl.CERT_REQUIRED, ca_certs=allowed_certs) +ssl_sock.connect(('localhost', 9151)) + +ssl_sock.write(to_identifier(0) + 'test data' + EOC + to_identifier(4190) + 'SAMPLEDATA' + EOC) + +print ssl_sock.read()[2:] + +ssl_sock.close() diff --git a/daemon/node/core.pyc b/daemon/node/core.pyc new file mode 100644 index 0000000..178fe4a Binary files /dev/null and b/daemon/node/core.pyc differ diff --git a/daemon/shared/__init__.pyc b/daemon/shared/__init__.pyc new file mode 100644 index 0000000..1af90af Binary files /dev/null and b/daemon/shared/__init__.pyc differ diff --git a/daemon/shared/core.py b/daemon/shared/core.py new file mode 100644 index 0000000..ea83340 --- /dev/null +++ b/daemon/shared/core.py @@ -0,0 +1,34 @@ +import math + +EOC = "\0" + +def to_numeric(identifier): + return ((ord(identifier[:1]) - 1) * 255) + (ord(identifier[1:]) - 1) + +def to_identifier(numeric): + return chr(int(math.floor(numeric / 255) + 1)) + chr((numeric % 255) + 1) + +class Channel: + numeric = 0 + binary = False + handler = None + client = None + + def __init__(self, client, handler, binary=False): + self.handler = handler + self.binary = binary + self.client = client + + def process_chunk(self, chunk): + self.handler.process(chunk) + + def send(self, data): + self.client.stream.send(to_identifier(self.numeric) + data + EOC) + +class Handler: + def process(self, chunk): + pass + +class EchoHandler(Handler): + def process(self, chunk): + print "Received %s" % chunk diff --git a/daemon/shared/core.pyc b/daemon/shared/core.pyc new file mode 100644 index 0000000..a529987 Binary files /dev/null and b/daemon/shared/core.pyc differ