diff --git a/README.md b/README.md index 9d81e678..d367a549 100644 --- a/README.md +++ b/README.md @@ -47,10 +47,9 @@ Onionr ships with various application plugins ready for use out of the box: Currently usable: * Mail -* Public anonymous chat +* Public anonymous chat/message board * Simple webpage hosting - Will be greatly extended * File sharing (Work in progress) -* Simple Message board Not yet usable: diff --git a/onionr/httpapi/directconnections/__init__.py b/onionr/httpapi/directconnections/__init__.py index 80e3b82a..b0a75f72 100644 --- a/onionr/httpapi/directconnections/__init__.py +++ b/onionr/httpapi/directconnections/__init__.py @@ -17,11 +17,11 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . ''' -import threading +import threading # For the client creation thread -from flask import Response -from flask import Blueprint -from flask import g +from flask import Response # For direct connection management HTTP endpoints +from flask import Blueprint # To make the direct connection management blueprint in the webUI +from flask import g # Mainly to access the shared toomanyobjs object import deadsimplekv import filepaths @@ -49,12 +49,16 @@ class DirectConnectionManagement: def make_new_connection(pubkey): communicator = _get_communicator(g) resp = "pending" + if pubkey in communicator.shared_state.get_by_string("ServicePool").bootstrap_pending: + return Response(resp) + if pubkey in communicator.direct_connection_clients: resp = communicator.direct_connection_clients[pubkey] else: """Spawn a thread that will create the client and eventually add it to the communicator.active_services """ - threading.Thread(target=onionrservices.OnionrServices().create_client, args=[pubkey, communicator], daemon=True).start() + threading.Thread(target=onionrservices.OnionrServices().create_client, + args=[pubkey, communicator], daemon=True).start() return Response(resp) \ No newline at end of file diff --git a/onionr/onionrexceptions.py b/onionr/onionrexceptions.py index f19a4c62..b510dee8 100755 --- a/onionr/onionrexceptions.py +++ b/onionr/onionrexceptions.py @@ -84,6 +84,9 @@ class InvalidAddress(Exception): class InvalidAPIVersion(Exception): pass +class Timeout(Exception): + pass + # file exceptions class DiskAllocationReached(Exception): diff --git a/onionr/onionrservices/__init__.py b/onionr/onionrservices/__init__.py index a2a6a26a..2aefbc07 100755 --- a/onionr/onionrservices/__init__.py +++ b/onionr/onionrservices/__init__.py @@ -31,7 +31,6 @@ class OnionrServices: self.servers = {} self.clients = {} self.shutdown = False - return def create_server(self, peer, address, comm_inst): ''' @@ -55,7 +54,7 @@ class OnionrServices: @staticmethod def create_client(peer, comm_inst=None): - # Create ephemeral onion service to bootstrap connection + # Create ephemeral onion service to bootstrap connection to server if not comm_inst == None: try: return comm_inst.direct_connection_clients[peer] diff --git a/onionr/onionrservices/bootstrapservice.py b/onionr/onionrservices/bootstrapservice.py index 4304cf6f..0ef8b42a 100755 --- a/onionr/onionrservices/bootstrapservice.py +++ b/onionr/onionrservices/bootstrapservice.py @@ -25,7 +25,16 @@ from netcontroller import get_open_port from . import httpheaders from onionrutils import stringvalidators, epoch import config, onionrblocks, filepaths +import onionrexceptions import deadsimplekv as simplekv +import warden +from . import pool + +def __bootstrap_timeout(server: WSGIServer, timeout: int, signal_object): + time.sleep(timeout) + signal_object.timed_out = True + server.stop() + def bootstrap_client_service(peer, comm_inst=None, bootstrap_timeout=300): ''' Bootstrap client services @@ -33,9 +42,17 @@ def bootstrap_client_service(peer, comm_inst=None, bootstrap_timeout=300): if not stringvalidators.validate_pub_key(peer): raise ValueError('Peer must be valid base32 ed25519 public key') + + connection_pool = None + + # here we use a lambda for the timeout thread to set to true + timed_out = lambda: None + timed_out.timed_out = False bootstrap_port = get_open_port() bootstrap_app = Flask(__name__) + bootstrap_app.config['MAX_CONTENT_LENGTH'] = 1 * 1024 + http_server = WSGIServer(('127.0.0.1', bootstrap_port), bootstrap_app, log=None) try: assert comm_inst is not None @@ -43,6 +60,7 @@ def bootstrap_client_service(peer, comm_inst=None, bootstrap_timeout=300): pass else: comm_inst.service_greenlets.append(http_server) + connection_pool = comm_inst.shared_state.get(pool.ServicePool) bootstrap_address = '' shutdown = False @@ -71,12 +89,16 @@ def bootstrap_client_service(peer, comm_inst=None, bootstrap_timeout=300): return Response("") with Controller.from_port(port=config.get('tor.controlPort')) as controller: + connection_pool.bootstrap_pending.append(peer) # Connect to the Tor process for Onionr controller.authenticate(config.get('tor.controlpassword')) # Create the v3 onion service response = controller.create_ephemeral_hidden_service({80: bootstrap_port}, key_type = 'NEW', key_content = 'ED25519-V3', await_publication = True) onionrblocks.insert(response.service_id, header='con', sign=True, encryptType='asym', asymPeer=peer, disableForward=True, expire=(epoch.get_epoch() + bootstrap_timeout)) + + threading.Thread(target=__bootstrap_timeout, args=[http_server, bootstrap_timeout], daemon=True) + # Run the bootstrap server try: http_server.serve_forever() @@ -86,5 +108,10 @@ def bootstrap_client_service(peer, comm_inst=None, bootstrap_timeout=300): # Add the address to the client pool if not comm_inst is None: comm_inst.direct_connection_clients[peer] = response.service_id + + connection_pool.bootstrap_pending.remove(peer) + + if timed_out.timed_out: + raise onionrexceptions.Timeout # Now that the bootstrap server has received a server, return the address return key_store.get(bs_id) diff --git a/onionr/onionrservices/pool.py b/onionr/onionrservices/pool.py new file mode 100644 index 00000000..4f26029c --- /dev/null +++ b/onionr/onionrservices/pool.py @@ -0,0 +1,29 @@ +''' + Onionr - Private P2P Communication + + Holds active onionrservices clients and servers +''' +''' + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +''' +from onionrutils import epoch +class ServicePool: + def __init__(self): + self.servers = [] + self.clients = [] + self.bootstrap_pending = [] + + def add_server(self, service): + self.servers.append((service, epoch.get_epoch())) + \ No newline at end of file diff --git a/onionr/onionrservices/warden/__init__.py b/onionr/onionrservices/warden/__init__.py new file mode 100644 index 00000000..7173f75c --- /dev/null +++ b/onionr/onionrservices/warden/__init__.py @@ -0,0 +1,3 @@ +from . import client # Client connection warden. Monitors & validates connection security. +from . import server # Server connection warden. Monitors and validates server security +from . import watchdog # Watchdog. Oversees running services for statistic collection and TTL control diff --git a/onionr/onionrservices/warden/bootstrap.py b/onionr/onionrservices/warden/bootstrap.py new file mode 100644 index 00000000..bd37e0a7 --- /dev/null +++ b/onionr/onionrservices/warden/bootstrap.py @@ -0,0 +1,19 @@ +''' + Onionr - Private P2P Communication + + Bootstrap warden monitors the bootstrap server +''' +''' + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +''' diff --git a/onionr/onionrservices/warden/client.py b/onionr/onionrservices/warden/client.py new file mode 100644 index 00000000..e69de29b diff --git a/onionr/onionrservices/warden/server.py b/onionr/onionrservices/warden/server.py new file mode 100644 index 00000000..e69de29b