Use dotted imports

This commit is contained in:
Dmitri Bogomolov 2021-03-09 16:40:59 +02:00
parent 6b63f8fbea
commit 83c2e48fe5
Signed by untrusted user: g1itch
GPG Key ID: 720A756F18DEED13
12 changed files with 65 additions and 54 deletions

View File

@ -2,8 +2,7 @@ import logging
import threading import threading
import time import time
import message from . import message, shared
import shared
class Advertiser(threading.Thread): class Advertiser(threading.Thread):

View File

@ -10,9 +10,7 @@ import threading
import queue import queue
import time import time
import message from . import message, shared, structure
import shared
import structure
class Connection(threading.Thread): class Connection(threading.Thread):
@ -367,4 +365,8 @@ class Connection(threading.Thread):
for vector in to_send: for vector in to_send:
obj = shared.objects.get(vector, None) obj = shared.objects.get(vector, None)
if obj: 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

View File

@ -0,0 +1,5 @@
from .controller import I2PController
from .dialer import I2PDialer
from .listener import I2PListener
__all__ = ["I2PController", "I2PDialer", "I2PListener"]

View File

@ -6,14 +6,14 @@ import socket
import threading import threading
import time import time
from i2p.util import receive_line, pub_from_priv from .util import receive_line, pub_from_priv
import shared
class I2PController(threading.Thread): 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') super().__init__(name='I2P Controller')
self.state = state
self.host = host self.host = host
self.port = port self.port = port
self.nick = b'MiNode_' + base64.b16encode(os.urandom(4)).lower() self.nick = b'MiNode_' + base64.b16encode(os.urandom(4)).lower()
@ -70,10 +70,11 @@ class I2PController(threading.Thread):
assert self.dest_priv assert self.dest_priv
def create_session(self): def create_session(self):
self._send(b'SESSION CREATE STYLE=STREAM ID=' + self.nick + self._send(
b' inbound.length=' + str(shared.i2p_tunnel_length).encode() + b'SESSION CREATE STYLE=STREAM ID=' + self.nick
b' outbound.length=' + str(shared.i2p_tunnel_length).encode() + + b' inbound.length=' + str(self.state.i2p_tunnel_length).encode()
b' DESTINATION=' + self.dest_priv + b'\n') + b' outbound.length=' + str(self.state.i2p_tunnel_length).encode()
+ b' DESTINATION=' + self.dest_priv + b'\n')
reply = self._receive_line().split() reply = self._receive_line().split()
if b'RESULT=OK' not in reply: if b'RESULT=OK' not in reply:
logging.warning(reply) logging.warning(reply)
@ -84,7 +85,7 @@ class I2PController(threading.Thread):
def run(self): def run(self):
self.s.settimeout(1) self.s.settimeout(1)
while True: while True:
if not shared.shutting_down: if not self.state.shutting_down:
try: try:
msg = self._receive_line().split(b' ') msg = self._receive_line().split(b' ')
if msg[0] == b'PING': if msg[0] == b'PING':

View File

@ -3,13 +3,14 @@ import logging
import socket import socket
import threading import threading
import shared from .util import receive_line
from connection import Connection
from i2p.util import receive_line
class I2PDialer(threading.Thread): 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_host = sam_host
self.sam_port = sam_port self.sam_port = sam_port
@ -26,10 +27,12 @@ class I2PDialer(threading.Thread):
def run(self): def run(self):
logging.debug('Connecting to {}'.format(self.destination)) logging.debug('Connecting to {}'.format(self.destination))
self._connect() self._connect()
if not shared.shutting_down and self.success: if not self.state.shutting_down and self.success:
c = Connection(self.destination, 'i2p', self.s, 'i2p', False, self.destination) c = self.state.connection(
self.destination, 'i2p', self.s, 'i2p',
False, self.destination)
c.start() c.start()
shared.connections.add(c) self.state.connections.add(c)
def _receive_line(self): def _receive_line(self):
line = receive_line(self.s) line = receive_line(self.s)

View File

