Example #1
0
def search_by_response_packet(newpacket, proto=None, host=None):
    #     if _Debug:
    #         lg.out(_DebugLevel, 'packet_out.search_by_response_packet [%s/%s/%s]:%s %s' % (
    #             nameurl.GetName(newpacket.OwnerID), nameurl.GetName(newpacket.CreatorID),
    #             nameurl.GetName(newpacket.RemoteID), newpacket.PacketID, newpacket.Command))
    result = []
    target_idurl = newpacket.CreatorID
    if newpacket.OwnerID == my_id.getLocalID():
        target_idurl = newpacket.RemoteID
    elif newpacket.OwnerID != newpacket.CreatorID and newpacket.RemoteID == my_id.getLocalID():
        target_idurl = newpacket.RemoteID
    for p in queue():
        if p.outpacket.PacketID != newpacket.PacketID:
            continue
        if p.outpacket.RemoteID != p.remote_idurl:
            if target_idurl != p.remote_idurl:
                # ????
                pass
        if target_idurl != p.outpacket.RemoteID:
            continue
        result.append(p)
        if _Debug:
            lg.out(_DebugLevel, 'packet_out.search_by_response_packet [%s/%s/%s]:%s(%s) from [%s://%s] cb:%s' % (
                nameurl.GetName(p.outpacket.OwnerID), nameurl.GetName(p.outpacket.CreatorID),
                nameurl.GetName(p.outpacket.RemoteID), p.outpacket.Command, p.outpacket.PacketID,
                proto, host,
                p.callbacks.keys()))
    if len(result) == 0:
        if _Debug:
            lg.out(_DebugLevel, 'packet_out.search_by_response_packet NOT FOUND pending packets in outbox queue for')
            lg.out(_DebugLevel, '        [%s/%s/%s]:%s:%s from [%s://%s]' % (
                nameurl.GetName(newpacket.OwnerID), nameurl.GetName(newpacket.CreatorID),
                nameurl.GetName(newpacket.RemoteID), newpacket.PacketID, newpacket.Command,
                proto, host))
    return result
Example #2
0
def SendMessage(remote_idurl, messagebody, packet_id=None):
    """
    Send command.Message() packet to remote peer.
    """
    global _OutgoingMessageCallback
    if not packet_id:
        packet_id = packetid.UniqueID()
    remote_identity = identitycache.FromCache(remote_idurl)
    if remote_identity is None:
        d = identitycache.immediatelyCaching(remote_idurl, 20)
        d.addCallback(lambda src: SendMessage(
            remote_idurl, messagebody, packet_id))
        d.addErrback(lambda err: lg.warn('failed to retrieve ' + remote_idurl))
        return d
    Amessage = MessageClass(remote_identity, messagebody)
    Payload = misc.ObjectToString(Amessage)
    lg.out(6, "message.SendMessage to %s with %d bytes" % (remote_idurl, len(Payload)))
    outpacket = signed.Packet(
        commands.Message(),
        my_id.getLocalID(),
        my_id.getLocalID(),
        packet_id,
        Payload,
        remote_idurl)
    result = gateway.outbox(outpacket, wide=True)
    if _OutgoingMessageCallback:
        _OutgoingMessageCallback(result, messagebody, remote_identity, packet_id)
    return result
Example #3
0
 def doSendRequestService(self, arg):
     """
     Action method.
     """
     if len(self.request_service_packet_id) >= 3:
         if _Debug:
             lg.warn('too many service requests to %s' % self.router_idurl)
         self.automat('service-refused', arg)
         return
     service_info = 'service_proxy_server \n'
     orig_identity = config.conf().getData('services/proxy-transport/my-original-identity').strip()
     if not orig_identity:
         orig_identity = my_id.getLocalIdentity().serialize()
     service_info += orig_identity
     # for t in gateway.transports().values():
     #     service_info += '%s://%s' % (t.proto, t.host)
     # service_info += ' '
     newpacket = signed.Packet(
         commands.RequestService(),
         my_id.getLocalID(),
         my_id.getLocalID(),
         packetid.UniqueID(),
         service_info,
         self.router_idurl,)
     packet_out.create(newpacket, wide=False, callbacks={
         commands.Ack(): self._on_request_service_ack,
         commands.Fail(): self._on_request_service_fail},)
     self.request_service_packet_id.append(newpacket.PacketID)
Example #4
0
def cmd_key(opts, args, overDict):
    if len(args) == 2:
        if args[1] == 'copy':
            from crypt import key 
            TextToSave = my_id.getLocalID() + "\n" + key.MyPrivateKey()
            misc.setClipboardText(TextToSave)
            print 'now you can "paste" with Ctr+V your private key where you want.'
            del TextToSave
            return 0
        elif args[1] == 'print':
            from crypt import key 
            TextToSave = my_id.getLocalID() + "\n" + key.MyPrivateKey()
            print 
            print TextToSave
            return 0
    elif len(args) == 3:
        if args[1] == 'copy':
            filenameto = args[2]
            from crypt import key 
            TextToSave = my_id.getLocalID() + "\n" + key.MyPrivateKey()
            if not bpio.AtomicWriteFile(filenameto, TextToSave):
                print 'error writing to', filenameto
                return 1
            print 'your private key were copied to file %s' % filenameto
            del TextToSave
            return 0
    return 2
