2016-06-24 22:45:56 +02:00
|
|
|
#!/usr/bin/python2.7
|
|
|
|
|
2017-05-07 20:15:16 +02:00
|
|
|
try:
|
|
|
|
import msgpack
|
|
|
|
except ImportError:
|
2017-06-12 12:54:44 +02:00
|
|
|
try:
|
|
|
|
import umsgpack as msgpack
|
|
|
|
except ImportError:
|
|
|
|
import fallback.umsgpack.umsgpack as msgpack
|
2016-11-12 17:20:45 +01:00
|
|
|
import string
|
2016-06-24 22:45:56 +02:00
|
|
|
import zlib
|
|
|
|
|
2017-05-15 12:23:16 +02:00
|
|
|
from bmconfigparser import BMConfigParser
|
2016-11-15 17:09:08 +01:00
|
|
|
from debug import logger
|
2016-11-03 22:41:36 +01:00
|
|
|
import messagetypes
|
2016-11-14 20:22:10 +01:00
|
|
|
from tr import _translate
|
2018-03-21 14:56:27 +01:00
|
|
|
import helper_random
|
2016-06-24 22:45:56 +02:00
|
|
|
|
|
|
|
BITMESSAGE_ENCODING_IGNORE = 0
|
|
|
|
BITMESSAGE_ENCODING_TRIVIAL = 1
|
|
|
|
BITMESSAGE_ENCODING_SIMPLE = 2
|
2016-08-31 10:33:25 +02:00
|
|
|
BITMESSAGE_ENCODING_EXTENDED = 3
|
2016-06-24 22:45:56 +02:00
|
|
|
|
2017-06-12 12:54:44 +02:00
|
|
|
|
2018-02-13 13:24:37 +01:00
|
|
|
class MsgEncodeException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class MsgDecodeException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class DecompressionSizeException(MsgDecodeException):
|
2017-05-15 12:23:16 +02:00
|
|
|
def __init__(self, size):
|
|
|
|
self.size = size
|
|
|
|
|
2016-11-15 17:09:08 +01:00
|
|
|
|
2016-06-24 22:45:56 +02:00
|
|
|
class MsgEncode(object):
|
2016-11-03 22:41:36 +01:00
|
|
|
def __init__(self, message, encoding=BITMESSAGE_ENCODING_SIMPLE):
|
2016-06-24 22:45:56 +02:00
|
|
|
self.data = None
|
|
|
|
self.encoding = encoding
|
|
|
|
self.length = 0
|
|
|
|
if self.encoding == BITMESSAGE_ENCODING_EXTENDED:
|
|
|
|
self.encodeExtended(message)
|
|
|
|
elif self.encoding == BITMESSAGE_ENCODING_SIMPLE:
|
|
|
|
self.encodeSimple(message)
|
|
|
|
elif self.encoding == BITMESSAGE_ENCODING_TRIVIAL:
|
|
|
|
self.encodeTrivial(message)
|
2016-11-14 20:22:10 +01:00
|
|
|
else:
|
2018-02-13 13:24:37 +01:00
|
|
|
raise MsgEncodeException("Unknown encoding %i" % (encoding))
|
2016-06-24 22:45:56 +02:00
|
|
|
|
|
|
|
def encodeExtended(self, message):
|
|
|
|
try:
|
2016-11-12 17:20:45 +01:00
|
|
|
msgObj = messagetypes.message.Message()
|
|
|
|
self.data = zlib.compress(msgpack.dumps(msgObj.encode(message)), 9)
|
2016-06-24 22:45:56 +02:00
|
|
|
except zlib.error:
|
2016-11-03 22:41:36 +01:00
|
|
|
logger.error("Error compressing message")
|
2018-02-13 13:24:37 +01:00
|
|
|
raise MsgEncodeException("Error compressing message")
|
2016-06-24 22:45:56 +02:00
|
|
|
except msgpack.exceptions.PackException:
|
2016-11-03 22:41:36 +01:00
|
|
|
logger.error("Error msgpacking message")
|
2018-02-13 13:24:37 +01:00
|
|
|
raise MsgEncodeException("Error msgpacking message")
|
2016-06-24 22:45:56 +02:00
|
|
|
self.length = len(self.data)
|
|
|
|
|
|
|
|
def encodeSimple(self, message):
|
|
|
|
self.data = 'Subject:' + message['subject'] + '\n' + 'Body:' + message['body']
|
|
|
|
self.length = len(self.data)
|
|
|
|
|
|
|
|
def encodeTrivial(self, message):
|
|
|
|
self.data = message['body']
|
|
|
|
self.length = len(self.data)
|
|
|
|
|
2016-11-14 20:22:10 +01:00
|
|
|
|
2016-06-24 22:45:56 +02:00
|
|
|
class MsgDecode(object):
|
|
|
|
def __init__(self, encoding, data):
|
|
|
|
self.encoding = encoding
|
|
|
|
if self.encoding == BITMESSAGE_ENCODING_EXTENDED:
|
|
|
|
self.decodeExtended(data)
|
|
|
|
elif self.encoding in [BITMESSAGE_ENCODING_SIMPLE, BITMESSAGE_ENCODING_TRIVIAL]:
|
|
|
|
self.decodeSimple(data)
|
2016-11-14 20:22:10 +01:00
|
|
|
else:
|
|
|
|
self.body = _translate("MsgDecode", "The message has an unknown encoding.\nPerhaps you should upgrade Bitmessage.")
|
|
|
|
self.subject = _translate("MsgDecode", "Unknown encoding")
|
2016-06-24 22:45:56 +02:00
|
|
|
|
|
|
|
def decodeExtended(self, data):
|
2017-05-15 12:23:16 +02:00
|
|
|
dc = zlib.decompressobj()
|
|
|
|
tmp = ""
|
|
|
|
while len(tmp) <= BMConfigParser().safeGetInt("zlib", "maxsize"):
|
|
|
|
try:
|
|
|
|
got = dc.decompress(data, BMConfigParser().safeGetInt("zlib", "maxsize") + 1 - len(tmp))
|
|
|
|
# EOF
|
|
|
|
if got == "":
|
|
|
|
break
|
|
|
|
tmp += got
|
|
|
|
data = dc.unconsumed_tail
|
|
|
|
except zlib.error:
|
|
|
|
logger.error("Error decompressing message")
|
2018-02-13 13:24:37 +01:00
|
|
|
raise MsgDecodeException("Error decompressing message")
|
2017-05-15 12:23:16 +02:00
|
|
|
else:
|
|
|
|
raise DecompressionSizeException(len(tmp))
|
|
|
|
|
2016-06-24 22:45:56 +02:00
|
|
|
try:
|
2017-05-15 12:23:16 +02:00
|
|
|
tmp = msgpack.loads(tmp)
|
2016-06-24 22:45:56 +02:00
|
|
|
except (msgpack.exceptions.UnpackException,
|
|
|
|
msgpack.exceptions.ExtraData):
|
2016-11-03 22:41:36 +01:00
|
|
|
logger.error("Error msgunpacking message")
|
2018-02-13 13:24:37 +01:00
|
|
|
raise MsgDecodeException("Error msgunpacking message")
|
2016-11-03 22:41:36 +01:00
|
|
|
|
2016-06-24 22:45:56 +02:00
|
|
|
try:
|
2016-11-03 22:41:36 +01:00
|
|
|
msgType = tmp[""]
|
|
|
|
except KeyError:
|
|
|
|
logger.error("Message type missing")
|
2018-02-13 13:24:37 +01:00
|
|
|
raise MsgDecodeException("Message type missing")
|
2016-06-24 22:45:56 +02:00
|
|
|
|
2016-11-12 17:20:45 +01:00
|
|
|
msgObj = messagetypes.constructObject(tmp)
|
2016-11-03 22:41:36 +01:00
|
|
|
if msgObj is None:
|
2018-02-13 13:24:37 +01:00
|
|
|
raise MsgDecodeException("Malformed message")
|
2016-11-03 22:41:36 +01:00
|
|
|
try:
|
|
|
|
msgObj.process()
|
|
|
|
except:
|
2018-02-13 13:24:37 +01:00
|
|
|
raise MsgDecodeException("Malformed message")
|
2016-11-12 17:20:45 +01:00
|
|
|
if msgType == "message":
|
2016-11-03 22:41:36 +01:00
|
|
|
self.subject = msgObj.subject
|
|
|
|
self.body = msgObj.body
|
|
|
|
|
2016-06-24 22:45:56 +02:00
|
|
|
def decodeSimple(self, data):
|
|
|
|
bodyPositionIndex = string.find(data, '\nBody:')
|
|
|
|
if bodyPositionIndex > 1:
|
2016-08-31 10:33:25 +02:00
|
|
|
subject = data[8:bodyPositionIndex]
|
2016-06-24 22:45:56 +02:00
|
|
|
# Only save and show the first 500 characters of the subject.
|
|
|
|
# Any more is probably an attack.
|
|
|
|
subject = subject[:500]
|
2016-08-31 10:33:25 +02:00
|
|
|
body = data[bodyPositionIndex + 6:]
|
2016-06-24 22:45:56 +02:00
|
|
|
else:
|
|
|
|
subject = ''
|
2016-08-31 10:33:25 +02:00
|
|
|
body = data
|
2016-06-24 22:45:56 +02:00
|
|
|
# Throw away any extra lines (headers) after the subject.
|
|
|
|
if subject:
|
|
|
|
subject = subject.splitlines()[0]
|
|
|
|
self.subject = subject
|
2016-11-12 17:20:45 +01:00
|
|
|
self.body = body
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
messageData = {
|
2018-03-21 14:56:27 +01:00
|
|
|
"subject": ''.join(helper_random.randomchoice(string.ascii_lowercase + string.digits) for _ in range(40)),
|
|
|
|
"body": ''.join(helper_random.randomchoice(string.ascii_lowercase + string.digits) for _ in range(10000))
|
2016-11-12 17:20:45 +01:00
|
|
|
}
|
|
|
|
obj1 = MsgEncode(messageData, 1)
|
|
|
|
obj2 = MsgEncode(messageData, 2)
|
|
|
|
obj3 = MsgEncode(messageData, 3)
|
|
|
|
print "1:%i 2:%i 3:%i" %(len(obj1.data), len(obj2.data), len(obj3.data))
|
|
|
|
|
|
|
|
obj1e = MsgDecode(1, obj1.data)
|
|
|
|
# no subject in trivial encoding
|
|
|
|
assert messageData["body"] == obj1e.body
|
|
|
|
obj2e = MsgDecode(2, obj2.data)
|
|
|
|
assert messageData["subject"] == obj2e.subject
|
|
|
|
assert messageData["body"] == obj2e.body
|
|
|
|
obj3e = MsgDecode(3, obj3.data)
|
|
|
|
assert messageData["subject"] == obj3e.subject
|
|
|
|
assert messageData["body"] == obj3e.body
|