Exemple #1
0
 def list_sessions(self):
     """
     """
     from transport.proxy import proxy_receiver
     from transport.proxy import proxy_sender
     result = []
     if proxy_receiver.A():
         result.append(proxy_receiver.A())
     if proxy_sender.A():
         result.append(proxy_sender.A())
     return result
Exemple #2
0
 def state_changed(self, oldstate, newstate, event, *args, **kwargs):
     """
     Method to catch the moment when proxy_receiver() state were changed.
     """
     if settings.enablePROXYsending():
         from transport.proxy import proxy_sender
         proxy_sender.A('proxy_receiver.state', newstate)
Exemple #3
0
 def shutdown(self):
     """
     """
     if _Debug:
         lg.out(4, 'proxy_interface.shutdown')
     from transport.proxy import proxy_receiver
     from transport.proxy import proxy_sender
     ret = self.disconnect()
     proxy_receiver.A('shutdown')
     proxy_sender.A('shutdown')
     global _GateProxy
     if _GateProxy:
         _GateProxy = None
     return ret
Exemple #4
0
 def connect(self, options):
     """
     """
     if _Debug:
         lg.out(4, 'proxy_interface.connect %s' % str(options))
     from transport.proxy import proxy_receiver
     from transport.proxy import proxy_sender
     if settings.enablePROXYreceiving():
         proxy_receiver.A('start', options)
     else:
         lg.warn('proxy transport receiving is disabled')
         interface_receiving_failed()
         return False
     if settings.enablePROXYsending():
         proxy_sender.A('start', options)
     return succeed(True)
Exemple #5
0
 def init(self, xml_rpc_url_or_object):
     """
     """
     global _GateProxy
     if _Debug:
         lg.out(4, 'proxy_interface.init')
     from transport.proxy import proxy_receiver
     from transport.proxy import proxy_sender
     if isinstance(xml_rpc_url_or_object, str):
         _GateProxy = xmlrpc.Proxy(xml_rpc_url_or_object, allowNone=True)
     else:
         _GateProxy = xml_rpc_url_or_object
     proxy_receiver.A('init')
     proxy_sender.A('init')
     proxy().callRemote('transport_initialized', 'proxy')
     return True
Exemple #6
0
 def disconnect(self):
     """
     """
     if _Debug:
         lg.out(4, 'proxy_interface.disconnect')
     from transport.proxy import proxy_receiver
     from transport.proxy import proxy_sender
     if not proxy_receiver.A():
         lg.warn('proxy_receiver is not ready')
         interface_disconnected()
     elif proxy_receiver.A().state != 'LISTEN':
         lg.warn('proxy_receiver is not listening now')
         interface_disconnected()
     else:
         proxy_receiver.A('stop')
     proxy_sender.A('stop')
     return succeed(True)