Example #5
0
def _test():
    from crypt import signed
    from lib import packetid

    lg.set_debug_level(24)
    A("init")
    A().offline_mode = True
    A("start")
    coins = []
    for _ in xrange(5):
        coin = coins_io.storage_contract_open("http://some.id-host.org/alice_customer.xml", 3600, 4096)
        if coins_io.verify_coin_signature(coin):
            coins.append(coin)
        else:
            print coin
            sys.exit()
    outpacket = signed.Packet(
        commands.Coin(),
        my_id.getLocalID(),
        my_id.getLocalID(),
        packetid.UniqueID(),
        json.dumps(coins),
        "http://server.com/id.xml",
    )
    reactor.callLater(0.1, callback.run_inbox_callbacks, outpacket, None, "finished", "")
    # reactor.callLater(5.1, A, 'shutdown')
    reactor.run()
 def doStart(self, arg):
     """
     Action method.
     """
     options = {'idurl': my_id.getLocalID(), }
     id_contact = ''
     default_host = ''
     ident = my_id.getLocalIdentity()
     if ident:
         id_contact = ident.getContactsByProto().get(self.proto, '')
     if id_contact:
         assert id_contact.startswith(self.proto + '://')
         id_contact = id_contact.strip(self.proto + '://')
     if self.proto == 'tcp':
         if not id_contact:
             default_host = misc.readExternalIP() + ':' + str(settings.getTCPPort())
         options['host'] = id_contact or default_host
         options['tcp_port'] = settings.getTCPPort()
     elif self.proto == 'udp':
         if not id_contact:
             default_host = nameurl.GetName(my_id.getLocalID()) + '@' + platform.node()
         options['host'] = id_contact or default_host
         options['dht_port'] = settings.getDHTPort()
         options['udp_port'] = settings.getUDPPort()
     elif self.proto == 'proxy':
         pass
     if _Debug:
         lg.out(8, 'network_transport.doStart connecting %s transport : %s' % (self.proto.upper(), options))
     self.interface.connect(options)
Example #7
0
def contacts_remote():
    """
    Return ID's list of all known peers.
    """
    allcontactslist = id_url.to_bin_list(contacts_full())
    if my_id.getLocalID().to_bin() in allcontactslist:
        allcontactslist.remove(my_id.getLocalID().to_bin())
    return id_url.fields_list(allcontactslist)
 def _on_my_identity_url_changed(self, evt):
     from services import driver
     if driver.is_on('service_entangled_dht'):
         from dht import dht_service
         from userid import my_id
         if my_id.getLocalID():
             dht_service.set_node_data('idurl',
                                       my_id.getLocalID().to_text())
Example #9
0
def contacts_remote():
    """
    Return ID's list of all known peers.
    """
    allcontactslist = contacts_full()
    if my_id.getLocalID() in allcontactslist:
        allcontactslist.remove(my_id.getLocalID())
    return allcontactslist
Example #10
0
def contacts_remote():
    """
    Return ID's list of all known peers.
    """
    allcontactslist = contacts_full()
    if my_id.getLocalID() in allcontactslist:
        allcontactslist.remove(my_id.getLocalID())
    return allcontactslist
Example #11
0
def SendRetreiveCoin(remote_idurl, query, wide=False, callbacks={}):
    if _Debug:
        lg.out(_DebugLevel, "p2p_service.SendRetreiveCoin to %s" % remote_idurl)
    outpacket = signed.Packet(
        commands.Coin(), my_id.getLocalID(), my_id.getLocalID(), packetid.UniqueID(), json.dumps(query), remote_idurl
    )
    gateway.outbox(outpacket, wide=wide, callbacks=callbacks)
    return outpacket
Example #12
0
 def doForwardInboxPacket(self, arg):
     """
     Action method.
     """
     # encrypt with proxy_receiver()'s key and sent to man behind my proxy
     receiver_idurl, newpacket, info = arg
     route_info = self.routes.get(receiver_idurl, None)
     if not route_info:
         lg.warn('route with %s not found for inbox packet: %s' % (receiver_idurl, newpacket))
         return
     hosts = route_info['address']
     if len(hosts) == 0:
         lg.warn('route with %s do not have actual info about the host, use identity contacts instead' % receiver_idurl)
         hosts = route_info['contacts']
     if len(hosts) == 0:
         lg.warn('has no known contacts for route with %s' % receiver_idurl)
         return
     receiver_proto, receiver_host = hosts[0]
     publickey = route_info['publickey']
     src = ''
     src += newpacket.Serialize()
     block = encrypted.Block(
         my_id.getLocalID(),
         'routed incoming data',
         0,
         key.NewSessionKey(),
         key.SessionKeyType(),
         True,
         src,
         EncryptFunc=lambda inp: key.EncryptStringPK(publickey, inp))
     routed_packet = signed.Packet(
         commands.Relay(),
         newpacket.OwnerID,
         my_id.getLocalID(),
         newpacket.PacketID,
         block.Serialize(),
         receiver_idurl)
     pout = packet_out.create(
         newpacket,
         wide=False,
         callbacks={},
         route={
             'packet': routed_packet,
             'proto': receiver_proto,
             'host': receiver_host,
             'remoteid': receiver_idurl,
             'description': ('Relay_%s[%s]_%s' % (
                 newpacket.Command, newpacket.PacketID,
                 nameurl.GetName(receiver_idurl)))})
     if _Debug:
         lg.out(_DebugLevel, '<<<Relay-IN %s %s:%s' % (
             str(newpacket), info.proto, info.host,))
         lg.out(_DebugLevel, '           sent to %s://%s with %d bytes in %s' % (
             receiver_proto, receiver_host, len(src), pout))
     del src
     del block
     del newpacket
     del routed_packet
