Пример #1
0
 def connectionMade(self):
     """
     Send the version message and start the handshake
     """
     self.timeouts["verack"] = reactor.callLater(5, self.response_timeout, "verack")
     self.timeouts["version"] = reactor.callLater(5, self.response_timeout, "version")
     self.transport.write(serialize(message(version(user_agent=self.user_agent), self.params)))
Пример #2
0
def main():
    exit_status = 0

    try:
        argv = sys.argv
        progname = os.path.basename(argv[0]) + ":"
        opts, args = getopt.getopt(argv[1:], "hvo:")

        for o, a in opts:
            if o in ("-h"):
                raise UsageError("help")
            elif o in ("-v"):
                raise UsageError("version")
            elif o in ("-o"):
                output_path = a
            else:
                raise UsageError("help")

        if len(opts) == 0:
            output_path = mpcd.read_config(check.path("~/.mplayer/config-ss"))

        if len(args) == 0:
            raise UsageError("help")
        if len(args) > 1:
            raise UsageError("Too many arguments")

        video_path = check.path(args[0])
        output_path = check.path(output_path, write=True, directory=True)
        mpcd.play_video(video_path, output_path)

    except UsageError as error:
        if error.message == "help":
            message.help()
        elif error.message == "version":
            message.version()
        else:
            exit_status = 1
            print(progname, error, file=sys.stderr)

    except (getopt.GetoptError, IOError, SyntaxError) as error:
        exit_status = 1
        print(progname, error, file=sys.stderr)

    finally:
        return exit_status
Пример #3
0
def deserialize(bytes, type="message"):
    try:
        if type == "message":
            obj = {
                "message": {
                    "magic": hex(struct.unpack('<L', bytes[:4])[0])[2:],
                    "command": hexlify(bytes[4:16]).decode("hex").replace("\x00", ""),
                    "length": struct.unpack('<L', bytes[16:20])[0],
                    "checksum": hex(struct.unpack('>L', bytes[20:24])[0])[2:],
                    "payload": bytes[24:24+struct.unpack('<L', bytes[16:20])[0]]
                }
            }
            while len(obj["message"]["checksum"]) < 8:
                obj["message"]["checksum"] = "0" + obj["message"]["checksum"]
            if sha256(sha256(obj["message"]["payload"]).digest()).digest().encode("hex")[:8] != \
                    obj["message"]["checksum"] and obj["message"]["command"] != "verack":
                raise Exception("Invalid Checksum")
            return obj

        elif type == "version":
            version_num = struct.unpack('<L', bytes[:4])[0]
            services = bytes[4:12].encode("hex")
            timestamp = struct.unpack('<Q', bytes[12:20])[0]
            addr_recv = {
                "services": hexlify(bytes[20:28]),
                "port": int(bytes[44:46].encode("hex"), 16)
            }
            if bytes[28:40].encode("hex") == "00000000000000000000ffff":
                addr_recv["ip"] = socket.inet_ntoa(bytes[40:44])
            else:
                addr_recv["ip"] = socket.inet_ntoa(bytes[28:44])
            addr_from = {
                "services": hexlify(bytes[46:54]),
                "port": int(bytes[70:72].encode("hex"), 16)
            }
            if bytes[54:66].encode("hex") == "00000000000000000000ffff":
                addr_from["ip"] = socket.inet_ntoa(bytes[66:70])
            else:
                addr_from["ip"] = socket.inet_ntoa(bytes[54:70])
            nonce = bytes[72:80].encode("hex")
            var_str = var_int(bytes[80:])
            user_agent = bytes[80+var_str[1]:80+var_str[1]+var_str[0]]
            start_height = struct.unpack('<L', bytes[80+var_str[1]+var_str[0]:80+var_str[1]+var_str[0]+4])[0]
            relay = struct.unpack('?', bytes[len(bytes)-1: len(bytes)])[0]
            return version(version_num, services, timestamp, addr_recv, addr_from, nonce, user_agent, start_height, relay)

        elif type == "inv" or type == "getdata":
            def get_type(i):
                i = struct.unpack("<L", i)[0]
                if i == 0:
                    return "ERROR"
                elif i == 1:
                    return "TX"
                elif i == 2:
                    return "BLOCK"
                elif i == 3:
                    return "MERKLEBLOCK"
            count = var_int(bytes)
            bytes = bytes[count[1]:]
            bytelist = []
            for i in range(len(bytes)):
                bytelist.append(bytes[i:i+1])
            inventory = []
            for i in range(count[0]):
                inv_type = ""
                for i in range(4):
                    inv_type += bytelist.pop(0)
                hash = ""
                for i in range(32):
                    hash += bytelist.pop(0)
                inventory.append((get_type(inv_type), ''.join(reversed(hash)).encode("hex")))
            return {type: inventory}

        elif type == "tx":
            return {"tx": bitcoin.deserialize(bytes.encode("hex"))}

    except Exception:
        return {"message": {"command": "error deserializing"}}
