diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index f2ffed92..16a11c6f 100644 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -325,7 +325,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): 'base64'), 'message': message.encode('base64'), 'encodingType': encodingtype, 'receivedTime': received, 'read': read}, indent=4, separators=(',', ': ')) data += ']}' return data - elif method == 'getAllInboxMessageIds' or method == 'getAllInboxMessageIDs': + elif method == 'getAllInboxMessageIds': shared.sqlLock.acquire() shared.sqlSubmitQueue.put( '''SELECT msgid FROM inbox where folder='inbox' ORDER BY received''') @@ -374,7 +374,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): data += json.dumps({'msgid':msgid.encode('hex'), 'toAddress':toAddress, 'fromAddress':fromAddress, 'subject':subject.encode('base64'), 'message':message.encode('base64'), 'encodingType':encodingtype, 'lastActionTime':lastactiontime, 'status':status, 'ackData':ackdata.encode('hex')}, indent=4, separators=(',', ': ')) data += ']}' return data - elif method == 'getAllSentMessageIds' or method == 'getAllSentMessageIDs': + elif method == 'getAllSentMessageIds': shared.sqlLock.acquire() shared.sqlSubmitQueue.put('''SELECT msgid FROM sent where folder='sent' ORDER BY lastactiontime''') shared.sqlSubmitQueue.put('') diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index 1d115af1..71da6c68 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -2820,6 +2820,11 @@ class MyForm(QtGui.QMainWindow): def tableWidgetInboxItemClicked(self): currentRow = self.ui.tableWidgetInbox.currentRow() if currentRow >= 0: + + font = QFont() + font.setBold(False) + self.ui.textEditInboxMessage.setCurrentFont(font) + fromAddress = str(self.ui.tableWidgetInbox.item( currentRow, 1).data(Qt.UserRole).toPyObject()) # If we have received this message from either a broadcast address @@ -2838,9 +2843,7 @@ class MyForm(QtGui.QMainWindow): else: self.ui.textEditInboxMessage.setPlainText(self.ui.tableWidgetInbox.item(currentRow, 2).data(Qt.UserRole).toPyObject()[ :30000] + '\n\nDisplay of the remainder of the message truncated because it is too long.') # Only show the first 30K characters - - font = QFont() - font.setBold(False) + self.ui.tableWidgetInbox.item(currentRow, 0).setFont(font) self.ui.tableWidgetInbox.item(currentRow, 1).setFont(font) self.ui.tableWidgetInbox.item(currentRow, 2).setFont(font) diff --git a/src/class_outgoingSynSender.py b/src/class_outgoingSynSender.py index 8a929c7d..ac0b11ac 100644 --- a/src/class_outgoingSynSender.py +++ b/src/class_outgoingSynSender.py @@ -32,15 +32,15 @@ class outgoingSynSender(threading.Thread): break random.seed() shared.knownNodesLock.acquire() - peer, = random.sample(shared.knownNodes[self.streamNumber], 1) + HOST, = random.sample(shared.knownNodes[self.streamNumber], 1) shared.knownNodesLock.release() shared.alreadyAttemptedConnectionsListLock.acquire() - while peer in shared.alreadyAttemptedConnectionsList or peer.host in shared.connectedHostsList: + while HOST in shared.alreadyAttemptedConnectionsList or HOST in shared.connectedHostsList: shared.alreadyAttemptedConnectionsListLock.release() # print 'choosing new sample' random.seed() shared.knownNodesLock.acquire() - peer, = random.sample(shared.knownNodes[self.streamNumber], 1) + HOST, = random.sample(shared.knownNodes[self.streamNumber], 1) shared.knownNodesLock.release() time.sleep(1) # Clear out the shared.alreadyAttemptedConnectionsList every half @@ -51,10 +51,10 @@ class outgoingSynSender(threading.Thread): shared.alreadyAttemptedConnectionsListResetTime = int( time.time()) shared.alreadyAttemptedConnectionsListLock.acquire() - shared.alreadyAttemptedConnectionsList[peer] = 0 + shared.alreadyAttemptedConnectionsList[HOST] = 0 shared.alreadyAttemptedConnectionsListLock.release() - timeNodeLastSeen = shared.knownNodes[ - self.streamNumber][peer] + PORT, timeNodeLastSeen = shared.knownNodes[ + self.streamNumber][HOST] sock = socks.socksocket(socket.AF_INET, socket.SOCK_STREAM) # This option apparently avoids the TIME_WAIT state so that we # can rebind faster @@ -62,13 +62,13 @@ class outgoingSynSender(threading.Thread): sock.settimeout(20) if shared.config.get('bitmessagesettings', 'socksproxytype') == 'none' and shared.verbose >= 2: with shared.printLock: - print 'Trying an outgoing connection to', peer + print 'Trying an outgoing connection to', HOST, ':', PORT # sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) elif shared.config.get('bitmessagesettings', 'socksproxytype') == 'SOCKS4a': if shared.verbose >= 2: with shared.printLock: - print '(Using SOCKS4a) Trying an outgoing connection to', peer + print '(Using SOCKS4a) Trying an outgoing connection to', HOST, ':', PORT proxytype = socks.PROXY_TYPE_SOCKS4 sockshostname = shared.config.get( @@ -89,7 +89,7 @@ class outgoingSynSender(threading.Thread): elif shared.config.get('bitmessagesettings', 'socksproxytype') == 'SOCKS5': if shared.verbose >= 2: with shared.printLock: - print '(Using SOCKS5) Trying an outgoing connection to', peer + print '(Using SOCKS5) Trying an outgoing connection to', HOST, ':', PORT proxytype = socks.PROXY_TYPE_SOCKS5 sockshostname = shared.config.get( @@ -109,19 +109,19 @@ class outgoingSynSender(threading.Thread): proxytype, sockshostname, socksport, rdns) try: - sock.connect((peer.host, peer.port)) + sock.connect((HOST, PORT)) rd = receiveDataThread() rd.daemon = True # close the main program even if there are threads left someObjectsOfWhichThisRemoteNodeIsAlreadyAware = {} # This is not necessairly a complete list; we clear it from time to time to save memory. - rd.setup(sock, peer.host, peer.port, self.streamNumber, + rd.setup(sock, HOST, PORT, self.streamNumber, someObjectsOfWhichThisRemoteNodeIsAlreadyAware, self.selfInitiatedConnections) rd.start() with shared.printLock: - print self, 'connected to', peer, 'during an outgoing attempt.' + print self, 'connected to', HOST, 'during an outgoing attempt.' sd = sendDataThread() - sd.setup(sock, peer.host, peer.port, self.streamNumber, + sd.setup(sock, HOST, PORT, self.streamNumber, someObjectsOfWhichThisRemoteNodeIsAlreadyAware) sd.start() sd.sendVersionMessage() @@ -129,16 +129,16 @@ class outgoingSynSender(threading.Thread): except socks.GeneralProxyError as err: if shared.verbose >= 2: with shared.printLock: - print 'Could NOT connect to', peer, 'during outgoing attempt.', err + print 'Could NOT connect to', HOST, 'during outgoing attempt.', err - timeLastSeen = shared.knownNodes[ - self.streamNumber][peer] + PORT, timeLastSeen = shared.knownNodes[ + self.streamNumber][HOST] if (int(time.time()) - timeLastSeen) > 172800 and len(shared.knownNodes[self.streamNumber]) > 1000: # for nodes older than 48 hours old if we have more than 1000 hosts in our list, delete from the shared.knownNodes data-structure. shared.knownNodesLock.acquire() - del shared.knownNodes[self.streamNumber][peer] + del shared.knownNodes[self.streamNumber][HOST] shared.knownNodesLock.release() with shared.printLock: - print 'deleting ', peer, 'from shared.knownNodes because it is more than 48 hours old and we could not connect to it.' + print 'deleting ', HOST, 'from shared.knownNodes because it is more than 48 hours old and we could not connect to it.' except socks.Socks5AuthError as err: shared.UISignalQueue.put(( @@ -155,16 +155,16 @@ class outgoingSynSender(threading.Thread): else: if shared.verbose >= 1: with shared.printLock: - print 'Could NOT connect to', peer, 'during outgoing attempt.', err + print 'Could NOT connect to', HOST, 'during outgoing attempt.', err - timeLastSeen = shared.knownNodes[ - self.streamNumber][peer] + PORT, timeLastSeen = shared.knownNodes[ + self.streamNumber][HOST] if (int(time.time()) - timeLastSeen) > 172800 and len(shared.knownNodes[self.streamNumber]) > 1000: # for nodes older than 48 hours old if we have more than 1000 hosts in our list, delete from the knownNodes data-structure. shared.knownNodesLock.acquire() - del shared.knownNodes[self.streamNumber][peer] + del shared.knownNodes[self.streamNumber][HOST] shared.knownNodesLock.release() with shared.printLock: - print 'deleting ', peer, 'from knownNodes because it is more than 48 hours old and we could not connect to it.' + print 'deleting ', HOST, 'from knownNodes because it is more than 48 hours old and we could not connect to it.' except Exception as err: sys.stderr.write( diff --git a/src/class_receiveDataThread.py b/src/class_receiveDataThread.py index 6adfe490..2693b293 100644 --- a/src/class_receiveDataThread.py +++ b/src/class_receiveDataThread.py @@ -42,13 +42,14 @@ class receiveDataThread(threading.Thread): someObjectsOfWhichThisRemoteNodeIsAlreadyAware, selfInitiatedConnections): self.sock = sock - self.peer = shared.Peer(HOST, port) + self.HOST = HOST + self.PORT = port self.streamNumber = streamNumber self.payloadLength = 0 # This is the protocol payload length thus it doesn't include the 24 byte message header self.objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave = {} self.selfInitiatedConnections = selfInitiatedConnections shared.connectedHostsList[ - self.peer.host] = 0 # The very fact that this receiveData thread exists shows that we are connected to the remote host. Let's add it to this list so that an outgoingSynSender thread doesn't try to connect to it. + self.HOST] = 0 # The very fact that this receiveData thread exists shows that we are connected to the remote host. Let's add it to this list so that an outgoingSynSender thread doesn't try to connect to it. self.connectionIsOrWasFullyEstablished = False # set to true after the remote node and I accept each other's version messages. This is needed to allow the user interface to accurately reflect the current number of connections. if self.streamNumber == -1: # This was an incoming connection. Send out a version message if we accept the other node's version message. self.initiatedConnection = False @@ -69,18 +70,18 @@ class receiveDataThread(threading.Thread): self.data += self.sock.recv(4096) except socket.timeout: with shared.printLock: - print 'Timeout occurred waiting for data from', self.peer, '. Closing receiveData thread. (ID:', str(id(self)) + ')' + print 'Timeout occurred waiting for data from', self.HOST + '. Closing receiveData thread. (ID:', str(id(self)) + ')' break except Exception as err: with shared.printLock: - print 'sock.recv error. Closing receiveData thread (HOST:', self.peer, 'ID:', str(id(self)) + ').', err + print 'sock.recv error. Closing receiveData thread (HOST:', self.HOST, 'ID:', str(id(self)) + ').', err break # print 'Received', repr(self.data) if len(self.data) == dataLen: # If self.sock.recv returned no data: with shared.printLock: - print 'Connection to', self.peer, 'closed. Closing receiveData thread. (ID:', str(id(self)) + ')' + print 'Connection to', self.HOST, 'closed. Closing receiveData thread. (ID:', str(id(self)) + ')' break else: self.processData() @@ -92,16 +93,16 @@ class receiveDataThread(threading.Thread): except: pass - shared.broadcastToSendDataQueues((0, 'shutdown', self.peer)) + shared.broadcastToSendDataQueues((0, 'shutdown', self.HOST)) try: - del shared.connectedHostsList[self.peer.host] + del shared.connectedHostsList[self.HOST] except Exception as err: with shared.printLock: - print 'Could not delete', self.peer.host, 'from shared.connectedHostsList.', err + print 'Could not delete', self.HOST, 'from shared.connectedHostsList.', err try: del shared.numberOfObjectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHavePerPeer[ - self.peer] + self.HOST] except: pass shared.UISignalQueue.put(('updateNetworkStatusTab', 'no data')) @@ -136,12 +137,13 @@ class receiveDataThread(threading.Thread): # that other peers can be made aware of its existance. if self.initiatedConnection and self.connectionIsOrWasFullyEstablished: # The remote port is only something we should share with others if it is the remote node's incoming port (rather than some random operating-system-assigned outgoing port). shared.knownNodesLock.acquire() - shared.knownNodes[self.streamNumber][self.peer] = int(time.time()) + shared.knownNodes[self.streamNumber][ + self.HOST] = (self.PORT, int(time.time())) shared.knownNodesLock.release() if self.payloadLength <= 180000000: # If the size of the message is greater than 180MB, ignore it. (I get memory errors when processing messages much larger than this though it is concievable that this value will have to be lowered if some systems are less tolarant of large messages.) remoteCommand = self.data[4:16] with shared.printLock: - print 'remoteCommand', repr(remoteCommand.replace('\x00', '')), ' from', self.peer + print 'remoteCommand', repr(remoteCommand.replace('\x00', '')), ' from', self.HOST if remoteCommand == 'version\x00\x00\x00\x00\x00': self.recversion(self.data[24:self.payloadLength + 24]) @@ -194,28 +196,28 @@ class receiveDataThread(threading.Thread): objectHash] # It is possible that the remote node doesn't respond with the object. In that case, we'll very likely get it from someone else anyway. if len(self.objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave) == 0: with shared.printLock: - print '(concerning', str(self.peer) + ')', 'number of objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave is now', len(self.objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave) + print '(concerning', self.HOST + ')', 'number of objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave is now', len(self.objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave) try: del shared.numberOfObjectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHavePerPeer[ - self.peer] # this data structure is maintained so that we can keep track of how many total objects, across all connections, are currently outstanding. If it goes too high it can indicate that we are under attack by multiple nodes working together. + self.HOST] # this data structure is maintained so that we can keep track of how many total objects, across all connections, are currently outstanding. If it goes too high it can indicate that we are under attack by multiple nodes working together. except: pass break if len(self.objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave) == 0: with shared.printLock: - print '(concerning', str(self.peer) + ')', 'number of objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave is now', len(self.objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave) + print '(concerning', self.HOST + ')', 'number of objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave is now', len(self.objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave) try: del shared.numberOfObjectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHavePerPeer[ - self.peer] # this data structure is maintained so that we can keep track of how many total objects, across all connections, are currently outstanding. If it goes too high it can indicate that we are under attack by multiple nodes working together. + self.HOST] # this data structure is maintained so that we can keep track of how many total objects, across all connections, are currently outstanding. If it goes too high it can indicate that we are under attack by multiple nodes working together. except: pass if len(self.objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave) > 0: with shared.printLock: - print '(concerning', str(self.peer) + ')', 'number of objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave is now', len(self.objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave) + print '(concerning', self.HOST + ')', 'number of objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave is now', len(self.objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave) - shared.numberOfObjectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHavePerPeer[self.peer] = len( + shared.numberOfObjectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHavePerPeer[self.HOST] = len( self.objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave) # this data structure is maintained so that we can keep track of how many total objects, across all connections, are currently outstanding. If it goes too high it can indicate that we are under attack by multiple nodes working together. if len(self.ackDataThatWeHaveYetToSend) > 0: self.data = self.ackDataThatWeHaveYetToSend.pop() @@ -260,22 +262,22 @@ class receiveDataThread(threading.Thread): self.sock.settimeout( 600) # We'll send out a pong every 5 minutes to make sure the connection stays alive if there has been no other traffic to send lately. shared.UISignalQueue.put(('updateNetworkStatusTab', 'no data')) - remoteNodeSeenTime = shared.knownNodes[ - self.streamNumber][self.peer] + remoteNodeIncomingPort, remoteNodeSeenTime = shared.knownNodes[ + self.streamNumber][self.HOST] with shared.printLock: - print 'Connection fully established with', self.peer + print 'Connection fully established with', self.HOST, remoteNodeIncomingPort print 'The size of the connectedHostsList is now', len(shared.connectedHostsList) print 'The length of sendDataQueues is now:', len(shared.sendDataQueues) print 'broadcasting addr from within connectionFullyEstablished function.' - self.broadcastaddr([(int(time.time()), self.streamNumber, 1, self.peer.host, - self.peer.port)]) # This lets all of our peers know about this new node. + self.broadcastaddr([(int(time.time()), self.streamNumber, 1, self.HOST, + remoteNodeIncomingPort)]) # This lets all of our peers know about this new node. self.sendaddr() # This is one large addr message to this one peer. if not self.initiatedConnection and len(shared.connectedHostsList) > 200: with shared.printLock: print 'We are connected to too many people. Closing connection.' - shared.broadcastToSendDataQueues((0, 'shutdown', self.peer)) + shared.broadcastToSendDataQueues((0, 'shutdown', self.HOST)) return self.sendBigInv() @@ -1495,7 +1497,7 @@ class receiveDataThread(threading.Thread): self.objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave[ data[lengthOfVarint + (32 * i):32 + lengthOfVarint + (32 * i)]] = 0 shared.numberOfObjectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHavePerPeer[ - self.peer] = len(self.objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave) + self.HOST] = len(self.objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave) # Send a getdata message to our peer to request the object with the given # hash @@ -1677,11 +1679,11 @@ class receiveDataThread(threading.Thread): shared.knownNodesLock.acquire() shared.knownNodes[recaddrStream] = {} shared.knownNodesLock.release() - peerFromAddrMessage = shared.Peer(hostFromAddrMessage, recaddrPort) - if peerFromAddrMessage not in shared.knownNodes[recaddrStream]: + if hostFromAddrMessage not in shared.knownNodes[recaddrStream]: if len(shared.knownNodes[recaddrStream]) < 20000 and timeSomeoneElseReceivedMessageFromThisNode > (int(time.time()) - 10800) and timeSomeoneElseReceivedMessageFromThisNode < (int(time.time()) + 10800): # If we have more than 20000 nodes in our list already then just forget about adding more. Also, make sure that the time that someone else received a message from this node is within three hours from now. shared.knownNodesLock.acquire() - shared.knownNodes[recaddrStream][peerFromAddrMessage] = timeSomeoneElseReceivedMessageFromThisNode + shared.knownNodes[recaddrStream][hostFromAddrMessage] = ( + recaddrPort, timeSomeoneElseReceivedMessageFromThisNode) shared.knownNodesLock.release() needToWriteKnownNodesToDisk = True hostDetails = ( @@ -1690,12 +1692,15 @@ class receiveDataThread(threading.Thread): listOfAddressDetailsToBroadcastToPeers.append( hostDetails) else: - timeLastReceivedMessageFromThisNode = shared.knownNodes[recaddrStream][ - peerFromAddrMessage] # PORT in this case is either the port we used to connect to the remote node, or the port that was specified by someone else in a past addr message. + PORT, timeLastReceivedMessageFromThisNode = shared.knownNodes[recaddrStream][ + hostFromAddrMessage] # PORT in this case is either the port we used to connect to the remote node, or the port that was specified by someone else in a past addr message. if (timeLastReceivedMessageFromThisNode < timeSomeoneElseReceivedMessageFromThisNode) and (timeSomeoneElseReceivedMessageFromThisNode < int(time.time())): shared.knownNodesLock.acquire() - shared.knownNodes[recaddrStream][peerFromAddrMessage] = timeSomeoneElseReceivedMessageFromThisNode + shared.knownNodes[recaddrStream][hostFromAddrMessage] = ( + PORT, timeSomeoneElseReceivedMessageFromThisNode) shared.knownNodesLock.release() + if PORT != recaddrPort: + print 'Strange occurance: The port specified in an addr message', str(recaddrPort), 'does not match the port', str(PORT), 'that this program (or some other peer) used to connect to it', str(hostFromAddrMessage), '. Perhaps they changed their port or are using a strange NAT configuration.' if needToWriteKnownNodesToDisk: # Runs if any nodes were new to us. Also, share those nodes with our peers. shared.knownNodesLock.acquire() output = open(shared.appdata + 'knownnodes.dat', 'wb') @@ -1781,15 +1786,14 @@ class receiveDataThread(threading.Thread): shared.knownNodesLock.acquire() shared.knownNodes[recaddrStream] = {} shared.knownNodesLock.release() - peerFromAddrMessage = shared.Peer(hostFromAddrMessage, recaddrPort) - if peerFromAddrMessage not in shared.knownNodes[recaddrStream]: + if hostFromAddrMessage not in shared.knownNodes[recaddrStream]: if len(shared.knownNodes[recaddrStream]) < 20000 and timeSomeoneElseReceivedMessageFromThisNode > (int(time.time()) - 10800) and timeSomeoneElseReceivedMessageFromThisNode < (int(time.time()) + 10800): # If we have more than 20000 nodes in our list already then just forget about adding more. Also, make sure that the time that someone else received a message from this node is within three hours from now. shared.knownNodesLock.acquire() - shared.knownNodes[recaddrStream][peerFromAddrMessage] = ( - timeSomeoneElseReceivedMessageFromThisNode) + shared.knownNodes[recaddrStream][hostFromAddrMessage] = ( + recaddrPort, timeSomeoneElseReceivedMessageFromThisNode) shared.knownNodesLock.release() with shared.printLock: - print 'added new node', peerFromAddrMessage, 'to knownNodes in stream', recaddrStream + print 'added new node', hostFromAddrMessage, 'to knownNodes in stream', recaddrStream needToWriteKnownNodesToDisk = True hostDetails = ( @@ -1798,12 +1802,15 @@ class receiveDataThread(threading.Thread): listOfAddressDetailsToBroadcastToPeers.append( hostDetails) else: - timeLastReceivedMessageFromThisNode = shared.knownNodes[recaddrStream][ - peerFromAddrMessage] # PORT in this case is either the port we used to connect to the remote node, or the port that was specified by someone else in a past addr message. + PORT, timeLastReceivedMessageFromThisNode = shared.knownNodes[recaddrStream][ + hostFromAddrMessage] # PORT in this case is either the port we used to connect to the remote node, or the port that was specified by someone else in a past addr message. if (timeLastReceivedMessageFromThisNode < timeSomeoneElseReceivedMessageFromThisNode) and (timeSomeoneElseReceivedMessageFromThisNode < int(time.time())): shared.knownNodesLock.acquire() - shared.knownNodes[recaddrStream][peerFromAddrMessage] = timeSomeoneElseReceivedMessageFromThisNode + shared.knownNodes[recaddrStream][hostFromAddrMessage] = ( + PORT, timeSomeoneElseReceivedMessageFromThisNode) shared.knownNodesLock.release() + if PORT != recaddrPort: + print 'Strange occurance: The port specified in an addr message', str(recaddrPort), 'does not match the port', str(PORT), 'that this program (or some other peer) used to connect to it', str(hostFromAddrMessage), '. Perhaps they changed their port or are using a strange NAT configuration.' if needToWriteKnownNodesToDisk: # Runs if any nodes were new to us. Also, share those nodes with our peers. shared.knownNodesLock.acquire() output = open(shared.appdata + 'knownnodes.dat', 'wb') @@ -1860,35 +1867,35 @@ class receiveDataThread(threading.Thread): if len(shared.knownNodes[self.streamNumber]) > 0: for i in range(500): random.seed() - peer, = random.sample(shared.knownNodes[self.streamNumber], 1) - if helper_generic.isHostInPrivateIPRange(peer.host): + HOST, = random.sample(shared.knownNodes[self.streamNumber], 1) + if helper_generic.isHostInPrivateIPRange(HOST): continue - addrsInMyStream[peer] = shared.knownNodes[ - self.streamNumber][peer] + addrsInMyStream[HOST] = shared.knownNodes[ + self.streamNumber][HOST] if len(shared.knownNodes[self.streamNumber * 2]) > 0: for i in range(250): random.seed() - peer, = random.sample(shared.knownNodes[ + HOST, = random.sample(shared.knownNodes[ self.streamNumber * 2], 1) - if helper_generic.isHostInPrivateIPRange(peer.host): + if helper_generic.isHostInPrivateIPRange(HOST): continue - addrsInChildStreamLeft[peer] = shared.knownNodes[ - self.streamNumber * 2][peer] + addrsInChildStreamLeft[HOST] = shared.knownNodes[ + self.streamNumber * 2][HOST] if len(shared.knownNodes[(self.streamNumber * 2) + 1]) > 0: for i in range(250): random.seed() - peer, = random.sample(shared.knownNodes[ + HOST, = random.sample(shared.knownNodes[ (self.streamNumber * 2) + 1], 1) - if helper_generic.isHostInPrivateIPRange(peer.host): + if helper_generic.isHostInPrivateIPRange(HOST): continue - addrsInChildStreamRight[peer] = shared.knownNodes[ - (self.streamNumber * 2) + 1][peer] + addrsInChildStreamRight[HOST] = shared.knownNodes[ + (self.streamNumber * 2) + 1][HOST] shared.knownNodesLock.release() numberOfAddressesInAddrMessage = 0 payload = '' # print 'addrsInMyStream.items()', addrsInMyStream.items() - for (HOST, PORT), value in addrsInMyStream.items(): - timeLastReceivedMessageFromThisNode = value + for HOST, value in addrsInMyStream.items(): + PORT, timeLastReceivedMessageFromThisNode = value if timeLastReceivedMessageFromThisNode > (int(time.time()) - shared.maximumAgeOfNodesThatIAdvertiseToOthers): # If it is younger than 3 hours old.. numberOfAddressesInAddrMessage += 1 payload += pack( @@ -1899,8 +1906,8 @@ class receiveDataThread(threading.Thread): payload += '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF' + \ socket.inet_aton(HOST) payload += pack('>H', PORT) # remote port - for (HOST, PORT), value in addrsInChildStreamLeft.items(): - timeLastReceivedMessageFromThisNode = value + for HOST, value in addrsInChildStreamLeft.items(): + PORT, timeLastReceivedMessageFromThisNode = value if timeLastReceivedMessageFromThisNode > (int(time.time()) - shared.maximumAgeOfNodesThatIAdvertiseToOthers): # If it is younger than 3 hours old.. numberOfAddressesInAddrMessage += 1 payload += pack( @@ -1911,8 +1918,8 @@ class receiveDataThread(threading.Thread): payload += '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF' + \ socket.inet_aton(HOST) payload += pack('>H', PORT) # remote port - for (HOST, PORT), value in addrsInChildStreamRight.items(): - timeLastReceivedMessageFromThisNode = value + for HOST, value in addrsInChildStreamRight.items(): + PORT, timeLastReceivedMessageFromThisNode = value if timeLastReceivedMessageFromThisNode > (int(time.time()) - shared.maximumAgeOfNodesThatIAdvertiseToOthers): # If it is younger than 3 hours old.. numberOfAddressesInAddrMessage += 1 payload += pack( @@ -1949,9 +1956,9 @@ class receiveDataThread(threading.Thread): elif not self.verackSent: self.remoteProtocolVersion, = unpack('>L', data[:4]) if self.remoteProtocolVersion <= 1: - shared.broadcastToSendDataQueues((0, 'shutdown', self.peer)) + shared.broadcastToSendDataQueues((0, 'shutdown', self.HOST)) with shared.printLock: - print 'Closing connection to old protocol version 1 node: ', self.peer + print 'Closing connection to old protocol version 1 node: ', self.HOST return # print 'remoteProtocolVersion', self.remoteProtocolVersion @@ -1973,29 +1980,30 @@ class receiveDataThread(threading.Thread): print 'Remote node useragent:', useragent, ' stream number:', self.streamNumber if self.streamNumber != 1: - shared.broadcastToSendDataQueues((0, 'shutdown', self.peer)) + shared.broadcastToSendDataQueues((0, 'shutdown', self.HOST)) with shared.printLock: - print 'Closed connection to', self.peer, 'because they are interested in stream', self.streamNumber, '.' + print 'Closed connection to', self.HOST, 'because they are interested in stream', self.streamNumber, '.' return shared.connectedHostsList[ - self.peer.host] = 1 # We use this data structure to not only keep track of what hosts we are connected to so that we don't try to connect to them again, but also to list the connections count on the Network Status tab. + self.HOST] = 1 # We use this data structure to not only keep track of what hosts we are connected to so that we don't try to connect to them again, but also to list the connections count on the Network Status tab. # If this was an incoming connection, then the sendData thread # doesn't know the stream. We have to set it. if not self.initiatedConnection: shared.broadcastToSendDataQueues(( - 0, 'setStreamNumber', (self.peer, self.streamNumber))) + 0, 'setStreamNumber', (self.HOST, self.streamNumber))) if data[72:80] == shared.eightBytesOfRandomDataUsedToDetectConnectionsToSelf: - shared.broadcastToSendDataQueues((0, 'shutdown', self.peer)) + shared.broadcastToSendDataQueues((0, 'shutdown', self.HOST)) with shared.printLock: - print 'Closing connection to myself: ', self.peer + print 'Closing connection to myself: ', self.HOST return shared.broadcastToSendDataQueues((0, 'setRemoteProtocolVersion', ( - self.peer, self.remoteProtocolVersion))) + self.HOST, self.remoteProtocolVersion))) shared.knownNodesLock.acquire() - shared.knownNodes[self.streamNumber][self.peer] = int(time.time()) + shared.knownNodes[self.streamNumber][self.HOST] = ( + self.remoteNodeIncomingPort, int(time.time())) output = open(shared.appdata + 'knownnodes.dat', 'wb') pickle.dump(shared.knownNodes, output) output.close() @@ -2012,7 +2020,7 @@ class receiveDataThread(threading.Thread): try: self.sock.sendall(shared.assembleVersionMessage( - self.peer.host, self.peer.port, self.streamNumber)) + self.HOST, self.PORT, self.streamNumber)) except Exception as err: # if not 'Bad file descriptor' in err: with shared.printLock: diff --git a/src/class_sendDataThread.py b/src/class_sendDataThread.py index 867d1d70..dec436e9 100644 --- a/src/class_sendDataThread.py +++ b/src/class_sendDataThread.py @@ -31,7 +31,8 @@ class sendDataThread(threading.Thread): streamNumber, someObjectsOfWhichThisRemoteNodeIsAlreadyAware): self.sock = sock - self.peer = shared.Peer(HOST, PORT) + self.HOST = HOST + self.PORT = PORT self.streamNumber = streamNumber self.remoteProtocolVersion = - \ 1 # This must be set using setRemoteProtocolVersion command which is sent through the self.mailbox queue. @@ -44,7 +45,7 @@ class sendDataThread(threading.Thread): def sendVersionMessage(self): datatosend = shared.assembleVersionMessage( - self.peer.host, self.peer.port, self.streamNumber) # the IP and port of the remote host, and my streamNumber. + self.HOST, self.PORT, self.streamNumber) # the IP and port of the remote host, and my streamNumber. with shared.printLock: print 'Sending version packet: ', repr(datatosend) @@ -61,12 +62,15 @@ class sendDataThread(threading.Thread): def run(self): while True: deststream, command, data = self.mailbox.get() + # with shared.printLock: + # print 'sendDataThread, destream:', deststream, ', Command:', command, ', ID:',id(self), ', HOST:', self.HOST + # if deststream == self.streamNumber or deststream == 0: if command == 'shutdown': - if data == self.peer or data == 'all': + if data == self.HOST or data == 'all': with shared.printLock: - print 'sendDataThread (associated with', self.peer, ') ID:', id(self), 'shutting down now.' + print 'sendDataThread (associated with', self.HOST, ') ID:', id(self), 'shutting down now.' try: self.sock.shutdown(socket.SHUT_RDWR) @@ -85,15 +89,15 @@ class sendDataThread(threading.Thread): # will continue on with the connection and will set the # streamNumber of this send data thread here: elif command == 'setStreamNumber': - peerInMessage, specifiedStreamNumber = data - if peerInMessage == self.peer: + hostInMessage, specifiedStreamNumber = data + if hostInMessage == self.HOST: with shared.printLock: print 'setting the stream number in the sendData thread (ID:', id(self), ') to', specifiedStreamNumber self.streamNumber = specifiedStreamNumber elif command == 'setRemoteProtocolVersion': - peerInMessage, specifiedRemoteProtocolVersion = data - if peerInMessage == self.peer: + hostInMessage, specifiedRemoteProtocolVersion = data + if hostInMessage == self.HOST: with shared.printLock: print 'setting the remote node\'s protocol version in the sendData thread (ID:', id(self), ') to', specifiedRemoteProtocolVersion @@ -109,14 +113,14 @@ class sendDataThread(threading.Thread): self.sock.sendall(data) self.lastTimeISentData = int(time.time()) except: - print 'sendaddr: self.sock.sendall failed' + print 'self.sock.sendall failed' try: self.sock.shutdown(socket.SHUT_RDWR) self.sock.close() except: pass shared.sendDataQueues.remove(self.mailbox) - print 'sendDataThread thread (ID:', str(id(self)) + ') ending now. Was connected to', self.peer + print 'sendDataThread thread (ID:', str(id(self)) + ') ending now. Was connected to', self.HOST break elif command == 'sendinv': if data not in self.someObjectsOfWhichThisRemoteNodeIsAlreadyAware: @@ -133,21 +137,21 @@ class sendDataThread(threading.Thread): self.sock.sendall(headerData + payload) self.lastTimeISentData = int(time.time()) except: - print 'sendinv: self.sock.sendall failed' + print 'self.sock.sendall failed' try: self.sock.shutdown(socket.SHUT_RDWR) self.sock.close() except: pass shared.sendDataQueues.remove(self.mailbox) - print 'sendDataThread thread (ID:', str(id(self)) + ') ending now. Was connected to', self.peer + print 'sendDataThread thread (ID:', str(id(self)) + ') ending now. Was connected to', self.HOST break elif command == 'pong': self.someObjectsOfWhichThisRemoteNodeIsAlreadyAware.clear() # To save memory, let us clear this data structure from time to time. As its function is to help us keep from sending inv messages to peers which sent us the same inv message mere seconds earlier, it will be fine to clear this data structure from time to time. if self.lastTimeISentData < (int(time.time()) - 298): # Send out a pong message to keep the connection alive. with shared.printLock: - print 'Sending pong to', self.peer, 'to keep connection alive.' + print 'Sending pong to', self.HOST, 'to keep connection alive.' try: self.sock.sendall( @@ -161,7 +165,7 @@ class sendDataThread(threading.Thread): except: pass shared.sendDataQueues.remove(self.mailbox) - print 'sendDataThread thread', self, 'ending now. Was connected to', self.peer + print 'sendDataThread thread', self, 'ending now. Was connected to', self.HOST break else: with shared.printLock: diff --git a/src/defaultKnownNodes.py b/src/defaultKnownNodes.py index 52e31fff..5c6d5b21 100644 --- a/src/defaultKnownNodes.py +++ b/src/defaultKnownNodes.py @@ -5,20 +5,19 @@ import time import random import sys from time import strftime, localtime -import shared def createDefaultKnownNodes(appdata): ############## Stream 1 ################ stream1 = {} - stream1[shared.Peer('85.171.174.131', 8444)] = int(time.time()) - stream1[shared.Peer('23.28.68.159', 8444)] = int(time.time()) - stream1[shared.Peer('66.108.210.240', 8444)] = int(time.time()) - stream1[shared.Peer('204.236.246.212', 8444)] = int(time.time()) - stream1[shared.Peer('78.81.56.239', 8444)] = int(time.time()) - stream1[shared.Peer('122.60.235.157', 8444)] = int(time.time()) - stream1[shared.Peer('204.236.246.212', 8444)] = int(time.time()) - stream1[shared.Peer('24.98.219.109', 8444)] = int(time.time()) + stream1['85.171.174.131'] = (8444,int(time.time())) + stream1['23.28.68.159'] = (8444,int(time.time())) + stream1['66.108.210.240'] = (8080,int(time.time())) + stream1['204.236.246.212'] = (8444,int(time.time())) + stream1['78.81.56.239'] = (8444,int(time.time())) + stream1['122.60.235.157'] = (8444,int(time.time())) + stream1['204.236.246.212'] = (8444,int(time.time())) + stream1['24.98.219.109'] = (8444,int(time.time())) ############# Stream 2 ################# @@ -49,15 +48,10 @@ def readDefaultKnownNodes(appdata): pickleFile = open(appdata + 'knownnodes.dat', 'rb') knownNodes = pickle.load(pickleFile) pickleFile.close() + knownNodes for stream, storedValue in knownNodes.items(): for host,value in storedValue.items(): - try: - # Old knownNodes format. - port, storedtime = value - except: - # New knownNodes format. - host, port = host - storedtime = value + port, storedtime = storedValue[host] print host, '\t', port, '\t', unicode(strftime('%a, %d %b %Y %I:%M %p',localtime(storedtime)),'utf-8') if __name__ == "__main__": diff --git a/src/helper_bootstrap.py b/src/helper_bootstrap.py index f0cae8f2..e0056342 100644 --- a/src/helper_bootstrap.py +++ b/src/helper_bootstrap.py @@ -9,20 +9,8 @@ def knownNodes(): # We shouldn't have to use the shared.knownNodesLock because this had # better be the only thread accessing knownNodes right now. pickleFile = open(shared.appdata + 'knownnodes.dat', 'rb') - loadedKnownNodes = pickle.load(pickleFile) + shared.knownNodes = pickle.load(pickleFile) pickleFile.close() - # The old format of storing knownNodes was as a 'host: (port, time)' - # mapping. The new format is as 'Peer: time' pairs. If we loaded - # data in the old format, transform it to the new style. - for stream, nodes in loadedKnownNodes.items(): - shared.knownNodes[stream] = {} - for node_tuple in nodes.items(): - try: - host, (port, time) = node_tuple - peer = shared.Peer(host, port) - except: - peer, time = node_tuple - shared.knownNodes[stream][peer] = time except: shared.knownNodes = defaultKnownNodes.createDefaultKnownNodes(shared.appdata) if shared.config.getint('bitmessagesettings', 'settingsversion') > 6: @@ -40,13 +28,13 @@ def dns(): try: for item in socket.getaddrinfo('bootstrap8080.bitmessage.org', 80): print 'Adding', item[4][0], 'to knownNodes based on DNS boostrap method' - shared.knownNodes[1][shared.Peer(item[4][0], 8080)] = int(time.time()) + shared.knownNodes[1][item[4][0]] = (8080, int(time.time())) except: print 'bootstrap8080.bitmessage.org DNS bootstrapping failed.' try: for item in socket.getaddrinfo('bootstrap8444.bitmessage.org', 80): print 'Adding', item[4][0], 'to knownNodes based on DNS boostrap method' - shared.knownNodes[1][shared.Peer(item[4][0], 8444)] = int(time.time()) + shared.knownNodes[1][item[4][0]] = (8444, int(time.time())) except: print 'bootstrap8444.bitmessage.org DNS bootstrapping failed.' else: diff --git a/src/shared.py b/src/shared.py index 214c124f..32370524 100644 --- a/src/shared.py +++ b/src/shared.py @@ -9,7 +9,6 @@ useVeryEasyProofOfWorkForTesting = False # If you set this to True while on the # Libraries. -import collections import ConfigParser import os import pickle @@ -371,7 +370,5 @@ def fixSensitiveFilePermissions(filename, hasEnabledKeys): logger.exception('Keyfile permissions could not be fixed.') raise -Peer = collections.namedtuple('Peer', ['host', 'port']) - helper_startup.loadConfig() -from debug import logger +from debug import logger \ No newline at end of file