Example #13
0
 def _on_proxy_routers_dht_layer_connected(self, ok):
     from logs import lg
     from dht import dht_service
     from dht import dht_records
     from userid import my_id
     lg.info('connected to DHT layer for proxy routers: %r' % ok)
     if my_id.getLocalID():
         dht_service.set_node_data('idurl', my_id.getLocalID().to_text(), layer_id=dht_records.LAYER_PROXY_ROUTERS)
     return ok
def SendRetrieveCoin(remote_idurl, query, wide=False, callbacks={}):
    if _Debug:
        lg.out(_DebugLevel,
               "p2p_service.SendRetrieveCoin to %s" % remote_idurl)
    outpacket = signed.Packet(commands.RetrieveCoin(), my_id.getLocalID(),
                              my_id.getLocalID(), packetid.UniqueID(),
                              json.dumps(query), remote_idurl)
    gateway.outbox(outpacket, wide=wide, callbacks=callbacks)
    return outpacket
Example #15
0
def SendAckNoRequest(remoteID, packetid, response="", wide=False, callbacks={}):
    result = signed.Packet(commands.Ack(), my_id.getLocalID(), my_id.getLocalID(), packetid, response, remoteID)
    if _Debug:
        lg.out(
            _DebugLevel,
            "p2p_service.SendAckNoRequest %s to %s    response: %s ..."
            % (result.PacketID, result.RemoteID, str(response)[:15]),
        )
    gateway.outbox(result, wide=wide, callbacks=callbacks)
def packet_for_listener(listener_idurl, json_data):
    # if 'broadcaster' not in json_data:
    json_data['broadcaster'] = my_id.getLocalID()
    return signed.Packet(commands.Broadcast(),
                         json_data['owner'],
                         my_id.getLocalID(),
                         json_data['id'],
                         json.dumps(json_data),
                         listener_idurl,)
Example #17
0
def SendCoin(remote_idurl, coins, packet_id=None, wide=False, callbacks={}):
    if _Debug:
        lg.out(_DebugLevel, "p2p_service.SendCoin to %s" % remote_idurl)
    if packet_id is None:
        packet_id = packetid.UniqueID()
    outpacket = signed.Packet(
        commands.Coin(), my_id.getLocalID(), my_id.getLocalID(), packet_id, json.dumps(coins), remote_idurl
    )
    gateway.outbox(outpacket, wide=wide, callbacks=callbacks)
    return outpacket
Example #18
0
 def _send(c):
     from transport.udp import udp_stream
     for idurl in sys.argv[2:]:
         print('_send', list(udp_stream.streams().keys()))
         p = signed.Packet(commands.Data(), my_id.getLocalID(),
                           my_id.getLocalID(), 'packet%d' % c,
                           bpio.ReadBinaryFile(sys.argv[1]), idurl)
         gateway.outbox(p)
     if c > 1:
         reactor.callLater(0.01, _send, c - 1)
Example #19
0
def SendCancelService(remote_idurl, service_info, callbacks={}):
    if _Debug:
        lg.out(
            _DebugLevel, "p2p_service.SendCancelService [%s]" %
            service_info.replace('\n', ' ')[:40])
    result = signed.Packet(commands.CancelService(), my_id.getLocalID(),
                           my_id.getLocalID(), packetid.UniqueID(),
                           service_info, remote_idurl)
    gateway.outbox(result, callbacks=callbacks)
    return result
Example #20
0
def SendAck(packettoack, response='', wide=False, callbacks={}, packetid=None):
    result = signed.Packet(commands.Ack(), my_id.getLocalID(),
                           my_id.getLocalID(), packetid
                           or packettoack.PacketID, response,
                           packettoack.OwnerID)
    if _Debug:
        lg.out(
            _DebugLevel, "p2p_service.SendAck %s to %s  with %d bytes" %
            (result.PacketID, result.RemoteID, len(response)))
    gateway.outbox(result, wide=wide, callbacks=callbacks)
    return result
Example #21
0
def SendRequestService(remote_idurl, service_info, wide=False, callbacks={}):
    if _Debug:
        lg.out(
            _DebugLevel, "p2p_service.SendRequestService to %s [%s]" %
            (nameurl.GetName(remote_idurl), service_info.replace('\n',
                                                                 ' ')[:40]))
    result = signed.Packet(commands.RequestService(), my_id.getLocalID(),
                           my_id.getLocalID(), packetid.UniqueID(),
                           service_info, remote_idurl)
    gateway.outbox(result, wide=wide, callbacks=callbacks)
    return result
Example #22
0
def Inbox(newpacket, info, status, message):
    """
    This is called when some ``packet`` was received from remote peer - user seems to be ONLINE.
    """
    if newpacket.OwnerID == my_id.getLocalID():
        return False
    if newpacket.RemoteID != my_id.getLocalID():
        return False
    A(newpacket.OwnerID, 'inbox-packet', (newpacket, info, status, message))
    ratings.remember_connected_time(newpacket.OwnerID)
    return False
Example #23
0
def packet_for_listener(listener_idurl, json_data):
    # if 'broadcaster' not in json_data:
    json_data['broadcaster'] = my_id.getLocalID()
    return signed.Packet(
        commands.Broadcast(),
        json_data['owner'],
        my_id.getLocalID(),
        json_data['id'],
        json.dumps(json_data),
        listener_idurl,
    )
