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 time
import message
import shared
from . import message, shared
class Advertiser(threading.Thread):

View File

@ -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

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 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':

View File

@ -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)

View File

@ -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

View File

@ -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):

View File

@ -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:

View File

@ -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)

View File

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

View File

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

View File

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