PyBitmessage/src/network/connectionchooser.py
Peter Surda 0b07b1c89a
Peer discovery updates
- allow loopback addresses, now you can bind different loopback IP
addresses on a single system and they will auto-cross-connect
- always listen for discovery on 0.0.0.0
- [network] - bind now also applies for the TCP socket as well as UDP
socket
- closing socket iterator fix
2017-08-09 17:34:47 +02:00

49 lines
1.4 KiB
Python

from queues import Queue
import random
from bmconfigparser import BMConfigParser
import knownnodes
from queues import portCheckerQueue
import state
def getDiscoveredPeer(stream):
try:
peer = random.choice(state.discoveredPeers.keys())
except (IndexError, KeyError):
raise ValueError
try:
del state.discoveredPeers[peer]
except KeyError:
pass
return peer
def chooseConnection(stream):
haveOnion = BMConfigParser().safeGet("bitmessagesettings", "socksproxytype")[0:5] == 'SOCKS'
if state.trustedPeer:
return state.trustedPeer
try:
retval = portCheckerQueue.get(False)
portCheckerQueue.task_done()
return retval
except Queue.Empty:
pass
if random.choice((False, True)):
return getDiscoveredPeer(stream)
for i in range(50):
peer = random.choice(knownnodes.knownNodes[stream].keys())
try:
rating = knownnodes.knownNodes[stream][peer]["rating"]
except TypeError:
print "Error in %s" % (peer)
rating = 0
if haveOnion and peer.host.endswith('.onion') and rating > 0:
rating *= 10
if rating > 1:
rating = 1
try:
if 0.05/(1.0-rating) > random.random():
return peer
except ZeroDivisionError:
return peer
raise ValueError