Example #24
0
def Inbox(newpacket, info, status, message):
    """
    This is called when some ``packet`` was received from remote peer - user seems to be ONLINE.
    """
    if newpacket.OwnerID == my_id.getLocalID():
        return False
    if newpacket.RemoteID != my_id.getLocalID():
        return False
    A(newpacket.OwnerID, 'inbox-packet', (newpacket, info, status, message))
    ratings.remember_connected_time(newpacket.OwnerID)
    return False
Example #25
0
 def encrypt(self, message_body, encrypt_session_func=None):
     new_sessionkey = key.NewSessionKey(
         session_key_type=key.SessionKeyType())
     if not encrypt_session_func:
         if my_keys.is_key_registered(self.recipient):
             if _Debug:
                 lg.out(
                     _DebugLevel,
                     'message.PrivateMessage.encrypt with "%s" key' %
                     self.recipient)
             encrypt_session_func = lambda inp: my_keys.encrypt(
                 self.recipient, inp)
     if not encrypt_session_func:
         glob_id = global_id.ParseGlobalID(self.recipient)
         if glob_id['key_alias'] == 'master':
             if glob_id['idurl'] == my_id.getLocalID():
                 lg.warn('making private message addressed to me ???')
                 if _Debug:
                     lg.out(
                         _DebugLevel,
                         'message.PrivateMessage.encrypt with "master" key')
                 encrypt_session_func = lambda inp: my_keys.encrypt(
                     'master', inp)
             else:
                 remote_identity = identitycache.FromCache(glob_id['idurl'])
                 if not remote_identity:
                     raise Exception(
                         'remote identity is not cached yet, not able to encrypt the message'
                     )
                 if _Debug:
                     lg.out(
                         _DebugLevel,
                         'message.PrivateMessage.encrypt with remote identity public key'
                     )
                 encrypt_session_func = remote_identity.encrypt
         else:
             own_key = global_id.MakeGlobalID(
                 idurl=my_id.getLocalID(), key_alias=glob_id['key_alias'])
             if my_keys.is_key_registered(own_key):
                 if _Debug:
                     lg.out(
                         _DebugLevel,
                         'message.PrivateMessage.encrypt with "%s" key' %
                         own_key)
                 encrypt_session_func = lambda inp: my_keys.encrypt(
                     own_key, inp)
     if not encrypt_session_func:
         raise Exception('can not find key for given recipient')
     self.encrypted_session = encrypt_session_func(new_sessionkey)
     self.encrypted_body = key.EncryptWithSessionKey(
         new_sessionkey,
         message_body,
         session_key_type=key.SessionKeyType())
     return self.encrypted_session, self.encrypted_body
Example #26
0
def SendCoin(remote_idurl, coins, packet_id=None, wide=False, callbacks={}):
    if _Debug:
        lg.out(_DebugLevel, "p2p_service.SendCoin to %s with %d records" % (remote_idurl, len(coins)))
    if packet_id is None:
        packet_id = packetid.UniqueID()
    outpacket = signed.Packet(
        commands.Coin(), my_id.getLocalID(),
        my_id.getLocalID(), packet_id,
        serialization.DictToBytes(coins), remote_idurl)
    gateway.outbox(outpacket, wide=wide, callbacks=callbacks)
    return outpacket
 def _on_message_brokers_dht_layer_connected(self, ok):
     from logs import lg
     from dht import dht_service
     from dht import dht_records
     from userid import my_id
     lg.info('connected to DHT layer for message brokers: %r' % ok)
     if my_id.getLocalID():
         dht_service.set_node_data(
             'idurl',
             my_id.getLocalID().to_text(),
             layer_id=dht_records.LAYER_MESSAGE_BROKERS)
     return ok
Example #28
0
def CustomerIDURL(backupID, as_field=True):
    """
    A wrapper for ``Split()`` method to get customer idurl from backup ID.
    """
    user, _, _ = backupID.strip().rpartition(':')
    if not user:
        from userid import my_id
        if as_field:
            return my_id.getLocalID()
        return my_id.getLocalID().to_bin()
    from userid import global_id
    return global_id.GlobalUserToIDURL(user, as_field=as_field)
Example #29
0
def SendIdentity(remote_idurl, wide=False, callbacks={}):
    """
    """
    if _Debug:
        lg.out(
            _DebugLevel,
            "p2p_service.SendIdentity to %s" % nameurl.GetName(remote_idurl))
    result = signed.Packet(commands.Identity(), my_id.getLocalID(),
                           my_id.getLocalID(), 'identity',
                           my_id.getLocalIdentity().serialize(), remote_idurl)
    gateway.outbox(result, wide=wide, callbacks=callbacks)
    return result
Example #30
0
 def _s():
     p = signed.Packet(
         commands.Data(),
         my_id.getLocalID(),
         my_id.getLocalID(),
         my_id.getLocalID(),
         bpio.ReadBinaryFile(args[1]),
         args[0],
     )
     outbox(p, wide=True)
     lg.out(2, "OUTBOX %d : %r" % (globals()["num_out"], p))
     globals()["num_out"] += 1
Example #31
0
def SendAckNoRequest(remoteID, packetid, response='', wide=False, callbacks={}):
    result = signed.Packet(
        Command=commands.Ack(),
        OwnerID=my_id.getLocalID(),
        CreatorID=my_id.getLocalID(),
        PacketID=packetid,
        Payload=response,
        RemoteID=remoteID,
    )
    if _Debug:
        lg.out(_DebugLevel, "p2p_service.SendAckNoRequest packetID=%s to %s  with %d bytes" % (
            result.PacketID, result.RemoteID, len(response)))
    gateway.outbox(result, wide=wide, callbacks=callbacks)
