2017-05-25 14:59:18 +02:00
|
|
|
from queues import Queue
|
2017-05-24 16:51:49 +02:00
|
|
|
import random
|
|
|
|
|
|
|
|
from bmconfigparser import BMConfigParser
|
|
|
|
import knownnodes
|
2017-05-27 19:09:21 +02:00
|
|
|
from queues import portCheckerQueue, peerDiscoveryQueue
|
2017-05-24 16:51:49 +02:00
|
|
|
import state
|
|
|
|
|
|
|
|
def chooseConnection(stream):
|
2017-07-05 09:17:01 +02:00
|
|
|
haveOnion = BMConfigParser().safeGet("bitmessagesettings", "socksproxytype")[0:5] == 'SOCKS'
|
2017-05-24 16:51:49 +02:00
|
|
|
if state.trustedPeer:
|
|
|
|
return state.trustedPeer
|
2017-06-24 12:13:35 +02:00
|
|
|
try:
|
|
|
|
retval = portCheckerQueue.get(False)
|
|
|
|
portCheckerQueue.task_done()
|
|
|
|
except Queue.Empty:
|
2017-05-25 14:59:18 +02:00
|
|
|
try:
|
2017-06-24 12:13:35 +02:00
|
|
|
retval = peerDiscoveryQueue.get(False)
|
|
|
|
peerDiscoveryQueue.task_done()
|
2017-05-25 14:59:18 +02:00
|
|
|
except Queue.Empty:
|
2017-07-05 09:17:01 +02:00
|
|
|
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
|
2017-06-24 12:13:35 +02:00
|
|
|
return retval
|