2017-06-09 20:41:33 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import logging
|
|
|
|
import socket
|
2017-07-01 15:05:06 +02:00
|
|
|
import threading
|
2017-06-09 20:41:33 +02:00
|
|
|
|
2021-03-09 15:40:59 +01:00
|
|
|
from .util import receive_line
|
2017-06-09 20:41:33 +02:00
|
|
|
|
|
|
|
|
2017-07-01 15:05:06 +02:00
|
|
|
class I2PDialer(threading.Thread):
|
2021-03-09 15:40:59 +01:00
|
|
|
def __init__(
|
|
|
|
self, state, destination, nick, sam_host='127.0.0.1', sam_port=7656
|
|
|
|
):
|
|
|
|
self.state = state
|
2017-07-01 15:05:06 +02:00
|
|
|
self.sam_host = sam_host
|
|
|
|
self.sam_port = sam_port
|
2017-06-09 20:41:33 +02:00
|
|
|
|
|
|
|
self.nick = nick
|
|
|
|
self.destination = destination
|
|
|
|
|
2017-07-01 15:05:06 +02:00
|
|
|
super().__init__(name='I2P Dial to {}'.format(self.destination))
|
|
|
|
|
|
|
|
self.s = socket.create_connection((self.sam_host, self.sam_port))
|
2017-06-09 20:41:33 +02:00
|
|
|
|
|
|
|
self.version_reply = []
|
2017-07-01 15:05:06 +02:00
|
|
|
self.success = True
|
2017-06-09 20:41:33 +02:00
|
|
|
|
2017-07-01 15:05:06 +02:00
|
|
|
def run(self):
|
2021-03-08 16:06:07 +01:00
|
|
|
logging.debug('Connecting to %s', self.destination)
|
2017-06-09 20:41:33 +02:00
|
|
|
self._connect()
|
2021-03-09 15:40:59 +01:00
|
|
|
if not self.state.shutting_down and self.success:
|
|
|
|
c = self.state.connection(
|
|
|
|
self.destination, 'i2p', self.s, 'i2p',
|
|
|
|
False, self.destination)
|
2017-07-01 15:05:06 +02:00
|
|
|
c.start()
|
2021-03-09 15:40:59 +01:00
|
|
|
self.state.connections.add(c)
|
2017-06-09 20:41:33 +02:00
|
|
|
|
|
|
|
def _receive_line(self):
|
|
|
|
line = receive_line(self.s)
|
2021-03-08 16:06:07 +01:00
|
|
|
# logging.debug('I2PDialer <- %s', line)
|
2017-06-09 20:41:33 +02:00
|
|
|
return line
|
|
|
|
|
|
|
|
def _send(self, command):
|
2021-03-08 16:06:07 +01:00
|
|
|
# logging.debug('I2PDialer -> %s', command)
|
2017-06-09 20:41:33 +02:00
|
|
|
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()
|
2017-07-01 15:05:06 +02:00
|
|
|
if b'RESULT=OK' not in self.version_reply:
|
2021-03-08 16:06:07 +01:00
|
|
|
logging.warning('Error while connecting to %s', self.destination)
|
2017-07-01 15:05:06 +02:00
|
|
|
self.success = False
|
2017-06-09 20:41:33 +02:00
|
|
|
|
2021-03-08 16:06:07 +01:00
|
|
|
self._send(
|
|
|
|
b'STREAM CONNECT ID=' + self.nick + b' DESTINATION='
|
|
|
|
+ self.destination + b'\n')
|
2017-06-09 20:41:33 +02:00
|
|
|
reply = self._receive_line().split(b' ')
|
2017-07-01 15:05:06 +02:00
|
|
|
if b'RESULT=OK' not in reply:
|
2021-03-08 16:06:07 +01:00
|
|
|
logging.warning(
|
|
|
|
'Error while connecting to %s', self.destination)
|
2017-07-01 15:05:06 +02:00
|
|
|
self.success = False
|