Example #32
0
def SendFailNoRequest(remoteID, packetID, response=''):
    result = signed.Packet(
        Command=commands.Fail(),
        OwnerID=my_id.getLocalID(),
        CreatorID=my_id.getLocalID(),
        PacketID=packetID,
        Payload=response,
        RemoteID=remoteID,
    )
    if _Debug:
        lg.out(_DebugLevel, "p2p_service.SendFailNoRequest packetID=%s to %s" % (result.PacketID, result.RemoteID))
    gateway.outbox(result)
    return result
Example #33
0
def Outbox(pkt_out):
    """
    Called when some ``packet`` is placed in the sending queue.

    This packet can be our Identity packet - this is a sort of PING operation
    to try to connect with that man.
    """
    if pkt_out.outpacket.RemoteID == my_id.getLocalID():
        return False
    if pkt_out.outpacket.CreatorID != my_id.getLocalID():
        return False
    A(pkt_out.outpacket.RemoteID, 'outbox-packet', pkt_out)
    return False
Example #34
0
def SendAckNoRequest(remoteID,
                     packetid,
                     response='',
                     wide=False,
                     callbacks={}):
    result = signed.Packet(commands.Ack(), my_id.getLocalID(),
                           my_id.getLocalID(), packetid, response, remoteID)
    if _Debug:
        lg.out(
            _DebugLevel,
            "p2p_service.SendAckNoRequest %s to %s    response: %s ..." %
            (result.PacketID, result.RemoteID, str(response)[:15]))
    gateway.outbox(result, wide=wide, callbacks=callbacks)
Example #35
0
def Outbox(pkt_out):
    """
    Called when some ``packet`` is placed in the sending queue.

    This packet can be our Identity packet - this is a sort of PING operation
    to try to connect with that man.
    """
    if pkt_out.outpacket.RemoteID == my_id.getLocalID():
        return False
    if pkt_out.outpacket.CreatorID != my_id.getLocalID():
        return False
    A(pkt_out.outpacket.RemoteID, 'outbox-packet', pkt_out)
    return False
 def _send(c):
     from transport.udp import udp_stream
     for idurl in sys.argv[2:]:
         print '_send', udp_stream.streams().keys()
         p = signed.Packet(commands.Data(),
                           my_id.getLocalID(),
                           my_id.getLocalID(),
                           'packet%d' % c,
                           bpio.ReadBinaryFile(sys.argv[1]),
                           idurl)
         gateway.outbox(p)
     if c > 1:
         reactor.callLater(0.01, _send, c - 1)
Example #37
0
def SendRetrieveCoin(remote_idurl, query, wide=False, callbacks={}):
    if _Debug:
        lg.out(_DebugLevel, "p2p_service.SendRetrieveCoin to %s" % remote_idurl)
    outpacket = signed.Packet(
        Command=commands.RetrieveCoin(),
        OwnerID=my_id.getLocalID(),
        CreatorID=my_id.getLocalID(),
        PacketID=packetid.UniqueID(),
        Payload=serialization.DictToBytes(query),
        RemoteID=remote_idurl,
    )
    gateway.outbox(outpacket, wide=wide, callbacks=callbacks)
    return outpacket
Example #38
0
def SendCancelService(remote_idurl, service_info, callbacks={}):
    if _Debug:
        lg.out(_DebugLevel, "p2p_service.SendCancelService [%s]" % service_info.replace("\n", " ")[:40])
    result = signed.Packet(
        commands.CancelService(),
        my_id.getLocalID(),
        my_id.getLocalID(),
        packetid.UniqueID(),
        service_info,
        remote_idurl,
    )
    gateway.outbox(result, callbacks=callbacks)
    return result
Example #39
0
 def doSuppliersRequestIndexFile(self, arg):
     """
     Action method.
     """
     if _Debug:
         lg.out(_DebugLevel,
                'index_synchronizer.doSuppliersRequestIndexFile')
     if driver.is_on('service_backups'):
         from storage import backup_control
         self.current_local_revision = backup_control.revision()
     else:
         self.current_local_revision = -1
     self.latest_supplier_revision = -1
     self.requesting_suppliers.clear()
     self.requested_suppliers_number = 0
     packetID = global_id.MakeGlobalID(idurl=my_id.getLocalID(),
                                       path=settings.BackupIndexFileName())
     # packetID = settings.BackupIndexFileName()
     localID = my_id.getLocalID()
     for supplierId in contactsdb.suppliers():
         if not supplierId:
             continue
         if not contact_status.isOnline(supplierId):
             continue
         pkt_out = p2p_service.SendRetreive(localID,
                                            localID,
                                            packetID,
                                            supplierId,
                                            callbacks={
                                                commands.Data():
                                                self._on_supplier_response,
                                                commands.Fail():
                                                self._on_supplier_response,
                                            })
         #             newpacket = signed.Packet(
         #                 commands.Retrieve(),
         #                 localID,
         #                 localID,
         #                 packetid.RemotePath(packetID),
         #                 '',
         #                 supplierId)
         #             pkt_out = gateway.outbox(newpacket, callbacks={
         #                 commands.Data(): self._on_supplier_response,
         #                 commands.Fail(): self._on_supplier_response, })
         if pkt_out:
             self.requesting_suppliers.add(supplierId)
             self.requested_suppliers_number += 1
         if _Debug:
             lg.out(
                 _DebugLevel, '    %s sending to %s' %
                 (pkt_out, nameurl.GetName(supplierId)))
