2016-12-01 16:48:04 +01:00
|
|
|
import socket
|
2017-06-24 12:23:56 +02:00
|
|
|
import time
|
2017-06-10 10:13:49 +02:00
|
|
|
|
2017-01-10 21:22:22 +01:00
|
|
|
from advanceddispatcher import AdvancedDispatcher
|
2017-03-10 23:11:57 +01:00
|
|
|
import asyncore_pollchoose as asyncore
|
2018-02-18 20:53:16 +01:00
|
|
|
from bmconfigparser import BMConfigParser
|
2017-06-24 12:23:56 +02:00
|
|
|
from debug import logger
|
2017-06-10 10:13:49 +02:00
|
|
|
import network.connectionpool
|
2017-06-24 12:23:56 +02:00
|
|
|
import state
|
|
|
|
|
|
|
|
class ProxyError(Exception):
|
|
|
|
errorCodes = ("UnknownError")
|
|
|
|
|
2017-12-29 08:49:08 +01:00
|
|
|
def __init__(self, code=-1):
|
2017-06-24 12:23:56 +02:00
|
|
|
self.code = code
|
|
|
|
try:
|
|
|
|
self.message = self.__class__.errorCodes[self.code]
|
|
|
|
except IndexError:
|
|
|
|
self.message = self.__class__.errorCodes[-1]
|
|
|
|
super(ProxyError, self).__init__(self.message)
|
|
|
|
|
|
|
|
|
|
|
|
class GeneralProxyError(ProxyError):
|
|
|
|
errorCodes = ("Success",
|
|
|
|
"Invalid data",
|
|
|
|
"Not connected",
|
|
|
|
"Not available",
|
|
|
|
"Bad proxy type",
|
|
|
|
"Bad input",
|
|
|
|
"Timed out",
|
|
|
|
"Network unreachable",
|
|
|
|
"Connection refused",
|
|
|
|
"Host unreachable")
|
2017-03-10 23:11:57 +01:00
|
|
|
|
2016-12-01 16:48:04 +01:00
|
|
|
|
2017-01-10 21:22:22 +01:00
|
|
|
class Proxy(AdvancedDispatcher):
|
2016-12-01 16:48:04 +01:00
|
|
|
# these are global, and if you change config during runtime, all active/new
|
|
|
|
# instances should change too
|
2017-03-10 23:11:57 +01:00
|
|
|
_proxy = ("127.0.0.1", 9050)
|
2016-12-01 16:48:04 +01:00
|
|
|
_auth = None
|
2018-02-04 21:03:54 +01:00
|
|
|
_onion_proxy = None
|
|
|
|
_onion_auth = None
|
2016-12-01 16:48:04 +01:00
|
|
|
_remote_dns = True
|
|
|
|
|
|
|
|
@property
|
|
|
|
def proxy(self):
|
|
|
|
return self.__class__._proxy
|
|
|
|
|
|
|
|
@proxy.setter
|
|
|
|
def proxy(self, address):
|
2017-06-24 12:23:56 +02:00
|
|
|
if not isinstance(address, tuple) or (len(address) < 2) or \
|
|
|
|
(not isinstance(address[0], str) or not isinstance(address[1], int)):
|
2017-03-10 23:11:57 +01:00
|
|
|
raise ValueError
|
2016-12-01 16:48:04 +01:00
|
|
|
self.__class__._proxy = address
|
|
|
|
|
|
|
|
@property
|
|
|
|
def auth(self):
|
|
|
|
return self.__class__._auth
|
|
|
|
|
|
|
|
@auth.setter
|
|
|
|
def auth(self, authTuple):
|
|
|
|
self.__class__._auth = authTuple
|
|
|
|
|
2018-02-04 21:03:54 +01:00
|
|
|
@property
|
|
|
|
def onion_proxy(self):
|
|
|
|
return self.__class__._onion_proxy
|
|
|
|
|
|
|
|
@onion_proxy.setter
|
|
|
|
def onion_proxy(self, address):
|
|
|
|
if address is not None and (not isinstance(address, tuple) or (len(address) < 2) or \
|
|
|
|
(not isinstance(address[0], str) or not isinstance(address[1], int))):
|
|
|
|
raise ValueError
|
|
|
|
self.__class__._onion_proxy = address
|
|
|
|
|
|
|
|
@property
|
|
|
|
def onion_auth(self):
|
|
|
|
return self.__class__._onion_auth
|
|
|
|
|
|
|
|
@onion_auth.setter
|
|
|
|
def onion_auth(self, authTuple):
|
|
|
|
self.__class__._onion_auth = authTuple
|
|
|
|
|
2017-03-10 23:11:57 +01:00
|
|
|
def __init__(self, address):
|
2017-06-10 10:13:49 +02:00
|
|
|
if not isinstance(address, state.Peer):
|
2017-03-10 23:11:57 +01:00
|
|
|
raise ValueError
|
|
|
|
AdvancedDispatcher.__init__(self)
|
2016-12-01 16:48:04 +01:00
|
|
|
self.destination = address
|
2017-06-10 10:13:49 +02:00
|
|
|
self.isOutbound = True
|
2017-11-19 13:48:43 +01:00
|
|
|
self.fullyEstablished = False
|
2016-12-01 16:48:04 +01:00
|
|
|
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
|
2018-02-18 20:53:16 +01:00
|
|
|
if BMConfigParser().safeGetBoolean("bitmessagesettings", "socksauthentication"):
|
|
|
|
self.auth = (BMConfigParser().safeGet("bitmessagesettings", "socksusername"),
|
|
|
|
BMConfigParser().safeGet("bitmessagesettings", "sockspassword"))
|
|
|
|
else:
|
|
|
|
self.auth = None
|
2018-02-04 21:03:54 +01:00
|
|
|
if address.host.endswith(".onion") and self.onion_proxy is not None:
|
|
|
|
self.connect(self.onion_proxy)
|
|
|
|
else:
|
|
|
|
self.connect(self.proxy)
|
2017-06-10 10:13:49 +02:00
|
|
|
|
|
|
|
def handle_connect(self):
|
2017-06-24 12:23:56 +02:00
|
|
|
self.set_state("init")
|
2017-06-10 10:13:49 +02:00
|
|
|
try:
|
|
|
|
AdvancedDispatcher.handle_connect(self)
|
|
|
|
except socket.error as e:
|
|
|
|
if e.errno in asyncore._DISCONNECTED:
|
2017-06-24 12:23:56 +02:00
|
|
|
logger.debug("%s:%i: Connection failed: %s", self.destination.host, self.destination.port, str(e))
|
2017-06-10 10:13:49 +02:00
|
|
|
return
|
2017-06-24 12:23:56 +02:00
|
|
|
self.state_init()
|
2017-06-10 10:13:49 +02:00
|
|
|
|
|
|
|
def state_proxy_handshake_done(self):
|
|
|
|
self.connectedAt = time.time()
|
|
|
|
return False
|