Worked on the network issues

This commit is contained in:
jai.s 2019-11-14 20:39:26 +05:30
parent f3cb78557b
commit fdbf7ad0f2
No known key found for this signature in database
GPG Key ID: 360CFA25EFC67D12
14 changed files with 144 additions and 89 deletions

View File

@ -452,8 +452,8 @@ class DropDownWidget(BoxLayout):
# pylint: disable=too-many-locals # pylint: disable=too-many-locals
fromAddress = str(self.ids.ti.text) fromAddress = str(self.ids.ti.text)
toAddress = str(self.ids.txt_input.text) toAddress = str(self.ids.txt_input.text)
subject = self.ids.subject.text.encode('utf-8').strip() subject = self.ids.subject.text.strip()
message = self.ids.body.text.encode('utf-8').strip() message = self.ids.body.text.strip()
encoding = 3 encoding = 3
print ("message: ", self.ids.body.text) print ("message: ", self.ids.body.text)
sendMessageToPeople = True sendMessageToPeople = True
@ -511,8 +511,8 @@ class DropDownWidget(BoxLayout):
0, 0,
'sent', 'sent',
encoding, encoding,
BMConfigParser().getint( int(BMConfigParser().safeGet(
'bitmessagesettings', 'ttl')) 'bitmessagesettings', 'ttl')))
state.check_sent_acc = fromAddress state.check_sent_acc = fromAddress
state.msg_counter_objs = self.parent.parent.parent.parent\ state.msg_counter_objs = self.parent.parent.parent.parent\
.parent.parent.children[0].children[2].children[0].ids .parent.parent.children[0].children[2].children[0].ids

View File

@ -358,7 +358,7 @@ class Main: # pylint: disable=no-init, old-style-class
# Not needed if objproc disabled # Not needed if objproc disabled
if state.enableObjProc: if state.enableObjProc:
shared.reloadMyAddressHashes() shared.reloadMyAddressHashes()
# shared.reloadBroadcastSendersForWhichImWatching() shared.reloadBroadcastSendersForWhichImWatching()
# API is also objproc dependent # API is also objproc dependent
if config.safeGetBoolean('bitmessagesettings', 'apienabled'): if config.safeGetBoolean('bitmessagesettings', 'apienabled'):
import api # pylint: disable=relative-import import api # pylint: disable=relative-import

View File