Example #40
0
def SendFail(request, response="", remote_idurl=None):
    if remote_idurl is None:
        remote_idurl = request.OwnerID
    result = signed.Packet(
        commands.Fail(), my_id.getLocalID(), my_id.getLocalID(), request.PacketID, response, remote_idurl
    )
    if _Debug:
        lg.out(
            _DebugLevel,
            "p2p_service.SendFail %s to %s    response: %s ..."
            % (result.PacketID, result.RemoteID, str(response)[:15]),
        )
    gateway.outbox(result)
    return result
Example #41
0
 def doSendCancelService(self, arg):
     """
     Action method.
     """
     newpacket = signed.Packet(
         commands.CancelService(),
         my_id.getLocalID(),
         my_id.getLocalID(),
         packetid.UniqueID(),
         'service_proxy_server',
         self.router_idurl,)
     packet_out.create(newpacket, wide=True, callbacks={
         commands.Ack(): self._on_request_service_ack,
         commands.Fail(): self._on_request_service_fail},)
Example #42
0
def SendMessage(remote_idurl, packet_id=None, payload=None, wide=True, callbacks={}, response_timeout=None):
    """
    """
    if packet_id is None:
        packet_id = packetid.UniqueID()
    outpacket = signed.Packet(
        Command=commands.Message(),
        OwnerID=my_id.getLocalID(),
        CreatorID=my_id.getLocalID(),
        PacketID=packet_id,
        Payload=payload,
        RemoteID=remote_idurl,
    )
    result = gateway.outbox(outpacket, wide=wide, callbacks=callbacks, response_timeout=response_timeout)
    return result, outpacket
Example #43
0
def SendIdentity(remote_idurl, wide=False, timeout=10, callbacks={}):
    """
    """
    if _Debug:
        lg.out(_DebugLevel, "p2p_service.SendIdentity to %s wide=%s" % (nameurl.GetName(remote_idurl), wide, ))
    result = signed.Packet(
        Command=commands.Identity(),
        OwnerID=my_id.getLocalID(),
        CreatorID=my_id.getLocalID(),
        PacketID='identity',
        Payload=my_id.getLocalIdentity().serialize(),
        RemoteID=remote_idurl,
    )
    gateway.outbox(result, wide=wide, callbacks=callbacks, response_timeout=timeout)
    return result
def SendFailNoRequest(remoteID, packetID, response=''):
    result = signed.Packet(
        commands.Fail(),
        my_id.getLocalID(),
        my_id.getLocalID(),
        packetID,
        response,
        remoteID,
    )
    if _Debug:
        lg.out(
            _DebugLevel, "p2p_service.SendFailNoRequest packetID=%s to %s" %
            (result.PacketID, result.RemoteID))
    gateway.outbox(result)
    return result
Example #45
0
def SendCoin(remote_idurl, coins, packet_id=None, wide=False, callbacks={}):
    if _Debug:
        lg.out(_DebugLevel, "p2p_service.SendCoin to %s with %d records" % (remote_idurl, len(coins)))
    if packet_id is None:
        packet_id = packetid.UniqueID()
    outpacket = signed.Packet(
        Command=commands.Coin(),
        OwnerID=my_id.getLocalID(),
        CreatorID=my_id.getLocalID(),
        PacketID=packet_id,
        Payload=serialization.DictToBytes(coins, keys_to_text=True),
        RemoteID=remote_idurl,
    )
    gateway.outbox(outpacket, wide=wide, callbacks=callbacks)
    return outpacket
Example #46
0
def cmd_key(opts, args, overDict, running, executablePath):
    from main import settings
    from lib import misc
    from system import bpio
    from userid import my_id
    from crypt import key
    settings.init()
    my_id.init()

    if not key.LoadMyKey():
        print_text('private key not exist or is not valid\n')
        return 0
    if not my_id.isLocalIdentityReady():
        print_text('local identity not exist, your key worth nothing\n')
        return 0

    if len(args) == 2:
        if args[1] == 'copy':
            TextToSave = my_id.getLocalID() + "\n" + key.MyPrivateKey()
            misc.setClipboardText(TextToSave)
            del TextToSave
            print_text('now you can "paste" with Ctr+V your private key where you want')
            print_text('WARNING! keep your key in safe place, do not publish it!\n')
            return 0
        elif args[1] == 'print':
            TextToSave = my_id.getLocalID() + "\n" + key.MyPrivateKey()
            print_text('\n' + TextToSave + '\n')
            del TextToSave
            print_text('WARNING! keep your key in safe place, do not publish it!\n')
            return 0
    elif len(args) == 3:
        if args[1] == 'copy' or args[1] == 'save' or args[1] == 'backup':
            from system import bpio
            curpath = os.getcwd()
            os.chdir(executablePath)
            filenameto = bpio.portablePath(args[2])
            os.chdir(curpath)
            TextToSave = my_id.getLocalID() + "\n" + key.MyPrivateKey()
            if not bpio.AtomicWriteFile(filenameto, TextToSave):
                del TextToSave
                print_text('error writing to %s\n' % filenameto)
                return 1
            del TextToSave
            print_text('your private key were copied to file %s' % filenameto)
            print_text('WARNING! keep your key in safe place, do not publish it!\n')
            return 0

    return 2