@ -3,15 +3,14 @@ import logging
import socket import socket
import threading import threading
from connection import Connection from .util import receive_line
from i2p.util import receive_line
import shared
class I2PListener(threading.Thread): 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') super().__init__(name='I2P Listener')
self.state = state
self.host = host self.host = host
self.port = port self.port = port
self.nick = nick self.nick = nick
@ -44,23 +43,24 @@ class I2PListener(threading.Thread):
self.s.settimeout(1) self.s.settimeout(1)
def run(self): def run(self):
while not shared.shutting_down: while not self.state.shutting_down:
try: try:
destination = self._receive_line().split()[0] destination = self._receive_line().split()[0]
logging.info('Incoming I2P connection from: {}'.format(destination.decode())) logging.info('Incoming I2P connection from: {}'.format(destination.decode()))
hosts = set() hosts = set()
for c in shared.connections.copy(): for c in self.state.connections.copy():
hosts.add(c.host) hosts.add(c.host)
for d in shared.i2p_dialers.copy(): for d in self.state.i2p_dialers.copy():
hosts.add(d.destination) hosts.add(d.destination)
if destination in hosts: if destination in hosts:
logging.debug('Rejecting duplicate I2P connection.') logging.debug('Rejecting duplicate I2P connection.')
self.s.close() self.s.close()
else: else:
c = Connection(destination, 'i2p', self.s, 'i2p', True, destination) c = self.state.connection(
destination, 'i2p', self.s, 'i2p', True, destination)
c.start() c.start()
shared.connections.add(c) self.state.connections.add(c)
self.new_socket() self.new_socket()
except socket.timeout: except socket.timeout:
pass pass

View File

@ -3,8 +3,8 @@ import logging
import socket import socket
import threading import threading
from connection import Connection from . import shared
import shared from .connection import Connection
class Listener(threading.Thread): class Listener(threading.Thread):

View File

@ -9,12 +9,10 @@ import pickle
import signal import signal
import socket import socket
from advertiser import Advertiser from . import i2p, shared
from manager import Manager from .advertiser import Advertiser
from listener import Listener from .manager import Manager
import i2p.controller from .listener import Listener
import i2p.listener
import shared
def handler(s, f): 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: with open(shared.data_directory + 'i2p_dest_priv.key', mode='br') as file:
dest_priv = file.read() dest_priv = file.read()
logging.debug('Loaded I2P destination private key.') logging.debug('Loaded I2P destination private key.')
except Exception as e: except Exception:
logging.warning('Error while loading I2P destination private key.') logging.warning(
logging.warning(e) 'Error while loading I2P destination private key.',
exc_info=True)
logging.info('Starting I2P Controller and creating tunnels. This may take a while.') logging.info(
i2p_controller = i2p.controller.I2PController(shared.i2p_sam_host, shared.i2p_sam_port, dest_priv) '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() i2p_controller.start()
shared.i2p_dest_pub = i2p_controller.dest_pub 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('I2P session nick: {}'.format(shared.i2p_session_nick.decode()))
logging.info('Starting I2P Listener') logging.info('Starting I2P Listener')
i2p_listener = i2p.listener.I2PListener(i2p_controller.nick) i2p_listener = i2p.I2PListener(shared, i2p_controller.nick)
i2p_listener.start() i2p_listener.start()
if not shared.i2p_transient: if not shared.i2p_transient:

View File

@ -7,11 +7,9 @@ import random
import threading import threading
import time import time
from connection import Connection from . import pow, shared, structure
from i2p.dialer import I2PDialer from .connection import Connection
import pow from .i2p import I2PDialer
import shared
import structure
class Manager(threading.Thread): class Manager(threading.Thread):
@ -107,7 +105,10 @@ class Manager(threading.Thread):
if addr[1] == 'i2p' and shared.i2p_enabled: if addr[1] == 'i2p' and shared.i2p_enabled:
if shared.i2p_session_nick and addr[0] != shared.i2p_dest_pub: if shared.i2p_session_nick and addr[0] != shared.i2p_dest_pub:
try: 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() d.start()
hosts.add(d.destination) hosts.add(d.destination)
shared.i2p_dialers.add(d) shared.i2p_dialers.add(d)

View File

@ -4,8 +4,7 @@ import hashlib
import struct import struct
import time import time
import shared from . import shared, structure
import structure
class Header(object): class Header(object):

View File

@ -7,7 +7,7 @@ import struct
import threading import threading
import time import time
import structure from . import shared, structure
def _pow_worker(target, initial_hash, q): def _pow_worker(target, initial_hash, q):

View File

@ -6,7 +6,7 @@ import struct
import socket import socket
import time import time
import shared from . import shared
class VarInt(object): class VarInt(object):