2020-01-06 12:44:47 +01:00
|
|
|
"""
|
|
|
|
Select which node to connect to
|
|
|
|
"""
|
2019-07-29 13:37:56 +02:00
|
|
|
# pylint: disable=too-many-branches
|
2019-08-06 13:04:33 +02:00
|
|
|
import logging
|
2019-07-29 14:54:19 +02:00
|
|
|
import random # nosec
|
2017-05-24 16:51:49 +02:00
|
|
|
|
|
|
|
import knownnodes
|
2017-10-19 09:16:29 +02:00
|
|
|
import protocol
|
2017-05-24 16:51:49 +02:00
|
|
|
import state
|
2019-07-29 14:54:19 +02:00
|
|
|
from bmconfigparser import BMConfigParser
|
|
|
|
from queues import Queue, portCheckerQueue
|
|
|
|
|
2019-08-06 13:04:33 +02:00
|
|
|
logger = logging.getLogger('default')
|
|
|
|
|
2017-05-24 16:51:49 +02:00
|
|
|
|
2017-08-22 13:49:27 +02:00
|
|
|
def getDiscoveredPeer():
|
2020-01-06 12:44:47 +01:00
|
|
|
"""Get a peer from the local peer discovery list"""
|
2017-08-06 21:29:54 +02:00
|
|
|
try:
|
2019-07-29 14:54:19 +02:00
|
|
|
peer = random.choice(state.discoveredPeers.keys())
|
2017-08-06 21:29:54 +02:00
|
|
|
except (IndexError, KeyError):
|
|
|
|
raise ValueError
|
2017-08-09 17:34:47 +02:00
|
|
|
try:
|
|
|
|
del state.discoveredPeers[peer]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
return peer
|
2017-08-06 21:29:54 +02:00
|
|
|
|
2019-07-29 14:54:19 +02:00
|
|
|
|
2017-05-24 16:51:49 +02:00
|
|
|
def chooseConnection(stream):
|
2020-01-06 12:44:47 +01:00
|
|
|
"""Returns an appropriate connection"""
|
2019-07-29 14:54:19 +02:00
|
|
|
haveOnion = BMConfigParser().safeGet(
|
|
|
|
"bitmessagesettings", "socksproxytype")[0:5] == 'SOCKS'
|
2019-10-08 22:08:42 +02:00
|
|
|
onionOnly = BMConfigParser().safeGetBoolean(
|
|
|
|
"bitmessagesettings", "onionservicesonly")
|
2017-06-24 12:13:35 +02:00
|
|
|
try:
|
|
|
|
retval = portCheckerQueue.get(False)
|
|
|
|
portCheckerQueue.task_done()
|
2017-08-06 21:29:54 +02:00
|
|
|
return retval
|
2017-06-24 12:13:35 +02:00
|
|
|
except Queue.Empty:
|
2017-08-06 21:29:54 +02:00
|
|
|
pass
|
2017-08-22 13:49:27 +02:00
|
|
|
# with a probability of 0.5, connect to a discovered peer
|
2019-07-29 14:54:19 +02:00
|
|
|
if random.choice((False, True)) and not haveOnion:
|
2017-08-22 13:49:27 +02:00
|
|
|
# discovered peers are already filtered by allowed streams
|
|
|
|
return getDiscoveredPeer()
|
|
|
|
for _ in range(50):
|
2019-07-29 14:54:19 +02:00
|
|
|
peer = random.choice(knownnodes.knownNodes[stream].keys())
|
2017-08-06 21:29:54 +02:00
|
|
|
try:
|
2019-07-29 13:37:56 +02:00
|
|
|
peer_info = knownnodes.knownNodes[stream][peer]
|
|
|
|
if peer_info.get('self'):
|
|
|
|
continue
|
|
|
|
rating = peer_info["rating"]
|
2017-08-06 21:29:54 +02:00
|
|
|
except TypeError:
|
2019-07-29 14:54:19 +02:00
|
|
|
logger.warning('Error in %s', peer)
|
2017-08-06 21:29:54 +02:00
|
|
|
rating = 0
|
2017-10-19 09:16:29 +02:00
|
|
|
if haveOnion:
|
2020-01-06 12:44:47 +01:00
|
|
|
# do not connect to raw IP addresses
|
|
|
|
# --keep all traffic within Tor overlay
|
2019-10-08 22:08:42 +02:00
|
|
|
if onionOnly and not peer.host.endswith('.onion'):
|
|
|
|
continue
|
2017-10-19 09:16:29 +02:00
|
|
|
# onion addresses have a higher priority when SOCKS
|
|
|
|
if peer.host.endswith('.onion') and rating > 0:
|
|
|
|
rating = 1
|
2019-07-12 17:19:06 +02:00
|
|
|
# TODO: need better check
|
|
|
|
elif not peer.host.startswith('bootstrap'):
|
2017-10-19 09:16:29 +02:00
|
|
|
encodedAddr = protocol.encodeHost(peer.host)
|
|
|
|
# don't connect to local IPs when using SOCKS
|
|
|
|
if not protocol.checkIPAddress(encodedAddr, False):
|
|
|
|
continue
|
2017-08-06 21:29:54 +02:00
|
|
|
if rating > 1:
|
|
|
|
rating = 1
|
2017-05-25 14:59:18 +02:00
|
|
|
try:
|
2019-07-29 14:54:19 +02:00
|
|
|
if 0.05 / (1.0 - rating) > random.random():
|
2017-08-06 21:29:54 +02:00
|
|
|
return peer
|
|
|
|
except ZeroDivisionError:
|
|
|
|
return peer
|
|
|
|
raise ValueError
|