Beispiel #1
0
def process(newpacket, info):
    from p2p import p2p_service
    from userid import my_id
    if not driver.is_on('service_p2p_hookups'):
        if _Debug:
            lg.out(
                _DebugLevel,
                'packet_in.process SKIP incoming packet, service_p2p_hookups is not started'
            )
        return None
    if _Debug:
        lg.out(
            _DebugLevel,
            'packet_in.process [%s/%s/%s]:%s(%s) from %s://%s is "%s"' % (
                nameurl.GetName(newpacket.OwnerID),
                nameurl.GetName(newpacket.CreatorID),
                nameurl.GetName(newpacket.RemoteID),
                newpacket.Command,
                newpacket.PacketID,
                info.proto,
                info.host,
                info.status,
            ))
    if info.status != 'finished':
        if _Debug:
            lg.out(_DebugLevel,
                   '    skip, packet status is : [%s]' % info.status)
        return None
    if newpacket.Command == commands.Identity():
        if newpacket.RemoteID != my_id.getLocalIDURL():
            if _Debug:
                lg.out(_DebugLevel,
                       '    incoming Identity is routed to another user')
            if not p2p_service.Identity(newpacket, send_ack=False):
                lg.warn('non-valid identity received')
                return None
            # remote peer sending a valid identity to another peer routed via my machine
            # need to handle that packet - it should be processed by proxy_server
            return handle(newpacket, info)
        # contact sending us current identity we might not have
        # so we handle it before check that packet is valid
        # because we might not have his identity on hands and so can not verify the packet
        # so we check that his Identity is valid and save it into cache
        # than we check the packet to be valid too.
        if not p2p_service.Identity(newpacket):
            lg.warn('non-valid identity received')
            return None
    if not identitycache.HasKey(newpacket.CreatorID):
        if _Debug:
            lg.out(
                _DebugLevel,
                '    will cache remote identity %s before processing incoming packet %s'
                % (newpacket.CreatorID, newpacket))
        d = identitycache.immediatelyCaching(newpacket.CreatorID)
        d.addCallback(lambda _: handle(newpacket, info))
        d.addErrback(lambda err: lg.err('failed caching remote %s identity: %s'
                                        % (newpacket.CreatorID, str(err))))
        return d
    return handle(newpacket, info)
Beispiel #2
0
def process(newpacket, info):
    """
    Main entry point where all incoming signed packets are coming from remote peers.
    The main aspect here is to "authenticate" remote node - need to know it identity.
    """
    from p2p import p2p_service
    from userid import my_id
    if not driver.is_on('service_p2p_hookups'):
        if _Debug:
            lg.out(_DebugLevel, 'packet_in.process SKIP incoming packet, service_p2p_hookups is not started')
        return None
    if _Debug:
        lg.out(_DebugLevel, 'packet_in.process [%s/%s/%s]:%s(%s) from %s://%s is "%s"' % (
            nameurl.GetName(newpacket.OwnerID), nameurl.GetName(newpacket.CreatorID), nameurl.GetName(newpacket.RemoteID),
            newpacket.Command, newpacket.PacketID, info.proto, info.host, info.status, ))
    if info.status != 'finished':
        if _Debug:
            lg.out(_DebugLevel, '    skip, packet status is : [%s]' % info.status)
        return None
#     if _PacketLogFileEnabled:
#         lg.out(0, '        \033[0;49;92mIN %s(%s) with %d bytes from %s to %s TID:%s\033[0m' % (
#             newpacket.Command, newpacket.PacketID, info.bytes_received,
#             global_id.UrlToGlobalID(info.sender_idurl), global_id.UrlToGlobalID(newpacket.RemoteID),
#             info.transfer_id), log_name='packet', showtime=True)
    # we must know recipient identity
    if not id_url.is_cached(newpacket.RemoteID):
        d = identitycache.immediatelyCaching(newpacket.RemoteID)
        d.addCallback(lambda _: process(newpacket, info))
        d.addErrback(lambda err: lg.err('incoming remote ID is unknown, failed caching remote %s identity: %s' % (newpacket.RemoteID, str(err))) and None)
        return d
    if newpacket.Command == commands.Identity():
        if newpacket.RemoteID != my_id.getIDURL():
            if _Debug:
                lg.out(_DebugLevel, '    incoming Identity is routed to another user')
            if not p2p_service.Identity(newpacket, send_ack=False):
                lg.warn('received identity was not processed')
                return None
            # remote peer sending a valid identity to another peer routed via my machine
            # need to handle that packet - it should be processed by proxy_server
            return handle(newpacket, info)
        # contact sending us current identity we might not have
        # so we handle it before check that packet is valid
        # because we might not have his identity on hands and so can not verify the packet
        # so we check that his Identity is valid and save it into cache
        # than we check the packet to be valid too.
        if not p2p_service.Identity(newpacket):
            lg.warn('received identity was not processed')
            return None
    if not identitycache.HasKey(newpacket.CreatorID):
        if _Debug:
            lg.out(_DebugLevel, '    will cache remote identity %s before processing incoming packet %s' % (newpacket.CreatorID, newpacket))
        d = identitycache.immediatelyCaching(newpacket.CreatorID)
        d.addCallback(lambda _: handle(newpacket, info))
        d.addErrback(lambda err: lg.err('failed caching remote %s identity: %s' % (newpacket.CreatorID, str(err))) and None)
        return d
    return handle(newpacket, info)
Beispiel #3
0
def process(newpacket, info):
    if not driver.is_on('service_p2p_hookups'):
        if _Debug:
            lg.out(
                _DebugLevel,
                'packet_in.process SKIP incoming packet, service_p2p_hookups is not started'
            )
        return
    handled = False
    if _Debug:
        lg.out(
            _DebugLevel, 'packet_in.process %s from %s://%s : %s' %
            (str(newpacket), info.proto, info.host, info.status))
    from p2p import commands
    from p2p import p2p_service
    if newpacket.Command == commands.Identity(
    ) and newpacket.RemoteID == my_id.getLocalID():
        # contact sending us current identity we might not have
        # so we handle it before check that packet is valid
        # because we might not have his identity on hands and so can not verify the packet
        # so we check that his Identity is valid and save it into cache
        # than we check the packet to be valid too.
        if not p2p_service.Identity(newpacket):
            return
    # check that signed by a contact of ours
    if not newpacket.Valid():
        lg.warn('new packet from %s://%s is NOT VALID: %r' %
                (info.proto, info.host, newpacket))
        return
    for p in packet_out.search_by_response_packet(newpacket, info.proto,
                                                  info.host):
        p.automat('inbox-packet', (newpacket, info))
        handled = True
    handled = callback.run_inbox_callbacks(newpacket, info, info.status,
                                           info.error_message) or handled
    if not handled and newpacket.Command not in [
            commands.Ack(), commands.Fail()
    ]:
        if _Debug:
            lg.out(
                _DebugLevel - 8, '    incoming %s from [%s://%s]' %
                (newpacket, info.proto, info.host))
            lg.out(_DebugLevel - 8, '        NOT HANDLED !!!')
    if _Debug:
        history().append({
            'time': newpacket.Date,
            'command': newpacket.Command,
            'packet_id': newpacket.PacketID,
            'creator_id': newpacket.CreatorID,
            'owner_id': newpacket.OwnerID,
            'remote_id': newpacket.RemoteID,
            'payload': len(newpacket.Payload),
            'address': '%s://%s' % (info.proto, info.host),
        })
        if len(history()) > 100:
            history().pop(0)