From fdbf7ad0f2c89db7d387b7fae3fa9cc6d5915ff4 Mon Sep 17 00:00:00 2001 From: "jai.s" Date: Thu, 14 Nov 2019 20:39:26 +0530 Subject: [PATCH] Worked on the network issues --- src/bitmessagekivy/mpybit.py | 8 ++-- src/bitmessagemain.py | 2 +- src/knownnodes.py | 4 +- src/network/announcethread.py | 8 ++-- src/network/bmproto.py | 46 ++++++++++++++------- src/network/connectionchooser.py | 2 +- src/network/connectionpool.py | 71 ++++++++++++++++++++------------ src/network/dandelion.py | 16 ++++++- src/network/objectracker.py | 2 +- src/network/stats.py | 9 +++- src/network/tcp.py | 20 ++++----- src/network/tls.py | 5 ++- src/protocol.py | 38 +++++++++-------- src/storage/sqlite.py | 2 +- 14 files changed, 144 insertions(+), 89 deletions(-) diff --git a/src/bitmessagekivy/mpybit.py b/src/bitmessagekivy/mpybit.py index f74bcf20..5c41577e 100644 --- a/src/bitmessagekivy/mpybit.py +++ b/src/bitmessagekivy/mpybit.py @@ -452,8 +452,8 @@ class DropDownWidget(BoxLayout): # pylint: disable=too-many-locals fromAddress = str(self.ids.ti.text) toAddress = str(self.ids.txt_input.text) - subject = self.ids.subject.text.encode('utf-8').strip() - message = self.ids.body.text.encode('utf-8').strip() + subject = self.ids.subject.text.strip() + message = self.ids.body.text.strip() encoding = 3 print ("message: ", self.ids.body.text) sendMessageToPeople = True @@ -511,8 +511,8 @@ class DropDownWidget(BoxLayout): 0, 'sent', encoding, - BMConfigParser().getint( - 'bitmessagesettings', 'ttl')) + int(BMConfigParser().safeGet( + 'bitmessagesettings', 'ttl'))) state.check_sent_acc = fromAddress state.msg_counter_objs = self.parent.parent.parent.parent\ .parent.parent.children[0].children[2].children[0].ids diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index dcf03118..b7735904 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -358,7 +358,7 @@ class Main: # pylint: disable=no-init, old-style-class # Not needed if objproc disabled if state.enableObjProc: shared.reloadMyAddressHashes() - # shared.reloadBroadcastSendersForWhichImWatching() + shared.reloadBroadcastSendersForWhichImWatching() # API is also objproc dependent if config.safeGetBoolean('bitmessagesettings', 'apienabled'): import api # pylint: disable=relative-import diff --git a/src/knownnodes.py b/src/knownnodes.py index 4052e3ae..7708e16f 100644 --- a/src/knownnodes.py +++ b/src/knownnodes.py @@ -134,7 +134,7 @@ def increaseRating(peer): increaseAmount = 0.1 maxRating = 1 with knownNodesLock: - for stream in knownNodes.keys(): + for stream in [key for key in knownNodes.keys()]: try: knownNodes[stream][peer]["rating"] = min( knownNodes[stream][peer]["rating"] + increaseAmount, @@ -160,7 +160,7 @@ def decreaseRating(peer): def trimKnownNodes(recAddrStream=1): if len(knownNodes[recAddrStream]) < \ - BMConfigParser().safeGetInt("knownnodes", "maxnodes"): + int(BMConfigParser().safeGet("knownnodes", "maxnodes")): return with knownNodesLock: oldestList = sorted( diff --git a/src/network/announcethread.py b/src/network/announcethread.py index 59fad128..b6f21eae 100644 --- a/src/network/announcethread.py +++ b/src/network/announcethread.py @@ -32,12 +32,12 @@ class AnnounceThread(StoppableThread): @staticmethod def announceSelf(): """Announce our presence""" - for connection in BMConnectionPool().udpSockets.values(): + for connection in [ udpSockets for udpSockets in BMConnectionPool().udpSockets.values()]: if not connection.announcing: continue for stream in state.streamsInWhichIAmParticipating: addr = ( stream, - state.Peer('127.0.0.1', BMConfigParser().safeGetInt("bitmessagesettings", "port")), - time.time()) - connection.append_write_buf(BMProto.assembleAddr([addr])) + state.Peer('127.0.0.1',int( BMConfigParser().safeGet("bitmessagesettings", "port"))), + int(time.time())) + connection.append_write_buf(BMProto.assembleAddr([addr])) \ No newline at end of file diff --git a/src/network/bmproto.py b/src/network/bmproto.py index cfd6672f..69f1debb 100644 --- a/src/network/bmproto.py +++ b/src/network/bmproto.py @@ -11,7 +11,7 @@ import time from binascii import hexlify import addresses -import network.connectionpool +from network import connectionpool import knownnodes import protocol import state @@ -83,12 +83,16 @@ class BMProto(AdvancedDispatcher, ObjectTracker): self.object = None def state_bm_header(self): + """Process incoming header""" self.magic, self.command, self.payloadLength, self.checksum = \ protocol.Header.unpack(self.read_buf[:protocol.Header.size]) + #its shoule be in string self.command = self.command.rstrip('\x00'.encode('utf-8')) if self.magic != 0xE9BEB4D9: # 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.bm_proto_reset() logger.debug('Bad magic') @@ -111,15 +115,17 @@ class BMProto(AdvancedDispatcher, ObjectTracker): self.invalid = True retval = True if not self.fullyEstablished and self.command not in ( - "error", "version", "verack"): + "error".encode(), "version".encode(), "verack".encode()): logger.error( - 'Received command %s before connection was fully' - ' established, ignoring', self.command) + 'Received command {} before connection was fully' + ' established, ignoring'.format (self.command)) self.invalid = True if not self.invalid: try: + command = self.command.decode() if self.command else self.command + retval = getattr( - self, "bm_command_" + str(self.command).lower())() + self, "bm_command_" +command)() except AttributeError: # unimplemented command logger.debug('unimplemented command %s', self.command) @@ -147,11 +153,12 @@ class BMProto(AdvancedDispatcher, ObjectTracker): # broken read, ignore pass else: - logger.debug('Closing due to invalid command %s', self.command) - self.close_reason = "Invalid command %s" % self.command + logger.debug('Closing due to invalid command {}'.format(self.command)) + self.close_reason = ("Invalid command {}".format(self.command)) self.set_state("close") return False if retval: + print('if retval is true and inside the if ') self.set_state("bm_header", length=self.payloadLength) self.bm_proto_reset() # else assume the command requires a different state to follow @@ -174,16 +181,16 @@ class BMProto(AdvancedDispatcher, ObjectTracker): # protocol.checkIPAddress() services, host, port = self.decode_payload_content("Q16sH") 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': # Onion, based on BMD/bitcoind host = base64.b32encode(host[6:]).lower() + ".onion" else: - host = socket.inet_ntop(socket.AF_INET6, str(host)) + host = socket.inet_ntop(socket.AF_INET6, host) if host == "": # This can happen on Windows systems which are not 64-bit # 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) @@ -327,6 +334,7 @@ class BMProto(AdvancedDispatcher, ObjectTracker): If we have them and some other conditions are fulfilled, append them to the write queue. """ + #32 an array bit long strings items = self.decode_payload_content("l32s") # skip? now = time.time() @@ -429,11 +437,13 @@ class BMProto(AdvancedDispatcher, ObjectTracker): return self.decode_payload_content("LQIQ16sH") def bm_command_addr(self): + print('+++++++++++++++++++++++++++\ + bm_command_addr bm_command_addr bm_command_addr ++++++++++++++++') """Incoming addresses, process them""" addresses = self._decode_addr() # pylint: disable=redefined-outer-name for i in addresses: seenTime, stream, services, ip, port = i - decodedIP = protocol.checkIPAddress(str(ip)) + decodedIP = protocol.checkIPAddress(ip) if stream not in state.streamsInWhichIAmParticipating: continue if ( @@ -495,8 +505,8 @@ class BMProto(AdvancedDispatcher, ObjectTracker): "tls_init" if self.isSSL else "connection_fully_established", length=self.payloadLength, expectBytes=0) return False - def bm_command_version(self): + print('inside the bmproto ') """ Incoming version. Parse and log, remember important things, like streams, bitfields, etc. @@ -533,6 +543,7 @@ class BMProto(AdvancedDispatcher, ObjectTracker): self.isSSL = True if not self.verackReceived: return True + print('inside the bmproto line 546') self.set_state( "tls_init" if self.isSSL else "connection_fully_established", length=self.payloadLength, expectBytes=0) @@ -578,13 +589,16 @@ class BMProto(AdvancedDispatcher, ObjectTracker): return False if self.destination in connectionpool.BMConnectionPool().inboundConnections: try: + print('+++++++++++++++++++++++++++') + print('self destination host -{}'.format(self.destination.host)) + print('++++++++++++++++++++++++++++++') if not protocol.checkSocksIP(self.destination.host): self.append_write_buf(protocol.assembleErrorMessage( errorText="Too many connections from your IP." - " Closing connection.", fatal=2)) + " Closing connection.", fatal=2)) logger.debug( - 'Closed connection to %s because we are already connected' - ' to that IP.', self.destination) + 'Closed connection to {} because we are already connected' + ' to that IP.'.format(self.destination)) return False except: pass @@ -631,7 +645,7 @@ class BMProto(AdvancedDispatcher, ObjectTracker): for address in peerList[i:i + BMProto.maxAddrCount]: stream, peer, timestamp = address payload += struct.pack( - '>Q', timestamp) # 64-bit time + '>Q', int(timestamp)) # 64-bit time payload += struct.pack('>I', stream) payload += struct.pack( '>q', 1) # service bit flags offered by this node diff --git a/src/network/connectionchooser.py b/src/network/connectionchooser.py index 53ce30b7..eb7c0f6e 100644 --- a/src/network/connectionchooser.py +++ b/src/network/connectionchooser.py @@ -37,7 +37,7 @@ def chooseConnection(stream): # discovered peers are already filtered by allowed streams return getDiscoveredPeer() for _ in range(50): - peer = random.choice(knownnodes.knownNodes[stream].keys()) + peer = random.choice(list(knownnodes.knownNodes[stream].keys())) try: peer_info = knownnodes.knownNodes[stream][peer] if peer_info.get('self'): diff --git a/src/network/connectionpool.py b/src/network/connectionpool.py index aad45e59..374ebc62 100644 --- a/src/network/connectionpool.py +++ b/src/network/connectionpool.py @@ -18,7 +18,7 @@ from debug import logger from network.proxy import Proxy from singleton import Singleton from network.tcp import ( - TCPServer, Socks5BMConnection, Socks4aBMConnection, TCPConnection) + TCPServer, Socks5BMConnection, Socks4aBMConnection, TCPConnection,bootstrap) from network.udp import UDPSocket @@ -71,9 +71,13 @@ class BMConnectionPool(object): def isAlreadyConnected(self, nodeid): """Check if we're already connected to this peer""" + # for i in ( + # self.inboundConnections.values() + + # self.outboundConnections.values() + # ): for i in ( - self.inboundConnections.values() + - self.outboundConnections.values() + [inboundConnections for inboundConnections in self.inboundConnections.values()] + + [outboundConnections for outboundConnections in self.outboundConnections.values()] ): try: if nodeid == i.nodeid: @@ -138,8 +142,9 @@ class BMConnectionPool(object): def startListening(self, bind=None): """Open a listening socket and start accepting connections on it""" if bind is None: + "this return blank host" bind = self.getListeningIP() - port = BMConfigParser().safeGetInt("bitmessagesettings", "port") + port = int(BMConfigParser().safeGet("bitmessagesettings", "port")) # correct port even if it changed ls = TCPServer(host=bind, port=port) self.listeningSockets[ls.destination] = ls @@ -178,11 +183,10 @@ class BMConnectionPool(object): # This should never happen because socksproxytype setting # is handled in bitmessagemain before starting the connectionpool return - bootstrapper = bootstrap(connection_base) if not hostname: port = helper_random.randomchoice([8080, 8444]) - hostname = 'bootstrap%s.bitmessage.org' % port + hostname = ('bootstrap{}.bitmessage.org'.format(port)) else: port = 8444 self.addConnection(bootstrapper(hostname, port)) @@ -195,8 +199,8 @@ class BMConnectionPool(object): if BMConfigParser().safeGetBoolean( 'bitmessagesettings', 'dontconnect'): acceptConnections = False - elif BMConfigParser().safeGetBoolean( - 'bitmessagesettings', 'sendoutgoingconnections'): + elif bool(BMConfigParser().safeGet( + 'bitmessagesettings', 'sendoutgoingconnections')): spawnConnections = True socksproxytype = BMConfigParser().safeGet( 'bitmessagesettings', 'socksproxytype', '') @@ -219,8 +223,8 @@ class BMConnectionPool(object): Proxy.proxy = ( BMConfigParser().safeGet( 'bitmessagesettings', 'sockshostname'), - BMConfigParser().safeGetInt( - 'bitmessagesettings', 'socksport') + int(BMConfigParser().safeGet( + 'bitmessagesettings', 'socksport')) ) # TODO AUTH # TODO reset based on GUI settings changes @@ -236,11 +240,11 @@ class BMConnectionPool(object): except ValueError: Proxy.onion_proxy = None 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)) pending = len(self.outboundConnections) - established - if established < BMConfigParser().safeGetInt( - 'bitmessagesettings', 'maxoutboundconnections'): + if established < int(BMConfigParser().safeGet( + 'bitmessagesettings', 'maxoutboundconnections')): for i in range( state.maximumNumberOfHalfOpenConnections - pending): try: @@ -275,18 +279,22 @@ class BMConnectionPool(object): self.lastSpawned = time.time() - print('++++++++++++++++++++++++++++++++++++++++++') - print('self.inboundConnections.values()-{}'.format(self.inboundConnections.values())) - print('self.outboundConnections.values() -{}'.format(self.outboundConnections.values())) - print('+++++++++++++++++++++++++++++++++++++++++++') + # print('++++++++++++++++++++++++++++++++++++++++++') + # print('self.inboundConnections.values()-{}'.format(self.inboundConnections.values())) + # print('self.outboundConnections.values() -{}'.format(self.outboundConnections.values())) + # print('+++++++++++++++++++++++++++++++++++++++++++') else: + + # for i in ( + # list(self.inboundConnections.values()) + + # list(self.outboundConnections.values()) + # ): for i in ( - list(self.inboundConnections.values()) + - list(self.outboundConnections.values()) + [inboundConnections for inboundConnections in self.inboundConnections.values()] + + [inboundConnections for inboundConnections in self.outboundConnections.values()] ): # FIXME: rating will be increased after next connection i.handle_close() - if acceptConnections: if not self.listeningSockets: if BMConfigParser().safeGet('network', 'bind') == '': @@ -299,6 +307,7 @@ class BMConnectionPool(object): self.startListening(bind) logger.info('Listening for incoming connections.') if not self.udpSockets: + # self.udpSockets :- {'0.0.0.0': } if BMConfigParser().safeGet('network', 'bind') == '': self.startUDPSocket() else: @@ -327,9 +336,13 @@ class BMConnectionPool(object): asyncore.loop(timeout=loopTime, count=1000) reaper = [] + # for i in ( + # list(self.inboundConnections.values()) + + # list(self.outboundConnections.values()) + # ): for i in ( - list(self.inboundConnections.values()) + - list(self.outboundConnections.values()) + [inboundConnections for inboundConnections in self.inboundConnections.values()] + + [outboundConnections for outboundConnections in self.outboundConnections.values()] ): minTx = time.time() - 20 if i.fullyEstablished: @@ -341,11 +354,17 @@ class BMConnectionPool(object): i.close_reason = "Timeout (%is)" % ( time.time() - i.lastTx) 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 ( - list(self.inboundConnections.values()) + - list(self.outboundConnections.values()) + - list(self.listeningSockets.values()) + - list(self.udpSockets.values()) + [inboundConnections for inboundConnections in self.inboundConnections.values()] + + [outboundConnections for outboundConnections in self.outboundConnections.values()] + + [listeningSockets for listeningSockets in self.listeningSockets.values()] + + [udpSockets for udpSockets in self.udpSockets.values()] ): if not (i.accepting or i.connecting or i.connected): reaper.append(i) diff --git a/src/network/dandelion.py b/src/network/dandelion.py index 2c23da5d..0adeddd6 100644 --- a/src/network/dandelion.py +++ b/src/network/dandelion.py @@ -101,13 +101,27 @@ class Dandelion(): # pylint: disable=old-style-class self.stem.append(connection) for k in (k for k, v in iter(self.nodeMap.items()) if v is None): 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({ - 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 }).items(): self.hashMap[k] = Stem( connection, v.stream, self.poissonTimeout()) 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): """ diff --git a/src/network/objectracker.py b/src/network/objectracker.py index 40a0f582..21bd6fea 100644 --- a/src/network/objectracker.py +++ b/src/network/objectracker.py @@ -71,7 +71,7 @@ class ObjectTracker(object): # release memory deadline = time.time() - ObjectTracker.trackingExpires 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() def hasObj(self, hashid): diff --git a/src/network/stats.py b/src/network/stats.py index 1a57629d..7d6a5550 100644 --- a/src/network/stats.py +++ b/src/network/stats.py @@ -20,14 +20,19 @@ currentSentSpeed = 0 def connectedHostsList(): """List of all the connected hosts""" retval = [] - for i in list(BMConnectionPool().inboundConnections.values()) + \ - list(BMConnectionPool().outboundConnections.values()): + # for i in list(BMConnectionPool().inboundConnections.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: continue try: retval.append(i) except AttributeError: pass + print('#################### retval -{}'.format(retval)) return retval diff --git a/src/network/tcp.py b/src/network/tcp.py index 3ed19462..61285080 100644 --- a/src/network/tcp.py +++ b/src/network/tcp.py @@ -11,7 +11,7 @@ import time import addresses import network.asyncore_pollchoose as asyncore -import network.connectionpool +from network import connectionpool import helper_random import knownnodes import protocol @@ -71,8 +71,8 @@ class TCPConnection(BMProto, TLSDispatcher): TLSDispatcher.__init__(self, sock, server_side=False) self.connect(self.destination) logger.debug( - 'Connecting to %s:%i', - self.destination.host, self.destination.port) + 'Connecting to {}:{}'.format( + self.destination.host, self.destination.port)) try: self.local = ( protocol.checkIPAddress( @@ -131,15 +131,15 @@ class TCPConnection(BMProto, TLSDispatcher): def set_connection_fully_established(self): """Initiate inventory synchronisation.""" - if not self.isOutbound and not self.local: - shared.clientHasReceivedIncomingConnections = True - UISignalQueue.put(('setStatusIcon', 'green')) + shared.clientHasReceivedIncomingConnections = True + UISignalQueue.put(('setStatusIcon', 'green')) UISignalQueue.put(( 'updateNetworkStatusTab', (self.isOutbound, True, self.destination) )) self.antiIntersectionDelay(True) self.fullyEstablished = True + print('inside the set_connection_fully_established in tcp file') if self.isOutbound: knownnodes.increaseRating(self.destination) Dandelion().maybeAddStem(self) @@ -165,7 +165,7 @@ class TCPConnection(BMProto, TLSDispatcher): # only if more recent than 3 hours # and having positive or neutral rating filtered = [ - (k, v) for k, v in nodes.iteritems() + (k, v) for k, v in iter(nodes.items()) if v["lastseen"] > int(time.time()) - shared.maximumAgeOfNodesThatIAdvertiseToOthers and v["rating"] >= 0 and len(k.host) <= 22 @@ -191,8 +191,8 @@ class TCPConnection(BMProto, TLSDispatcher): if objectCount == 0: return logger.debug( - 'Sending huge inv message with %i objects to just this' - ' one peer', objectCount) + 'Sending huge inv message with {} objects to jcust this' + ' one peer'.format(objectCount)) self.append_write_buf(protocol.CreatePacket( 'inv', addresses.encodeVarint(objectCount) + payload)) @@ -208,7 +208,7 @@ class TCPConnection(BMProto, TLSDispatcher): continue bigInvList[objHash] = 0 objectCount = 0 - payload = b'' + payload = bytes() # Now let us start appending all of these hashes together. They will be # sent out in a big inv message to our new peer. for obj_hash, _ in bigInvList.items(): diff --git a/src/network/tls.py b/src/network/tls.py index 5f4be3b9..5a0f4c99 100644 --- a/src/network/tls.py +++ b/src/network/tls.py @@ -21,8 +21,8 @@ if sys.version_info >= (2, 7, 13): # this means TLSv1 or higher # in the future change to # ssl.PROTOCOL_TLS1.2 - # 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 + # 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 try: sslProtocolVersion = ssl.PROTOCOL_TLS # pylint: disable=no-member except AttributeError: @@ -83,6 +83,7 @@ class TLSDispatcher(AdvancedDispatcher): # pylint: disable=too-many-instanc # also exclude TLSv1 and TLSv1.1 in the future context.options = ssl.OP_ALL | ssl.OP_NO_SSLv2 |\ ssl.OP_NO_SSLv3 | ssl.OP_SINGLE_ECDH_USE | ssl.OP_CIPHER_SERVER_PREFERENCE + self.sslSocket = context.wrap_socket( self.socket, server_side=self.server_side, do_handshake_on_connect=False) else: diff --git a/src/protocol.py b/src/protocol.py index abb310db..a8d5a9a3 100644 --- a/src/protocol.py +++ b/src/protocol.py @@ -96,7 +96,7 @@ def encodeHost(host): if host.find('.onion') > -1: return '\xfd\x87\xd8\x7e\xeb\x43'.encode('utf-8') + base64.b32decode(host.split(".")[0], True) 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) return socket.inet_pton(socket.AF_INET6, host) @@ -156,17 +156,17 @@ def checkIPv4Address(host, hostStandardFormat, private=False): def checkIPv6Address(host, hostStandardFormat, private=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: - logger.debug('Ignoring loopback address: %s', hostStandardFormat) + logger.debug('Ignoring loopback address: {}'.format( hostStandardFormat)) return False if host[0] == '\xFE' and (ord(host[1]) & 0xc0) == 0x80: if not private: - logger.debug('Ignoring local address: %s', hostStandardFormat) + logger.debug('Ignoring local address: {}'.format( hostStandardFormat)) return hostStandardFormat if private else False - if (ord(host[0]) & 0xfe) == 0xfc: + if (ord(host[0:1]) & 0xfe) == 0xfc: 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 False if private else hostStandardFormat @@ -234,18 +234,18 @@ def isProofOfWorkSufficient(data, def CreatePacket(command, payload=''): """Construct and return a number of bytes from a payload""" + payload = payload if type(payload) == bytes else payload.encode() payload_length = len(payload) checksum = hashlib.sha512(payload).digest()[0:4] - - b = bytearray(Header.size + payload_length) - Header.pack_into(b, 0, 0xE9BEB4D9, command, payload_length, checksum) - b[Header.size:] = payload - return bytes(b) + byte = bytearray(Header.size + payload_length) + Header.pack_into(byte, 0, 0xE9BEB4D9, command.encode(), payload_length, checksum) + byte[Header.size:] = payload + return byte 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""" - payload = '' + payload = bytes() payload += pack('>L', 3) # protocol version. # bitflags of the services I offer. payload += pack( @@ -278,7 +278,9 @@ def assembleVersionMessage(remoteHost, remotePort, participatingStreams, server= (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. - 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 # or outgoing through clearnet extport = BMConfigParser().safeGetInt('bitmessagesettings', 'extport') @@ -289,9 +291,9 @@ def assembleVersionMessage(remoteHost, remotePort, participatingStreams, server= ): payload += pack('>H', extport) 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 - payload += pack('>H', BMConfigParser().getint('bitmessagesettings', 'port')) + payload += pack('>H', int(BMConfigParser().safeGet('bitmessagesettings', 'port'))) if nodeid is not None: payload += nodeid[0:8] @@ -299,7 +301,7 @@ def assembleVersionMessage(remoteHost, remotePort, participatingStreams, server= payload += eightBytesOfRandomDataUsedToDetectConnectionsToSelf userAgent = '/PyBitmessage:' + softwareVersion + '/' payload += encodeVarint(len(userAgent)) - payload += userAgent + payload += userAgent.encode() # Streams payload += encodeVarint(len(participatingStreams)) @@ -319,9 +321,9 @@ def assembleErrorMessage(fatal=0, banTime=0, inventoryVector='', errorText=''): payload = encodeVarint(fatal) payload += encodeVarint(banTime) payload += encodeVarint(len(inventoryVector)) - payload += inventoryVector + payload += inventoryVector.encode() if type(payload) == bytes else inventoryVector payload += encodeVarint(len(errorText)) - payload += errorText + payload += errorText.encode() if type(payload)== bytes else errorText return CreatePacket('error', payload) diff --git a/src/storage/sqlite.py b/src/storage/sqlite.py index 03265fd1..8b69ad1b 100644 --- a/src/storage/sqlite.py +++ b/src/storage/sqlite.py @@ -79,7 +79,7 @@ class SqliteInventory(InventoryStorage): # pylint: disable=too-many-ancestors with self.lock: t = int(time.time()) 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)) return hashes