def peerValidityChecks(self): if self.remoteProtocolVersion < 3: self.sendDataThreadQueue.put((0, 'sendRawData',protocol.assembleErrorMessage( fatal=2, errorText="Your is using an old protocol. Closing connection."))) logger.debug ('Closing connection to old protocol version ' + str(self.remoteProtocolVersion) + ' node: ' + str(self.peer)) return False if self.timeOffset > 3600: self.sendDataThreadQueue.put((0, 'sendRawData', protocol.assembleErrorMessage( fatal=2, errorText="Your time is too far in the future compared to mine. Closing connection."))) logger.info("%s's time is too far in the future (%s seconds). Closing connection to it.", self.peer, self.timeOffset) shared.timeOffsetWrongCount += 1 time.sleep(2) return False elif self.timeOffset < -3600: self.sendDataThreadQueue.put((0, 'sendRawData', protocol.assembleErrorMessage( fatal=2, errorText="Your time is too far in the past compared to mine. Closing connection."))) logger.info("%s's time is too far in the past (timeOffset %s seconds). Closing connection to it.", self.peer, self.timeOffset) shared.timeOffsetWrongCount += 1 return False else: shared.timeOffsetWrongCount = 0 if len(self.streamNumber) == 0: self.sendDataThreadQueue.put((0, 'sendRawData', protocol.assembleErrorMessage( fatal=2, errorText="We don't have shared stream interests. Closing connection."))) logger.debug ('Closed connection to ' + str(self.peer) + ' because there is no overlapping interest in streams.') return False return True
def validatePeer(self): if self.remoteProtocolVersion < 3: self.sendDataThreadQueue.put((0, 'sendRawData',protocol.assembleErrorMessage( fatal=2, errorText="Your is using an old protocol. Closing connection."))) logger.debug ('Closing connection to old protocol version ' + str(self.remoteProtocolVersion) + ' node: ' + str(self.peer)) return False if self.timeOffset > 3600: self.sendDataThreadQueue.put((0, 'sendRawData', protocol.assembleErrorMessage( fatal=2, errorText="Your time is too far in the future compared to mine. Closing connection."))) logger.info("%s's time is too far in the future (%s seconds). Closing connection to it.", self.peer, self.timeOffset) shared.timeOffsetWrongCount += 1 time.sleep(2) return False elif self.timeOffset < -3600: self.sendDataThreadQueue.put((0, 'sendRawData', protocol.assembleErrorMessage( fatal=2, errorText="Your time is too far in the past compared to mine. Closing connection."))) logger.info("%s's time is too far in the past (timeOffset %s seconds). Closing connection to it.", self.peer, self.timeOffset) shared.timeOffsetWrongCount += 1 return False else: shared.timeOffsetWrongCount = 0 if len(self.streamNumber) == 0: self.sendDataThreadQueue.put((0, 'sendRawData', protocol.assembleErrorMessage( fatal=2, errorText="We don't have shared stream interests. Closing connection."))) logger.debug ('Closed connection to ' + str(self.peer) + ' because there is no overlapping interest in streams.') return False if self.remoteNodeId == protocol.ServerNodeId: logger.debug('Closing connection to myself: ' + str(self.peer)) return False return True
def connectionFullyEstablished(self): if self.connectionIsOrWasFullyEstablished: # there is no reason to run this function a second time return if not self.sslHandshake(): return if self.peerValidityChecks() == False: time.sleep(2) self.sendDataThreadQueue.put((0, 'shutdown','no data')) self.checkTimeOffsetNotification() return self.connectionIsOrWasFullyEstablished = True shared.timeOffsetWrongCount = 0 # Command the corresponding sendDataThread to set its own connectionIsOrWasFullyEstablished variable to True also self.sendDataThreadQueue.put((0, 'connectionIsOrWasFullyEstablished', (self.services, self.sslSock))) if not self.initiatedConnection: shared.clientHasReceivedIncomingConnections = True queues.UISignalQueue.put(('setStatusIcon', 'green')) self.sock.settimeout( 600) # We'll send out a ping every 5 minutes to make sure the connection stays alive if there has been no other traffic to send lately. queues.UISignalQueue.put(('updateNetworkStatusTab', 'no data')) logger.debug('Connection fully established with ' + str(self.peer) + "\n" + \ 'The size of the connectedHostsList is now ' + str(len(shared.connectedHostsList)) + "\n" + \ 'The length of sendDataQueues is now: ' + str(len(state.sendDataQueues)) + "\n" + \ 'broadcasting addr from within connectionFullyEstablished function.') if self.initiatedConnection: state.networkProtocolAvailability[protocol.networkType(self.peer.host)] = True # we need to send our own objects to this node PendingUpload().add() # Let all of our peers know about this new node. for stream in self.remoteStreams: dataToSend = (int(time.time()), stream, self.services, self.peer.host, self.remoteNodeIncomingPort) protocol.broadcastToSendDataQueues(( stream, 'advertisepeer', dataToSend)) self.sendaddr() # This is one large addr message to this one peer. if len(shared.connectedHostsList) > \ BMConfigParser().safeGetInt("bitmessagesettings", "maxtotalconnections", 200): logger.info ('We are connected to too many people. Closing connection.') if self.initiatedConnection: self.sendDataThreadQueue.put((0, 'sendRawData', protocol.assembleErrorMessage(fatal=2, errorText="Thank you for providing a listening node."))) else: self.sendDataThreadQueue.put((0, 'sendRawData', protocol.assembleErrorMessage(fatal=2, errorText="Server full, please try again later."))) self.sendDataThreadQueue.put((0, 'shutdown','no data')) return self.sendBigInv()
def connectionFullyEstablished(self): if self.connectionIsOrWasFullyEstablished: # there is no reason to run this function a second time return if ((self.services & protocol.NODE_SSL == protocol.NODE_SSL) and protocol.haveSSL(not self.initiatedConnection)): try: self.sslHandshake() except socket.error as ex: logger.error("SSL socket handshake failed, shutting down connection", exc_info=(type(ex), ex, None)) self.sendDataThreadQueue.put((0, 'shutdown', None)) return self.connectionIsOrWasFullyEstablished = True shared.timeOffsetWrongCount = 0 # Command the corresponding sendDataThread to set its own connectionIsOrWasFullyEstablished variable to True also self.sendDataThreadQueue.put((0, 'connectionIsOrWasFullyEstablished', (self.services, self.sock))) if not self.initiatedConnection: shared.clientHasReceivedIncomingConnections = True queues.UISignalQueue.put(('setStatusIcon', 'green')) self.sock.settimeout( 600) # We'll send out a ping every 5 minutes to make sure the connection stays alive if there has been no other traffic to send lately. queues.UISignalQueue.put(('updateNetworkStatusTab', 'no data')) logger.debug('Connection fully established with ' + str(self.peer) + "\n" + \ 'The size of the connectedHostsList is now ' + str(len(shared.connectedHostsList)) + "\n" + \ 'The length of sendDataQueues is now: ' + str(len(state.sendDataQueues)) + "\n" + \ 'broadcasting addr from within connectionFullyEstablished function.') if self.initiatedConnection: state.networkProtocolAvailability[protocol.networkType(self.peer.host)] = True # we need to send our own objects to this node PendingUpload().add() self.sendaddr() # This is one large addr message to this one peer. if len(shared.connectedHostsList) > \ BMConfigParser().safeGetInt("bitmessagesettings", "maxtotalconnections", 200): logger.info ('We are connected to too many people. Closing connection.') if self.initiatedConnection: self.sendDataThreadQueue.put((0, 'sendRawData', protocol.assembleErrorMessage(fatal=2, errorText="Thank you for providing a listening node."))) else: self.sendDataThreadQueue.put((0, 'sendRawData', protocol.assembleErrorMessage(fatal=2, errorText="Server full, please try again later."))) self.sendDataThreadQueue.put((0, 'shutdown','no data')) return self.sendBigInv()
def peerValidityChecks(self): if self.remoteProtocolVersion < 3: self.append_write_buf(protocol.assembleErrorMessage(fatal=2, errorText="Your is using an old protocol. Closing connection.")) logger.debug ('Closing connection to old protocol version %s, node: %s', str(self.remoteProtocolVersion), str(self.destination)) return False if self.timeOffset > BMProto.maxTimeOffset: self.append_write_buf(protocol.assembleErrorMessage(fatal=2, errorText="Your time is too far in the future compared to mine. Closing connection.")) logger.info("%s's time is too far in the future (%s seconds). Closing connection to it.", self.destination, self.timeOffset) shared.timeOffsetWrongCount += 1 return False elif self.timeOffset < -BMProto.maxTimeOffset: self.append_write_buf(protocol.assembleErrorMessage(fatal=2, errorText="Your time is too far in the past compared to mine. Closing connection.")) logger.info("%s's time is too far in the past (timeOffset %s seconds). Closing connection to it.", self.destination, self.timeOffset) shared.timeOffsetWrongCount += 1 return False else: shared.timeOffsetWrongCount = 0 if not self.streams: self.append_write_buf(protocol.assembleErrorMessage(fatal=2, errorText="We don't have shared stream interests. Closing connection.")) logger.debug ('Closed connection to %s because there is no overlapping interest in streams.', str(self.destination)) return False if self.destination in network.connectionpool.BMConnectionPool().inboundConnections: try: if not protocol.checkSocksIP(self.destination.host): self.append_write_buf(protocol.assembleErrorMessage(fatal=2, errorText="Too many connections from your IP. Closing connection.")) logger.debug ('Closed connection to %s because we are already connected to that IP.', str(self.destination)) return False except: pass if network.connectionpool.BMConnectionPool().isAlreadyConnected(self.nonce): self.append_write_buf(protocol.assembleErrorMessage(fatal=2, errorText="I'm connected to myself. Closing connection.")) logger.debug ("Closed connection to %s because I'm connected to myself.", str(self.destination)) return False return True
def peerValidityChecks(self): if self.remoteProtocolVersion < 3: self.append_write_buf(protocol.assembleErrorMessage(fatal=2, errorText="Your is using an old protocol. Closing connection.")) logger.debug ('Closing connection to old protocol version %s, node: %s', str(self.remoteProtocolVersion), str(self.destination)) return False if self.timeOffset > BMProto.maxTimeOffset: self.append_write_buf(protocol.assembleErrorMessage(fatal=2, errorText="Your time is too far in the future compared to mine. Closing connection.")) logger.info("%s's time is too far in the future (%s seconds). Closing connection to it.", self.destination, self.timeOffset) shared.timeOffsetWrongCount += 1 return False elif self.timeOffset < -BMProto.maxTimeOffset: self.append_write_buf(protocol.assembleErrorMessage(fatal=2, errorText="Your time is too far in the past compared to mine. Closing connection.")) logger.info("%s's time is too far in the past (timeOffset %s seconds). Closing connection to it.", self.destination, self.timeOffset) shared.timeOffsetWrongCount += 1 return False else: shared.timeOffsetWrongCount = 0 if not self.streams: self.append_write_buf(protocol.assembleErrorMessage(fatal=2, errorText="We don't have shared stream interests. Closing connection.")) logger.debug ('Closed connection to %s because there is no overlapping interest in streams.', str(self.destination)) return False if self.destination in network.connectionpool.BMConnectionPool().inboundConnections: try: if not protocol.checkSocksIP(self.destination.host): self.append_write_buf(protocol.assembleErrorMessage(fatal=2, errorText="Too many connections from your IP. Closing connection.")) logger.debug ('Closed connection to %s because we are already connected to that IP.', str(self.destination)) return False except: pass if not self.isOutbound: # incoming from a peer we're connected to as outbound, or server full # report the same error to counter deanonymisation if state.Peer(self.destination.host, self.peerNode.port) in \ network.connectionpool.BMConnectionPool().inboundConnections or \ len(network.connectionpool.BMConnectionPool().inboundConnections) + \ len(network.connectionpool.BMConnectionPool().outboundConnections) > \ BMConfigParser().safeGetInt("bitmessagesettings", "maxtotalconnections") + \ BMConfigParser().safeGetInt("bitmessagesettings", "maxbootstrapconnections"): self.append_write_buf(protocol.assembleErrorMessage(fatal=2, errorText="Server full, please try again later.")) logger.debug ("Closed connection to %s due to server full or duplicate inbound/outbound.", str(self.destination)) return False if network.connectionpool.BMConnectionPool().isAlreadyConnected(self.nonce): self.append_write_buf(protocol.assembleErrorMessage(fatal=2, errorText="I'm connected to myself. Closing connection.")) logger.debug ("Closed connection to %s because I'm connected to myself.", str(self.destination)) return False return True
def connectionFullyEstablished(self): if self.connectionIsOrWasFullyEstablished: # there is no reason to run this function a second time return if not self.sslHandshake(): return if self.peerValidityChecks() == False: time.sleep(2) self.sendDataThreadQueue.put((0, 'shutdown', 'no data')) self.checkTimeOffsetNotification() return self.connectionIsOrWasFullyEstablished = True shared.timeOffsetWrongCount = 0 # Command the corresponding sendDataThread to set its own connectionIsOrWasFullyEstablished variable to True also self.sendDataThreadQueue.put((0, 'connectionIsOrWasFullyEstablished', (self.services, self.sslSock))) if not self.initiatedConnection: shared.clientHasReceivedIncomingConnections = True queues.UISignalQueue.put(('setStatusIcon', 'green')) self.sock.settimeout( 600 ) # We'll send out a ping every 5 minutes to make sure the connection stays alive if there has been no other traffic to send lately. queues.UISignalQueue.put(('updateNetworkStatusTab', 'no data')) logger.debug('Connection fully established with ' + str(self.peer) + "\n" + \ 'The size of the connectedHostsList is now ' + str(len(shared.connectedHostsList)) + "\n" + \ 'The length of sendDataQueues is now: ' + str(len(state.sendDataQueues)) + "\n" + \ 'broadcasting addr from within connectionFullyEstablished function.') if self.initiatedConnection: state.networkProtocolAvailability[protocol.networkType( self.peer.host)] = True # we need to send our own objects to this node PendingUpload().add() # Let all of our peers know about this new node. for stream in self.remoteStreams: dataToSend = (int(time.time()), stream, self.services, self.peer.host, self.remoteNodeIncomingPort) protocol.broadcastToSendDataQueues( (stream, 'advertisepeer', dataToSend)) self.sendaddr() # This is one large addr message to this one peer. if len(shared.connectedHostsList) > \ BMConfigParser().safeGetInt("bitmessagesettings", "maxtotalconnections", 200): logger.info( 'We are connected to too many people. Closing connection.') if self.initiatedConnection: self.sendDataThreadQueue.put( (0, 'sendRawData', protocol.assembleErrorMessage( fatal=2, errorText="Thank you for providing a listening node.") )) else: self.sendDataThreadQueue.put( (0, 'sendRawData', protocol.assembleErrorMessage( fatal=2, errorText="Server full, please try again later."))) self.sendDataThreadQueue.put((0, 'shutdown', 'no data')) return self.sendBigInv()
def peerValidityChecks(self): """Check the validity of the peer""" if self.remoteProtocolVersion < 3: self.append_write_buf( protocol.assembleErrorMessage( errorText= "Your is using an old protocol. Closing connection.", fatal=2)) logger.debug( 'Closing connection to old protocol version %s, node: %s', self.remoteProtocolVersion, self.destination) return False if self.timeOffset > MAX_TIME_OFFSET: self.append_write_buf( protocol.assembleErrorMessage( errorText="Your time is too far in the future" " compared to mine. Closing connection.", fatal=2)) logger.info( "%s's time is too far in the future (%s seconds)." " Closing connection to it.", self.destination, self.timeOffset) BMProto.timeOffsetWrongCount += 1 return False elif self.timeOffset < -MAX_TIME_OFFSET: self.append_write_buf( protocol.assembleErrorMessage( errorText= "Your time is too far in the past compared to mine." " Closing connection.", fatal=2)) logger.info( "%s's time is too far in the past (timeOffset %s seconds)." " Closing connection to it.", self.destination, self.timeOffset) BMProto.timeOffsetWrongCount += 1 return False else: BMProto.timeOffsetWrongCount = 0 if not self.streams: self.append_write_buf( protocol.assembleErrorMessage( errorText="We don't have shared stream interests." " Closing connection.", fatal=2)) logger.debug( 'Closed connection to %s because there is no overlapping' ' interest in streams.', self.destination) return False if self.destination in connectionpool.BMConnectionPool( ).inboundConnections: try: if not protocol.checkSocksIP(self.destination.host): self.append_write_buf( protocol.assembleErrorMessage( errorText="Too many connections from your IP." " Closing connection.", fatal=2)) logger.debug( 'Closed connection to %s because we are already' ' connected to that IP.', self.destination) return False except Exception: pass if not self.isOutbound: # incoming from a peer we're connected to as outbound, # or server full report the same error to counter deanonymisation if (Peer(self.destination.host, self.peerNode.port) in connectionpool.BMConnectionPool().inboundConnections or len(connectionpool.BMConnectionPool()) > BMConfigParser().safeGetInt('bitmessagesettings', 'maxtotalconnections') + BMConfigParser().safeGetInt('bitmessagesettings', 'maxbootstrapconnections')): self.append_write_buf( protocol.assembleErrorMessage( errorText="Server full, please try again later.", fatal=2)) logger.debug( 'Closed connection to %s due to server full' ' or duplicate inbound/outbound.', self.destination) return False if connectionpool.BMConnectionPool().isAlreadyConnected(self.nonce): self.append_write_buf( protocol.assembleErrorMessage( errorText="I'm connected to myself. Closing connection.", fatal=2)) logger.debug( "Closed connection to %s because I'm connected to myself.", self.destination) return False return True