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
|
2017-06-10 10:13:49 +02:00
|
|
|
from network.proxy import Proxy
|
2017-05-24 16:51:49 +02:00
|
|
|
import network.bmproto
|
2017-10-20 01:21:49 +02:00
|
|
|
from network.dandelion import Dandelion
|
2017-05-27 19:09:21 +02:00
|
|
|
import network.tcp
|
|
|
|
import network.udp
|
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
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
2017-05-29 14:52:31 +02:00
|
|
|
def handleReceivedObject(self, streamNumber, hashid, connection = None):
|
2017-05-24 16:51:49 +02:00
|
|
|
for i in self.inboundConnections.values() + self.outboundConnections.values():
|
2017-05-27 19:09:21 +02:00
|
|
|
if not isinstance(i, network.bmproto.BMProto):
|
2017-05-24 16:51:49 +02:00
|
|
|
continue
|
2017-05-31 23:34:06 +02:00
|
|
|
if not i.fullyEstablished:
|
|
|
|
continue
|
2017-05-27 19:09:21 +02:00
|
|
|
try:
|
2017-06-02 07:09:35 +02:00
|
|
|
with i.objectsNewToMeLock:
|
|
|
|
del i.objectsNewToMe[hashid]
|
2017-05-27 19:09:21 +02:00
|
|
|
except KeyError:
|
2017-06-02 07:09:35 +02:00
|
|
|
with i.objectsNewToThemLock:
|
2017-07-05 09:25:49 +02:00
|
|
|
i.objectsNewToThem[hashid] = time.time()
|
2017-05-24 16:51:49 +02:00
|
|
|
if i == connection:
|
|
|
|
try:
|
2017-06-02 07:09:35 +02:00
|
|
|
with i.objectsNewToThemLock:
|
|
|
|
del i.objectsNewToThem[hashid]
|
2017-05-24 16:51:49 +02:00
|
|
|
except KeyError:
|
|
|
|
pass
|
2017-11-18 09:47:17 +01:00
|
|
|
if hashid in Dandelion().fluff:
|
2017-11-18 10:05:41 +01:00
|
|
|
Dandelion().removeHash(hashid)
|
2017-05-24 16:51:49 +02:00
|
|
|
|
2017-10-20 01:21:49 +02:00
|
|
|
def reRandomiseDandelionStems(self):
|
2017-09-25 01:17:04 +02:00
|
|
|
# Choose 2 peers randomly
|
|
|
|
# TODO: handle streams
|
2017-10-20 01:21:49 +02:00
|
|
|
Dandelion().reRandomiseStems(self.outboundConnections.values())
|
2017-09-25 01:17:04 +02:00
|
|
|
|
2017-05-24 16:51:49 +02:00
|
|
|
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):
|
2017-05-27 19:09:21 +02:00
|
|
|
if isinstance(connection, network.udp.UDPSocket):
|
|
|
|
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):
|
2017-05-27 19:09:21 +02:00
|
|
|
if isinstance(connection, network.udp.UDPSocket):
|
2017-06-11 14:11:39 +02:00
|
|
|
del self.udpSockets[connection.listening.host]
|
2017-06-24 12:23:16 +02:00
|
|
|
elif isinstance(connection, network.tcp.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
|
2017-08-09 23:30:22 +02:00
|
|
|
ls = network.tcp.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()
|
2017-08-09 17:34:47 +02:00
|
|
|
udpSocket = network.udp.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:
|
|
|
|
udpSocket = network.udp.UDPSocket(announcing=False)
|
|
|
|
else:
|
|
|
|
udpSocket = network.udp.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:
|
|
|
|
if not self.bootstrapped:
|
|
|
|
helper_bootstrap.dns()
|
|
|
|
self.bootstrapped = True
|
2017-06-10 10:13:49 +02:00
|
|
|
Proxy.proxy = (BMConfigParser().safeGet("bitmessagesettings", "sockshostname"),
|
|
|
|
BMConfigParser().safeGetInt("bitmessagesettings", "socksport"))
|
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:
|
|
|
|
chosen = chooseConnection(random.choice(self.streams))
|
|
|
|
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:
|
|
|
|
if (BMConfigParser().safeGet("bitmessagesettings", "socksproxytype") == "SOCKS5"):
|
2017-05-27 19:09:21 +02:00
|
|
|
self.addConnection(network.tcp.Socks5BMConnection(chosen))
|
2017-05-25 23:04:33 +02:00
|
|
|
elif (BMConfigParser().safeGet("bitmessagesettings", "socksproxytype") == "SOCKS4a"):
|
2017-05-27 19:09:21 +02:00
|
|
|
self.addConnection(network.tcp.Socks4aBMConnection(chosen))
|
2017-05-25 23:04:33 +02:00
|
|
|
elif not chosen.host.endswith(".onion"):
|
2017-05-27 19:09:21 +02:00
|
|
|
self.addConnection(network.tcp.TCPConnection(chosen))
|
2017-05-25 23:04:33 +02:00
|
|
|
except socket.error as e:
|
|
|
|
if e.errno == errno.ENETUNREACH:
|
|
|
|
continue
|
2017-05-24 16:51:49 +02:00
|
|
|
|
2017-05-29 12:56:59 +02:00
|
|
|
self.lastSpawned = time.time()
|
|
|
|
|
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)
|