diff --git a/minode/advertiser.py b/minode/advertiser.py index 04e0291..16d15da 100644 --- a/minode/advertiser.py +++ b/minode/advertiser.py @@ -2,8 +2,7 @@ import logging import threading import time -import message -import shared +from . import message, shared class Advertiser(threading.Thread): diff --git a/minode/connection.py b/minode/connection.py index ca9be4b..233d801 100644 --- a/minode/connection.py +++ b/minode/connection.py @@ -10,9 +10,7 @@ import threading import queue import time -import message -import shared -import structure +from . import message, shared, structure class Connection(threading.Thread): @@ -367,4 +365,8 @@ class Connection(threading.Thread): for vector in to_send: obj = shared.objects.get(vector, None) if obj: - self.send_queue.put(message.Message(b'object', obj.to_bytes())) + self.send_queue.put( + message.Message(b'object', obj.to_bytes())) + + +shared.connection = Connection diff --git a/minode/i2p/__init__.py b/minode/i2p/__init__.py index e69de29..e71aa66 100644 --- a/minode/i2p/__init__.py +++ b/minode/i2p/__init__.py @@ -0,0 +1,5 @@ +from .controller import I2PController +from .dialer import I2PDialer +from .listener import I2PListener + +__all__ = ["I2PController", "I2PDialer", "I2PListener"] diff --git a/minode/i2p/controller.py b/minode/i2p/controller.py index 6f5f675..0f2c89c 100644 --- a/minode/i2p/controller.py +++ b/minode/i2p/controller.py @@ -6,14 +6,14 @@ import socket import threading import time -from i2p.util import receive_line, pub_from_priv -import shared +from .util import receive_line, pub_from_priv class I2PController(threading.Thread): - def __init__(self, host='127.0.0.1', port=7656, dest_priv=b''): + def __init__(self, state, host='127.0.0.1', port=7656, dest_priv=b''): super().__init__(name='I2P Controller') + self.state = state self.host = host self.port = port self.nick = b'MiNode_' + base64.b16encode(os.urandom(4)).lower() @@ -70,10 +70,11 @@ class I2PController(threading.Thread): assert self.dest_priv def create_session(self): - self._send(b'SESSION CREATE STYLE=STREAM ID=' + self.nick + - b' inbound.length=' + str(shared.i2p_tunnel_length).encode() + - b' outbound.length=' + str(shared.i2p_tunnel_length).encode() + - b' DESTINATION=' + self.dest_priv + b'\n') + self._send( + b'SESSION CREATE STYLE=STREAM ID=' + self.nick + + b' inbound.length=' + str(self.state.i2p_tunnel_length).encode() + + b' outbound.length=' + str(self.state.i2p_tunnel_length).encode() + + b' DESTINATION=' + self.dest_priv + b'\n') reply = self._receive_line().split() if b'RESULT=OK' not in reply: logging.warning(reply) @@ -84,7 +85,7 @@ class I2PController(threading.Thread): def run(self): self.s.settimeout(1) while True: - if not shared.shutting_down: + if not self.state.shutting_down: try: msg = self._receive_line().split(b' ') if msg[0] == b'PING': diff --git a/minode/i2p/dialer.py b/minode/i2p/dialer.py index 609cacb..2afe84c 100644 --- a/minode/i2p/dialer.py +++ b/minode/i2p/dialer.py @@ -3,13 +3,14 @@ import logging import socket import threading -import shared -from connection import Connection -from i2p.util import receive_line +from .util import receive_line class I2PDialer(threading.Thread): - def __init__(self, destination, nick, sam_host='127.0.0.1', sam_port=7656): + def __init__( + self, state, destination, nick, sam_host='127.0.0.1', sam_port=7656 + ): + self.state = state self.sam_host = sam_host self.sam_port = sam_port @@ -26,10 +27,12 @@ class I2PDialer(threading.Thread): def run(self): logging.debug('Connecting to {}'.format(self.destination)) self._connect() - if not shared.shutting_down and self.success: - c = Connection(self.destination, 'i2p', self.s, 'i2p', False, self.destination) + if not self.state.shutting_down and self.success: + c = self.state.connection( + self.destination, 'i2p', self.s, 'i2p', + False, self.destination) c.start() - shared.connections.add(c) + self.state.connections.add(c) def _receive_line(self): line = receive_line(self.s) diff --git a/minode/i2p/listener.py b/minode/i2p/listener.py index 854c257..f8e6127 100644 --- a/minode/i2p/listener.py +++ b/minode/i2p/listener.py @@ -3,15 +3,14 @@ import logging import socket import threading -from connection import Connection -from i2p.util import receive_line -import shared +from .util import receive_line class I2PListener(threading.Thread): - def __init__(self, nick, host='127.0.0.1', port=7656): + def __init__(self, state, nick, host='127.0.0.1', port=7656): super().__init__(name='I2P Listener') + self.state = state self.host = host self.port = port self.nick = nick @@ -44,23 +43,24 @@ class I2PListener(threading.Thread): self.s.settimeout(1) def run(self): - while not shared.shutting_down: + while not self.state.shutting_down: try: destination = self._receive_line().split()[0] logging.info('Incoming I2P connection from: {}'.format(destination.decode())) hosts = set() - for c in shared.connections.copy(): + for c in self.state.connections.copy(): hosts.add(c.host) - for d in shared.i2p_dialers.copy(): + for d in self.state.i2p_dialers.copy(): hosts.add(d.destination) if destination in hosts: logging.debug('Rejecting duplicate I2P connection.') self.s.close() else: - c = Connection(destination, 'i2p', self.s, 'i2p', True, destination) + c = self.state.connection( + destination, 'i2p', self.s, 'i2p', True, destination) c.start() - shared.connections.add(c) + self.state.connections.add(c) self.new_socket() except socket.timeout: pass diff --git a/minode/listener.py b/minode/listener.py index 3cf568f..cb19c04 100644 --- a/minode/listener.py +++ b/minode/listener.py @@ -3,8 +3,8 @@ import logging import socket import threading -from connection import Connection -import shared +from . import shared +from .connection import Connection class Listener(threading.Thread): diff --git a/minode/main.py b/minode/main.py index c5e2853..407ae67 100644 --- a/minode/main.py +++ b/minode/main.py @@ -9,12 +9,10 @@ import pickle import signal import socket -from advertiser import Advertiser -from manager import Manager -from listener import Listener -import i2p.controller -import i2p.listener -import shared +from . import i2p, shared +from .advertiser import Advertiser +from .manager import Manager +from .listener import Listener def handler(s, f): @@ -171,12 +169,15 @@ def start_i2p_listener(): with open(shared.data_directory + 'i2p_dest_priv.key', mode='br') as file: dest_priv = file.read() logging.debug('Loaded I2P destination private key.') - except Exception as e: - logging.warning('Error while loading I2P destination private key.') - logging.warning(e) + except Exception: + logging.warning( + 'Error while loading I2P destination private key.', + exc_info=True) - logging.info('Starting I2P Controller and creating tunnels. This may take a while.') - i2p_controller = i2p.controller.I2PController(shared.i2p_sam_host, shared.i2p_sam_port, dest_priv) + logging.info( + 'Starting I2P Controller and creating tunnels. This may take a while.') + i2p_controller = i2p.I2PController( + shared, shared.i2p_sam_host, shared.i2p_sam_port, dest_priv) i2p_controller.start() shared.i2p_dest_pub = i2p_controller.dest_pub @@ -186,7 +187,7 @@ def start_i2p_listener(): logging.info('I2P session nick: {}'.format(shared.i2p_session_nick.decode())) logging.info('Starting I2P Listener') - i2p_listener = i2p.listener.I2PListener(i2p_controller.nick) + i2p_listener = i2p.I2PListener(shared, i2p_controller.nick) i2p_listener.start() if not shared.i2p_transient: diff --git a/minode/manager.py b/minode/manager.py index bcde093..ad6c701 100644 --- a/minode/manager.py +++ b/minode/manager.py @@ -7,11 +7,9 @@ import random import threading import time -from connection import Connection -from i2p.dialer import I2PDialer -import pow -import shared -import structure +from . import pow, shared, structure +from .connection import Connection +from .i2p import I2PDialer class Manager(threading.Thread): @@ -107,7 +105,10 @@ class Manager(threading.Thread): if addr[1] == 'i2p' and shared.i2p_enabled: if shared.i2p_session_nick and addr[0] != shared.i2p_dest_pub: try: - d = I2PDialer(addr[0], shared.i2p_session_nick, shared.i2p_sam_host, shared.i2p_sam_port) + d = I2PDialer( + shared, + addr[0], shared.i2p_session_nick, + shared.i2p_sam_host, shared.i2p_sam_port) d.start() hosts.add(d.destination) shared.i2p_dialers.add(d) diff --git a/minode/message.py b/minode/message.py index a4ddbd5..33e723c 100644 --- a/minode/message.py +++ b/minode/message.py @@ -4,8 +4,7 @@ import hashlib import struct import time -import shared -import structure +from . import shared, structure class Header(object): diff --git a/minode/pow.py b/minode/pow.py index ab73af7..98fedd1 100644 --- a/minode/pow.py +++ b/minode/pow.py @@ -7,7 +7,7 @@ import struct import threading import time -import structure +from . import shared, structure def _pow_worker(target, initial_hash, q): diff --git a/minode/structure.py b/minode/structure.py index 9c2adae..68f47db 100644 --- a/minode/structure.py +++ b/minode/structure.py @@ -6,7 +6,7 @@ import struct import socket import time -import shared +from . import shared class VarInt(object):