further refined SOCKS exception messages

This commit is contained in:
Jonathan Warren 2012-12-18 16:36:37 -05:00
parent 7c69c93f82
commit 6586bd2d6d

View File

@ -5,7 +5,7 @@
#Right now, PyBitmessage only support connecting to stream 1. It doesn't yet contain logic to expand into further streams. #Right now, PyBitmessage only support connecting to stream 1. It doesn't yet contain logic to expand into further streams.
softwareVersion = '0.1.2' softwareVersion = '0.1.4'
verbose = 2 verbose = 2
maximumAgeOfAnObjectThatIAmWillingToAccept = 216000 #Equals two days and 12 hours. maximumAgeOfAnObjectThatIAmWillingToAccept = 216000 #Equals two days and 12 hours.
lengthOfTimeToLeaveObjectsInInventory = 237600 #Equals two days and 18 hours. This should be longer than maximumAgeOfAnObjectThatIAmWillingToAccept so that we don't process messages twice. lengthOfTimeToLeaveObjectsInInventory = 237600 #Equals two days and 18 hours. This should be longer than maximumAgeOfAnObjectThatIAmWillingToAccept so that we don't process messages twice.
@ -147,7 +147,8 @@ class outgoingSynSender(QThread):
#self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),"SOCKS4 error: "+str(err)) #self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),"SOCKS4 error: "+str(err))
except socket.error, err: except socket.error, err:
if config.get('bitmessagesettings', 'socksproxytype')[0:5] == 'SOCKS': if config.get('bitmessagesettings', 'socksproxytype')[0:5] == 'SOCKS':
self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),"Problem: Bitmessage can not connect to the SOCKS server. "+str(err)) print 'Bitmessage MIGHT be having trouble connecting to the SOCKS server. '+str(err)
#self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),"Problem: Bitmessage can not connect to the SOCKS server. "+str(err))
else: else:
print 'Could NOT connect to', HOST, 'during outgoing attempt.', err print 'Could NOT connect to', HOST, 'during outgoing attempt.', err
PORT, timeLastSeen = knownNodes[self.streamNumber][HOST] PORT, timeLastSeen = knownNodes[self.streamNumber][HOST]
@ -155,7 +156,7 @@ class outgoingSynSender(QThread):
del knownNodes[self.streamNumber][HOST] del knownNodes[self.streamNumber][HOST]
print 'deleting ', HOST, '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, err: except Exception, err:
print 'An acception has occurred in the outgoingSynSender thread that was not caught by other exception types:', err print 'An exception has occurred in the outgoingSynSender thread that was not caught by other exception types:', err
time.sleep(1) time.sleep(1)
#Only one singleListener thread will ever exist. It creates the receiveDataThread and sendDataThread for each incoming connection. Note that it cannot set the stream number because it is not known yet- the other node will have to tell us its stream number in a version message. If we don't care about their stream, we will close the connection (within the recversion function of the recieveData thread) #Only one singleListener thread will ever exist. It creates the receiveDataThread and sendDataThread for each incoming connection. Note that it cannot set the stream number because it is not known yet- the other node will have to tell us its stream number in a version message. If we don't care about their stream, we will close the connection (within the recversion function of the recieveData thread)
@ -339,10 +340,10 @@ class receiveDataThread(QThread):
random.seed() random.seed()
objectHash, = random.sample(self.objectsThatWeHaveYetToGet, 1) objectHash, = random.sample(self.objectsThatWeHaveYetToGet, 1)
if objectHash in inventory: if objectHash in inventory:
print 'Inventory (in memory) already has object that we received in an inv message.' print 'Inventory (in memory) already has object the hash of which we received in an inv message.'
del self.objectsThatWeHaveYetToGet[objectHash] del self.objectsThatWeHaveYetToGet[objectHash]
elif isInSqlInventory(objectHash): elif isInSqlInventory(objectHash):
print 'Inventory (SQL on disk) already has object that we received in an inv message.' print 'Inventory (SQL on disk) already has object the hash of which we received in an inv message.'
del self.objectsThatWeHaveYetToGet[objectHash] del self.objectsThatWeHaveYetToGet[objectHash]
else: else:
print 'processData function making request for object:', repr(objectHash) print 'processData function making request for object:', repr(objectHash)
@ -457,17 +458,20 @@ class receiveDataThread(QThread):
if self.payloadLength < 66: if self.payloadLength < 66:
print 'The payload length of this broadcast packet is unreasonably low. Someone is probably trying funny business. Ignoring message.' print 'The payload length of this broadcast packet is unreasonably low. Someone is probably trying funny business. Ignoring message.'
return return
inventoryLock.acquire()
inventoryHash = calculateInventoryHash(self.data[24:self.payloadLength+24]) inventoryHash = calculateInventoryHash(self.data[24:self.payloadLength+24])
if inventoryHash in inventory: if inventoryHash in inventory:
print 'We have already received this broadcast object. Ignoring.' print 'We have already received this broadcast object. Ignoring.'
inventoryLock.release()
return return
elif isInSqlInventory(inventoryHash): elif isInSqlInventory(inventoryHash):
print 'We have already received this broadcast object (it is stored on disk in the SQL inventory). Ignoring it.' print 'We have already received this broadcast object (it is stored on disk in the SQL inventory). Ignoring it.'
inventoryLock.release()
return return
#It is valid so far. Let's let our peers know about it. #It is valid so far. Let's let our peers know about it.
objectType = 'broadcast' objectType = 'broadcast'
inventory[inventoryHash] = (objectType, self.streamNumber, self.data[24:self.payloadLength+24], embeddedTime) inventory[inventoryHash] = (objectType, self.streamNumber, self.data[24:self.payloadLength+24], embeddedTime)
inventoryLock.release()
self.broadcastinv(inventoryHash) self.broadcastinv(inventoryHash)
self.emit(SIGNAL("incrementNumberOfBroadcastsProcessed()")) self.emit(SIGNAL("incrementNumberOfBroadcastsProcessed()"))
@ -580,20 +584,25 @@ class receiveDataThread(QThread):
return return
readPosition += 4 readPosition += 4
inventoryHash = calculateInventoryHash(self.data[24:self.payloadLength+24]) inventoryHash = calculateInventoryHash(self.data[24:self.payloadLength+24])
if inventoryHash in inventory:
print 'We have already received this msg message. Ignoring.'
return
elif isInSqlInventory(inventoryHash):
print 'We have already received this msg message (it is stored on disk in the SQL inventory). Ignoring it.'
return
streamNumberAsClaimedByMsg, streamNumberAsClaimedByMsgLength = decodeVarint(self.data[readPosition:readPosition+9]) streamNumberAsClaimedByMsg, streamNumberAsClaimedByMsgLength = decodeVarint(self.data[readPosition:readPosition+9])
if streamNumberAsClaimedByMsg != self.streamNumber: if streamNumberAsClaimedByMsg != self.streamNumber:
print 'The stream number encoded in this msg (' + streamNumberAsClaimedByMsg + ') message does not match the stream number on which it was received. Ignoring it.' print 'The stream number encoded in this msg (' + streamNumberAsClaimedByMsg + ') message does not match the stream number on which it was received. Ignoring it.'
return return
readPosition += streamNumberAsClaimedByMsgLength readPosition += streamNumberAsClaimedByMsgLength
inventoryLock.acquire()
if inventoryHash in inventory:
print 'We have already received this msg message. Ignoring.'
inventoryLock.release()
return
elif isInSqlInventory(inventoryHash):
print 'We have already received this msg message (it is stored on disk in the SQL inventory). Ignoring it.'
inventoryLock.release()
return
#This msg message is valid. Let's let our peers know about it. #This msg message is valid. Let's let our peers know about it.
objectType = 'msg' objectType = 'msg'
inventory[inventoryHash] = (objectType, self.streamNumber, self.data[24:self.payloadLength+24], embeddedTime) inventory[inventoryHash] = (objectType, self.streamNumber, self.data[24:self.payloadLength+24], embeddedTime)
inventoryLock.release()
self.broadcastinv(inventoryHash) self.broadcastinv(inventoryHash)
self.emit(SIGNAL("incrementNumberOfMessagesProcessed()")) self.emit(SIGNAL("incrementNumberOfMessagesProcessed()"))
@ -846,21 +855,25 @@ class receiveDataThread(QThread):
#We have received a pubkey #We have received a pubkey
def recpubkey(self): def recpubkey(self):
inventoryHash = calculateInventoryHash(self.data[24:self.payloadLength+24])
if inventoryHash in inventory:
print 'We have already received this pubkey. Ignoring it.'
return
elif isInSqlInventory(inventoryHash):
print 'We have already received this pubkey (it is stored on disk in the SQL inventory). Ignoring it.'
return
#We must check to make sure the proof of work is sufficient. #We must check to make sure the proof of work is sufficient.
if not self.isProofOfWorkSufficient(): if not self.isProofOfWorkSufficient():
print 'Proof of work in pubkey message insufficient.' print 'Proof of work in pubkey message insufficient.'
return return
inventoryHash = calculateInventoryHash(self.data[24:self.payloadLength+24])
inventoryLock.acquire()
if inventoryHash in inventory:
print 'We have already received this pubkey. Ignoring it.'
inventoryLock.release()
return
elif isInSqlInventory(inventoryHash):
print 'We have already received this pubkey (it is stored on disk in the SQL inventory). Ignoring it.'
inventoryLock.release()
return
objectType = 'pubkey' objectType = 'pubkey'
inventory[inventoryHash] = (objectType, self.streamNumber, self.data[24:self.payloadLength+24], int(time.time())) inventory[inventoryHash] = (objectType, self.streamNumber, self.data[24:self.payloadLength+24], int(time.time()))
inventoryLock.release()
self.broadcastinv(inventoryHash) self.broadcastinv(inventoryHash)
self.emit(SIGNAL("incrementNumberOfPubkeysProcessed()")) self.emit(SIGNAL("incrementNumberOfPubkeysProcessed()"))
@ -906,23 +919,24 @@ class receiveDataThread(QThread):
#We have received a getpubkey message #We have received a getpubkey message
def recgetpubkey(self): def recgetpubkey(self):
inventoryHash = calculateInventoryHash(self.data[24:self.payloadLength+24])
if inventoryHash in inventory:
print 'We have already received this getpubkey request. Ignoring it.'
elif isInSqlInventory(inventoryHash):
print 'We have already received this getpubkey request (it is stored on disk in the SQL inventory). Ignoring it.'
else:
objectType = 'pubkeyrequest'
inventory[inventoryHash] = (objectType, self.streamNumber, self.data[24:self.payloadLength+24], int(time.time()))
#First we must check to make sure the proof of work is sufficient.
#POW, = unpack('>Q',hashlib.sha512(hashlib.sha512(self.data[24:24+self.payloadLength]).digest()).digest()[4:12])
#print 'POW:', POW
#if POW > 2**64 / ((self.payloadLength+payloadLengthExtraBytes) * averageProofOfWorkNonceTrialsPerByte):
# print 'POW value in getpubkey message is insufficient. Ignoring it.'
# return
if not self.isProofOfWorkSufficient(): if not self.isProofOfWorkSufficient():
print 'Proof of work in getpubkey message insufficient.' print 'Proof of work in getpubkey message insufficient.'
return return
inventoryLock.acquire()
inventoryHash = calculateInventoryHash(self.data[24:self.payloadLength+24])
if inventoryHash in inventory:
print 'We have already received this getpubkey request. Ignoring it.'
inventoryLock.release()
return
elif isInSqlInventory(inventoryHash):
print 'We have already received this getpubkey request (it is stored on disk in the SQL inventory). Ignoring it.'
inventoryLock.release()
return
objectType = 'pubkeyrequest'
inventory[inventoryHash] = (objectType, self.streamNumber, self.data[24:self.payloadLength+24], int(time.time()))
inventoryLock.release()
#Now let us make sure that the getpubkey request isn't too old or with a fake (future) time. #Now let us make sure that the getpubkey request isn't too old or with a fake (future) time.
embeddedTime, = unpack('>I',self.data[32:36]) embeddedTime, = unpack('>I',self.data[32:36])
if embeddedTime > int(time.time())+10800: if embeddedTime > int(time.time())+10800:
@ -1249,7 +1263,7 @@ class receiveDataThread(QThread):
addrsInMyStream = {} addrsInMyStream = {}
addrsInChildStreamLeft = {} addrsInChildStreamLeft = {}
addrsInChildStreamRight = {} addrsInChildStreamRight = {}
print 'knownNodes', knownNodes #print 'knownNodes', knownNodes
#We are going to share a maximum number of 1000 addrs with our peer. 500 from this stream, 250 from the left child stream, and 250 from the right child stream. #We are going to share a maximum number of 1000 addrs with our peer. 500 from this stream, 250 from the left child stream, and 250 from the right child stream.
@ -2091,57 +2105,6 @@ class addressGenerator(QThread):
self.emit(SIGNAL("writeNewAddressToTable(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"),self.label,address,str(self.streamNumber)) self.emit(SIGNAL("writeNewAddressToTable(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"),self.label,address,str(self.streamNumber))
'''class streamThread(QThread):
def __init__(self, parent = None):
QThread.__init__(self, parent)
self.mailbox = Queue.Queue()
streamQueues.append(self.mailbox)
self.sendDataQueues = []
def __del__(self):
self.wait()
def broadcastTosendDataQueues(self,data):
for q in self.sendDataQueues:
q.put(data)
def setup(self,streamNumber):
self.streamNumber = streamNumber
def run(self):
#self.listOfSendDataThreads = []
for i in range(1,2):
x = sendDataThread()
#self.listOfSendDataThreads.append(x)
x.setup(i,self.sendDataQueues)
#x.daemon = False
x.start()
#print 'length of listOfSendDataThreads', len(self.listOfSendDataThreads)
while True:
print self.streamNumber
data = self.mailbox.get()
if data == 'shutdown':
self.broadcastTosendDataQueues('shutdown')
print 'Stream thread', self, 'shutting down now'
print 'len of streamQueues', len(streamQueues)
while len(self.sendDataQueues) > 0 :
pass
streamQueues.remove(self.mailbox)
return
print self, 'received a message:', data
returnedthing = str(self)+"dololly"
self.emit(SIGNAL("updatebox(PyQt_PyObject)"),returnedthing)'''
#time.sleep(1)
#while True:
# print 'test'
# time.sleep(1)
class iconGlossaryDialog(QtGui.QDialog): class iconGlossaryDialog(QtGui.QDialog):
def __init__(self,parent): def __init__(self,parent):
QtGui.QWidget.__init__(self, parent) QtGui.QWidget.__init__(self, parent)
@ -2149,6 +2112,7 @@ class iconGlossaryDialog(QtGui.QDialog):
self.ui.setupUi(self) self.ui.setupUi(self)
self.parent = parent self.parent = parent
self.ui.labelPortNumber.setText('You are using TCP port ' + str(config.getint('bitmessagesettings', 'port')) + '. (This can be changed in the settings).') self.ui.labelPortNumber.setText('You are using TCP port ' + str(config.getint('bitmessagesettings', 'port')) + '. (This can be changed in the settings).')
QtGui.QWidget.resize(self,QtGui.QWidget.sizeHint(self))
class helpDialog(QtGui.QDialog): class helpDialog(QtGui.QDialog):
def __init__(self,parent): def __init__(self,parent):
@ -3573,6 +3537,7 @@ broadcastSendersForWhichImWatching = {}
statusIconColor = 'red' statusIconColor = 'red'
connectionsCount = {} #Used for the 'network status' tab. connectionsCount = {} #Used for the 'network status' tab.
connectionsCountLock = threading.Lock() connectionsCountLock = threading.Lock()
inventoryLock = threading.Lock() #Guarantees that two receiveDataThreads don't receive and process the same message concurrently (probably sent by a malicious individual)
eightBytesOfRandomDataUsedToDetectConnectionsToSelf = pack('>Q',random.randrange(1, 18446744073709551615)) eightBytesOfRandomDataUsedToDetectConnectionsToSelf = pack('>Q',random.randrange(1, 18446744073709551615))
connectedHostsList = {} #List of hosts to which we are connected. Used to guarantee that the outgoingSynSender thread won't connect to the same remote node twice. connectedHostsList = {} #List of hosts to which we are connected. Used to guarantee that the outgoingSynSender thread won't connect to the same remote node twice.
neededPubkeys = {} neededPubkeys = {}