2013-06-20 23:23:03 +02:00
|
|
|
import threading
|
|
|
|
import shared
|
|
|
|
import socket
|
2017-02-22 09:34:54 +01:00
|
|
|
from bmconfigparser import BMConfigParser
|
2013-06-22 00:29:04 +02:00
|
|
|
from class_sendDataThread import *
|
|
|
|
from class_receiveDataThread import *
|
2013-07-24 18:43:51 +02:00
|
|
|
import helper_bootstrap
|
2015-11-24 01:55:17 +01:00
|
|
|
from helper_threading import *
|
2017-01-11 14:27:19 +01:00
|
|
|
import protocol
|
2014-02-16 17:21:20 +01:00
|
|
|
import errno
|
|
|
|
import re
|
2013-06-22 00:29:04 +02:00
|
|
|
|
2017-01-12 06:58:35 +01:00
|
|
|
import state
|
|
|
|
|
2013-06-20 23:23:03 +02:00
|
|
|
# Only one singleListener thread will ever exist. It creates the
|
|
|
|
# receiveDataThread and sendDataThread for each incoming connection. Note
|
|
|
|
# that it cannot set the stream number because it is not known yet- the
|
|
|
|
# other node will have to tell us its stream number in a version message.
|
|
|
|
# If we don't care about their stream, we will close the connection
|
|
|
|
# (within the recversion function of the recieveData thread)
|
|
|
|
|
|
|
|
|
2015-11-24 01:55:17 +01:00
|
|
|
class singleListener(threading.Thread, StoppableThread):
|
2013-06-20 23:23:03 +02:00
|
|
|
|
|
|
|
def __init__(self):
|
2015-11-18 16:22:17 +01:00
|
|
|
threading.Thread.__init__(self, name="singleListener")
|
2015-11-24 01:55:17 +01:00
|
|
|
self.initStop()
|
2013-06-20 23:23:03 +02:00
|
|
|
|
2013-06-22 01:49:50 +02:00
|
|
|
def setup(self, selfInitiatedConnections):
|
|
|
|
self.selfInitiatedConnections = selfInitiatedConnections
|
|
|
|
|
2014-02-16 17:21:20 +01:00
|
|
|
def _createListenSocket(self, family):
|
|
|
|
HOST = '' # Symbolic name meaning all available interfaces
|
2016-06-07 21:59:48 +02:00
|
|
|
# If not sockslisten, but onionhostname defined, only listen on localhost
|
2017-01-11 14:27:19 +01:00
|
|
|
if not BMConfigParser().safeGetBoolean('bitmessagesettings', 'sockslisten') and ".onion" in BMConfigParser().get('bitmessagesettings', 'onionhostname'):
|
|
|
|
HOST = BMConfigParser().get('bitmessagesettings', 'onionbindip')
|
|
|
|
PORT = BMConfigParser().getint('bitmessagesettings', 'port')
|
2014-02-16 17:21:20 +01:00
|
|
|
sock = socket.socket(family, socket.SOCK_STREAM)
|
|
|
|
if family == socket.AF_INET6:
|
|
|
|
# Make sure we can accept both IPv4 and IPv6 connections.
|
|
|
|
# This is the default on everything apart from Windows
|
|
|
|
sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)
|
|
|
|
# This option apparently avoids the TIME_WAIT state so that we can
|
|
|
|
# rebind faster
|
|
|
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
|
|
sock.bind((HOST, PORT))
|
|
|
|
sock.listen(2)
|
|
|
|
return sock
|
2015-11-24 01:55:17 +01:00
|
|
|
|
|
|
|
def stopThread(self):
|
|
|
|
super(singleListener, self).stopThread()
|
|
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
2017-01-11 14:27:19 +01:00
|
|
|
for ip in ('127.0.0.1', BMConfigParser().get('bitmessagesettings', 'onionbindip')):
|
2016-06-07 21:59:48 +02:00
|
|
|
try:
|
2017-01-11 14:27:19 +01:00
|
|
|
s.connect((ip, BMConfigParser().getint('bitmessagesettings', 'port')))
|
2016-06-07 21:59:48 +02:00
|
|
|
s.shutdown(socket.SHUT_RDWR)
|
|
|
|
s.close()
|
|
|
|
break
|
|
|
|
except:
|
|
|
|
pass
|
2014-02-16 17:21:20 +01:00
|
|
|
|
2013-06-20 23:23:03 +02:00
|
|
|
def run(self):
|
2014-02-06 14:16:07 +01:00
|
|
|
# If there is a trusted peer then we don't want to accept
|
|
|
|
# incoming connections so we'll just abandon the thread
|
2017-01-12 06:58:35 +01:00
|
|
|
if state.trustedPeer:
|
2014-02-06 14:16:07 +01:00
|
|
|
return
|
|
|
|
|
2017-01-14 23:20:15 +01:00
|
|
|
while BMConfigParser().safeGetBoolean('bitmessagesettings', 'dontconnect') and state.shutdown == 0:
|
2015-11-24 01:55:17 +01:00
|
|
|
self.stop.wait(1)
|
2013-07-24 18:43:51 +02:00
|
|
|
helper_bootstrap.dns()
|
2013-07-12 20:40:06 +02:00
|
|
|
# We typically don't want to accept incoming connections if the user is using a
|
|
|
|
# SOCKS proxy, unless they have configured otherwise. If they eventually select
|
|
|
|
# proxy 'none' or configure SOCKS listening then this will start listening for
|
2016-06-07 21:59:48 +02:00
|
|
|
# connections. But if on SOCKS and have an onionhostname, listen
|
|
|
|
# (socket is then only opened for localhost)
|
2017-01-11 14:27:19 +01:00
|
|
|
while BMConfigParser().get('bitmessagesettings', 'socksproxytype')[0:5] == 'SOCKS' and \
|
|
|
|
(not BMConfigParser().getboolean('bitmessagesettings', 'sockslisten') and \
|
|
|
|
".onion" not in BMConfigParser().get('bitmessagesettings', 'onionhostname')) and \
|
2017-01-14 23:20:15 +01:00
|
|
|
state.shutdown == 0:
|
2015-11-24 01:55:17 +01:00
|
|
|
self.stop.wait(5)
|
2013-06-20 23:23:03 +02:00
|
|
|
|
2015-11-18 16:22:17 +01:00
|
|
|
logger.info('Listening for incoming connections.')
|
2013-06-29 19:29:35 +02:00
|
|
|
|
2014-02-16 17:21:20 +01:00
|
|
|
# First try listening on an IPv6 socket. This should also be
|
|
|
|
# able to accept connections on IPv4. If that's not available
|
|
|
|
# we'll fall back to IPv4-only.
|
|
|
|
try:
|
|
|
|
sock = self._createListenSocket(socket.AF_INET6)
|
2017-02-26 12:42:18 +01:00
|
|
|
except socket.error as e:
|
2014-02-16 17:21:20 +01:00
|
|
|
if (isinstance(e.args, tuple) and
|
|
|
|
e.args[0] in (errno.EAFNOSUPPORT,
|
|
|
|
errno.EPFNOSUPPORT,
|
2016-06-07 21:59:48 +02:00
|
|
|
errno.EADDRNOTAVAIL,
|
2017-02-26 12:55:43 +01:00
|
|
|
errno.ENOPROTOOPT,
|
|
|
|
errno.EINVAL)):
|
2014-02-16 17:21:20 +01:00
|
|
|
sock = self._createListenSocket(socket.AF_INET)
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
|
|
|
# regexp to match an IPv4-mapped IPv6 address
|
|
|
|
mappedAddressRegexp = re.compile(r'^::ffff:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)$')
|
2013-06-20 23:23:03 +02:00
|
|
|
|
2017-01-14 23:20:15 +01:00
|
|
|
while state.shutdown == 0:
|
2013-07-12 20:40:06 +02:00
|
|
|
# We typically don't want to accept incoming connections if the user is using a
|
|
|
|
# SOCKS proxy, unless they have configured otherwise. If they eventually select
|
|
|
|
# proxy 'none' or configure SOCKS listening then this will start listening for
|
|
|
|
# connections.
|
2017-01-14 23:20:15 +01:00
|
|
|
while BMConfigParser().get('bitmessagesettings', 'socksproxytype')[0:5] == 'SOCKS' and not BMConfigParser().getboolean('bitmessagesettings', 'sockslisten') and ".onion" not in BMConfigParser().get('bitmessagesettings', 'onionhostname') and state.shutdown == 0:
|
2015-11-24 01:55:17 +01:00
|
|
|
self.stop.wait(10)
|
2017-02-26 17:46:02 +01:00
|
|
|
while len(shared.connectedHostsList) > \
|
|
|
|
BMConfigParser().safeGetInt("bitmessagesettings", "maxtotalconnections", 200) + \
|
|
|
|
BMConfigParser().safeGetInt("bitmessagesettings", "maxbootstrapconnections", 20) \
|
|
|
|
and state.shutdown == 0:
|
2015-11-18 16:22:17 +01:00
|
|
|
logger.info('We are connected to too many people. Not accepting further incoming connections for ten seconds.')
|
2013-06-29 19:29:35 +02:00
|
|
|
|
2015-11-24 01:55:17 +01:00
|
|
|
self.stop.wait(10)
|
2013-06-20 23:23:03 +02:00
|
|
|
|
2017-01-14 23:20:15 +01:00
|
|
|
while state.shutdown == 0:
|
2017-02-26 12:42:18 +01:00
|
|
|
try:
|
|
|
|
socketObject, sockaddr = sock.accept()
|
|
|
|
except socket.error as e:
|
2017-02-26 12:52:28 +01:00
|
|
|
if isinstance(e.args, tuple) and \
|
2017-02-26 12:42:18 +01:00
|
|
|
e.args[0] in (errno.EINTR,):
|
|
|
|
continue
|
|
|
|
time.wait(1)
|
|
|
|
continue
|
|
|
|
|
2014-02-16 17:21:20 +01:00
|
|
|
(HOST, PORT) = sockaddr[0:2]
|
|
|
|
|
|
|
|
# If the address is an IPv4-mapped IPv6 address then
|
|
|
|
# convert it to just the IPv4 representation
|
|
|
|
md = mappedAddressRegexp.match(HOST)
|
|
|
|
if md != None:
|
|
|
|
HOST = md.group(1)
|
|
|
|
|
|
|
|
# The following code will, unfortunately, block an
|
|
|
|
# incoming connection if someone else on the same LAN
|
|
|
|
# is already connected because the two computers will
|
|
|
|
# share the same external IP. This is here to prevent
|
|
|
|
# connection flooding.
|
2016-06-07 21:59:48 +02:00
|
|
|
# permit repeated connections from Tor
|
2017-02-27 23:31:12 +01:00
|
|
|
if HOST in shared.connectedHostsList and \
|
|
|
|
(".onion" not in BMConfigParser().get('bitmessagesettings', 'onionhostname') or not protocol.checkSocksIP(HOST)):
|
|
|
|
# bootstrap provider? Then accept, we'll most likely drop it a little bit later
|
|
|
|
if BMConfigParser().safeGetInt('bitmessagesettings', 'maxoutboundconnections') >= \
|
|
|
|
BMConfigParser().safeGetInt('bitmessagesettings', 'maxtotalconnections', 200):
|
|
|
|
break
|
2014-08-27 09:14:32 +02:00
|
|
|
socketObject.close()
|
2015-11-18 16:22:17 +01:00
|
|
|
logger.info('We are already connected to ' + str(HOST) + '. Ignoring connection.')
|
2014-02-16 17:21:20 +01:00
|
|
|
else:
|
|
|
|
break
|
2013-06-29 19:29:35 +02:00
|
|
|
|
2013-06-24 21:51:01 +02:00
|
|
|
someObjectsOfWhichThisRemoteNodeIsAlreadyAware = {} # This is not necessairly a complete list; we clear it from time to time to save memory.
|
2017-02-02 15:52:32 +01:00
|
|
|
sendDataThreadQueue = Queue.Queue() # Used to submit information to the send data thread for this connection.
|
2014-08-27 09:14:32 +02:00
|
|
|
socketObject.settimeout(20)
|
2013-06-20 23:23:03 +02:00
|
|
|
|
2013-12-30 04:36:23 +01:00
|
|
|
sd = sendDataThread(sendDataThreadQueue)
|
2013-06-20 23:23:03 +02:00
|
|
|
sd.setup(
|
2014-08-27 09:14:32 +02:00
|
|
|
socketObject, HOST, PORT, -1, someObjectsOfWhichThisRemoteNodeIsAlreadyAware)
|
2013-06-20 23:23:03 +02:00
|
|
|
sd.start()
|
|
|
|
|
|
|
|
rd = receiveDataThread()
|
|
|
|
rd.daemon = True # close the main program even if there are threads left
|
|
|
|
rd.setup(
|
2016-02-18 00:53:13 +01:00
|
|
|
socketObject, HOST, PORT, -1, someObjectsOfWhichThisRemoteNodeIsAlreadyAware, self.selfInitiatedConnections, sendDataThreadQueue, sd.objectHashHolderInstance)
|
2013-06-20 23:23:03 +02:00
|
|
|
rd.start()
|
|
|
|
|
2015-11-18 16:22:17 +01:00
|
|
|
logger.info('connected to ' + HOST + ' during INCOMING request.')
|
2013-06-29 19:29:35 +02:00
|
|
|
|