diff --git a/LICENSE b/LICENSE index fb35515..169531c 100755 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2021 hornet +Copyright (c) 2021 hornetfighter515 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..cfaee23 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,7 @@ +gevent==21.8.0 +greenlet==1.1.2 +PyYAML==6.0 +websocket-client==1.2.1 +websocket-server==0.6.0 +zope.event==4.5.0 +zope.interface==5.4.0 diff --git a/src/client/cli.py b/src/client/cli.py index 074ebe1..ea434ec 100644 --- a/src/client/cli.py +++ b/src/client/cli.py @@ -26,7 +26,6 @@ def on_open(ws): _thread.start_new_thread(run, ()) def open_socket(url, port): - websocket.enableTrace(True) ws = websocket.WebSocketApp(f"ws://{url}:{port}", on_open=on_open, on_message=on_message, diff --git a/src/client/config.yml b/src/client/config.yml new file mode 100644 index 0000000..f030a27 --- /dev/null +++ b/src/client/config.yml @@ -0,0 +1,3 @@ +ws: + url: localhost + port: 6873 diff --git a/src/py-cli/encrypt_decrypt.py b/src/client/encrypt_decrypt.py similarity index 89% rename from src/py-cli/encrypt_decrypt.py rename to src/client/encrypt_decrypt.py index b24c438..76ef455 100755 --- a/src/py-cli/encrypt_decrypt.py +++ b/src/client/encrypt_decrypt.py @@ -8,10 +8,7 @@ https://www.geeksforgeeks.org/how-to-encrypt-and-decrypt-strings-in-python/ import rsa -class encrypt_decrypt(): - - # the message we want to encrypt - message = "Hello World" +class EncryptDecrypt(): def make_keys(key_length): publicKey, privateKey = "" diff --git a/src/client/protocol.yml b/src/client/protocol.yml new file mode 100644 index 0000000..e69de29 diff --git a/src/client/test-cli.py b/src/client/test-cli.py deleted file mode 100644 index 02daae0..0000000 --- a/src/client/test-cli.py +++ /dev/null @@ -1,43 +0,0 @@ -import websocket -import _thread -import ssl -import json - -def on_message(ws, message): - print(f"<< {message}") - -def on_error(ws, error): - print(error) - -def on_close(ws, close_status_code, close_msg): - print(f"### closed. reason: {close_msg} ###") - -def on_open(ws): - def run(*args): - running = True - uname = input('input username:') - msg = { - "user":uname, - "content":None - } - ws.send(json.dumps(msg)) - while running: - outbound = input(">") - if outbound == 'q': - running = False - else: - msg = { - "user":uname, - "content":outbound - } - ws.send(json.dumps(msg)) - ws.close() - _thread.start_new_thread(run, ()) - -if __name__ == "__main__": - ws = websocket.WebSocketApp("wss://hornetfighter.com:9443", - on_open=on_open, - on_message=on_message, - on_error=on_error, - on_close=on_close) - ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE}) diff --git a/src/client/test_config.yaml b/src/client/test_config.yml similarity index 100% rename from src/client/test_config.yaml rename to src/client/test_config.yml diff --git a/src/hub/config.yml b/src/hub/config.yml new file mode 100644 index 0000000..3a4146a --- /dev/null +++ b/src/hub/config.yml @@ -0,0 +1,7 @@ +ws: + ext: + url: localhost + port: 6873 + int: + url: localhost + port: 6872 diff --git a/src/hub/hub.py b/src/hub/hub.py index 8281607..aee6c43 100644 --- a/src/hub/hub.py +++ b/src/hub/hub.py @@ -6,17 +6,31 @@ Creates hub for distributed IoT """ from websocket_server import WebsocketServer +import time -def new_client(client, server): - print("Client has joined") +class Serv(): + def __init__(self, host, port): + self.host = host + self.port = port + self.ws_s = WebsocketServer(host=host, port=port) + self.ws_s.set_fn_message_received(self.message_received) + self.ws_s.set_fn_new_client(self.new_client) + self.ws_s.run_forever() -def message_received(client, server, message): - print(message) + def _log_prefix(self): + return str(time.time()) + ' [' + self.host + ':' + str(self.port) + '] ' -int_server = WebsocketServer(host='127.0.0.1', port=6872) + def new_client(self, client, server): + print(self._log_prefix() + 'Client joined.') + + def message_received(self, client, server, message): + # figure out how to identify clients... + print(self._log_prefix() + message ) -ext_server = WebsocketServer(host='127.0.0.1', port=6873) -ext_server.set_fn_message_received(message_received) -ext_server.set_fn_new_client(new_client) -ext_server.run_forever() +if __name__ == "__main__": + import yaml + with open('config.yml', 'r') as f: + conf = yaml.safe_load(f) + ws_conf = conf['ws'] + ext_server = Serv(ws_conf['ext']['url'], ws_conf['ext']['port']) diff --git a/src/hub/test_config.yml b/src/hub/test_config.yml new file mode 100644 index 0000000..e37f41a --- /dev/null +++ b/src/hub/test_config.yml @@ -0,0 +1,7 @@ +ws: + ext: + url: localhost + port: 6873 + int: + url: localhost + port: 6872 diff --git a/src/py-cli/main.py b/src/py-cli/main.py deleted file mode 100755 index 7f2ec33..0000000 --- a/src/py-cli/main.py +++ /dev/null @@ -1,25 +0,0 @@ -""" -A client to send an encrypted message to another client - -@author hornetfighter515, Lachezar Todorov -""" -import encrypt_decrypt -import websock.alice -import websock.bob -import asyncio - -def main(): - message = "Hello World" - - key_length = 512 - - #Bob's keys - publicKey, privateKey = encrypt_decrypt.encrypt_decrypt.make_keys(key_length) - - asyncio.run(websock.alice.Alice.messageEncrypt("ws://localhost:8765", publicKey, message)) - - asyncio.run(websock.bob.Bob.messageDecrypt(privateKey)) - - - - diff --git a/src/py-cli/make_sockets.py b/src/py-cli/make_sockets.py deleted file mode 100755 index 85f5eae..0000000 --- a/src/py-cli/make_sockets.py +++ /dev/null @@ -1,37 +0,0 @@ -""" -A file that makes a bunch of sockets - -@author Lachezar Todorov -""" - -class WebSockestServerExternal(): - - def __init__(self): - self.wsl = [10] - self.count = 0 - - - #start session - def login(self, name): - self.wsl[self.count] = name - self.count += 1 - self.printing() - - #exit session - def logout(self, name): - for name in self.wsl: - if name == name: - index = self.wsl.index(name) - self.wsl[index] = "" - self.printing() - - #print out the list - def printing(self): - for name in self.wsl: - print(name) - -WebSockestServerExternal() - -#alice.login() -#alice.logout() -