A few more print conversions.

This commit is contained in:
Gregor Robinson 2013-06-24 00:47:39 -06:00
parent 3ec795e2b1
commit d9c55f82a5

View File

@ -1225,17 +1225,22 @@ class receiveDataThread(threading.Thread):
# If this message is bound for one of my version 3 addresses (or # If this message is bound for one of my version 3 addresses (or
# higher), then we must check to make sure it meets our demanded # higher), then we must check to make sure it meets our demanded
# proof of work requirement. # proof of work requirement.
if decodeAddress(toAddress)[1] >= 3: # If the toAddress version number is 3 or higher: if decodeAddress(toAddress)[1] >= 3:
if not shared.isAddressInMyAddressBookSubscriptionsListOrWhitelist(fromAddress): # If I'm not friendly with this person: # If I'm not friendly with this person:
if not shared.isAddressInMyAddressBookSubscriptionsListOrWhitelist(fromAddress):
requiredNonceTrialsPerByte = shared.config.getint( requiredNonceTrialsPerByte = shared.config.getint(
toAddress, 'noncetrialsperbyte') toAddress, 'noncetrialsperbyte')
requiredPayloadLengthExtraBytes = shared.config.getint( requiredPayloadLengthExtraBytes = shared.config.getint(
toAddress, 'payloadlengthextrabytes') toAddress, 'payloadlengthextrabytes')
if not self.isProofOfWorkSufficient(encryptedData, requiredNonceTrialsPerByte, requiredPayloadLengthExtraBytes): if not self.isProofOfWorkSufficient(encryptedData, requiredNonceTrialsPerByte,
print 'Proof of work in msg message insufficient only because it does not meet our higher requirement.' requiredPayloadLengthExtraBytes):
logger.info('Proof of work in msg message insufficient only because it '
'does not meet our higher requirement.'
return return
blockMessage = False # Gets set to True if the user shouldn't see the message according to black or white lists. # Gets set to True if the user shouldn't see the message according to black/white lists.
if shared.config.get('bitmessagesettings', 'blackwhitelist') == 'black': # If we are using a blacklist blockMessage = False
# If we are using a blacklist
if shared.config.get('bitmessagesettings', 'blackwhitelist') == 'black':
t = (fromAddress,) t = (fromAddress,)
shared.sqlLock.acquire() shared.sqlLock.acquire()
shared.sqlSubmitQueue.put( shared.sqlSubmitQueue.put(
@ -1244,9 +1249,7 @@ class receiveDataThread(threading.Thread):
queryreturn = shared.sqlReturnQueue.get() queryreturn = shared.sqlReturnQueue.get()
shared.sqlLock.release() shared.sqlLock.release()
if queryreturn != []: if queryreturn != []:
shared.printLock.acquire() logging.info('Message ignored because address is in blacklist.')
print 'Message ignored because address is in blacklist.'
shared.printLock.release()
blockMessage = True blockMessage = True
else: # We're using a whitelist else: # We're using a whitelist
t = (fromAddress,) t = (fromAddress,)
@ -1257,11 +1260,11 @@ class receiveDataThread(threading.Thread):
queryreturn = shared.sqlReturnQueue.get() queryreturn = shared.sqlReturnQueue.get()
shared.sqlLock.release() shared.sqlLock.release()
if queryreturn == []: if queryreturn == []:
print 'Message ignored because address not in whitelist.' logging.info('Message ignored because address not in whitelist.')
blockMessage = True blockMessage = True
if not blockMessage: if not blockMessage:
print 'fromAddress:', fromAddress logger.debug('fromAddress: %s', fromAddress)
print 'First 150 characters of message:', repr(message[:150]) logger.debug('First 150 characters of message: %s', repr(message[:150]))
toLabel = shared.config.get(toAddress, 'label') toLabel = shared.config.get(toAddress, 'label')
if toLabel == '': if toLabel == '':
@ -1271,8 +1274,10 @@ class receiveDataThread(threading.Thread):
bodyPositionIndex = string.find(message, '\nBody:') bodyPositionIndex = string.find(message, '\nBody:')
if bodyPositionIndex > 1: if bodyPositionIndex > 1:
subject = message[8:bodyPositionIndex] subject = message[8:bodyPositionIndex]
# Only save and show the first 500 characters of the subject. Any more is
# probably an attack.
subject = subject[ subject = subject[
:500] # Only save and show the first 500 characters of the subject. Any more is probably an attak. :500]
body = message[bodyPositionIndex + 6:] body = message[bodyPositionIndex + 6:]
else: else:
subject = '' subject = ''
@ -1281,7 +1286,9 @@ class receiveDataThread(threading.Thread):
body = message body = message
subject = '' subject = ''
elif messageEncodingType == 0: elif messageEncodingType == 0:
print 'messageEncodingType == 0. Doing nothing with the message. They probably just sent it so that we would store their public key or send their ack data for them.' logging.info('messageEncodingType == 0. Doing nothing with the message. '
'They probably just sent it so that we would store their '
'public key or send their ack data for them.')
else: else:
body = 'Unknown encoding type.\n\n' + repr(message) body = 'Unknown encoding type.\n\n' + repr(message)
subject = '' subject = ''
@ -1334,7 +1341,7 @@ class receiveDataThread(threading.Thread):
shared.workerQueue.put(('sendbroadcast', '')) shared.workerQueue.put(('sendbroadcast', ''))
if self.isAckDataValid(ackData): if self.isAckDataValid(ackData):
print 'ackData is valid. Will process it.' logger.info('ackData is valid. Will process it.')
self.ackDataThatWeHaveYetToSend.append( self.ackDataThatWeHaveYetToSend.append(
ackData) # When we have processed all data, the processData function will pop the ackData out and process it as if it is a message received from our peer. ackData) # When we have processed all data, the processData function will pop the ackData out and process it as if it is a message received from our peer.
# Display timing data # Display timing data
@ -1345,21 +1352,22 @@ class receiveDataThread(threading.Thread):
sum = 0 sum = 0
for item in successfullyDecryptMessageTimings: for item in successfullyDecryptMessageTimings:
sum += item sum += item
shared.printLock.acquire() logger.info('Time to decrypt this message successfully: %s',
print 'Time to decrypt this message successfully:', timeRequiredToAttemptToDecryptMessage timeRequiredToAttemptToDecryptMessage)
print 'Average time for all message decryption successes since startup:', sum / len(successfullyDecryptMessageTimings) logger.info('Average time for all message decryption successes since startup: %s',
shared.printLock.release() sum / len(successfullyDecryptMessageTimings))
def isAckDataValid(self, ackData): def isAckDataValid(self, ackData):
if len(ackData) < 24: if len(ackData) < 24:
print 'The length of ackData is unreasonably short. Not sending ackData.' logger.info('The length of ackData is unreasonably short. Not sending ackData.')
return False return False
if ackData[0:4] != '\xe9\xbe\xb4\xd9': if ackData[0:4] != '\xe9\xbe\xb4\xd9':
print 'Ackdata magic bytes were wrong. Not sending ackData.' logger.info('Ackdata magic bytes were wrong. Not sending ackData.')
return False return False
ackDataPayloadLength, = unpack('>L', ackData[16:20]) ackDataPayloadLength, = unpack('>L', ackData[16:20])
if len(ackData) - 24 != ackDataPayloadLength: if len(ackData) - 24 != ackDataPayloadLength:
print 'ackData payload length doesn\'t match the payload length specified in the header. Not sending ackdata.' logger.info('ackData payload length doesn\'t match the payload length specified in the '
'header. Not sending ackdata.')
return False return False
if ackData[4:16] != 'getpubkey\x00\x00\x00' and ackData[4:16] != 'pubkey\x00\x00\x00\x00\x00\x00' and ackData[4:16] != 'msg\x00\x00\x00\x00\x00\x00\x00\x00\x00' and ackData[4:16] != 'broadcast\x00\x00\x00': if ackData[4:16] != 'getpubkey\x00\x00\x00' and ackData[4:16] != 'pubkey\x00\x00\x00\x00\x00\x00' and ackData[4:16] != 'msg\x00\x00\x00\x00\x00\x00\x00\x00\x00' and ackData[4:16] != 'broadcast\x00\x00\x00':
return False return False
@ -1376,7 +1384,7 @@ class receiveDataThread(threading.Thread):
def possibleNewPubkey(self, toRipe): def possibleNewPubkey(self, toRipe):
if toRipe in neededPubkeys: if toRipe in neededPubkeys:
print 'We have been awaiting the arrival of this pubkey.' logger.info('We have been awaiting the arrival of this pubkey.')
del neededPubkeys[toRipe] del neededPubkeys[toRipe]
t = (toRipe,) t = (toRipe,)
shared.sqlLock.acquire() shared.sqlLock.acquire()
@ -1388,9 +1396,8 @@ class receiveDataThread(threading.Thread):
shared.sqlLock.release() shared.sqlLock.release()
shared.workerQueue.put(('sendmessage', '')) shared.workerQueue.put(('sendmessage', ''))
else: else:
shared.printLock.acquire() logger.info('We don\'t need this pub key. We didn\'t ask for it. Pubkey hash:',
print 'We don\'t need this pub key. We didn\'t ask for it. Pubkey hash:', toRipe.encode('hex') toRipe.encode('hex'))
shared.printLock.release()
# We have received a pubkey # We have received a pubkey
def recpubkey(self, data): def recpubkey(self, data):
@ -1399,7 +1406,7 @@ class receiveDataThread(threading.Thread):
return 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(data): if not self.isProofOfWorkSufficient(data):
print 'Proof of work in pubkey message insufficient.' logger.info('Proof of work in pubkey message insufficient.')
return return
readPosition = 8 # for the nonce readPosition = 8 # for the nonce
@ -1414,14 +1421,12 @@ class receiveDataThread(threading.Thread):
readPosition += 4 readPosition += 4
if embeddedTime < int(time.time()) - lengthOfTimeToHoldOnToAllPubkeys: if embeddedTime < int(time.time()) - lengthOfTimeToHoldOnToAllPubkeys:
shared.printLock.acquire() logger.info('The embedded time in this pubkey message is too old. Ignoring. '
print 'The embedded time in this pubkey message is too old. Ignoring. Embedded time is:', embeddedTime 'Embedded time is: %s', embeddedTime
shared.printLock.release()
return return
if embeddedTime > int(time.time()) + 10800: if embeddedTime > int(time.time()) + 10800:
shared.printLock.acquire() logger.info('The embedded time in this pubkey message more than several hours in the '
print 'The embedded time in this pubkey message more than several hours in the future. This is irrational. Ignoring message.' 'future. Ignoring message.')
shared.printLock.release()
return return
addressVersion, varintLength = decodeVarint( addressVersion, varintLength = decodeVarint(
data[readPosition:readPosition + 10]) data[readPosition:readPosition + 10])
@ -1430,17 +1435,20 @@ class receiveDataThread(threading.Thread):
data[readPosition:readPosition + 10]) data[readPosition:readPosition + 10])
readPosition += varintLength readPosition += varintLength
if self.streamNumber != streamNumber: if self.streamNumber != streamNumber:
print 'stream number embedded in this pubkey doesn\'t match our stream number. Ignoring.' logger.info('Stream number (%s) embedded in this pubkey doesn\'t match our stream '
'number. Ignoring.',
streamNumber)
return return
inventoryHash = calculateInventoryHash(data) inventoryHash = calculateInventoryHash(data)
shared.inventoryLock.acquire() shared.inventoryLock.acquire()
if inventoryHash in shared.inventory: if inventoryHash in shared.inventory:
print 'We have already received this pubkey. Ignoring it.' logger.info('We have already received this pubkey. Ignoring it.')
shared.inventoryLock.release() shared.inventoryLock.release()
return return
elif isInSqlInventory(inventoryHash): elif isInSqlInventory(inventoryHash):
print 'We have already received this pubkey (it is stored on disk in the SQL inventory). Ignoring it.' logger.info('We have already received this pubkey (it is stored on disk in the SQL '
'inventory). Ignoring it.')
shared.inventoryLock.release() shared.inventoryLock.release()
return return
objectType = 'pubkey' objectType = 'pubkey'
@ -1457,13 +1465,10 @@ class receiveDataThread(threading.Thread):
sleepTime = lengthOfTimeWeShouldUseToProcessThisMessage - \ sleepTime = lengthOfTimeWeShouldUseToProcessThisMessage - \
(time.time() - self.pubkeyProcessingStartTime) (time.time() - self.pubkeyProcessingStartTime)
if sleepTime > 0: if sleepTime > 0:
shared.printLock.acquire() logger.debug('Timing attack mitigation: Sleeping for %s seconds.', sleepTime)
print 'Timing attack mitigation: Sleeping for', sleepTime, 'seconds.'
shared.printLock.release()
time.sleep(sleepTime) time.sleep(sleepTime)
shared.printLock.acquire() logger.debug('Total pubkey processing time: %s seconds.',
print 'Total pubkey processing time:', time.time() - self.pubkeyProcessingStartTime, 'seconds.' time.time() - self.pubkeyProcessingStartTime)
shared.printLock.release()
def processpubkey(self, data): def processpubkey(self, data):
readPosition = 8 # for the nonce readPosition = 8 # for the nonce
@ -1484,16 +1489,16 @@ class receiveDataThread(threading.Thread):
data[readPosition:readPosition + 10]) data[readPosition:readPosition + 10])
readPosition += varintLength readPosition += varintLength
if addressVersion == 0: if addressVersion == 0:
print '(Within processpubkey) addressVersion of 0 doesn\'t make sense.' logger.info('addressVersion of 0 doesn\'t make sense.')
return return
if addressVersion >= 4 or addressVersion == 1: if addressVersion >= 4 or addressVersion == 1:
shared.printLock.acquire() logger.info('This version of Bitmessage cannot handle version %s addresses.',
print 'This version of Bitmessage cannot handle version', addressVersion, 'addresses.' addressVersion)
shared.printLock.release()
return return
if addressVersion == 2: if addressVersion == 2:
if len(data) < 146: # sanity check. This is the minimum possible length. if len(data) < 146: # sanity check. This is the minimum possible length.
print '(within processpubkey) payloadLength less than 146. Sanity check failed.' logger.info('(within processpubkey) payloadLength less than 146. '
'Sanity check failed.'
return return
bitfieldBehaviors = data[readPosition:readPosition + 4] bitfieldBehaviors = data[readPosition:readPosition + 4]
readPosition += 4 readPosition += 4
@ -1504,7 +1509,7 @@ class receiveDataThread(threading.Thread):
readPosition += 64 readPosition += 64
publicEncryptionKey = data[readPosition:readPosition + 64] publicEncryptionKey = data[readPosition:readPosition + 64]
if len(publicEncryptionKey) < 64: if len(publicEncryptionKey) < 64:
print 'publicEncryptionKey length less than 64. Sanity check failed.' logger.info('publicEncryptionKey length less than 64. Sanity check failed.')
return return
sha = hashlib.new('sha512') sha = hashlib.new('sha512')
sha.update( sha.update(