Пример #4
0
def deserialize(bytes, type="message"):
    try:
        if type == "message":
            obj = {
                "message": {
                    "magic":
                    hex(struct.unpack('<L', bytes[:4])[0])[2:],
                    "command":
                    hexlify(bytes[4:16]).decode("hex").replace("\x00", ""),
                    "length":
                    struct.unpack('<L', bytes[16:20])[0],
                    "checksum":
                    hex(struct.unpack('>L', bytes[20:24])[0])[2:],
                    "payload":
                    bytes[24:24 + struct.unpack('<L', bytes[16:20])[0]]
                }
            }
            while len(obj["message"]["checksum"]) < 8:
                obj["message"]["checksum"] = "0" + obj["message"]["checksum"]
            if sha256(sha256(obj["message"]["payload"]).digest()).digest().encode("hex")[:8] != \
                    obj["message"]["checksum"] and obj["message"]["command"] != "verack":
                raise Exception("Invalid Checksum")
            return obj

        elif type == "version":
            version_num = struct.unpack('<L', bytes[:4])[0]
            services = bytes[4:12].encode("hex")
            timestamp = struct.unpack('<Q', bytes[12:20])[0]
            addr_recv = {
                "services": hexlify(bytes[20:28]),
                "port": int(bytes[44:46].encode("hex"), 16)
            }
            if bytes[28:40].encode("hex") == "00000000000000000000ffff":
                addr_recv["ip"] = socket.inet_ntoa(bytes[40:44])
            else:
                addr_recv["ip"] = socket.inet_ntoa(bytes[28:44])
            addr_from = {
                "services": hexlify(bytes[46:54]),
                "port": int(bytes[70:72].encode("hex"), 16)
            }
            if bytes[54:66].encode("hex") == "00000000000000000000ffff":
                addr_from["ip"] = socket.inet_ntoa(bytes[66:70])
            else:
                addr_from["ip"] = socket.inet_ntoa(bytes[54:70])
            nonce = bytes[72:80].encode("hex")
            var_str = var_int(bytes[80:])
            user_agent = bytes[80 + var_str[1]:80 + var_str[1] + var_str[0]]
            start_height = struct.unpack(
                '<L', bytes[80 + var_str[1] + var_str[0]:80 + var_str[1] +
                            var_str[0] + 4])[0]
            relay = struct.unpack('?', bytes[len(bytes) - 1:len(bytes)])[0]
            return version(version_num, services, timestamp, addr_recv,
                           addr_from, nonce, user_agent, start_height, relay)

        elif type == "inv" or type == "getdata":

            def get_type(i):
                i = struct.unpack("<L", i)[0]
                if i == 0:
                    return "ERROR"
                elif i == 1:
                    return "TX"
                elif i == 2:
                    return "BLOCK"
                elif i == 3:
                    return "MERKLEBLOCK"

            count = var_int(bytes)
            bytes = bytes[count[1]:]
            bytelist = []
            for i in range(len(bytes)):
                bytelist.append(bytes[i:i + 1])
            inventory = []
            for i in range(count[0]):
                inv_type = ""
                for i in range(4):
                    inv_type += bytelist.pop(0)
                hash = ""
                for i in range(32):
                    hash += bytelist.pop(0)
                inventory.append((get_type(inv_type),
                                  ''.join(reversed(hash)).encode("hex")))
            return {type: inventory}

        elif type == "tx":
            return {"tx": bitcoin.deserialize(bytes.encode("hex"))}

    except Exception:
        return {"message": {"command": "error deserializing"}}