2018-02-04 21:03:54 +01:00
|
|
|
from ConfigParser import NoOptionError, NoSectionError
|
2017-05-24 16:51:49 +02:00
|
|
|
import errno
|
|
|
|
import socket
|
|
|
|
import time
|
|
|
|
import random
|
2017-05-27 19:09:21 +02:00
|
|
|
import re
|
2017-05-24 16:51:49 +02:00
|
|
|
|
|
|
|
from bmconfigparser import BMConfigParser
|
|
|
|
from debug import logger
|
|
|
|
import helper_bootstrap
|
2018-07-22 18:24:08 +02:00
|
|
|
from knownnodes import knownNodes
|
2017-06-10 10:13:49 +02:00
|
|
|
from network.proxy import Proxy
|
2018-02-03 11:46:39 +01:00
|
|
|
from network.tcp import TCPServer, Socks5BMConnection, Socks4aBMConnection, TCPConnection
|
|
|
|
from network.udp import UDPSocket
|
2017-05-24 16:51:49 +02:00
|
|
|
from network.connectionchooser import chooseConnection
|
|
|
|
import network.asyncore_pollchoose as asyncore
|
|
|
|
import protocol
|
|
|
|
from singleton import Singleton
|
|
|
|
import state
|
2018-03-21 14:56:27 +01:00
|
|
|
import helper_random
|
2017-05-24 16:51:49 +02:00
|
|
|
|
|
|
|
@Singleton
|
|
|
|
class BMConnectionPool(object):
|
|
|
|
def __init__(self):
|
|
|
|
asyncore.set_rates(
|
2018-01-01 12:49:08 +01:00
|
|
|
BMConfigParser().safeGetInt("bitmessagesettings", "maxdownloadrate"),
|
|
|
|
BMConfigParser().safeGetInt("bitmessagesettings", "maxuploadrate"))
|
2017-05-24 16:51:49 +02:00
|
|
|
self.outboundConnections = {}
|
|
|
|
self.inboundConnections = {}
|
|
|
|
self.listeningSockets = {}
|
2017-05-27 19:09:21 +02:00
|
|
|
self.udpSockets = {}
|
2017-05-24 16:51:49 +02:00
|
|
|
self.streams = []
|
2017-05-29 12:56:59 +02:00
|
|
|
self.lastSpawned = 0
|
2017-07-06 19:45:36 +02:00
|
|
|
self.spawnWait = 2
|
2017-05-24 16:51:49 +02:00
|
|
|
self.bootstrapped = False
|
|
|
|
|
|
|
|
def connectToStream(self, streamNumber):
|
|
|
|
self.streams.append(streamNumber)
|
|
|
|
|
2017-07-08 18:02:47 +02:00
|
|
|
def getConnectionByAddr(self, addr):
|
|
|
|
if addr in self.inboundConnections:
|
|
|
|
return self.inboundConnections[addr]
|
2018-01-02 22:23:03 +01:00
|
|
|
try:
|
|
|
|
if addr.host in self.inboundConnections:
|
|
|
|
return self.inboundConnections[addr.host]
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2017-07-08 18:02:47 +02:00
|
|
|
if addr in self.outboundConnections:
|
|
|
|
return self.outboundConnections[addr]
|
2018-01-02 22:23:03 +01:00
|
|
|
try:
|
|
|
|
if addr.host in self.udpSockets:
|
|
|
|
return self.udpSockets[addr.host]
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2017-07-08 18:02:47 +02:00
|
|
|
raise KeyError
|
|
|
|
|
2017-07-10 07:10:05 +02:00
|
|
|
def isAlreadyConnected(self, nodeid):
|
|
|
|
for i in self.inboundConnections.values() + self.outboundConnections.values():
|
|
|
|
try:
|
|
|
|
if nodeid == i.nodeid:
|
|
|
|
return True
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
return False
|
|
|
|
|
2017-05-24 16:51:49 +02:00
|
|
|
def addConnection(self, connection):
|
2018-02-03 11:46:39 +01:00
|
|
|
if isinstance(connection, UDPSocket):
|
2017-05-27 19:09:21 +02:00
|
|
|
return
|
2017-05-24 16:51:49 +02:00
|
|
|
if connection.isOutbound:
|
|
|
|
self.outboundConnections[connection.destination] = connection
|
|
|
|
else:
|
|
|
|
if connection.destination.host in self.inboundConnections:
|
|
|
|
self.inboundConnections[connection.destination] = connection
|
|
|
|
else:
|
|
|
|
self.inboundConnections[connection.destination.host] = connection
|
|
|
|
|
|
|
|
def removeConnection(self, connection):
|
2018-02-03 11:46:39 +01:00
|
|
|
if isinstance(connection, UDPSocket):
|
2017-06-11 14:11:39 +02:00
|
|
|
del self.udpSockets[connection.listening.host]
|
2018-02-03 11:46:39 +01:00
|
|
|
elif isinstance(connection, TCPServer):
|
2017-05-30 23:53:43 +02:00
|
|
|
del self.listeningSockets[state.Peer(connection.destination.host, connection.destination.port)]
|
2017-05-27 19:09:21 +02:00
|
|
|
elif connection.isOutbound:
|
2017-05-24 16:51:49 +02:00
|
|
|
try:
|
|
|
|
del self.outboundConnections[connection.destination]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
del self.inboundConnections[connection.destination]
|
|
|
|
except KeyError:
|
|
|
|
try:
|
|
|
|
del self.inboundConnections[connection.destination.host]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2017-11-19 00:05:55 +01:00
|
|
|
connection.close()
|
2017-05-24 16:51:49 +02:00
|
|
|
|
2017-05-27 19:09:21 +02:00
|
|
|
def getListeningIP(self):
|
2017-05-24 16:51:49 +02:00
|
|
|
if BMConfigParser().safeGet("bitmessagesettings", "onionhostname").endswith(".onion"):
|
2017-08-09 17:29:48 +02:00
|
|
|
host = BMConfigParser().safeGet("bitmessagesettings", "onionbindip")
|
2017-05-24 16:51:49 +02:00
|
|
|
else:
|
|
|
|
host = '127.0.0.1'
|
|
|
|
if BMConfigParser().safeGetBoolean("bitmessagesettings", "sockslisten") or \
|
|
|
|
BMConfigParser().get("bitmessagesettings", "socksproxytype") == "none":
|
2017-05-27 19:09:21 +02:00
|
|
|
# python doesn't like bind + INADDR_ANY?
|
|
|
|
#host = socket.INADDR_ANY
|
2017-08-09 17:34:47 +02:00
|
|
|
host = BMConfigParser().get("network", "bind")
|
2017-05-27 19:09:21 +02:00
|
|
|
return host
|
|
|
|
|
2017-08-09 23:30:22 +02:00
|
|
|
def startListening(self, bind=None):
|
|
|
|
if bind is None:
|
|
|
|
bind = self.getListeningIP()
|
2017-05-27 19:09:21 +02:00
|
|
|
port = BMConfigParser().safeGetInt("bitmessagesettings", "port")
|
2017-08-09 17:29:23 +02:00
|
|
|
# correct port even if it changed
|
2018-02-03 11:46:39 +01:00
|
|
|
ls = TCPServer(host=bind, port=port)
|
2017-08-09 17:29:23 +02:00
|
|
|
self.listeningSockets[ls.destination] = ls
|
2017-05-27 19:09:21 +02:00
|
|
|
|
|
|
|
def startUDPSocket(self, bind=None):
|
|
|
|
if bind is None:
|
|
|
|
host = self.getListeningIP()
|
2018-02-03 11:46:39 +01:00
|
|
|
udpSocket = UDPSocket(host=host, announcing=True)
|
2017-05-27 19:09:21 +02:00
|
|
|
else:
|
2017-08-09 17:34:47 +02:00
|
|
|
if bind is False:
|
2018-02-03 11:46:39 +01:00
|
|
|
udpSocket = UDPSocket(announcing=False)
|
2017-08-09 17:34:47 +02:00
|
|
|
else:
|
2018-02-03 11:46:39 +01:00
|
|
|
udpSocket = UDPSocket(host=bind, announcing=True)
|
2017-06-11 14:11:39 +02:00
|
|
|
self.udpSockets[udpSocket.listening.host] = udpSocket
|
2017-05-24 16:51:49 +02:00
|
|
|
|
|
|
|
def loop(self):
|
|
|
|
# defaults to empty loop if outbound connections are maxed
|
|
|
|
spawnConnections = False
|
|
|
|
acceptConnections = True
|
|
|
|
if BMConfigParser().safeGetBoolean('bitmessagesettings', 'dontconnect'):
|
|
|
|
acceptConnections = False
|
2017-09-21 18:18:42 +02:00
|
|
|
elif BMConfigParser().safeGetBoolean('bitmessagesettings', 'sendoutgoingconnections'):
|
2017-05-24 16:51:49 +02:00
|
|
|
spawnConnections = True
|
|
|
|
if BMConfigParser().get('bitmessagesettings', 'socksproxytype')[0:5] == 'SOCKS' and \
|
|
|
|
(not BMConfigParser().getboolean('bitmessagesettings', 'sockslisten') and \
|
|
|
|
".onion" not in BMConfigParser().get('bitmessagesettings', 'onionhostname')):
|
|
|
|
acceptConnections = False
|
|
|
|
|
|
|
|
if spawnConnections:
|
2018-07-22 18:24:08 +02:00
|
|
|
if not any([knownNodes.iteritems()]):
|
2017-05-24 16:51:49 +02:00
|
|
|
helper_bootstrap.dns()
|
2018-07-22 18:24:08 +02:00
|
|
|
if not self.bootstrapped:
|
2017-05-24 16:51:49 +02:00
|
|
|
self.bootstrapped = True
|
2017-06-10 10:13:49 +02:00
|
|
|
Proxy.proxy = (BMConfigParser().safeGet("bitmessagesettings", "sockshostname"),
|
|
|
|
BMConfigParser().safeGetInt("bitmessagesettings", "socksport"))
|
2018-02-04 21:03:54 +01:00
|
|
|
# TODO AUTH
|
|
|
|
# TODO reset based on GUI settings changes
|
|
|
|
try:
|
2018-02-04 21:16:30 +01:00
|
|
|
if not BMConfigParser().get("network", "onionsocksproxytype").startswith("SOCKS"):
|
2018-02-04 21:03:54 +01:00
|
|
|
raise NoOptionError
|
|
|
|
Proxy.onionproxy = (BMConfigParser().get("network", "onionsockshostname"),
|
|
|
|
BMConfigParser().getint("network", "onionsocksport"))
|
|
|
|
except (NoOptionError, NoSectionError):
|
|
|
|
Proxy.onionproxy = None
|
2017-05-25 23:04:33 +02:00
|
|
|
established = sum(1 for c in self.outboundConnections.values() if (c.connected and c.fullyEstablished))
|
|
|
|
pending = len(self.outboundConnections) - established
|
|
|
|
if established < BMConfigParser().safeGetInt("bitmessagesettings", "maxoutboundconnections"):
|
|
|
|
for i in range(state.maximumNumberOfHalfOpenConnections - pending):
|
2017-07-05 09:17:01 +02:00
|
|
|
try:
|
2018-03-21 14:56:27 +01:00
|
|
|
chosen = chooseConnection(helper_random.randomchoice(self.streams))
|
2017-07-05 09:17:01 +02:00
|
|
|
except ValueError:
|
|
|
|
continue
|
2017-05-25 23:04:33 +02:00
|
|
|
if chosen in self.outboundConnections:
|
|
|
|
continue
|
|
|
|
if chosen.host in self.inboundConnections:
|
2017-05-24 16:51:49 +02:00
|
|
|
continue
|
2017-05-31 00:04:21 +02:00
|
|
|
# don't connect to self
|
2017-05-31 00:22:07 +02:00
|
|
|
if chosen in state.ownAddresses:
|
2017-05-31 00:04:21 +02:00
|
|
|
continue
|
2017-05-25 23:04:33 +02:00
|
|
|
|
|
|
|
#for c in self.outboundConnections:
|
|
|
|
# if chosen == c.destination:
|
|
|
|
# continue
|
|
|
|
#for c in self.inboundConnections:
|
|
|
|
# if chosen.host == c.destination.host:
|
|
|
|
# continue
|
|
|
|
try:
|
2018-02-04 21:03:54 +01:00
|
|
|
if chosen.host.endswith(".onion") and Proxy.onionproxy is not None:
|
|
|
|
if BMConfigParser().get("network", "onionsocksproxytype") == "SOCKS5":
|
|
|
|
self.addConnection(Socks5BMConnection(chosen))
|
|
|
|
elif BMConfigParser().get("network", "onionsocksproxytype") == "SOCKS4a":
|
|
|
|
self.addConnection(Socks4aBMConnection(chosen))
|
|
|
|
elif BMConfigParser().safeGet("bitmessagesettings", "socksproxytype") == "SOCKS5":
|
2018-02-03 11:46:39 +01:00
|
|
|
self.addConnection(Socks5BMConnection(chosen))
|
2018-02-04 21:03:54 +01:00
|
|
|
elif BMConfigParser().safeGet("bitmessagesettings", "socksproxytype") == "SOCKS4a":
|
2018-02-03 11:46:39 +01:00
|
|
|
self.addConnection(Socks4aBMConnection(chosen))
|
2018-02-04 21:03:54 +01:00
|
|
|
else:
|
2018-02-03 11:46:39 +01:00
|
|
|
self.addConnection(TCPConnection(chosen))
|
2017-05-25 23:04:33 +02:00
|
|
|
except socket.error as e:
|
|
|
|
if e.errno == errno.ENETUNREACH:
|
|
|
|
continue
|
2018-02-04 21:03:54 +01:00
|
|
|
except (NoSectionError, NoOptionError):
|
|
|
|
# shouldn't happen
|
|
|
|
pass
|
2017-05-24 16:51:49 +02:00
|
|
|
|
2017-05-29 12:56:59 +02:00
|
|
|
self.lastSpawned = time.time()
|
2018-01-30 12:55:01 +01:00
|
|
|
else:
|
|
|
|
for i in (
|
|
|
|
self.inboundConnections.values() +
|
|
|
|
self.outboundConnections.values()
|
|
|
|
):
|
|
|
|
i.set_state("close")
|
|
|
|
# FIXME: rating will be increased after next connection
|
|
|
|
i.handle_close()
|
2017-05-29 12:56:59 +02:00
|
|
|
|
2017-06-24 12:23:16 +02:00
|
|
|
if acceptConnections:
|
|
|
|
if not self.listeningSockets:
|
2017-08-09 23:30:22 +02:00
|
|
|
if BMConfigParser().safeGet("network", "bind") == '':
|
|
|
|
self.startListening()
|
|
|
|
else:
|
|
|
|
for bind in re.sub("[^\w.]+", " ", BMConfigParser().safeGet("network", "bind")).split():
|
|
|
|
self.startListening(bind)
|
2017-06-24 12:23:16 +02:00
|
|
|
logger.info('Listening for incoming connections.')
|
|
|
|
if not self.udpSockets:
|
2017-08-09 17:34:47 +02:00
|
|
|
if BMConfigParser().safeGet("network", "bind") == '':
|
2017-06-24 12:23:16 +02:00
|
|
|
self.startUDPSocket()
|
|
|
|
else:
|
|
|
|
for bind in re.sub("[^\w.]+", " ", BMConfigParser().safeGet("network", "bind")).split():
|
|
|
|
self.startUDPSocket(bind)
|
2017-08-09 17:34:47 +02:00
|
|
|
self.startUDPSocket(False)
|
2017-06-24 12:23:16 +02:00
|
|
|
logger.info('Starting UDP socket(s).')
|
|
|
|
else:
|
|
|
|
if self.listeningSockets:
|
2017-08-09 17:34:47 +02:00
|
|
|
for i in self.listeningSockets.values():
|
2017-11-17 13:37:51 +01:00
|
|
|
i.close_reason = "Stopping listening"
|
|
|
|
i.accepting = i.connecting = i.connected = False
|
2017-06-24 12:23:16 +02:00
|
|
|
logger.info('Stopped listening for incoming connections.')
|
|
|
|
if self.udpSockets:
|
2017-08-09 17:34:47 +02:00
|
|
|
for i in self.udpSockets.values():
|
2017-11-17 13:37:51 +01:00
|
|
|
i.close_reason = "Stopping UDP socket"
|
|
|
|
i.accepting = i.connecting = i.connected = False
|
2017-06-24 12:23:16 +02:00
|
|
|
logger.info('Stopped udp sockets.')
|
2017-05-24 16:51:49 +02:00
|
|
|
|
2017-05-29 12:56:59 +02:00
|
|
|
loopTime = float(self.spawnWait)
|
|
|
|
if self.lastSpawned < time.time() - self.spawnWait:
|
2017-07-06 19:45:36 +02:00
|
|
|
loopTime = 2.0
|
|
|
|
asyncore.loop(timeout=loopTime, count=1000)
|
2017-05-24 16:51:49 +02:00
|
|
|
|
2017-05-30 23:53:43 +02:00
|
|
|
reaper = []
|
2017-05-24 16:51:49 +02:00
|
|
|
for i in self.inboundConnections.values() + self.outboundConnections.values():
|
|
|
|
minTx = time.time() - 20
|
2017-05-25 23:04:33 +02:00
|
|
|
if i.fullyEstablished:
|
2017-05-24 16:51:49 +02:00
|
|
|
minTx -= 300 - 20
|
|
|
|
if i.lastTx < minTx:
|
2017-05-25 23:04:33 +02:00
|
|
|
if i.fullyEstablished:
|
2017-07-06 19:45:36 +02:00
|
|
|
i.append_write_buf(protocol.CreatePacket('ping'))
|
2017-05-25 14:59:18 +02:00
|
|
|
else:
|
2017-10-19 09:08:05 +02:00
|
|
|
i.close_reason = "Timeout (%is)" % (time.time() - i.lastTx)
|
2017-11-14 23:43:05 +01:00
|
|
|
i.set_state("close")
|
2017-05-30 23:53:43 +02:00
|
|
|
for i in self.inboundConnections.values() + self.outboundConnections.values() + self.listeningSockets.values() + self.udpSockets.values():
|
|
|
|
if not (i.accepting or i.connecting or i.connected):
|
|
|
|
reaper.append(i)
|
2017-11-17 19:50:39 +01:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
if i.state == "close":
|
|
|
|
reaper.append(i)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2017-05-30 23:53:43 +02:00
|
|
|
for i in reaper:
|
|
|
|
self.removeConnection(i)
|