Example #47
0
def ListFiles(request):
    """
    We will want to use this to see what needs to be resent, and expect normal
    case is very few missing.

    This is to build the ``Files()`` we are holding for a customer.
    """
    if not driver.is_started("service_supplier"):
        return SendFail(request, "supplier service is off")
    MyID = my_id.getLocalID()
    RemoteID = request.OwnerID
    PacketID = request.PacketID
    Payload = request.Payload
    if _Debug:
        lg.out(
            _DebugLevel, "p2p_service.ListFiles from [%s], format is %s" % (nameurl.GetName(request.OwnerID), Payload)
        )
    custdir = settings.getCustomersFilesDir()
    ownerdir = os.path.join(custdir, nameurl.UrlFilename(request.OwnerID))
    if not os.path.isdir(ownerdir):
        if _Debug:
            lg.out(_DebugLevel, "p2p_service.ListFiles did not find customer dir " + ownerdir)
        src = PackListFiles("", Payload)
        result = signed.Packet(commands.Files(), MyID, MyID, PacketID, src, RemoteID)
        gateway.outbox(result)
        return result
    plaintext = TreeSummary(ownerdir)
    if _Debug:
        lg.out(_DebugLevel + 4, "\n%s" % (plaintext))
    src = PackListFiles(plaintext, Payload)
    result = signed.Packet(commands.Files(), MyID, MyID, PacketID, src, RemoteID)
    gateway.outbox(result)
    return result
Example #48
0
def SendAck(packettoack, response="", wide=False, callbacks={}, packetid=None):
    result = signed.Packet(
        commands.Ack(),
        my_id.getLocalID(),
        my_id.getLocalID(),
        packetid or packettoack.PacketID,
        response,
        packettoack.OwnerID,
    )
    if _Debug:
        lg.out(
            _DebugLevel,
            "p2p_service.SendAck %s to %s    response: %s ..." % (result.PacketID, result.RemoteID, str(response)[:15]),
        )
    gateway.outbox(result, wide=wide, callbacks=callbacks)
    return result
Example #49
0
 def doDHTWriteKey(self, *args, **kwargs):
     """
     Action method.
     """
     d = dht_records.set_nickname(self.key, my_id.getLocalID())
     d.addCallback(self._dht_write_result)
     d.addErrback(lambda err: self.automat('dht-write-failed', err))
Example #50
0
def make_key_id(alias, creator_idurl=None, creator_glob_id=None):
    """
    Every key has a creator, and we include his IDURL in the final key_id string.
    Here is a global unique address to a remote copy of `cat.png` file:

        [email protected]:animals/cat.png#F20160313043757PM

    key_id here is:

        [email protected]

    key alias is `group_abc` and creator IDURL is:

        http://first-machine.com/alice.xml

    By knowing full key_id we can find and connect to the correct node(s)
    who is supporting that resource.
    """
    if not alias:
        alias = 'master'
    if creator_glob_id is not None:
        return global_id.MakeGlobalID(
            customer=creator_glob_id,
            key_alias=alias,
        )
    if creator_idurl is None:
        creator_idurl = my_id.getLocalID()
    return global_id.MakeGlobalID(
        idurl=creator_idurl,
        key_alias=alias,
    )
 def doSuppliersSendDBInfo(self, arg):
     from p2p import contact_status
     lg.out(4, 'backup_db_keeper.doSuppliersSendDBInfo')
     packetID = settings.BackupIndexFileName()
     self.sentSuppliers.clear()
     src = bpio.ReadBinaryFile(settings.BackupIndexFilePath())
     localID = my_id.getLocalID()
     b = encrypted.Block(
         localID,
         packetID,
         0,
         key.NewSessionKey(),
         key.SessionKeyType(),
         True,
         src)
     Payload = b.Serialize()
     for supplierId in contactsdb.suppliers():
         if not supplierId:
             continue
         if not contact_status.isOnline(supplierId):
             continue
         newpacket = signed.Packet(
             commands.Data(), localID, localID, packetID,
             Payload, supplierId)
         pkt_out = gateway.outbox(newpacket, callbacks={
             commands.Ack(): self._supplier_acked,
             commands.Fail(): self._supplier_acked, })
         self.sentSuppliers.add(supplierId)
         lg.out(
             4, '    %s sending to %s' %
             (pkt_out, nameurl.GetName(supplierId)))
Example #52
0
 def doSendMyListFiles(self, *args, **kwargs):
     """
     Action method.
     """
     json_list_files = backup_fs.Serialize(
         to_json=True,
         filter_cb=lambda path_id, path, info: True if strng.to_text(info.key_id) == strng.to_text(self.key_id) else False,
     )
     # raw_list_files = json.dumps(json_list_files, indent=2, encoding='utf-8')
     raw_list_files = serialization.DictToBytes(json_list_files, keys_to_text=True, values_to_text=True)
     if _Debug:
         lg.out(_DebugLevel, 'shared_access_donor.doSendMyListFiles prepared list of files for %s :\n%s' % (
             self.remote_idurl, raw_list_files))
     block = encrypted.Block(
         CreatorID=my_id.getLocalID(),
         BackupID=self.key_id,
         Data=raw_list_files,
         SessionKey=key.NewSessionKey(),
         EncryptKey=self.key_id,
     )
     encrypted_list_files = block.Serialize()
     packet_id = "%s:%s" % (self.key_id, packetid.UniqueID(), )
     p2p_service.SendFiles(
         idurl=self.remote_idurl,
         raw_list_files_info=encrypted_list_files,
         packet_id=packet_id,
         callbacks={
             commands.Ack(): lambda response, _: self.automat('list-files-ok', response),
             commands.Fail(): lambda response, _: self.automat('fail', Exception(str(response))),
             None: lambda pkt_out: self.automat('fail', Exception('timeout')),
         },
     )
