Example #1
0
 def unpack(msg: Buffer):
     obj = {}
     count = VarInt.unpack(msg)
     for _ in range(count):
         key = String.unpack(msg).data
         index = ord(msg.read(1))
         type_ = Variant.allowed_types[index]
         value = type_.unpack(msg)
         obj[key] = value
     return VariantObject(obj)
Example #2
0
 def unpack(msg: Buffer):
     value = 0
     i = 0
     while True:
         byte = ord(msg.read(1))
         value += (byte & 0x7f) << (i * 7)
         if byte & 0x80 != 0x80:
             break
         i += 1
     return value
Example #3
0
 def unpack(msg: Buffer):
     return RIPEMD160(msg.read(20))
Example #4
0
 def unpack(msg: Buffer):
     return SHA256(msg.read(32))
Example #5
0
 def unpack(msg: Buffer):
     return SHA1(msg.read(20))
Example #6
0
 def unpack(msg: Buffer):
     return Signature(msg.read(65))
Example #7
0
 def unpack(msg: Buffer):
     length = VarInt.unpack(msg)
     return Data(msg.read(length))
Example #8
0
 def unpack(msg: Buffer):
     data = unpack("<Q", msg.read(8))[0]
     type_ = (data & (0xff << 48)) >> 48
     id_ = data & 0xffffffffffff
     return FullObjectID.oid_types[type_](id_)
Example #9
0
 def unpack(msg: Buffer):
     data = msg.read(4)
     return IPAddress("%s.%s.%s.%s" % (data[3], data[2], data[1], data[0]))
Example #10
0
 def unpack(msg: Buffer):
     return Int64(unpack("<q", msg.read(8))[0])
Example #11
0
 def unpack(msg: Buffer):
     return Uint64(unpack("<Q", msg.read(8))[0])
Example #12
0
 def unpack(msg: Buffer):
     return Uint32(unpack("<I", msg.read(4))[0])
Example #13
0
 def unpack(msg: Buffer):
     return Uint16(unpack("<H", msg.read(2))[0])
Example #14
0
 def unpack(msg: Buffer):
     return Uint8(ord(msg.read(1)))
Example #15
0
 def unpack(msg: Buffer):
     return Bool(True) if ord(msg.read(1)) == True else Bool(False)
Example #16
0
 def unpack(msg: Buffer):
     data = msg.read(6)
     return IPEndpoint(
         "%s.%s.%s.%s:%s" %
         (data[3], data[2], data[1], data[0], unpack("<H", data[4:6])[0]))
Example #17
0
 def unpack(msg: Buffer):
     value = unpack("<I", msg.read(4))[0]
     type_ = value & 0xff
     instance = (value & 0xffffff00) >> 8
     return VoteID(type_, instance)
Example #18
0
 def unpack(msg: Buffer):
     return FakePublicKey(msg.read(33))
Example #19
0
 def unpack(msg: Buffer):
     length = VarInt.unpack(msg)
     return String((msg.read(length)).decode("utf8"))
Example #20
0
 def unpack(msg: Buffer):
     data = msg.read(33)
     return PublicKey(GraphenePublicKey(data.hex(), prefix="BTS"))
Example #21
0
class Connection:
    def __init__(self, ip, port):
        self.stream = Buffer()
        self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.s.connect((ip, port))
        raw_pk = self.s.recv(33)
        self.pk = GraphenePublicKey(raw_pk.hex())
        sk = PrivateKey()
        point = self.pk.point() * int.from_bytes(bytes(sk), "big")
        x: int = point.x()
        raw_data = x.to_bytes(32, "big")
        self.shared_secret = sha512(raw_data).digest()
        key = sha256(self.shared_secret).digest()
        crc = cityhash.CityHash128(self.shared_secret)
        data = crc.to_bytes(16, "little")
        iv = data[8:16] + data[:8]
        self.s.sendall(bytes.fromhex(repr(sk.pubkey)))
        self.encryptor = AES.new(key, AES.MODE_CBC, iv)
        self.test = AES.new(key, AES.MODE_CBC, iv)
        self.decryptor = AES.new(key, AES.MODE_CBC, iv)
        self.worker_thread = threading.Thread(target=self.worker)
        self.worker_thread.start()
        self.send(
            5006, {
                "user_agent":
                "Haruka Mock Client",
                "core_protocol_version":
                106,
                "inbound_address":
                "0.0.0.0",
                "inbound_port":
                0,
                "outbound_port":
                0,
                "node_public_key":
                sk.pubkey,
                "signed_shared_secret":
                ecdsa.sign_message(self.shared_secret, str(sk)),
                "chain_id":
                bytes.fromhex(
                    "4018d7844c78f6a6c41c6a552b898022310fc5dec06da467ee7905a8dad512c8"
                ),
                "user_data": {
                    "platform": String("unknown")
                }
            })

    def send(self, msg_type, data: dict):
        message_type = message_type_table[msg_type]
        message = message_type(data)
        res = message.pack()
        # for name, type_ in definition.items():
        #     res.extend(pack_field(data.get(name, None), type_))
        length = len(res)
        if length % 16 != 8:
            pad_length = (8 - length % 16)
            if pad_length < 0:
                pad_length += 16
            res += b"\x00" * pad_length
        res = pack("<II", length, msg_type) + res
        data = self.encryptor.encrypt(res)
        logging.debug("SEND >>> %s" % res)
        parse_message(res, None, 1)
        self.s.sendall(data)

    def worker(self):
        data = bytearray()
        while True:
            data.extend(self.s.recv(65535))
            if len(data) % 16 == 0:
                msg = self.decryptor.decrypt(bytes(data))
            else:
                continue
            data = bytearray()
            self.stream.write(msg)
            if len(msg) == 0:
                break
            logging.debug("RECV <<< %s" % msg)
            while self.stream.count():
                size = unpack("<I", self.stream.peek(4))[0]
                expect = size + 8 + (16 - (size + 8) % 16) % 16
                logging.debug("expect %s have %s" %
                              (expect, self.stream.count()))
                if expect <= self.stream.count():
                    parse_message(self.stream.read(expect), self, 2)
                else:
                    break
Example #22
0
 def unpack(self, msg: Buffer):
     null = ord(msg.read(1))
     if null == 0:
         return self(None)
     else:
         return self(self.types[0].unpack(msg))