network.connectionpool: get any setting with .safeGet..

This commit is contained in:
Dmitri Bogomolov 2019-05-28 16:28:35 +03:00
parent 996e71ae6f
commit 19ab56a979
Signed by untrusted user: g1itch
GPG Key ID: 720A756F18DEED13

View File

@ -2,7 +2,6 @@ import errno
import re import re
import socket import socket
import time import time
from ConfigParser import NoOptionError, NoSectionError
import asyncore_pollchoose as asyncore import asyncore_pollchoose as asyncore
import helper_bootstrap import helper_bootstrap
@ -114,7 +113,7 @@ class BMConnectionPool(object):
host = '127.0.0.1' host = '127.0.0.1'
if (BMConfigParser().safeGetBoolean( if (BMConfigParser().safeGetBoolean(
"bitmessagesettings", "sockslisten") or "bitmessagesettings", "sockslisten") or
BMConfigParser().get( BMConfigParser().safeGet(
"bitmessagesettings", "socksproxytype") == "none"): "bitmessagesettings", "socksproxytype") == "none"):
# python doesn't like bind + INADDR_ANY? # python doesn't like bind + INADDR_ANY?
# host = socket.INADDR_ANY # host = socket.INADDR_ANY
@ -150,12 +149,15 @@ class BMConnectionPool(object):
elif BMConfigParser().safeGetBoolean( elif BMConfigParser().safeGetBoolean(
'bitmessagesettings', 'sendoutgoingconnections'): 'bitmessagesettings', 'sendoutgoingconnections'):
spawnConnections = True spawnConnections = True
if (BMConfigParser().get( socksproxytype = BMConfigParser().safeGet(
'bitmessagesettings', 'socksproxytype')[:5] == 'SOCKS' and 'bitmessagesettings', 'socksproxytype', '')
not BMConfigParser().getboolean( onionsocksproxytype = BMConfigParser().safeGet(
'bitmessagesettings', 'onionsocksproxytype', '')
if (socksproxytype[:5] == 'SOCKS' and
not BMConfigParser().safeGetBoolean(
'bitmessagesettings', 'sockslisten') and 'bitmessagesettings', 'sockslisten') and
".onion" not in BMConfigParser().get( '.onion' not in BMConfigParser().safeGet(
'bitmessagesettings', 'onionhostname')): 'bitmessagesettings', 'onionhostname', '')):
acceptConnections = False acceptConnections = False
if spawnConnections: if spawnConnections:
@ -165,31 +167,29 @@ class BMConnectionPool(object):
self.bootstrapped = True self.bootstrapped = True
Proxy.proxy = ( Proxy.proxy = (
BMConfigParser().safeGet( BMConfigParser().safeGet(
"bitmessagesettings", "sockshostname"), 'bitmessagesettings', 'sockshostname'),
BMConfigParser().safeGetInt( BMConfigParser().safeGetInt(
"bitmessagesettings", "socksport") 'bitmessagesettings', 'socksport')
) )
# TODO AUTH # TODO AUTH
# TODO reset based on GUI settings changes # TODO reset based on GUI settings changes
try: try:
if not BMConfigParser().get( if not onionsocksproxytype.startswith("SOCKS"):
"network", "onionsocksproxytype" raise ValueError
).startswith("SOCKS"): Proxy.onion_proxy = (
raise NoOptionError BMConfigParser().safeGet(
Proxy.onionproxy = ( 'network', 'onionsockshostname', None),
BMConfigParser().get( BMConfigParser().safeGet(
"network", "onionsockshostname"), 'network', 'onionsocksport', None)
BMConfigParser().getint(
"network", "onionsocksport")
) )
except (NoOptionError, NoSectionError): except ValueError:
Proxy.onionproxy = None Proxy.onion_proxy = None
established = sum( established = sum(
1 for c in self.outboundConnections.values() 1 for c in self.outboundConnections.values()
if (c.connected and c.fullyEstablished)) if (c.connected and c.fullyEstablished))
pending = len(self.outboundConnections) - established pending = len(self.outboundConnections) - established
if established < BMConfigParser().safeGetInt( if established < BMConfigParser().safeGetInt(
"bitmessagesettings", "maxoutboundconnections"): 'bitmessagesettings', 'maxoutboundconnections'):
for i in range( for i in range(
state.maximumNumberOfHalfOpenConnections - pending): state.maximumNumberOfHalfOpenConnections - pending):
try: try:
@ -207,31 +207,20 @@ class BMConnectionPool(object):
try: try:
if (chosen.host.endswith(".onion") and if (chosen.host.endswith(".onion") and
Proxy.onionproxy is not None): Proxy.onion_proxy is not None):
if BMConfigParser().get( if onionsocksproxytype == "SOCKS5":
"network", "onionsocksproxytype"
) == "SOCKS5":
self.addConnection(Socks5BMConnection(chosen)) self.addConnection(Socks5BMConnection(chosen))
elif BMConfigParser().get( elif onionsocksproxytype == "SOCKS4a":
"network", "onionsocksproxytype"
) == "SOCKS4a":
self.addConnection(Socks4aBMConnection(chosen)) self.addConnection(Socks4aBMConnection(chosen))
elif BMConfigParser().safeGet( elif socksproxytype == "SOCKS5":
"bitmessagesettings", "socksproxytype"
) == "SOCKS5":
self.addConnection(Socks5BMConnection(chosen)) self.addConnection(Socks5BMConnection(chosen))
elif BMConfigParser().safeGet( elif socksproxytype == "SOCKS4a":
"bitmessagesettings", "socksproxytype"
) == "SOCKS4a":
self.addConnection(Socks4aBMConnection(chosen)) self.addConnection(Socks4aBMConnection(chosen))
else: else:
self.addConnection(TCPConnection(chosen)) self.addConnection(TCPConnection(chosen))
except socket.error as e: except socket.error as e:
if e.errno == errno.ENETUNREACH: if e.errno == errno.ENETUNREACH:
continue continue
except (NoSectionError, NoOptionError):
# shouldn't happen
pass
self.lastSpawned = time.time() self.lastSpawned = time.time()
else: else:
@ -244,22 +233,22 @@ class BMConnectionPool(object):
if acceptConnections: if acceptConnections:
if not self.listeningSockets: if not self.listeningSockets:
if BMConfigParser().safeGet("network", "bind") == '': if BMConfigParser().safeGet('network', 'bind') == '':
self.startListening() self.startListening()
else: else:
for bind in re.sub( for bind in re.sub(
"[^\w.]+", " ", "[^\w.]+", " ",
BMConfigParser().safeGet("network", "bind") BMConfigParser().safeGet('network', 'bind')
).split(): ).split():
self.startListening(bind) self.startListening(bind)
logger.info('Listening for incoming connections.') logger.info('Listening for incoming connections.')
if not self.udpSockets: if not self.udpSockets:
if BMConfigParser().safeGet("network", "bind") == '': if BMConfigParser().safeGet('network', 'bind') == '':
self.startUDPSocket() self.startUDPSocket()
else: else:
for bind in re.sub( for bind in re.sub(
"[^\w.]+", " ", "[^\w.]+", " ",
BMConfigParser().safeGet("network", "bind") BMConfigParser().safeGet('network', 'bind')
).split(): ).split():
self.startUDPSocket(bind) self.startUDPSocket(bind)
self.startUDPSocket(False) self.startUDPSocket(False)