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
|
|
|
|
import network.bmproto
|
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 shared
|
|
|
|
import state
|
|
|
|
|
|
|
|
@Singleton
|
|
|
|
class BMConnectionPool(object):
|
2017-05-29 12:56:59 +02:00
|
|
|
|
2017-05-24 16:51:49 +02:00
|
|
|
def __init__(self):
|
|
|
|
asyncore.set_rates(
|
2017-05-29 00:24:07 +02:00
|
|
|
BMConfigParser().safeGetInt("bitmessagesettings", "maxdownloadrate") * 1024,
|
|
|
|
BMConfigParser().safeGetInt("bitmessagesettings", "maxuploadrate") * 1024)
|
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
|
|
|
|
self.spawnWait = 0.3
|
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-27 19:09:21 +02:00
|
|
|
try:
|
|
|
|
del i.objectsNewToMe[hashid]
|
|
|
|
except KeyError:
|
|
|
|
i.objectsNewToThem[hashid] = True
|
2017-05-24 16:51:49 +02:00
|
|
|
if i == connection:
|
|
|
|
try:
|
|
|
|
del i.objectsNewToThem[hashid]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def connectToStream(self, streamNumber):
|
|
|
|
self.streams.append(streamNumber)
|
|
|
|
|
|
|
|
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-05-30 23:53:43 +02:00
|
|
|
del self.udpSockets[connection.destination.host]
|
|
|
|
if isinstance(connection, network.tcp.TCPServer):
|
|
|
|
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-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"):
|
|
|
|
host = BMConfigParser().safeGet("bitmessagesettigns", "onionbindip")
|
|
|
|
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-05-24 16:51:49 +02:00
|
|
|
host = ''
|
2017-05-27 19:09:21 +02:00
|
|
|
return host
|
|
|
|
|
|
|
|
def startListening(self):
|
|
|
|
host = self.getListeningIP()
|
|
|
|
port = BMConfigParser().safeGetInt("bitmessagesettings", "port")
|
|
|
|
self.listeningSockets[state.Peer(host, port)] = network.tcp.TCPServer(host=host, port=port)
|
|
|
|
|
|
|
|
def startUDPSocket(self, bind=None):
|
|
|
|
if bind is None:
|
|
|
|
host = self.getListeningIP()
|
2017-05-30 23:53:43 +02:00
|
|
|
udpSocket = network.udp.UDPSocket(host=host)
|
2017-05-27 19:09:21 +02:00
|
|
|
else:
|
2017-05-30 23:53:43 +02:00
|
|
|
udpSocket = network.udp.UDPSocket(host=bind)
|
|
|
|
self.udpSockets[udpSocket.destination.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
|
|
|
|
else:
|
|
|
|
spawnConnections = True
|
|
|
|
if BMConfigParser().safeGetBoolean('bitmessagesettings', 'sendoutgoingconnections'):
|
|
|
|
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-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):
|
|
|
|
chosen = chooseConnection(random.choice(self.streams))
|
|
|
|
if chosen in self.outboundConnections:
|
|
|
|
continue
|
|
|
|
if chosen.host in self.inboundConnections:
|
2017-05-24 16:51:49 +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-05-24 16:51:49 +02:00
|
|
|
if acceptConnections and len(self.listeningSockets) == 0:
|
|
|
|
self.startListening()
|
|
|
|
logger.info('Listening for incoming connections.')
|
2017-05-27 19:09:21 +02:00
|
|
|
if acceptConnections and len(self.udpSockets) == 0:
|
|
|
|
if BMConfigParser().safeGet("network", "bind") is None:
|
|
|
|
self.startUDPSocket()
|
|
|
|
else:
|
|
|
|
for bind in re.sub("[^\w.]+", " ", BMConfigParser().safeGet("network", "bind")).split():
|
|
|
|
self.startUDPSocket(bind)
|
|
|
|
logger.info('Starting UDP socket(s).')
|
2017-05-24 16:51:49 +02:00
|
|
|
if len(self.listeningSockets) > 0 and not acceptConnections:
|
|
|
|
for i in self.listeningSockets:
|
|
|
|
i.close()
|
|
|
|
logger.info('Stopped listening for incoming connections.')
|
2017-05-27 19:09:21 +02:00
|
|
|
if len(self.udpSockets) > 0 and not acceptConnections:
|
|
|
|
for i in self.udpSockets:
|
|
|
|
i.close()
|
|
|
|
logger.info('Stopped udp sockets.')
|
2017-05-24 16:51:49 +02:00
|
|
|
|
|
|
|
# while len(asyncore.socket_map) > 0 and state.shutdown == 0:
|
|
|
|
# print "loop, state = %s" % (proxy.state)
|
2017-05-29 12:56:59 +02:00
|
|
|
loopTime = float(self.spawnWait)
|
|
|
|
if self.lastSpawned < time.time() - self.spawnWait:
|
|
|
|
loopTime = 1.0
|
|
|
|
asyncore.loop(timeout=loopTime, count=10)
|
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:
|
|
|
|
i.writeQueue.put(protocol.CreatePacket('ping'))
|
2017-05-25 14:59:18 +02:00
|
|
|
else:
|
|
|
|
i.close("Timeout (%is)" % (time.time() - i.lastTx))
|
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)
|
|
|
|
for i in reaper:
|
|
|
|
self.removeConnection(i)
|