2016-10-05 20:06:47 +02:00
|
|
|
import os
|
2016-01-26 12:04:06 +01:00
|
|
|
import socket
|
2013-06-23 21:52:39 +02:00
|
|
|
import sys
|
2016-03-23 23:26:57 +01:00
|
|
|
from binascii import hexlify, unhexlify
|
2016-10-05 20:06:47 +02:00
|
|
|
from multiprocessing import current_process
|
|
|
|
from threading import current_thread, enumerate
|
2017-05-27 19:02:05 +02:00
|
|
|
import traceback
|
2013-06-21 11:10:13 +02:00
|
|
|
|
2017-02-22 09:34:54 +01:00
|
|
|
from bmconfigparser import BMConfigParser
|
2016-06-30 12:30:05 +02:00
|
|
|
from debug import logger
|
2017-02-08 13:41:56 +01:00
|
|
|
import queues
|
|
|
|
import shutdown
|
2016-01-26 12:04:06 +01:00
|
|
|
|
2016-10-05 20:06:47 +02:00
|
|
|
def powQueueSize():
|
2017-02-08 13:41:56 +01:00
|
|
|
curWorkerQueue = queues.workerQueue.qsize()
|
2016-10-05 20:06:47 +02:00
|
|
|
for thread in enumerate():
|
|
|
|
try:
|
|
|
|
if thread.name == "singleWorker":
|
|
|
|
curWorkerQueue += thread.busy
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
return curWorkerQueue
|
|
|
|
|
2013-06-21 11:10:13 +02:00
|
|
|
def convertIntToString(n):
|
|
|
|
a = __builtins__.hex(n)
|
|
|
|
if a[-1:] == 'L':
|
|
|
|
a = a[:-1]
|
|
|
|
if (len(a) % 2) == 0:
|
2016-03-23 23:26:57 +01:00
|
|
|
return unhexlify(a[2:])
|
2013-06-21 11:10:13 +02:00
|
|
|
else:
|
2016-03-23 23:26:57 +01:00
|
|
|
return unhexlify('0' + a[2:])
|
2013-06-21 11:10:13 +02:00
|
|
|
|
|
|
|
def convertStringToInt(s):
|
2016-03-23 23:26:57 +01:00
|
|
|
return int(hexlify(s), 16)
|
2013-06-21 11:10:13 +02:00
|
|
|
|
2017-05-27 19:02:05 +02:00
|
|
|
def allThreadTraceback(frame):
|
|
|
|
id2name = dict([(th.ident, th.name) for th in enumerate()])
|
|
|
|
code = []
|
|
|
|
for threadId, stack in sys._current_frames().items():
|
|
|
|
code.append("\n# Thread: %s(%d)" % (id2name.get(threadId,""), threadId))
|
|
|
|
for filename, lineno, name, line in traceback.extract_stack(stack):
|
|
|
|
code.append('File: "%s", line %d, in %s' % (filename, lineno, name))
|
|
|
|
if line:
|
|
|
|
code.append(" %s" % (line.strip()))
|
|
|
|
print "\n".join(code)
|
|
|
|
|
2013-06-21 11:10:13 +02:00
|
|
|
def signal_handler(signal, frame):
|
2016-10-05 20:06:47 +02:00
|
|
|
logger.error("Got signal %i in %s/%s", signal, current_process().name, current_thread().name)
|
2016-10-21 15:54:02 +02:00
|
|
|
if current_process().name == "RegExParser":
|
2016-10-22 01:45:32 +02:00
|
|
|
# on Windows this isn't triggered, but it's fine, it has its own process termination thing
|
|
|
|
raise SystemExit
|
2016-10-22 05:00:35 +02:00
|
|
|
if "PoolWorker" in current_process().name:
|
|
|
|
raise SystemExit
|
2016-10-05 20:06:47 +02:00
|
|
|
if current_thread().name != "MainThread":
|
|
|
|
return
|
2016-06-30 12:30:05 +02:00
|
|
|
logger.error("Got signal %i", signal)
|
2017-01-11 14:27:19 +01:00
|
|
|
if BMConfigParser().safeGetBoolean('bitmessagesettings', 'daemon'):
|
2017-02-08 13:41:56 +01:00
|
|
|
shutdown.doCleanShutdown()
|
2013-06-21 11:10:13 +02:00
|
|
|
else:
|
2017-05-27 19:02:05 +02:00
|
|
|
allThreadTraceback(frame)
|
2013-06-21 11:10:13 +02:00
|
|
|
print 'Unfortunately you cannot use Ctrl+C when running the UI because the UI captures the signal.'
|
|
|
|
|
|
|
|
def isHostInPrivateIPRange(host):
|
2016-01-26 12:04:06 +01:00
|
|
|
if ":" in host: #IPv6
|
|
|
|
hostAddr = socket.inet_pton(socket.AF_INET6, host)
|
|
|
|
if hostAddr == ('\x00' * 15) + '\x01':
|
|
|
|
return False
|
|
|
|
if hostAddr[0] == '\xFE' and (ord(hostAddr[1]) & 0xc0) == 0x80:
|
|
|
|
return False
|
|
|
|
if (ord(hostAddr[0]) & 0xfe) == 0xfc:
|
|
|
|
return False
|
|
|
|
pass
|
2017-01-11 14:46:10 +01:00
|
|
|
elif ".onion" not in host:
|
2016-01-26 12:04:06 +01:00
|
|
|
if host[:3] == '10.':
|
|
|
|
return True
|
|
|
|
if host[:4] == '172.':
|
|
|
|
if host[6] == '.':
|
|
|
|
if int(host[4:6]) >= 16 and int(host[4:6]) <= 31:
|
|
|
|
return True
|
|
|
|
if host[:8] == '192.168.':
|
|
|
|
return True
|
2017-01-11 14:46:10 +01:00
|
|
|
# Multicast
|
|
|
|
if host[:3] >= 224 and host[:3] <= 239 and host[4] == '.':
|
|
|
|
return True
|
2013-06-21 11:10:13 +02:00
|
|
|
return False
|
2014-05-02 16:46:36 +02:00
|
|
|
|
2014-05-02 18:47:50 +02:00
|
|
|
def addDataPadding(data, desiredMsgLength = 12, paddingChar = '\x00'):
|
|
|
|
return data + paddingChar * (desiredMsgLength - len(data))
|