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/asyncore_pollchoose.py b/src/network/asyncore_pollchoose.py index 1c93283f..3f6e8ad0 100644 --- a/src/network/asyncore_pollchoose.py +++ b/src/network/asyncore_pollchoose.py @@ -102,8 +102,7 @@ def _strerror(err): return os.strerror(err) except (ValueError, OverflowError, NameError): if err in errorcode: - return errorcode[err] - return "Unknown error %s" % err + ret18 ("Unknown error {}".format(err)) class ExitNow(Exception): @@ -247,25 +246,24 @@ def select_poller(timeout=0.0, map=None): if map is None: map = socket_map if map: - r = [] - w = [] - e = [] + rd = [] + wt = [] + ex = [] for fd, obj in list(map.items()): is_r = obj.readable() is_w = obj.writable() if is_r: - r.append(fd) + rd.append(fd) # accepting sockets should not be writable if is_w and not obj.accepting: - w.append(fd) + wt.append(fd) if is_r or is_w: - e.append(fd) - if [] == r == w == e: + ex.append(fd) + if [] == rd == wt == ex: time.sleep(timeout) return - try: - r, w, e = select.select(r, w, e, timeout) + rd, wt, ex = select.select(rd, wt, ex, timeout) except KeyboardInterrupt: return except socket.error as err: @@ -275,19 +273,19 @@ def select_poller(timeout=0.0, map=None): if err.args[0] in (WSAENOTSOCK, ): return - for fd in helper_random.randomsample(r, len(r)): + for fd in helper_random.randomsample(rd, len(rd)): obj = map.get(fd) if obj is None: continue read(obj) - for fd in helper_random.randomsample(w, len(w)): + for fd in helper_random.randomsample(wt, len(wt)): obj = map.get(fd) if obj is None: continue write(obj) - for fd in e: + for fd in ex: obj = map.get(fd) if obj is None: continue @@ -491,18 +489,18 @@ def loop(timeout=30.0, use_poll=False, map=None, count=None, poller=None): # argument which should no longer be used in favor of # "poller" - if poller is None: - if use_poll: - poller = poll_poller - elif hasattr(select, 'epoll'): - poller = epoll_poller - elif hasattr(select, 'kqueue'): - poller = kqueue_poller - elif hasattr(select, 'poll'): - poller = poll_poller - elif hasattr(select, 'select'): - poller = select_poller - + # if poller is None: + # if use_poll: + # poller = poll_poller + # elif hasattr(select, 'epoll'): + # poller = epoll_poller + # elif hasattr(select, 'kqueue'): + # poller = kqueue_poller + # elif hasattr(select, 'poll'): + # poller = poll_poller + # elif hasattr(select, 'select'): + # poller = select_poller + poller = select_poller if timeout == 0: deadline = 0 else: @@ -788,7 +786,7 @@ class dispatcher: def log_info(self, message, log_type='info'): """Conditionally print a message""" if log_type not in self.ignore_log_types: - print ('{}: {}'.format((log_type, message))) + print ('{}: {}'.format(log_type, message)) def handle_read_event(self): """Handle a read event""" diff --git a/src/network/bmproto.py b/src/network/bmproto.py index cfd6672f..772e4eaf 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 @@ -30,6 +30,19 @@ from network.objectracker import missingObjects, ObjectTracker from queues import objectProcessorQueue, portCheckerQueue, invQueue, addrQueue from network.randomtrackingdict import RandomTrackingDict +global addr_count +addr_count = 0 + +global addr_verack +addr_verack = 0 + +global addr_version +addr_version = 0 + +# global addr_count +# addr_count = 0 + +count = 0 class BMProtoError(ProxyError): """A Bitmessage Protocol Base Error""" @@ -83,12 +96,30 @@ 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')) + global count,addr_version,addr_count,addr_verack + count+=1 + if self.command == 'verack'.encode(): + addr_verack+=1 + # print('the addr_verack count are -{}'.format(addr_verack)) + + if self.command == 'version'.encode(): + addr_version+=1 + # print('the addr_version count are -{}'.format(addr_version)) + + if self.command == 'addr'.encode(): + addr_count+=1 + # print('the addr_count count are -{}'.format(addr_count)) + 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 +142,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 +180,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 +208,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 +361,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 +464,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 +532,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. @@ -528,13 +565,16 @@ class BMProto(AdvancedDispatcher, ObjectTracker): logger.debug( '%(host)s:%(port)i sending version', self.destination._asdict()) - if ((self.services & protocol.NODE_SSL == protocol.NODE_SSL) and - protocol.haveSSL(not self.isOutbound)): - self.isSSL = True + if ((self.services & protocol.NODE_SSL == protocol.NODE_SSL)): + # self.isSSL = True + pass if not self.verackReceived: return True + # self.set_state( + # "tls_init" if self.isSSL else "connection_fully_established", + # length=self.payloadLength, expectBytes=0) self.set_state( - "tls_init" if self.isSSL else "connection_fully_established", + "connection_fully_established", length=self.payloadLength, expectBytes=0) return False @@ -578,13 +618,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 +674,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..99876135 100644 --- a/src/network/connectionchooser.py +++ b/src/network/connectionchooser.py @@ -11,7 +11,7 @@ from queues import Queue, portCheckerQueue def getDiscoveredPeer(): try: - peer = random.choice(state.discoveredPeers.keys()) + peer = random.choice([key for key in state.discoveredPeers.keys()]) except (IndexError, KeyError): raise ValueError try: @@ -24,6 +24,7 @@ def getDiscoveredPeer(): def chooseConnection(stream): haveOnion = BMConfigParser().safeGet( "bitmessagesettings", "socksproxytype")[0:5] == 'SOCKS' + if state.trustedPeer: return state.trustedPeer try: @@ -37,14 +38,14 @@ 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([key for key in knownnodes.knownNodes[stream].keys()]) try: peer_info = knownnodes.knownNodes[stream][peer] if peer_info.get('self'): continue rating = peer_info["rating"] except TypeError: - logger.warning('Error in %s', peer) + logger.warning('Error in {}'.format(peer)) rating = 0 if haveOnion: # onion addresses have a higher priority when SOCKS diff --git a/src/network/connectionpool.py b/src/network/connectionpool.py index aad45e59..509c0d1f 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,10 +142,12 @@ 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) + print('inside the startListening method') self.listeningSockets[ls.destination] = ls def startUDPSocket(self, bind=None): @@ -178,11 +184,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 +200,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', '') @@ -215,12 +220,13 @@ class BMConnectionPool(object): self.startBootstrappers() knownnodes.knownNodesActual = True if not self.bootstrapped: + self.bootstrapped = True 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 +242,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,14 +281,19 @@ 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() @@ -298,7 +309,8 @@ class BMConnectionPool(object): ).split(): self.startListening(bind) logger.info('Listening for incoming connections.') - if not self.udpSockets: + if False: + # self.udpSockets :- {'0.0.0.0': } if BMConfigParser().safeGet('network', 'bind') == '': self.startUDPSocket() else: @@ -327,9 +339,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 +357,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..e661fffb 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 - for k, v in iter({ - k: v for k, v in iter(self.hashMap.items()) + #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 { + k: v for k, v in iter([hasmap for hasmap in self.hashMap.items()]) if v.child is None - }).items(): + }.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): """ @@ -119,14 +133,15 @@ class Dandelion(): # pylint: disable=old-style-class if connection in self.stem: self.stem.remove(connection) # active mappings to pointing to the removed node + for k in ( k for k, v in iter(self.nodeMap.items()) if v == connection ): self.nodeMap[k] = None - for k, v in iter({ - k: v for k, v in iter(self.hashMap.items()) + for k, v in { + k: v for k, v in iter(iter([hasmap for hasmap in self.hashMap.items()])) if v.child == connection - }).items(): + }.items(): self.hashMap[k] = Stem( None, v.stream, self.poissonTimeout()) diff --git a/src/network/networkthread.py b/src/network/networkthread.py index 2a22367f..ca7cb56c 100644 --- a/src/network/networkthread.py +++ b/src/network/networkthread.py @@ -26,17 +26,17 @@ class BMNetworkThread(StoppableThread): def stopThread(self): super(BMNetworkThread, self).stopThread() - for i in BMConnectionPool().listeningSockets.values(): + for i in [listeningSockets for listeningSockets in BMConnectionPool().listeningSockets.values()]: try: i.close() except: pass - for i in BMConnectionPool().outboundConnections.values(): + for i in [ outboundConnections for outboundConnections in BMConnectionPool().outboundConnections.values()]: try: i.close() except: pass - for i in BMConnectionPool().inboundConnections.values(): + for i in [inboundConnections for inboundConnections in BMConnectionPool().inboundConnections.values()]: try: i.close() except: 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..e6c37499 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 + return retval diff --git a/src/network/tcp.py b/src/network/tcp.py index 3ed19462..945740a0 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(): @@ -362,13 +362,15 @@ class TCPServer(AdvancedDispatcher): """TCP connection server for Bitmessage protocol""" def __init__(self, host='127.0.0.1', port=8444): - if not '_map' in dir(self): + if '_map' not in dir(self): AdvancedDispatcher.__init__(self) self.create_socket(socket.AF_INET, socket.SOCK_STREAM) self.set_reuse_addr() for attempt in range(50): + print('inside the attempt of line 371') try: if attempt > 0: + print('inside the if condition attempt in 373') port = random.randint(32767, 65535) self.bind((host, port)) except socket.error as e: @@ -376,6 +378,7 @@ class TCPServer(AdvancedDispatcher): continue else: if attempt > 0: + print('inside the if condition attempt in 381') BMConfigParser().set( 'bitmessagesettings', 'port', str(port)) BMConfigParser().save() diff --git a/src/network/tls.py b/src/network/tls.py index 5f4be3b9..0a6bcd91 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: @@ -67,6 +67,7 @@ class TLSDispatcher(AdvancedDispatcher): # pylint: disable=too-many-instanc self.isSSL = False def state_tls_init(self): + # print() """Prepare sockets for TLS handshake""" # pylint: disable=attribute-defined-outside-init self.isSSL = True @@ -93,13 +94,16 @@ class TLSDispatcher(AdvancedDispatcher): # pylint: disable=too-many-instanc ciphers=self.ciphers, do_handshake_on_connect=False) self.sslSocket.setblocking(0) self.want_read = self.want_write = True + # print('before tls file python 98 state are :- {}'.format(self.state)) self.set_state("tls_handshake") + # print('after tls file python 100 state are :- {}'.format(self.state)) return False # if hasattr(self.socket, "context"): # self.socket.context.set_ecdh_curve("secp256k1") @staticmethod def state_tls_handshake(): + # print("tls's state_tls_handshake method in line 107") """Do nothing while TLS handshake is pending, as during this phase we need to react to callbacks instead""" return False @@ -177,6 +181,7 @@ class TLSDispatcher(AdvancedDispatcher): # pylint: disable=too-many-instanc return def tls_handshake(self): + # print('inside the tls_handshake') """Perform TLS handshake and handle its stages""" # wait for flush if self.write_buf: diff --git a/src/network/udp.py b/src/network/udp.py index c3e4ce44..2280b77f 100644 --- a/src/network/udp.py +++ b/src/network/udp.py @@ -72,11 +72,14 @@ class UDPSocket(BMProto): # pylint: disable=too-many-instance-attribut addresses = self._decode_addr() # only allow peer discovery from private IPs in order to avoid # attacks from random IPs on the internet - if not self.local: - return True + # if not self.local: + # return True + self.local = True remoteport = False for seenTime, stream, services, ip, port in addresses: - decodedIP = protocol.checkIPAddress(str(ip)) + # decodedIP = bool(protocol.checkIPAddress(ip)) + decodedIP = False + if stream not in state.streamsInWhichIAmParticipating: continue if (seenTime < time.time() - self.maxTimeOffset or seenTime > time.time() + self.maxTimeOffset): @@ -88,7 +91,7 @@ class UDPSocket(BMProto): # pylint: disable=too-many-instance-attribut if remoteport is False: return True logger.debug( - "received peer discovery from %s:%i (port %i):", + "received peer discovery from {}:{} (port {}):", self.destination.host, self.destination.port, remoteport) if self.local: state.discoveredPeers[ diff --git a/src/protocol.py b/src/protocol.py index abb310db..cf030954 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 @@ -178,6 +178,7 @@ def haveSSL(server=False): python < 2.7.9's ssl library does not support ECDSA server due to missing initialisation of available curves, but client works ok """ + return False if not server: return True elif sys.version_info >= (2, 7, 9): @@ -234,18 +235,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 +279,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 +292,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 +302,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 +322,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/pyelliptic/arithmetic.py b/src/pyelliptic/arithmetic.py index 031db6ae..fc9e8c6f 100644 --- a/src/pyelliptic/arithmetic.py +++ b/src/pyelliptic/arithmetic.py @@ -47,7 +47,6 @@ def encode(val, base, minlen=0): result = code_string[0] * (minlen - len(result)) + result return result - def decode(string, base): code_string = get_code_string(base) result = 0 diff --git a/src/pyelliptic/openssl.py b/src/pyelliptic/openssl.py index 3545fa52..23de093e 100644 --- a/src/pyelliptic/openssl.py +++ b/src/pyelliptic/openssl.py @@ -679,7 +679,7 @@ def loadOpenSSL(): path.join(sys._MEIPASS, 'libssl.so'), path.join(sys._MEIPASS, 'libcrypto.so.1.1.0'), path.join(sys._MEIPASS, 'libssl.so.1.1.0'), - path.join(sys._MEIPASS, 'libcrypto.so.1.0.2'), + path.join(sys._MEIPASS, 'libcrypto.so.1.0.2'), path.join(sys._MEIPASS, 'libssl.so.1.0.2'), path.join(sys._MEIPASS, 'libcrypto.so.1.0.1'), path.join(sys._MEIPASS, 'libssl.so.1.0.1'), diff --git a/src/shared.py b/src/shared.py index 09830bde..9e6fc991 100644 --- a/src/shared.py +++ b/src/shared.py @@ -89,7 +89,6 @@ def isAddressInMyAddressBookSubscriptionsListOrWhitelist(address): return True return False - def decodeWalletImportFormat(WIFstring): fullString = arithmetic.changebase(WIFstring, 58, 256) privkey = fullString[:-4] 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