Exemple #7
0
    def _do_process_inbox_packet(self, *args, **kwargs):
        newpacket, info, _, _ = args[0]
        block = encrypted.Unserialize(newpacket.Payload)
        if block is None:
            lg.err('reading data from %s' % newpacket.CreatorID)
            return
        try:
            session_key = key.DecryptLocalPrivateKey(block.EncryptedSessionKey)
            padded_data = key.DecryptWithSessionKey(session_key, block.EncryptedData, session_key_type=block.SessionKeyType)
            inpt = BytesIO(padded_data[:int(block.Length)])
            data = inpt.read()
        except:
            lg.err('reading data from %s' % newpacket.CreatorID)
            lg.exc()
            try:
                inpt.close()
            except:
                pass
            return
        inpt.close()

        if newpacket.Command == commands.RelayAck():
            try:
                ack_info = serialization.BytesToDict(data, keys_to_text=True, values_to_text=True)
            except:
                lg.exc()
                return
            if _Debug:
                lg.out(_DebugLevel, '<<<Relay-ACK %s:%s from %s://%s with %d bytes %s' % (
                    ack_info['command'], ack_info['packet_id'], info.proto, info.host, len(data), ack_info['error'], ))
            if _PacketLogFileEnabled:
                lg.out(0, '                \033[0;49;33mRELAY ACK %s(%s) with %d bytes from %s to %s TID:%s\033[0m' % (
                    ack_info['command'], ack_info['packet_id'], info.bytes_received,
                    global_id.UrlToGlobalID(ack_info['from']), global_id.UrlToGlobalID(ack_info['to']),
                    info.transfer_id), log_name='packet', showtime=True)
            from transport.proxy import proxy_sender
            if proxy_sender.A():
                proxy_sender.A('relay-ack', ack_info, info)
            return True

        if newpacket.Command == commands.RelayFail():
            try:
                fail_info = serialization.BytesToDict(data, keys_to_text=True, values_to_text=True)
            except:
                lg.exc()
                return
            if _Debug:
                lg.out(_DebugLevel, '<<<Relay-FAIL %s:%s from %s://%s with %d bytes %s' % (
                    fail_info['command'], fail_info['packet_id'], info.proto, info.host, len(data), fail_info['error'], ))
            if _PacketLogFileEnabled:
                lg.out(0, '                \033[0;49;33mRELAY FAIL %s(%s) with %d bytes from %s to %s TID:%s\033[0m' % (
                    fail_info['command'], fail_info['packet_id'], info.bytes_received,
                    global_id.UrlToGlobalID(fail_info['from']), global_id.UrlToGlobalID(fail_info['to']),
                    info.transfer_id), log_name='packet', showtime=True)
            from transport.proxy import proxy_sender
            if proxy_sender.A():
                proxy_sender.A('relay-failed', fail_info, info)
            return True

        routed_packet = signed.Unserialize(data)
        if not routed_packet:
            lg.err('unserialize packet failed from %s' % newpacket.CreatorID)
            return

        if _Debug:
            lg.out(_DebugLevel, '<<<Relay-IN %s from %s://%s with %d bytes' % (
                str(routed_packet), info.proto, info.host, len(data)))
        if _PacketLogFileEnabled:
            lg.out(0, '                \033[0;49;33mRELAY IN %s(%s) with %d bytes from %s to %s TID:%s\033[0m' % (
                routed_packet.Command, routed_packet.PacketID, info.bytes_received,
                global_id.UrlToGlobalID(info.sender_idurl), global_id.UrlToGlobalID(routed_packet.RemoteID),
                info.transfer_id), log_name='packet', showtime=True)

        if routed_packet.Command == commands.Identity():
            if _Debug:
                lg.out(_DebugLevel, '    found identity in relay packet %s' % routed_packet)
            newidentity = identity.identity(xmlsrc=routed_packet.Payload)
            idurl = newidentity.getIDURL()
            if not identitycache.HasKey(idurl):
                lg.info('received new identity %s rev %r' % (idurl.original(), newidentity.getRevisionValue(), ))
            if not identitycache.UpdateAfterChecking(idurl, routed_packet.Payload):
                lg.warn("ERROR has non-Valid identity")
                return

        if routed_packet.Command in [commands.Relay(), commands.RelayIn(), ] and routed_packet.PacketID.lower().startswith('identity:'):
            if _Debug:
                lg.out(_DebugLevel, '    found routed identity in relay packet %s' % routed_packet)
            try:
                routed_identity = signed.Unserialize(routed_packet.Payload)
                newidentity = identity.identity(xmlsrc=routed_identity.Payload)
                idurl = newidentity.getIDURL()
                if not identitycache.HasKey(idurl):
                    lg.warn('received new "routed" identity: %s' % idurl)
                if not identitycache.UpdateAfterChecking(idurl, routed_identity.Payload):
                    lg.warn("ERROR has non-Valid identity")
                    return
            except:
                lg.exc()

        if newpacket.Command == commands.RelayIn() and routed_packet.Command == commands.Fail():
            if routed_packet.Payload == b'route not exist' or routed_packet.Payload == b'route already closed':
                for pout in packet_out.search_by_packet_id(routed_packet.PacketID):
                    lg.warn('received %r from %r, outgoing packet is failed: %r' % (routed_packet.Payload, newpacket.CreatorID, pout, ))
                    pout.automat('request-failed')
                return

        self.traffic_in += len(data)
        packet_in.process(routed_packet, info)
        del block
        del data
        del padded_data
        del inpt
        del session_key
        del routed_packet