Esempio n. 1
0
    def stringReceived(self, string):
        sign_message = SignMessage()
        sign_message.ParseFromString(string)

        valid = sign_message_verify(sign_message)

        if not valid:
            error = 'wrong signature'
            return self.proxy_reply_error(error)

        message = Message()
        message.ParseFromString(sign_message.data)
        valid = message_sanity_check(message)
        if not valid or message.type == Message.PROXY_REPLY:
            error = "wrong client request"
            return self.proxy_reply_error(error)

        public_key = sign_message.public_key

        if message.type == Message.SELLER_DATA:
            public_addr = message.seller_data.seller_addr
        elif message.type == Message.BUYER_DATA:
            public_addr = message.buyer_data.buyer_addr

        if not is_address_from_key(public_addr, public_key):
            error = "public key does not match with address"
            return self.proxy_reply_error(error)

        self.handle_message(message)
Esempio n. 2
0
    def stringReceived(self, string):
        message = Message()
        message.ParseFromString(string)
        valid = message_sanity_check(message)

        if not valid or message.type != Message.PROXY_REPLY:
            # should never happen
            proxy_reply = proxy_reply_error("wrong server response")
        else:
            proxy_reply = message.proxy_reply

        self.proxy_reply = proxy_reply

        self.transport.loseConnection()
Esempio n. 3
0
    def check_response(self, reply_error):
        string = self.transport.value()
        _, string = string.split(b':', 1)

        message = Message()
        message.ParseFromString(string)

        assert message.type == Message.PROXY_REPLY

        proxy_reply = message.proxy_reply

        if not proxy_reply.error:
            self.assertEqual(proxy_reply.AES_key, b'AES_key')
        else:
            self.assertEqual(reply_error, proxy_reply.error)
Esempio n. 4
0
    def run(self, sign_message):

        message = Message()
        message.ParseFromString(sign_message.data)
        valid = message_sanity_check(message)
        if not valid:
            logger.error("wrong message format")
            return

        self.factory.sign_message = sign_message

        self.trans = reactor.connectSSL(self.host, self.port, self.factory,
                                        ssl.ClientContextFactory())

        return self.factory.d
Esempio n. 5
0
    def stringReceived(self, string):
        message = Message()
        message.ParseFromString(string)
        valid = message_sanity_check(message)
        if valid and message.type == Message.PROXY_REPLY:
            proxy_reply = message.proxy_reply

            if not proxy_reply.error and self.factory.need_download_file:
                d = download_file(proxy_reply.file_uuid)
                self.reply_message = message
                d.addBoth(self.download_finish)
            else:
                self.factory.d.callback(message)

        else:
            # should never happen
            message = proxy_reply_error("wrong server response")
            self.factory.d.callback(message)

        self.transport.loseConnection()
Esempio n. 6
0
def start_client(sign_message, addr=None):

    if addr:
        host = str(addr[0])
        port = int(addr[1])
    else:
        host = config.proxy.server_host
        port = config.proxy.server_ctrl_port

    message = Message()
    message.ParseFromString(sign_message.data)
    valid = message_sanity_check(message)
    if not valid:
        logger.error("wrong message format")
        return

    factory = SSLClientFactory(sign_message)

    reactor.connectSSL(host, port, factory,
                       ssl.ClientContextFactory())

    buyer_request = message.type == Message.BUYER_DATA

    d = defer.Deferred()

    def handle_proxy_response(proxy_reply):

        if not proxy_reply.error:
            logger.debug('file_uri: %s' % proxy_reply.file_uri)
            logger.debug('AES_key: %s' % proxy_reply.AES_key)
            if buyer_request:
                return download_file(proxy_reply.file_uri).addCallback(
                    lambda _: d.callback(proxy_reply))
        else:
            logger.debug(proxy_reply.error)

        d.callback(proxy_reply)

    factory.d.addCallback(handle_proxy_response)

    return d
Esempio n. 7
0
def start_client(sign_message):

    log.startLogging(sys.stdout)

    host = config.proxy.server_host
    ctrl_port = config.proxy.server_ctrl_port

    message = Message()
    message.ParseFromString(sign_message.data)
    valid = message_sanity_check(message)
    if not valid:
        print("wrong message format")
        return

    d = defer.Deferred()
    factory = SSLClientFactory(sign_message)
    factory.d = d

    if message.type == Message.BUYER_DATA:
        factory.need_download_file = True

    reactor.connectSSL(host, ctrl_port, factory, ssl.ClientContextFactory())

    return d
Esempio n. 8
0
    def stringReceived(self, string):
        sign_message = SignMessage()
        sign_message.ParseFromString(string)

        valid = sign_message_verify(sign_message)

        if not valid:
            error = 'wrong signature'
            self.proxy_reply_error(error)
            return

        message = Message()
        message.ParseFromString(sign_message.data)
        valid = message_sanity_check(message)
        if not valid or message.type == Message.PROXY_REPLY:
            error = "wrong client request"
            self.proxy_reply_error(error)
            return

        public_key = sign_message.public_key

        proxy_db = self.proxy_db
        self.trade = Trade()
        trade = self.trade
        error = None

        if message.type == Message.SELLER_DATA:
            data = message.seller_data
            trade.order_id = data.order_id
            trade.seller_addr = data.seller_addr
            trade.buyer_addr = data.buyer_addr
            trade.market_hash = data.market_hash
            trade.AES_key = data.AES_key

            if pub_key_der_to_addr(public_key) != data.seller_addr:
                print("pubkey seller addr")
                print(pub_key_der_to_addr(public_key))
                print(data.seller_addr)
                error = "not seller's signature"
                self.proxy_reply_error(error)
                return

            storage = data.storage
            if storage.type == Message.Storage.IPFS:
                ipfs = storage.ipfs
                trade.file_hash = ipfs.file_hash
                if (proxy_db.count(trade)):
                    error = "trade record already in database"
                    self.proxy_reply_error(error)
                    return
                else:
                    file_path = os.path.join(server_root,
                                             trade.file_hash.decode())

                    # seller sold the same file to another buyer
                    if os.path.isfile(file_path):
                        mtime = time.time()
                        os.utime(file_path, (mtime, mtime))
                        proxy_db.insert(trade)
                        self.proxy_reply_success()

                        self.proxy_claim_relay()

                        return
                    else:
                        d = threads.deferToThread(self.get_ipfs_file,
                                                  ipfs.gateway,
                                                  trade.file_hash)

                        d.addBoth(self.ipfs_callback)

            elif storage.type == Message.Storage.S3:
                error = "not support S3 storage yet"
                self.proxy_reply_error(error)
                return

        elif message.type == Message.BUYER_DATA:
            data = message.buyer_data
            trade.order_id = data.order_id
            trade.seller_addr = data.seller_addr
            trade.buyer_addr = data.buyer_addr
            trade.market_hash = data.market_hash

            if pub_key_der_to_addr(public_key) != data.buyer_addr:
                error = "not buyer's signature"
                self.proxy_reply_error(error)
                return

            if proxy_db.count(trade):
                self.trade = proxy_db.query(trade)
                self.proxy_reply_success()
                return
            else:
                error = "trade record not found in database"
                self.proxy_reply_error(error)
                return