2018-05-08 16:01:20 +02:00
|
|
|
"""
|
|
|
|
Message encoding end decoding functions
|
|
|
|
"""
|
|
|
|
|
|
|
|
import string
|
|
|
|
import zlib
|
2016-06-24 22:45:56 +02:00
|
|
|
|
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-06-24 22:45:56 +02:00
|
|
|
|
2018-05-08 16:01:20 +02:00
|
|
|
import messagetypes
|
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-14 20:22:10 +01:00
|
|
|
from tr import _translate
|
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):
|
2019-10-08 09:03:48 +02:00
|
|
|
"""Exception during message encoding"""
|
2018-02-13 13:24:37 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class MsgDecodeException(Exception):
|
2019-10-08 09:03:48 +02:00
|
|
|
"""Exception during message decoding"""
|
2018-02-13 13:24:37 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class DecompressionSizeException(MsgDecodeException):
|
2019-10-08 09:03:48 +02:00
|
|
|
# pylint: disable=super-init-not-called
|
|
|
|
"""Decompression resulted in too much data (attack protection)"""
|
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):
|
2019-10-08 09:03:48 +02:00
|
|
|
"""Message encoder class"""
|
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):
|
2019-10-08 09:03:48 +02:00
|
|
|
"""Handle extended encoding"""
|
2016-06-24 22:45:56 +02:00
|
|
|
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):
|
2019-10-08 09:03:48 +02:00
|
|
|
"""Handle simple encoding"""
|
2018-05-08 16:01:20 +02:00
|
|
|
self.data = 'Subject:%(subject)s\nBody:%(body)s' % message
|
2016-06-24 22:45:56 +02:00
|
|
|
self.length = len(self.data)
|
|
|
|
|
|
|
|
def encodeTrivial(self, message):
|
2019-10-08 09:03:48 +02:00
|
|
|
"""Handle trivial encoding"""
|
2016-06-24 22:45:56 +02:00
|
|
|
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):
|
2019-10-08 09:03:48 +02:00
|
|
|
"""Message decoder class"""
|
2016-06-24 22:45:56 +02:00
|
|
|
def __init__(self, encoding, data):
|
|
|
|
self.encoding = encoding
|
|
|
|
if self.encoding == BITMESSAGE_ENCODING_EXTENDED:
|
|
|
|
self.decodeExtended(data)
|
2018-05-08 16:01:20 +02:00
|
|
|
elif self.encoding in (
|
|
|
|
BITMESSAGE_ENCODING_SIMPLE, BITMESSAGE_ENCODING_TRIVIAL):
|
2016-06-24 22:45:56 +02:00
|
|
|
self.decodeSimple(data)
|
2016-11-14 20:22:10 +01:00
|
|
|
else:
|
2018-05-08 16:01:20 +02:00
|
|
|
self.body = _translate(
|
|
|
|
"MsgDecode",
|
|
|
|
"The message has an unknown encoding.\n"
|
|
|
|
"Perhaps you should upgrade Bitmessage.")
|
2016-11-14 20:22:10 +01:00
|
|
|
self.subject = _translate("MsgDecode", "Unknown encoding")
|
2016-06-24 22:45:56 +02:00
|
|
|
|
|
|
|
def decodeExtended(self, data):
|
2019-10-08 09:03:48 +02:00
|
|
|
"""Handle extended encoding"""
|
2017-05-15 12:23:16 +02:00
|
|
|
dc = zlib.decompressobj()
|
|
|
|
tmp = ""
|
|
|
|
while len(tmp) <= BMConfigParser().safeGetInt("zlib", "maxsize"):
|
|
|
|
try:
|
2018-05-08 16:01:20 +02:00
|
|
|
got = dc.decompress(
|
|
|
|
data, BMConfigParser().safeGetInt("zlib", "maxsize") +
|
|
|
|
1 - len(tmp))
|
2017-05-15 12:23:16 +02:00
|
|
|
# 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):
|
2019-10-08 09:03:48 +02:00
|
|
|
"""Handle simple encoding"""
|
2016-06-24 22:45:56 +02:00
|
|
|
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
|