Example #53
0
 def __init__(
     self,
     CreatorID=None,
     BackupID='',
     BlockNumber=0,
     SessionKey='',
     SessionKeyType=None,
     LastBlock=True,
     Data='',
     EncryptKey=None,
     DecryptKey=None,
 ):
     self.CreatorID = CreatorID
     if not self.CreatorID:
         self.CreatorID = my_id.getLocalID()
     self.BackupID = str(BackupID)
     self.BlockNumber = BlockNumber
     if callable(EncryptKey):
         self.EncryptedSessionKey = EncryptKey(SessionKey)
     elif isinstance(EncryptKey, basestring):
         self.EncryptedSessionKey = my_keys.encrypt(EncryptKey, SessionKey)
     else:
         self.EncryptedSessionKey = key.EncryptLocalPublicKey(SessionKey)
     self.SessionKeyType = SessionKeyType
     if not self.SessionKeyType:
         self.SessionKeyType = key.SessionKeyType()
     self.Length = len(Data)
     self.LastBlock = bool(LastBlock)
     self.EncryptedData = key.EncryptWithSessionKey(SessionKey,
                                                    Data)  # DataLonger
     self.Signature = None
     self.Sign()
     self.DecryptKey = DecryptKey
     if _Debug:
         lg.out(_DebugLevel, 'new data in %s' % self)
Example #54
0
def SendIdentity(remote_idurl, wide=False, callbacks={}):
    """
    
    """
    if _Debug:
        lg.out(_DebugLevel, "p2p_service.SendIdentity to %s" % nameurl.GetName(remote_idurl))
    result = signed.Packet(
        commands.Identity(),
        my_id.getLocalID(),
        my_id.getLocalID(),
        "identity",
        my_id.getLocalIdentity().serialize(),
        remote_idurl,
    )
    gateway.outbox(result, wide=wide, callbacks=callbacks)
    return result
Example #55
0
 def doDHTWriteKey(self, arg):
     """
     Action method.
     """
     d = dht_service.set_value(self.key, my_id.getLocalID(), age=int(time.time()))
     d.addCallback(self._dht_write_result)
     d.addErrback(lambda x: self.automat('dht-write-failed'))
Example #56
0
def SendRequestService(remote_idurl, service_info, wide=False, callbacks={}):
    if _Debug:
        lg.out(
            _DebugLevel,
            "p2p_service.SendRequestService to %s [%s]"
            % (nameurl.GetName(remote_idurl), service_info.replace("\n", " ")[:40]),
        )
    result = signed.Packet(
        commands.RequestService(),
        my_id.getLocalID(),
        my_id.getLocalID(),
        packetid.UniqueID(),
        service_info,
        remote_idurl,
    )
    gateway.outbox(result, wide=wide, callbacks=callbacks)
    return result
Example #57
0
def PacketSendingTimeout(remoteID, packetID):
    """
    Called from ``p2p.io_throttle`` when some packet is timed out.

    Right now this do nothing, state machine ignores that event.
    """
    if remoteID == my_id.getLocalID():
        return
    A(remoteID, 'sent-timeout', packetID)
Example #58
0
def OutboxStatus(pkt_out, status, error=''):
    """
    This method is called when raised a status report after sending a packet to
    remote peer.

    If packet sending was failed - user seems to be OFFLINE.
    """
    if pkt_out.remote_idurl == my_id.getLocalID():
        return False
    if pkt_out.outpacket.CreatorID != my_id.getLocalID():
        return False
    if status == 'finished':
        A(pkt_out.remote_idurl, 'sent-done', (pkt_out, status, error))
    else:
        if _Debug:
            lg.out(_DebugLevel, 'contact_status.OutboxStatus %s: [%s] with %s' % (status, pkt_out, pkt_out.outpacket))
        A(pkt_out.remote_idurl, 'sent-failed', (pkt_out, status, error))
    return False
Example #59
0
def SendDeleteBackup(SupplierID, BackupID):
    if _Debug:
        lg.out(_DebugLevel, "p2p_service.SendDeleteBackup SupplierID=%s  BackupID=%s " % (SupplierID, BackupID))
    MyID = my_id.getLocalID()
    PacketID = BackupID
    RemoteID = SupplierID
    result = signed.Packet(commands.DeleteBackup(), MyID, MyID, PacketID, "", RemoteID)
    gateway.outbox(result)
    return result
Example #60
0
 def _bk_done(bid, result):
     from crypt import signed
     try:
         os.mkdir(os.path.join(settings.getLocalBackupsDir(), bid + '.out'))
     except:
         pass
     for filename in os.listdir(os.path.join(settings.getLocalBackupsDir(), bid)):
         filepath = os.path.join(settings.getLocalBackupsDir(), bid, filename)
         payld = str(bpio.ReadBinaryFile(filepath))
         newpacket = signed.Packet(
             'Data',
             my_id.getLocalID(),
             my_id.getLocalID(),
             filename,
             payld,
             'http://megafaq.ru/cvps1010.xml')
         newfilepath = os.path.join(settings.getLocalBackupsDir(), bid + '.out', filename)
         bpio.AtomicWriteFile(newfilepath, newpacket.Serialize())
     reactor.stop()