Inherit I2P classes from base util.I2PThread()
This commit is contained in:
parent
0584956d13
commit
82c4062325
|
@ -3,13 +3,12 @@ import base64
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import socket
|
import socket
|
||||||
import threading
|
|
||||||
import time
|
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''):
|
def __init__(self, state, host='127.0.0.1', port=7656, dest_priv=b''):
|
||||||
super().__init__(name='I2P Controller')
|
super().__init__(name='I2P Controller')
|
||||||
|
|
||||||
|
@ -41,15 +40,6 @@ class I2PController(threading.Thread):
|
||||||
|
|
||||||
self.create_session()
|
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):
|
def init_connection(self):
|
||||||
self._send(b'HELLO VERSION MIN=3.0 MAX=3.3\n')
|
self._send(b'HELLO VERSION MIN=3.0 MAX=3.3\n')
|
||||||
self.version_reply = self._receive_line().split()
|
self.version_reply = self._receive_line().split()
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
import logging
|
import logging
|
||||||
import socket
|
import socket
|
||||||
import threading
|
|
||||||
|
|
||||||
from .util import receive_line
|
from .util import I2PThread
|
||||||
|
|
||||||
|
|
||||||
class I2PDialer(threading.Thread):
|
class I2PDialer(I2PThread):
|
||||||
def __init__(
|
def __init__(
|
||||||
self, state, destination, nick, sam_host='127.0.0.1', sam_port=7656
|
self, state, destination, nick, sam_host='127.0.0.1', sam_port=7656
|
||||||
):
|
):
|
||||||
|
@ -34,15 +33,6 @@ class I2PDialer(threading.Thread):
|
||||||
c.start()
|
c.start()
|
||||||
self.state.connections.add(c)
|
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):
|
def _connect(self):
|
||||||
self._send(b'HELLO VERSION MIN=3.0 MAX=3.3\n')
|
self._send(b'HELLO VERSION MIN=3.0 MAX=3.3\n')
|
||||||
self.version_reply = self._receive_line().split()
|
self.version_reply = self._receive_line().split()
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
import logging
|
import logging
|
||||||
import socket
|
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):
|
def __init__(self, state, nick, host='127.0.0.1', port=7656):
|
||||||
super().__init__(name='I2P Listener')
|
super().__init__(name='I2P Listener')
|
||||||
|
|
||||||
|
@ -21,15 +20,6 @@ class I2PListener(threading.Thread):
|
||||||
|
|
||||||
self.new_socket()
|
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):
|
def new_socket(self):
|
||||||
self.s = socket.create_connection((self.host, self.port))
|
self.s = socket.create_connection((self.host, self.port))
|
||||||
self._send(b'HELLO VERSION MIN=3.0 MAX=3.3\n')
|
self._send(b'HELLO VERSION MIN=3.0 MAX=3.3\n')
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
import base64
|
import base64
|
||||||
import hashlib
|
import hashlib
|
||||||
|
import threading
|
||||||
|
|
||||||
|
|
||||||
def receive_line(s):
|
def receive_line(s):
|
||||||
|
@ -14,16 +15,30 @@ def receive_line(s):
|
||||||
return data[0]
|
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):
|
def pub_from_priv(priv):
|
||||||
priv = base64.b64decode(priv, altchars=b'-~')
|
priv = base64.b64decode(priv, altchars=b'-~')
|
||||||
# 256 for public key + 128 for signing key + 3 for certificate header
|
# 256 for public key + 128 for signing key + 3 for certificate header
|
||||||
# + value of bytes priv[385:387]
|
# + value of bytes priv[385:387]
|
||||||
pub = priv[:387 + int.from_bytes(priv[385:387], byteorder='big')]
|
pub = priv[:387 + int.from_bytes(priv[385:387], byteorder='big')]
|
||||||
pub = base64.b64encode(pub, altchars=b'-~')
|
return base64.b64encode(pub, altchars=b'-~')
|
||||||
return pub
|
|
||||||
|
|
||||||
|
|
||||||
def b32_from_pub(pub):
|
def b32_from_pub(pub):
|
||||||
return base64.b32encode(
|
return base64.b32encode(
|
||||||
hashlib.sha256(base64.b64decode(pub, b'-~')).digest()
|
hashlib.sha256(base64.b64decode(pub, b'-~')).digest()
|
||||||
).replace(b"=", b"").lower() + b'.b32.i2p'
|
).replace(b'=', b'').lower() + b'.b32.i2p'
|
||||||
|
|
Loading…
Reference in New Issue
Block a user