@ -134,7 +134,7 @@ def increaseRating(peer):
increaseAmount = 0.1 increaseAmount = 0.1
maxRating = 1 maxRating = 1
with knownNodesLock: with knownNodesLock:
for stream in knownNodes.keys(): for stream in [key for key in knownNodes.keys()]:
try: try:
knownNodes[stream][peer]["rating"] = min( knownNodes[stream][peer]["rating"] = min(
knownNodes[stream][peer]["rating"] + increaseAmount, knownNodes[stream][peer]["rating"] + increaseAmount,
@ -160,7 +160,7 @@ def decreaseRating(peer):
def trimKnownNodes(recAddrStream=1): def trimKnownNodes(recAddrStream=1):
if len(knownNodes[recAddrStream]) < \ if len(knownNodes[recAddrStream]) < \
BMConfigParser().safeGetInt("knownnodes", "maxnodes"): int(BMConfigParser().safeGet("knownnodes", "maxnodes")):
return return
with knownNodesLock: with knownNodesLock:
oldestList = sorted( oldestList = sorted(

View File

@ -32,12 +32,12 @@ class AnnounceThread(StoppableThread):
@staticmethod @staticmethod
def announceSelf(): def announceSelf():
"""Announce our presence""" """Announce our presence"""
for connection in BMConnectionPool().udpSockets.values(): for connection in [ udpSockets for udpSockets in BMConnectionPool().udpSockets.values()]:
if not connection.announcing: if not connection.announcing:
continue continue
for stream in state.streamsInWhichIAmParticipating: for stream in state.streamsInWhichIAmParticipating:
addr = ( addr = (
stream, stream,
state.Peer('127.0.0.1', BMConfigParser().safeGetInt("bitmessagesettings", "port")), state.Peer('127.0.0.1',int( BMConfigParser().safeGet("bitmessagesettings", "port"))),
time.time()) int(time.time()))
connection.append_write_buf(BMProto.assembleAddr([addr])) connection.append_write_buf(BMProto.assembleAddr([addr]))

View File

@ -11,7 +11,7 @@ import time
from binascii import hexlify from binascii import hexlify
import addresses import addresses
import network.connectionpool from network import connectionpool
import knownnodes import knownnodes
import protocol import protocol
import state import state
@ -83,12 +83,16 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
self.object = None self.object = None
def state_bm_header(self): def state_bm_header(self):
"""Process incoming header""" """Process incoming header"""
self.magic, self.command, self.payloadLength, self.checksum = \ self.magic, self.command, self.payloadLength, self.checksum = \
protocol.Header.unpack(self.read_buf[:protocol.Header.size]) protocol.Header.unpack(self.read_buf[:protocol.Header.size])
#its shoule be in string
self.command = self.command.rstrip('\x00'.encode('utf-8')) self.command = self.command.rstrip('\x00'.encode('utf-8'))
if self.magic != 0xE9BEB4D9: if self.magic != 0xE9BEB4D9:
# skip 1 byte in order to sync # skip 1 byte in order to sync
#in the advancedispatched and length commend's
#escape the 1 length
self.set_state("bm_header", length=1) self.set_state("bm_header", length=1)
self.bm_proto_reset() self.bm_proto_reset()
logger.debug('Bad magic') logger.debug('Bad magic')
@ -111,15 +115,17 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
self.invalid = True self.invalid = True
retval = True retval = True
if not self.fullyEstablished and self.command not in ( if not self.fullyEstablished and self.command not in (
"error", "version", "verack"): "error".encode(), "version".encode(), "verack".encode()):
logger.error( logger.error(
'Received command %s before connection was fully' 'Received command {} before connection was fully'
' established, ignoring', self.command) ' established, ignoring'.format (self.command))
self.invalid = True self.invalid = True
if not self.invalid: if not self.invalid:
try: try:
command = self.command.decode() if self.command else self.command
retval = getattr( retval = getattr(
self, "bm_command_" + str(self.command).lower())() self, "bm_command_" +command)()
except AttributeError: except AttributeError:
# unimplemented command # unimplemented command
logger.debug('unimplemented command %s', self.command) logger.debug('unimplemented command %s', self.command)
@ -147,11 +153,12 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
# broken read, ignore # broken read, ignore
pass pass
else: else:
logger.debug('Closing due to invalid command %s', self.command) logger.debug('Closing due to invalid command {}'.format(self.command))
self.close_reason = "Invalid command %s" % self.command self.close_reason = ("Invalid command {}".format(self.command))
self.set_state("close") self.set_state("close")
return False return False
if retval: if retval:
print('if retval is true and inside the if ')
self.set_state("bm_header", length=self.payloadLength) self.set_state("bm_header", length=self.payloadLength)
self.bm_proto_reset() self.bm_proto_reset()
# else assume the command requires a different state to follow # else assume the command requires a different state to follow
@ -174,16 +181,16 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
# protocol.checkIPAddress() # protocol.checkIPAddress()
services, host, port = self.decode_payload_content("Q16sH") services, host, port = self.decode_payload_content("Q16sH")
if host[0:12] == '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF': if host[0:12] == '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF':
host = socket.inet_ntop(socket.AF_INET, str(host[12:16])) host = socket.inet_ntop(socket.AF_INET, host[12:16])
elif host[0:6] == '\xfd\x87\xd8\x7e\xeb\x43': elif host[0:6] == '\xfd\x87\xd8\x7e\xeb\x43':
# Onion, based on BMD/bitcoind # Onion, based on BMD/bitcoind
host = base64.b32encode(host[6:]).lower() + ".onion" host = base64.b32encode(host[6:]).lower() + ".onion"
else: else:
host = socket.inet_ntop(socket.AF_INET6, str(host)) host = socket.inet_ntop(socket.AF_INET6, host)
if host == "": if host == "":
# This can happen on Windows systems which are not 64-bit # This can happen on Windows systems which are not 64-bit
# compatible so let us drop the IPv6 address. # compatible so let us drop the IPv6 address.
host = socket.inet_ntop(socket.AF_INET, str(host[12:16])) host = socket.inet_ntop(socket.AF_INET, host[12:16])
return Node(services, host, port) return Node(services, host, port)
@ -327,6 +334,7 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
If we have them and some other conditions are fulfilled, If we have them and some other conditions are fulfilled,
append them to the write queue. append them to the write queue.
""" """
#32 an array bit long strings
items = self.decode_payload_content("l32s") items = self.decode_payload_content("l32s")
# skip? # skip?
now = time.time() now = time.time()
@ -429,11 +437,13 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
return self.decode_payload_content("LQIQ16sH") return self.decode_payload_content("LQIQ16sH")
def bm_command_addr(self): def bm_command_addr(self):
print('+++++++++++++++++++++++++++\
bm_command_addr bm_command_addr bm_command_addr ++++++++++++++++')
"""Incoming addresses, process them""" """Incoming addresses, process them"""
addresses = self._decode_addr() # pylint: disable=redefined-outer-name addresses = self._decode_addr() # pylint: disable=redefined-outer-name
for i in addresses: for i in addresses:
seenTime, stream, services, ip, port = i seenTime, stream, services, ip, port = i
decodedIP = protocol.checkIPAddress(str(ip)) decodedIP = protocol.checkIPAddress(ip)
if stream not in state.streamsInWhichIAmParticipating: if stream not in state.streamsInWhichIAmParticipating:
continue continue
if ( if (
@ -495,8 +505,8 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
"tls_init" if self.isSSL else "connection_fully_established", "tls_init" if self.isSSL else "connection_fully_established",
length=self.payloadLength, expectBytes=0) length=self.payloadLength, expectBytes=0)
return False return False
def bm_command_version(self): def bm_command_version(self):
print('inside the bmproto ')
""" """
Incoming version. Incoming version.
Parse and log, remember important things, like streams, bitfields, etc. Parse and log, remember important things, like streams, bitfields, etc.
@ -533,6 +543,7 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
self.isSSL = True self.isSSL = True
if not self.verackReceived: if not self.verackReceived:
return True return True
print('inside the bmproto line 546')
self.set_state( self.set_state(
"tls_init" if self.isSSL else "connection_fully_established", "tls_init" if self.isSSL else "connection_fully_established",
length=self.payloadLength, expectBytes=0) length=self.payloadLength, expectBytes=0)
@ -578,13 +589,16 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
return False return False
if self.destination in connectionpool.BMConnectionPool().inboundConnections: if self.destination in connectionpool.BMConnectionPool().inboundConnections:
try: try:
print('+++++++++++++++++++++++++++')
print('self destination host -{}'.format(self.destination.host))
print('++++++++++++++++++++++++++++++')
if not protocol.checkSocksIP(self.destination.host): if not protocol.checkSocksIP(self.destination.host):
self.append_write_buf(protocol.assembleErrorMessage( self.append_write_buf(protocol.assembleErrorMessage(
errorText="Too many connections from your IP." errorText="Too many connections from your IP."
" Closing connection.", fatal=2)) " Closing connection.", fatal=2))
logger.debug( logger.debug(
'Closed connection to %s because we are already connected' 'Closed connection to {} because we are already connected'
' to that IP.', self.destination) ' to that IP.'.format(self.destination))
return False return False
except: except:
pass pass
@ -631,7 +645,7 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
for address in peerList[i:i + BMProto.maxAddrCount]: for address in peerList[i:i + BMProto.maxAddrCount]:
stream, peer, timestamp = address stream, peer, timestamp = address
payload += struct.pack( payload += struct.pack(
'>Q', timestamp) # 64-bit time '>Q', int(timestamp)) # 64-bit time
payload += struct.pack('>I', stream) payload += struct.pack('>I', stream)
payload += struct.pack( payload += struct.pack(
'>q', 1) # service bit flags offered by this node '>q', 1) # service bit flags offered by this node

View File

@ -37,7 +37,7 @@ def chooseConnection(stream):
# discovered peers are already filtered by allowed streams # discovered peers are already filtered by allowed streams
return getDiscoveredPeer() return getDiscoveredPeer()
for _ in range(50): for _ in range(50):
peer = random.choice(knownnodes.knownNodes[stream].keys()) peer = random.choice(list(knownnodes.knownNodes[stream].keys()))
try: try:
peer_info = knownnodes.knownNodes[stream][peer] peer_info = knownnodes.knownNodes[stream][peer]
if peer_info.get('self'): if peer_info.get('self'):

View File

@ -18,7 +18,7 @@ from debug import logger
from network.proxy import Proxy from network.proxy import Proxy
from singleton import Singleton from singleton import Singleton
from network.tcp import ( from network.tcp import (
TCPServer, Socks5BMConnection, Socks4aBMConnection, TCPConnection) TCPServer, Socks5BMConnection, Socks4aBMConnection, TCPConnection,bootstrap)
from network.udp import UDPSocket from network.udp import UDPSocket
@ -71,9 +71,13 @@ class BMConnectionPool(object):
def isAlreadyConnected(self, nodeid): def isAlreadyConnected(self, nodeid):
"""Check if we're already connected to this peer""" """Check if we're already connected to this peer"""
# for i in (
# self.inboundConnections.values() +
# self.outboundConnections.values()
# ):
for i in ( for i in (
self.inboundConnections.values() + [inboundConnections for inboundConnections in self.inboundConnections.values()] +
self.outboundConnections.values() [outboundConnections for outboundConnections in self.outboundConnections.values()]
): ):
try: try:
if nodeid == i.nodeid: if nodeid == i.nodeid:
@ -138,8 +142,9 @@ class BMConnectionPool(object):
def startListening(self, bind=None): def startListening(self, bind=None):
"""Open a listening socket and start accepting connections on it""" """Open a listening socket and start accepting connections on it"""
if bind is None: if bind is None:
"this return blank host"
bind = self.getListeningIP() bind = self.getListeningIP()
port = BMConfigParser().safeGetInt("bitmessagesettings", "port") port = int(BMConfigParser().safeGet("bitmessagesettings", "port"))
# correct port even if it changed # correct port even if it changed
ls = TCPServer(host=bind, port=port) ls = TCPServer(host=bind, port=port)
self.listeningSockets[ls.destination] = ls self.listeningSockets[ls.destination] = ls
@ -178,11 +183,10 @@ class BMConnectionPool(object):
# This should never happen because socksproxytype setting # This should never happen because socksproxytype setting
# is handled in bitmessagemain before starting the connectionpool # is handled in bitmessagemain before starting the connectionpool
return return
bootstrapper = bootstrap(connection_base) bootstrapper = bootstrap(connection_base)
if not hostname: if not hostname:
port = helper_random.randomchoice([8080, 8444]) port = helper_random.randomchoice([8080, 8444])
hostname = 'bootstrap%s.bitmessage.org' % port hostname = ('bootstrap{}.bitmessage.org'.format(port))
else: else:
port = 8444 port = 8444
self.addConnection(bootstrapper(hostname, port)) self.addConnection(bootstrapper(hostname, port))
@ -195,8 +199,8 @@ class BMConnectionPool(object):
if BMConfigParser().safeGetBoolean( if BMConfigParser().safeGetBoolean(
'bitmessagesettings', 'dontconnect'): 'bitmessagesettings', 'dontconnect'):
acceptConnections = False acceptConnections = False
elif BMConfigParser().safeGetBoolean( elif bool(BMConfigParser().safeGet(
'bitmessagesettings', 'sendoutgoingconnections'): 'bitmessagesettings', 'sendoutgoingconnections')):
spawnConnections = True spawnConnections = True
socksproxytype = BMConfigParser().safeGet( socksproxytype = BMConfigParser().safeGet(
'bitmessagesettings', 'socksproxytype', '') 'bitmessagesettings', 'socksproxytype', '')
@ -219,8 +223,8 @@ class BMConnectionPool(object):
Proxy.proxy = ( Proxy.proxy = (
BMConfigParser().safeGet( BMConfigParser().safeGet(
'bitmessagesettings', 'sockshostname'), 'bitmessagesettings', 'sockshostname'),
BMConfigParser().safeGetInt( int(BMConfigParser().safeGet(
'bitmessagesettings', 'socksport') 'bitmessagesettings', 'socksport'))
) )
# TODO AUTH # TODO AUTH
# TODO reset based on GUI settings changes # TODO reset based on GUI settings changes
@ -236,11 +240,11 @@ class BMConnectionPool(object):
except ValueError: except ValueError:
Proxy.onion_proxy = None Proxy.onion_proxy = None
established = sum( established = sum(
1 for c in list(self.outboundConnections.values()) 1 for c in [outboundConnections for outboundConnections in self.outboundConnections.values()]
if (c.connected and c.fullyEstablished)) if (c.connected and c.fullyEstablished))
pending = len(self.outboundConnections) - established pending = len(self.outboundConnections) - established
if established < BMConfigParser().safeGetInt( if established < int(BMConfigParser().safeGet(
'bitmessagesettings', 'maxoutboundconnections'): 'bitmessagesettings', 'maxoutboundconnections')):
for i in range( for i in range(
state.maximumNumberOfHalfOpenConnections - pending): state.maximumNumberOfHalfOpenConnections - pending):
try: try:
@ -275,18 +279,22 @@ class BMConnectionPool(object):
self.lastSpawned = time.time() self.lastSpawned = time.time()
print('++++++++++++++++++++++++++++++++++++++++++') # print('++++++++++++++++++++++++++++++++++++++++++')
print('self.inboundConnections.values()-{}'.format(self.inboundConnections.values())) # print('self.inboundConnections.values()-{}'.format(self.inboundConnections.values()))
print('self.outboundConnections.values() -{}'.format(self.outboundConnections.values())) # print('self.outboundConnections.values() -{}'.format(self.outboundConnections.values()))
print('+++++++++++++++++++++++++++++++++++++++++++') # print('+++++++++++++++++++++++++++++++++++++++++++')
else: else:
# for i in (
# list(self.inboundConnections.values()) +
# list(self.outboundConnections.values())
# ):
for i in ( for i in (
list(self.inboundConnections.values()) + [inboundConnections for inboundConnections in self.inboundConnections.values()] +
list(self.outboundConnections.values()) [inboundConnections for inboundConnections in self.outboundConnections.values()]
): ):
# FIXME: rating will be increased after next connection # FIXME: rating will be increased after next connection
i.handle_close() i.handle_close()
if acceptConnections: if acceptConnections:
if not self.listeningSockets: if not self.listeningSockets:
if BMConfigParser().safeGet('network', 'bind') == '': if BMConfigParser().safeGet('network', 'bind') == '':
@ -299,6 +307,7 @@ class BMConnectionPool(object):
self.startListening(bind) self.startListening(bind)
logger.info('Listening for incoming connections.') logger.info('Listening for incoming connections.')
if not self.udpSockets: if not self.udpSockets:
# self.udpSockets :- {'0.0.0.0': <network.udp.UDPSocket connected at 0x7f95cce7d7b8>}
if BMConfigParser().safeGet('network', 'bind') == '': if BMConfigParser().safeGet('network', 'bind') == '':
self.startUDPSocket() self.startUDPSocket()
else: else:
@ -327,9 +336,13 @@ class BMConnectionPool(object):
asyncore.loop(timeout=loopTime, count=1000) asyncore.loop(timeout=loopTime, count=1000)
reaper = [] reaper = []
# for i in (
# list(self.inboundConnections.values()) +
# list(self.outboundConnections.values())
# ):
for i in ( for i in (
list(self.inboundConnections.values()) + [inboundConnections for inboundConnections in self.inboundConnections.values()] +
list(self.outboundConnections.values()) [outboundConnections for outboundConnections in self.outboundConnections.values()]
): ):
minTx = time.time() - 20 minTx = time.time() - 20
if i.fullyEstablished: if i.fullyEstablished:
@ -341,11 +354,17 @@ class BMConnectionPool(object):
i.close_reason = "Timeout (%is)" % ( i.close_reason = "Timeout (%is)" % (
time.time() - i.lastTx) time.time() - i.lastTx)
i.set_state("close") i.set_state("close")
# for i in (
# list(self.inboundConnections.values()) +
# list(self.outboundConnections.values()) +
# list(self.listeningSockets.values()) +
# list(self.udpSockets.values())
# ):
for i in ( for i in (
list(self.inboundConnections.values()) + [inboundConnections for inboundConnections in self.inboundConnections.values()] +
list(self.outboundConnections.values()) + [outboundConnections for outboundConnections in self.outboundConnections.values()] +
list(self.listeningSockets.values()) + [listeningSockets for listeningSockets in self.listeningSockets.values()] +
list(self.udpSockets.values()) [udpSockets for udpSockets in self.udpSockets.values()]
): ):
if not (i.accepting or i.connecting or i.connected): if not (i.accepting or i.connecting or i.connected):
reaper.append(i) reaper.append(i)

View File

@ -101,13 +101,27 @@ class Dandelion(): # pylint: disable=old-style-class
self.stem.append(connection) self.stem.append(connection)
for k in (k for k, v in iter(self.nodeMap.items()) if v is None): for k in (k for k, v in iter(self.nodeMap.items()) if v is None):
self.nodeMap[k] = connection self.nodeMap[k] = connection
#The Purpose of adding this condition that if self
#hashMap is has any value
# if not [hasmap for hasmap in self.hashMap.items()] ==[]:
try:
for k, v in iter({ for k, v in iter({
k: v for k, v in iter(self.hashMap.items()) k: v for k, v in iter([hasmap for hasamp in self.hashMap.items()])
if v.child is None if v.child is None
}).items(): }).items():
self.hashMap[k] = Stem( self.hashMap[k] = Stem(
connection, v.stream, self.poissonTimeout()) connection, v.stream, self.poissonTimeout())
invQueue.put((v.stream, k, v.child)) invQueue.put((v.stream, k, v.child))
except AttributeError:
pass
# for k, v in iter({
# k: v for k, v in iter([hasmap for hasamp in self.hashMap.items()])
# if v.child is None
# }).items():
# self.hashMap[k] = Stem(
# connection, v.stream, self.poissonTimeout())
# invQueue.put((v.stream, k, v.child))
def maybeRemoveStem(self, connection): def maybeRemoveStem(self, connection):
""" """

View File

@ -71,7 +71,7 @@ class ObjectTracker(object):
# release memory # release memory
deadline = time.time() - ObjectTracker.trackingExpires deadline = time.time() - ObjectTracker.trackingExpires
with self.objectsNewToThemLock: with self.objectsNewToThemLock:
self.objectsNewToThem = {k: v for k, v in self.objectsNewToThem.iteritems() if v >= deadline} self.objectsNewToThem = {k: v for k, v in iter(self.objectsNewToThem.items()) if v >= deadline}
self.lastCleaned = time.time() self.lastCleaned = time.time()
def hasObj(self, hashid): def hasObj(self, hashid):

View File

@ -20,14 +20,19 @@ currentSentSpeed = 0
def connectedHostsList(): def connectedHostsList():
"""List of all the connected hosts""" """List of all the connected hosts"""
retval = [] retval = []
for i in list(BMConnectionPool().inboundConnections.values()) + \ # for i in list(BMConnectionPool().inboundConnections.values()) + \
list(BMConnectionPool().outboundConnections.values()): # list(BMConnectionPool().outboundConnections.values()):
outBoundConnections = [outConnection for outConnection in BMConnectionPool().outboundConnections.values()]
inBoundConnections = [inConnection for inConnection in BMConnectionPool().inboundConnections.values()]
for i in outBoundConnections+inBoundConnections:
if not i.fullyEstablished: if not i.fullyEstablished:
continue continue
try: try:
retval.append(i) retval.append(i)
except AttributeError: except AttributeError:
pass pass
print('#################### retval -{}'.format(retval))
return retval return retval

View File

@ -11,7 +11,7 @@ import time
import addresses import addresses
import network.asyncore_pollchoose as asyncore import network.asyncore_pollchoose as asyncore
import network.connectionpool from network import connectionpool
import helper_random import helper_random
import knownnodes import knownnodes
import protocol import protocol
@ -71,8 +71,8 @@ class TCPConnection(BMProto, TLSDispatcher):
TLSDispatcher.__init__(self, sock, server_side=False) TLSDispatcher.__init__(self, sock, server_side=False)
self.connect(self.destination) self.connect(self.destination)
logger.debug( logger.debug(
'Connecting to %s:%i', 'Connecting to {}:{}'.format(
self.destination.host, self.destination.port) self.destination.host, self.destination.port))
try: try:
self.local = ( self.local = (
protocol.checkIPAddress( protocol.checkIPAddress(
@ -131,15 +131,15 @@ class TCPConnection(BMProto, TLSDispatcher):
def set_connection_fully_established(self): def set_connection_fully_established(self):
"""Initiate inventory synchronisation.""" """Initiate inventory synchronisation."""
if not self.isOutbound and not self.local: shared.clientHasReceivedIncomingConnections = True
shared.clientHasReceivedIncomingConnections = True UISignalQueue.put(('setStatusIcon', 'green'))
UISignalQueue.put(('setStatusIcon', 'green'))
UISignalQueue.put(( UISignalQueue.put((
'updateNetworkStatusTab', 'updateNetworkStatusTab',
(self.isOutbound, True, self.destination) (self.isOutbound, True, self.destination)
)) ))
self.antiIntersectionDelay(True) self.antiIntersectionDelay(True)
self.fullyEstablished = True self.fullyEstablished = True
print('inside the set_connection_fully_established in tcp file')
if self.isOutbound: if self.isOutbound:
knownnodes.increaseRating(self.destination) knownnodes.increaseRating(self.destination)
Dandelion().maybeAddStem(self) Dandelion().maybeAddStem(self)
@ -165,7 +165,7 @@ class TCPConnection(BMProto, TLSDispatcher):
# only if more recent than 3 hours # only if more recent than 3 hours
# and having positive or neutral rating # and having positive or neutral rating
filtered = [ filtered = [
(k, v) for k, v in nodes.iteritems() (k, v) for k, v in iter(nodes.items())
if v["lastseen"] > int(time.time()) - if v["lastseen"] > int(time.time()) -
shared.maximumAgeOfNodesThatIAdvertiseToOthers and shared.maximumAgeOfNodesThatIAdvertiseToOthers and
v["rating"] >= 0 and len(k.host) <= 22 v["rating"] >= 0 and len(k.host) <= 22
@ -191,8 +191,8 @@ class TCPConnection(BMProto, TLSDispatcher):
if objectCount == 0: if objectCount == 0:
return return
logger.debug( logger.debug(
'Sending huge inv message with %i objects to just this' 'Sending huge inv message with {} objects to jcust this'
' one peer', objectCount) ' one peer'.format(objectCount))
self.append_write_buf(protocol.CreatePacket( self.append_write_buf(protocol.CreatePacket(
'inv', addresses.encodeVarint(objectCount) + payload)) 'inv', addresses.encodeVarint(objectCount) + payload))
@ -208,7 +208,7 @@ class TCPConnection(BMProto, TLSDispatcher):
continue continue
bigInvList[objHash] = 0 bigInvList[objHash] = 0
objectCount = 0 objectCount = 0
payload = b'' payload = bytes()
# Now let us start appending all of these hashes together. They will be # Now let us start appending all of these hashes together. They will be
# sent out in a big inv message to our new peer. # sent out in a big inv message to our new peer.
for obj_hash, _ in bigInvList.items(): for obj_hash, _ in bigInvList.items():

View File

@ -21,8 +21,8 @@ if sys.version_info >= (2, 7, 13):
# this means TLSv1 or higher # this means TLSv1 or higher
# in the future change to # in the future change to
# ssl.PROTOCOL_TLS1.2 # ssl.PROTOCOL_TLS1.2
# Right now I am using the python3.5.2 and I faced the ssl for protocol due to this I # Right now I am using the python3.5.2 and I faced the ssl for protocol due to this I
# have used try and catch # have used try and catch
try: try:
sslProtocolVersion = ssl.PROTOCOL_TLS # pylint: disable=no-member sslProtocolVersion = ssl.PROTOCOL_TLS # pylint: disable=no-member
except AttributeError: except AttributeError:
@ -83,6 +83,7 @@ class TLSDispatcher(AdvancedDispatcher): # pylint: disable=too-many-instanc
# also exclude TLSv1 and TLSv1.1 in the future # also exclude TLSv1 and TLSv1.1 in the future
context.options = ssl.OP_ALL | ssl.OP_NO_SSLv2 |\ context.options = ssl.OP_ALL | ssl.OP_NO_SSLv2 |\
ssl.OP_NO_SSLv3 | ssl.OP_SINGLE_ECDH_USE | ssl.OP_CIPHER_SERVER_PREFERENCE ssl.OP_NO_SSLv3 | ssl.OP_SINGLE_ECDH_USE | ssl.OP_CIPHER_SERVER_PREFERENCE
self.sslSocket = context.wrap_socket( self.sslSocket = context.wrap_socket(
self.socket, server_side=self.server_side, do_handshake_on_connect=False) self.socket, server_side=self.server_side, do_handshake_on_connect=False)
else: else:

View File

@ -96,7 +96,7 @@ def encodeHost(host):
if host.find('.onion') > -1: if host.find('.onion') > -1:
return '\xfd\x87\xd8\x7e\xeb\x43'.encode('utf-8') + base64.b32decode(host.split(".")[0], True) return '\xfd\x87\xd8\x7e\xeb\x43'.encode('utf-8') + base64.b32decode(host.split(".")[0], True)
elif host.find(':') == -1: elif host.find(':') == -1:
return '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF'.encode('utf-8') + \ return '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF'.encode('raw_unicode_escape') + \
socket.inet_aton(host) socket.inet_aton(host)
return socket.inet_pton(socket.AF_INET6, host) return socket.inet_pton(socket.AF_INET6, host)
@ -156,17 +156,17 @@ def checkIPv4Address(host, hostStandardFormat, private=False):
def checkIPv6Address(host, hostStandardFormat, private=False): def checkIPv6Address(host, hostStandardFormat, private=False):
"""Returns hostStandardFormat if it is an IPv6 address, otherwise returns False""" """Returns hostStandardFormat if it is an IPv6 address, otherwise returns False"""
if host == ('\x00' * 15) + '\x01': if host == ('\x00'.encode() * 15) + '\x01'.encode():
if not private: if not private:
logger.debug('Ignoring loopback address: %s', hostStandardFormat) logger.debug('Ignoring loopback address: {}'.format( hostStandardFormat))
return False return False
if host[0] == '\xFE' and (ord(host[1]) & 0xc0) == 0x80: if host[0] == '\xFE' and (ord(host[1]) & 0xc0) == 0x80:
if not private: if not private:
logger.debug('Ignoring local address: %s', hostStandardFormat) logger.debug('Ignoring local address: {}'.format( hostStandardFormat))
return hostStandardFormat if private else False return hostStandardFormat if private else False
if (ord(host[0]) & 0xfe) == 0xfc: if (ord(host[0:1]) & 0xfe) == 0xfc:
if not private: if not private:
logger.debug('Ignoring unique local address: %s', hostStandardFormat) logger.debug('Ignoring unique local address: {}'.format( hostStandardFormat))
return hostStandardFormat if private else False return hostStandardFormat if private else False
return False if private else hostStandardFormat return False if private else hostStandardFormat
@ -234,18 +234,18 @@ def isProofOfWorkSufficient(data,
def CreatePacket(command, payload=''): def CreatePacket(command, payload=''):
"""Construct and return a number of bytes from a payload""" """Construct and return a number of bytes from a payload"""
payload = payload if type(payload) == bytes else payload.encode()
payload_length = len(payload) payload_length = len(payload)
checksum = hashlib.sha512(payload).digest()[0:4] checksum = hashlib.sha512(payload).digest()[0:4]
byte = bytearray(Header.size + payload_length)
b = bytearray(Header.size + payload_length) Header.pack_into(byte, 0, 0xE9BEB4D9, command.encode(), payload_length, checksum)
Header.pack_into(b, 0, 0xE9BEB4D9, command, payload_length, checksum) byte[Header.size:] = payload
b[Header.size:] = payload return byte
return bytes(b)
def assembleVersionMessage(remoteHost, remotePort, participatingStreams, server=False, nodeid=None): def assembleVersionMessage(remoteHost, remotePort, participatingStreams, server=False, nodeid=None):
"""Construct the payload of a version message, return the resultng bytes of running CreatePacket() on it""" """Construct the payload of a version message, return the resultng bytes of running CreatePacket() on it"""
payload = '' payload = bytes()
payload += pack('>L', 3) # protocol version. payload += pack('>L', 3) # protocol version.
# bitflags of the services I offer. # bitflags of the services I offer.
payload += pack( payload += pack(
@ -278,7 +278,9 @@ def assembleVersionMessage(remoteHost, remotePort, participatingStreams, server=
(NODE_DANDELION if state.dandelion else 0) (NODE_DANDELION if state.dandelion else 0)
) )
# = 127.0.0.1. This will be ignored by the remote host. The actual remote connected IP will be used. # = 127.0.0.1. This will be ignored by the remote host. The actual remote connected IP will be used.
payload += '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF' + pack('>L', 2130706433)
#python3 need to check
payload += '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF'.encode() + pack('>L', 2130706433)
# we have a separate extPort and incoming over clearnet # we have a separate extPort and incoming over clearnet
# or outgoing through clearnet # or outgoing through clearnet
extport = BMConfigParser().safeGetInt('bitmessagesettings', 'extport') extport = BMConfigParser().safeGetInt('bitmessagesettings', 'extport')
@ -289,9 +291,9 @@ def assembleVersionMessage(remoteHost, remotePort, participatingStreams, server=
): ):
payload += pack('>H', extport) payload += pack('>H', extport)
elif checkSocksIP(remoteHost) and server: # incoming connection over Tor elif checkSocksIP(remoteHost) and server: # incoming connection over Tor
payload += pack('>H', BMConfigParser().getint('bitmessagesettings', 'onionport')) payload += pack('>H', int(BMConfigParser().safeGet('bitmessagesettings', 'onionport')))
else: # no extport and not incoming over Tor else: # no extport and not incoming over Tor
payload += pack('>H', BMConfigParser().getint('bitmessagesettings', 'port')) payload += pack('>H', int(BMConfigParser().safeGet('bitmessagesettings', 'port')))
if nodeid is not None: if nodeid is not None:
payload += nodeid[0:8] payload += nodeid[0:8]
@ -299,7 +301,7 @@ def assembleVersionMessage(remoteHost, remotePort, participatingStreams, server=
payload += eightBytesOfRandomDataUsedToDetectConnectionsToSelf payload += eightBytesOfRandomDataUsedToDetectConnectionsToSelf
userAgent = '/PyBitmessage:' + softwareVersion + '/' userAgent = '/PyBitmessage:' + softwareVersion + '/'
payload += encodeVarint(len(userAgent)) payload += encodeVarint(len(userAgent))
payload += userAgent payload += userAgent.encode()
# Streams # Streams
payload += encodeVarint(len(participatingStreams)) payload += encodeVarint(len(participatingStreams))
@ -319,9 +321,9 @@ def assembleErrorMessage(fatal=0, banTime=0, inventoryVector='', errorText=''):
payload = encodeVarint(fatal) payload = encodeVarint(fatal)
payload += encodeVarint(banTime) payload += encodeVarint(banTime)
payload += encodeVarint(len(inventoryVector)) payload += encodeVarint(len(inventoryVector))
payload += inventoryVector payload += inventoryVector.encode() if type(payload) == bytes else inventoryVector
payload += encodeVarint(len(errorText)) payload += encodeVarint(len(errorText))
payload += errorText payload += errorText.encode() if type(payload)== bytes else errorText
return CreatePacket('error', payload) return CreatePacket('error', payload)

View File

@ -79,7 +79,7 @@ class SqliteInventory(InventoryStorage): # pylint: disable=too-many-ancestors
with self.lock: with self.lock:
t = int(time.time()) t = int(time.time())
hashes = [x for x, value in self._inventory.items() if value.stream == stream and value.expires > t] hashes = [x for x, value in self._inventory.items() if value.stream == stream and value.expires > t]
hashes += (str(payload) for payload, in sqlQuery( hashes += (payload for payload, in sqlQuery(
'SELECT hash FROM inventory WHERE streamnumber=? AND expirestime>?', stream, t)) 'SELECT hash FROM inventory WHERE streamnumber=? AND expirestime>?', stream, t))
return hashes return hashes