diff --git a/minode/i2p/controller.py b/minode/i2p/controller.py index 25f0997..67e2372 100644 --- a/minode/i2p/controller.py +++ b/minode/i2p/controller.py @@ -3,13 +3,12 @@ import base64 import logging import os import socket -import threading import time -from .util import receive_line, pub_from_priv +from .util import I2PThread, pub_from_priv -class I2PController(threading.Thread): +class I2PController(I2PThread): def __init__(self, state, host='127.0.0.1', port=7656, dest_priv=b''): super().__init__(name='I2P Controller') @@ -41,15 +40,6 @@ class I2PController(threading.Thread): self.create_session() - def _receive_line(self): - line = receive_line(self.s) - # logging.debug('I2PController <- %s', line) - return line - - def _send(self, command): - # logging.debug('I2PController -> %s', command) - self.s.sendall(command) - def init_connection(self): self._send(b'HELLO VERSION MIN=3.0 MAX=3.3\n') self.version_reply = self._receive_line().split() diff --git a/minode/i2p/dialer.py b/minode/i2p/dialer.py index 67cec5c..acd8ab9 100644 --- a/minode/i2p/dialer.py +++ b/minode/i2p/dialer.py @@ -1,12 +1,11 @@ # -*- coding: utf-8 -*- import logging import socket -import threading -from .util import receive_line +from .util import I2PThread -class I2PDialer(threading.Thread): +class I2PDialer(I2PThread): def __init__( self, state, destination, nick, sam_host='127.0.0.1', sam_port=7656 ): @@ -34,15 +33,6 @@ class I2PDialer(threading.Thread): c.start() self.state.connections.add(c) - def _receive_line(self): - line = receive_line(self.s) - # logging.debug('I2PDialer <- %s', line) - return line - - def _send(self, command): - # logging.debug('I2PDialer -> %s', command) - self.s.sendall(command) - def _connect(self): self._send(b'HELLO VERSION MIN=3.0 MAX=3.3\n') self.version_reply = self._receive_line().split() diff --git a/minode/i2p/listener.py b/minode/i2p/listener.py index 3737797..c072d68 100644 --- a/minode/i2p/listener.py +++ b/minode/i2p/listener.py @@ -1,12 +1,11 @@ # -*- coding: utf-8 -*- import logging import socket -import threading -from .util import receive_line +from .util import I2PThread -class I2PListener(threading.Thread): +class I2PListener(I2PThread): def __init__(self, state, nick, host='127.0.0.1', port=7656): super().__init__(name='I2P Listener') @@ -21,15 +20,6 @@ class I2PListener(threading.Thread): self.new_socket() - def _receive_line(self): - line = receive_line(self.s) - # logging.debug('I2PListener <- %s', line) - return line - - def _send(self, command): - # logging.debug('I2PListener -> %s', command) - self.s.sendall(command) - def new_socket(self): self.s = socket.create_connection((self.host, self.port)) self._send(b'HELLO VERSION MIN=3.0 MAX=3.3\n') diff --git a/minode/i2p/util.py b/minode/i2p/util.py index f5fa651..26d31f3 100644 --- a/minode/i2p/util.py +++ b/minode/i2p/util.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- import base64 import hashlib +import threading def receive_line(s): @@ -14,16 +15,30 @@ def receive_line(s): return data[0] +class I2PThread(threading.Thread): + """ + Abstract I2P thread with _receive_line() and _send() methods, + reused in I2PDialer, I2PListener and I2PController + """ + def _receive_line(self): + line = receive_line(self.s) + # logging.debug('I2PListener <- %s', line) + return line + + def _send(self, command): + # logging.debug('I2PListener -> %s', command) + self.s.sendall(command) + + def pub_from_priv(priv): priv = base64.b64decode(priv, altchars=b'-~') # 256 for public key + 128 for signing key + 3 for certificate header # + value of bytes priv[385:387] pub = priv[:387 + int.from_bytes(priv[385:387], byteorder='big')] - pub = base64.b64encode(pub, altchars=b'-~') - return pub + return base64.b64encode(pub, altchars=b'-~') def b32_from_pub(pub): return base64.b32encode( hashlib.sha256(base64.b64decode(pub, b'-~')).digest() - ).replace(b"=", b"").lower() + b'.b32.i2p' + ).replace(b'=', b'').lower() + b'.b32.i2p'