def doReportFailed(self, arg):
     """
     Action method.
     """
     if self.result_defer:
         if arg:
             events.send(
                 'private-key-share-failed',
                 dict(
                     global_id=global_id.UrlToGlobalID(self.remote_idurl),
                     remote_idurl=self.remote_idurl,
                     key_id=self.key_id,
                     reason=arg,
                 ))
             self.result_defer.errback(Exception(arg))
         else:
             if self.remote_identity is None:
                 events.send(
                     'private-key-share-failed',
                     dict(
                         global_id=global_id.UrlToGlobalID(
                             self.remote_idurl),
                         remote_idurl=self.remote_idurl,
                         key_id=self.key_id,
                         reason='remote id caching failed',
                     ))
                 self.result_defer.errback(
                     Exception('remote id caching failed'))
             else:
                 if self.ping_response is None:
                     events.send(
                         'private-key-share-failed',
                         dict(
                             global_id=global_id.UrlToGlobalID(
                                 self.remote_idurl),
                             remote_idurl=self.remote_idurl,
                             key_id=self.key_id,
                             reason='remote node not responding',
                         ))
                     self.result_defer.errback(
                         Exception('remote node not responding'))
                 else:
                     events.send(
                         'private-key-share-failed',
                         dict(
                             global_id=global_id.UrlToGlobalID(
                                 self.remote_idurl),
                             remote_idurl=self.remote_idurl,
                             key_id=self.key_id,
                             reason='failed',
                         ))
                     self.result_defer.errback(Exception('failed'))
Exemple #2
0
def on_delete_file(newpacket):
    # TODO: call verify_packet_ownership()
    if not newpacket.Payload:
        ids = [
            newpacket.PacketID,
        ]
    else:
        ids = strng.to_text(newpacket.Payload).split('\n')
    filescount = 0
    dirscount = 0
    lg.warn('going to erase files: %s' % ids)
    customer_id = global_id.UrlToGlobalID(newpacket.OwnerID)
    for pcktID in ids:
        glob_path = global_id.ParseGlobalID(pcktID)
        if not glob_path['customer']:
            glob_path = global_id.ParseGlobalID(customer_id + ':' + pcktID)
        if not glob_path['path']:
            lg.err("got incorrect PacketID")
            p2p_service.SendFail(newpacket, 'incorrect path')
            return False
        if customer_id != glob_path['customer']:
            lg.warn('trying to delete file stored for another cusomer')
            continue
        # TODO: add validation of customerGlobID
        # TODO: process requests from another customer
        filename = make_valid_filename(newpacket.OwnerID, glob_path)
        if not filename:
            lg.warn("got empty filename, bad customer or wrong packetID?")
            p2p_service.SendFail(newpacket,
                                 'not a customer, or file not found')
            return False
        if os.path.isfile(filename):
            try:
                os.remove(filename)
                filescount += 1
            except:
                lg.exc()
        elif os.path.isdir(filename):
            try:
                bpio._dir_remove(filename)
                dirscount += 1
            except:
                lg.exc()
        else:
            lg.warn("path not found %s" % filename)


#         if self.publish_event_supplier_file_modified:
#             events.send('supplier-file-modified', data=dict(
#                 action='delete',
#                 glob_path=glob_path['path'],
#                 owner_id=newpacket.OwnerID,
#             ))
    if _Debug:
        lg.dbg(
            _DebugLevel,
            "from [%s] with %d IDs, %d files and %d folders were removed" %
            (newpacket.OwnerID, len(ids), filescount, dirscount))
    p2p_service.SendAck(newpacket)
    return True
def load_suppliers(path=None, customer_idurl=None, all_customers=False):
    """
    Load suppliers list from disk.
    """
    if all_customers:
        for customer_id in os.listdir(settings.SuppliersDir()):
            if not global_id.IsValidGlobalUser(customer_id):
                lg.warn('invalid customer record %s found in %s' %
                        (customer_id, settings.SuppliersDir()))
                continue
            path = os.path.join(settings.SuppliersDir(), customer_id,
                                'supplierids')
            lst = bpio._read_list(path)
            if lst is None:
                lst = list()
            set_suppliers(
                lst, customer_idurl=global_id.GlobalUserToIDURL(customer_id))
            lg.out(
                4, 'contactsdb.load_suppliers %d items from %s' %
                (len(lst), path))
        return True
    if path is None:
        if customer_idurl is None:
            path = settings.SupplierIDsFilename()
        else:
            path = os.path.join(settings.SuppliersDir(),
                                global_id.UrlToGlobalID(customer_idurl),
                                'supplierids')
    lst = bpio._read_list(path)
    if lst is None:
        lst = list()
    set_suppliers(lst)
    lg.out(4, 'contactsdb.load_suppliers %d items from %s' % (len(lst), path))
    return True
def WriteIndex(filepath=None, encoding='utf-8'):
    """
    Write index data base to the local file .bitdust/metadata/index.
    """
    global _LoadingFlag
    if _LoadingFlag:
        return
    if filepath is None:
        filepath = settings.BackupIndexFilePath()
    json_data = {}
    # json_data = backup_fs.Serialize(to_json=True, encoding=encoding)
    for customer_idurl in backup_fs.known_customers():
        customer_id = global_id.UrlToGlobalID(customer_idurl)
        json_data[customer_id] = backup_fs.Serialize(
            iterID=backup_fs.fsID(customer_idurl),
            to_json=True,
            encoding=encoding,
        )
    src = '%d\n' % revision()
    src += json.dumps(json_data,
                      indent=1,
                      separators=(',', ':'),
                      encoding=encoding)
    if _Debug:
        import pprint
        lg.out(_DebugLevel, pprint.pformat(json_data))
    return bpio.WriteTextFile(filepath, src)
Exemple #5
0
def save_suppliers(path=None, customer_idurl=None):
    """
    Write current suppliers list on the disk, ``path`` is a file path to save.
    """
    if not customer_idurl:
        customer_idurl = my_id.getLocalID()
    customer_idurl = strng.to_bin(customer_idurl.strip())
    customer_id = global_id.UrlToGlobalID(customer_idurl)
    if path is None:
        path = os.path.join(
            settings.SuppliersDir(),
            customer_id,
            'supplierids',
        )
    lst = suppliers(customer_idurl=customer_idurl)
    lst = list(map(strng.to_text, lst))
    if not os.path.exists(os.path.dirname(path)):
        bpio._dirs_make(os.path.dirname(path))
    bpio._write_list(path, lst)
    if _Debug:
        lg.out(
            _DebugLevel, 'contactsdb.save_suppliers for customer [%s]:\n%r' % (
                customer_id,
                lst,
            ))
    return True
Exemple #6
0
def SendFiles(
    idurl,
    raw_list_files_info,
    packet_id=None,
    callbacks={},
    timeout=10,
):
    """
    Sending information about known files stored locally for given customer (if you are supplier).
    You can also send a list of your files to another user if you wish to grand access.
    This will not send any personal data : only file names, ids, versions, etc.
    So pass list of files in encrypted form in the `payload` or leave it empty.
    """
    MyID = my_id.getLocalID()
    if not packet_id:
        packet_id = "%s:%s" % (global_id.UrlToGlobalID(idurl),
                               packetid.UniqueID())
    if _Debug:
        lg.out(
            _DebugLevel, 'p2p_service.SendFiles %d bytes in packetID=%s' %
            (len(raw_list_files_info), packet_id))
        lg.out(_DebugLevel, '  to remoteID=%s' % idurl)
    newpacket = signed.Packet(
        Command=commands.Files(),
        OwnerID=MyID,
        CreatorID=MyID,
        PacketID=packet_id,
        Payload=raw_list_files_info,
        RemoteID=idurl,
    )
    result = gateway.outbox(newpacket,
                            callbacks=callbacks,
                            response_timeout=timeout)
    return result
 def doRequestService(self, arg):
     """
     Action method.
     """
     service_info = {
         'needed_bytes': self.needed_bytes,
         'customer_id': global_id.UrlToGlobalID(self.customer_idurl),
     }
     my_customer_key_id = my_id.getGlobalID(key_alias='customer')
     if my_keys.is_key_registered(my_customer_key_id):
         service_info['customer_public_key'] = my_keys.get_key_info(
             key_id=my_customer_key_id,
             include_private=False,
         )
     if self.key_id:
         service_info['key_id'] = self.key_id
     if self.customer_idurl == my_id.getLocalIDURL():
         service_info['ecc_map'] = eccmap.Current().name
     request = p2p_service.SendRequestService(
         remote_idurl=self.supplier_idurl,
         service_name='service_supplier',
         json_payload=service_info,
         callbacks={
             commands.Ack(): self._supplier_acked,
             commands.Fail(): self._supplier_failed,
         },
     )
     self.request_packet_id = request.PacketID
 def doReportFailed(self, *args, **kwargs):
     """
     Action method.
     """
     lg.warn('share key [%s] with %s FAILED: %s' % (self.key_id, self.remote_idurl, args, ))
     reason = 'share key failed with unknown reason'
     if args and args[0]:
         reason = args[0]
     else:
         if self.remote_identity is None:
             reason='remote id caching failed',
         else:
             if self.ping_response is None:
                 reason='remote node not responding',
             else:
                 if self.suppliers_responses:
                     reason = 'connection timeout with my suppliers'
     events.send('private-key-share-failed', dict(
         global_id=global_id.UrlToGlobalID(self.remote_idurl),
         remote_idurl=self.remote_idurl,
         key_id=self.key_id,
         reason=reason,
     ))
     if self.result_defer:
         self.result_defer.errback(Exception(reason))
Exemple #9
0
def load_suppliers(path=None, customer_idurl=None, all_customers=False):
    """
    Load suppliers list from disk.
    """
    if all_customers:
        for customer_id in os.listdir(settings.SuppliersDir()):
            if not global_id.IsValidGlobalUser(customer_id):
                lg.warn('invalid customer record %s found in %s' % (customer_id, settings.SuppliersDir()))
                continue
            path = os.path.join(settings.SuppliersDir(), customer_id, 'supplierids')
            lst = bpio._read_list(path)
            if lst is None:
                lg.warn('did not found suppliers ids at %s' % path)
                continue
            lst = list(map(strng.to_bin, lst))
            set_suppliers(lst, customer_idurl=global_id.GlobalUserToIDURL(customer_id))
            lg.out(4, 'contactsdb.load_suppliers %d items from %s' % (len(lst), path))
        return True
    if not customer_idurl:
        customer_idurl = my_id.getLocalID()
    customer_idurl = strng.to_bin(customer_idurl.strip())
    if path is None:
        path = os.path.join(settings.SuppliersDir(), global_id.UrlToGlobalID(customer_idurl), 'supplierids')
    lst = bpio._read_list(path)
    if lst is None:
        lst = list()
    lst = list(map(strng.to_bin, lst))
    set_suppliers(lst)
    lg.out(4, 'contactsdb.load_suppliers %d items from %s' % (len(lst), path))
    return True
 def doReportRetry(self, event, *args, **kwargs):
     """
     Action method.
     """
     if _PacketLogFileEnabled:
         label = event.upper().replace('-', ' ')
         fail_info = args[0]
         lg.out(
             0,
             '\033[0;49;36m%s %s(%s) from %s to %s %s\033[0m' %
             (label, fail_info['command'], fail_info['packet_id'],
              global_id.UrlToGlobalID(fail_info['from']),
              global_id.UrlToGlobalID(fail_info['to']), fail_info['error']),
             log_name='packet',
             showtime=True,
         )
Exemple #11
0
 def doRequestService(self, *args, **kwargs):
     """
     Action method.
     """
     service_info = {
         'needed_bytes': self.needed_bytes,
         'customer_id': global_id.UrlToGlobalID(self.customer_idurl),
     }
     my_customer_key_id = my_id.getGlobalID(key_alias='customer')
     if my_keys.is_key_registered(my_customer_key_id):
         service_info['customer_public_key'] = my_keys.get_key_info(
             key_id=my_customer_key_id,
             include_private=False,
         )
     if self.key_id:
         service_info['key_id'] = self.key_id
     self._last_known_ecc_map = kwargs.get('ecc_map')
     if self._last_known_ecc_map is not None:
         service_info['ecc_map'] = self._last_known_ecc_map
     self._last_known_family_position = kwargs.get('family_position')
     if self._last_known_family_position is not None:
         service_info['position'] = self._last_known_family_position
     self._last_known_family_snapshot = kwargs.get('family_snapshot')
     if self._last_known_family_snapshot is not None:
         service_info['family_snapshot'] = self._last_known_family_snapshot
     request = p2p_service.SendRequestService(
         remote_idurl=self.supplier_idurl,
         service_name='service_supplier',
         json_payload=service_info,
         callbacks={
             commands.Ack(): self._supplier_acked,
             commands.Fail(): self._supplier_failed,
         },
     )
     self.request_packet_id = request.PacketID
Exemple #12
0
def populate_messages(recipient_id=None,
                      sender_id=None,
                      message_types=[],
                      offset=0,
                      limit=100):
    if recipient_id:
        if not recipient_id.count('@'):
            from contacts import contactsdb
            recipient_idurl = contactsdb.find_correspondent_by_nickname(
                recipient_id)
            if not recipient_idurl:
                lg.err('recipient %r was not found' % recipient_id)
                return
            recipient_id = global_id.UrlToGlobalID(recipient_idurl)
        recipient_glob_id = global_id.ParseGlobalID(recipient_id)
        if not recipient_glob_id['idurl']:
            lg.err('wrong recipient_id')
            return
        recipient_id = global_id.MakeGlobalID(**recipient_glob_id)
        if not my_keys.is_valid_key_id(recipient_id):
            lg.err('invalid recipient_id: %s' % recipient_id)
            return
    if sender_id:
        sender_local_key_id = my_keys.get_local_key_id(sender_id)
        if sender_local_key_id is None:
            return
    if recipient_id:
        recipient_local_key_id = my_keys.get_local_key_id(recipient_id)
        if recipient_local_key_id is None:
            lg.warn('recipient %r local key id was not registered' %
                    recipient_id)
            return
    for row in query_messages(
            sender_id=sender_id,
            recipient_id=recipient_id,
            bidirectional=False,
            message_types=message_types,
            offset=offset,
            limit=limit,
            raw_results=True,
    ):
        conversation_id = get_conversation_id(row[0], row[2], int(row[5]))
        if conversation_id is None:
            continue
        snap_id = '{}/{}'.format(conversation_id, row[7])
        listeners.push_snapshot('message',
                                snap_id=snap_id,
                                created=row[6],
                                data=build_json_message(
                                    sender=row[1],
                                    recipient=row[3],
                                    direction='in' if row[4] == 0 else 'out',
                                    conversation_id=conversation_id,
                                    message_type=MESSAGE_TYPE_CODES.get(
                                        int(row[5]), 'private_message'),
                                    message_time=row[6],
                                    message_id=row[7],
                                    data=json.loads(row[8]),
                                ))
Exemple #13
0
 def _on_delete_file(self, newpacket):
     import os
     from logs import lg
     from system import bpio
     from lib import strng
     from userid import global_id
     from p2p import p2p_service
     from main import events
     if not newpacket.Payload:
         ids = [newpacket.PacketID, ]
     else:
         ids = strng.to_text(newpacket.Payload).split('\n')
     filescount = 0
     dirscount = 0
     lg.warn('going to erase files: %s' % ids)
     customer_id = global_id.UrlToGlobalID(newpacket.OwnerID)
     for pcktID in ids:
         glob_path = global_id.ParseGlobalID(pcktID)
         if not glob_path['customer']:
             glob_path = global_id.ParseGlobalID(customer_id + ':' + pcktID)
         if not glob_path['path']:
             lg.err("got incorrect PacketID")
             p2p_service.SendFail(newpacket, 'incorrect path')
             return False
         if customer_id != glob_path['customer']:
             lg.warn('trying to delete file stored for another cusomer')
             continue
         # TODO: add validation of customerGlobID
         # TODO: process requests from another customer
         filename = self._do_make_valid_filename(newpacket.OwnerID, glob_path)
         if not filename:
             lg.warn("got empty filename, bad customer or wrong packetID?")
             p2p_service.SendFail(newpacket, 'not a customer, or file not found')
             return False
         if os.path.isfile(filename):
             try:
                 os.remove(filename)
                 filescount += 1
             except:
                 lg.exc()
         elif os.path.isdir(filename):
             try:
                 bpio._dir_remove(filename)
                 dirscount += 1
             except:
                 lg.exc()
         else:
             lg.warn("path not found %s" % filename)
         if self.publish_event_supplier_file_modified:
             events.send('supplier-file-modified', data=dict(
                 action='delete',
                 glob_path=glob_path['path'],
                 owner_id=newpacket.OwnerID,
             ))
     lg.out(self.debug_level, "service_supplier._on_delete_file from [%s] with %d IDs, %d files and %d folders were removed" % (
         newpacket.OwnerID, len(ids), filescount, dirscount))
     p2p_service.SendAck(newpacket)
     return True
Exemple #14
0
 def doStartListening(self, *args, **kwargs):
     """
     Action method.
     """
     try:
         _, info = args[0]
         self.router_proto_host = (info.proto, info.host)
     except:
         try:
             s = config.conf().getString('services/proxy-transport/current-router').strip()
             _, router_proto, router_host = s.split(' ')
             self.router_proto_host = (router_proto, strng.to_bin(router_host), )
         except:
             lg.exc()
     self.router_identity = identitycache.FromCache(self.router_idurl)
     config.conf().setString('services/proxy-transport/current-router', '%s %s %s' % (
         strng.to_text(self.router_idurl),
         strng.to_text(self.router_proto_host[0]),
         strng.to_text(self.router_proto_host[1]),
     ))
     current_identity = my_id.getLocalIdentity().serialize()
     previous_identity = ReadMyOriginalIdentitySource()
     if previous_identity:
         lg.warn('my original identity is not empty, SKIP overwriting')
         lg.out(2, '\nPREVIOUS ORIGINAL IDENTITY:\n%s\n' % current_identity)
     else:
         WriteMyOriginalIdentitySource(current_identity)
         lg.warn('current identity was stored as my-original-identity')
     self.request_service_packet_id = []
     callback.insert_inbox_callback(0, self._on_inbox_packet_received)
     if contact_status.isKnown(self.router_idurl):
         contact_status.A(self.router_idurl).addStateChangedCallback(
             self._on_router_contact_status_connected, newstate='CONNECTED')
         contact_status.A(self.router_idurl).addStateChangedCallback(
             self._on_router_contact_status_offline, newstate='OFFLINE')
     active_router_sessions = gateway.find_active_session(info.proto, info.host)
     if active_router_sessions:
         self.router_connection_info = {
             'id': active_router_sessions[0].id,
             'index': active_router_sessions[0].index,
             'proto': info.proto,
             'host': info.host,
             'idurl': self.router_idurl,
             'global_id': global_id.UrlToGlobalID(self.router_idurl),
         }
         active_router_session_machine = automat.objects().get(self.router_connection_info['index'], None)
         if active_router_session_machine:
             active_router_session_machine.addStateChangedCallback(
                 self._on_router_session_disconnected, oldstate='CONNECTED')
             lg.info('connected to proxy router and set active session: %s' % self.router_connection_info)
         else:
             lg.err('not found proxy router session state machine: %s' % self.router_connection_info['index'])
     else:
         lg.err('active connection with proxy router at %s:%s was not found' % (info.proto, info.host, ))
     if _Debug:
         lg.out(2, 'proxy_receiver.doStartListening !!!!!!! router: %s at %s://%s' % (
             self.router_idurl, self.router_proto_host[0], self.router_proto_host[1]))
Exemple #15
0
def save_suppliers(path=None, customer_idurl=None):
    """
    Write current suppliers list on the disk, ``path`` is a file path to save.
    """
    if path is None:
        path = settings.SupplierIDsFilename()
    if customer_idurl is not None:
        path += ('_%s' % global_id.UrlToGlobalID(customer_idurl))
    bpio._write_list(path, suppliers())
Exemple #16
0
def getGlobalID(key_alias=None):
    """
    Return my global user id - according to my current IDURL.
    """
    from userid import global_id
    glob_id = global_id.UrlToGlobalID(getLocalID())
    if key_alias:
        glob_id = '{}${}'.format(key_alias, glob_id)
    return glob_id
Exemple #17
0
 def doSendHisFiles(self, arg):
     """
     Action method.
     """
     packet_id = '%s:%s' % (
         global_id.UrlToGlobalID(self.customer_idurl),
         packetid.UniqueID(),
     )
     list_files.send(self.customer_idurl, packet_id,
                     settings.ListFilesFormat())
 def doReportDone(self, *args, **kwargs):
     """
     Action method.
     """
     lg.info('share key [%s] with %r finished with SUCCESS !!!!!' % (self.key_id, self.remote_idurl, ))
     events.send('private-key-shared', dict(
         global_id=global_id.UrlToGlobalID(self.remote_idurl),
         remote_idurl=self.remote_idurl,
         key_id=self.key_id,
     ))
     if self.result_defer:
         self.result_defer.callback(True)
Exemple #19
0
 def doReportDone(self, *args, **kwargs):
     """
     Action method.
     """
     lg.info('share group key [%s] with %r finished with success' % (self.group_key_id, self.remote_idurl, ))
     events.send('group-key-shared', data=dict(
         global_id=global_id.UrlToGlobalID(self.remote_idurl),
         remote_idurl=self.remote_idurl,
         group_key_id=self.group_key_id,
     ))
     if self.result_defer:
         self.result_defer.callback(True)
 def doReportDone(self, arg):
     """
     Action method.
     """
     events.send(
         'private-key-shared',
         dict(
             global_id=global_id.UrlToGlobalID(self.remote_idurl),
             remote_idurl=self.remote_idurl,
             key_id=self.key_id,
         ))
     if self.result_defer:
         self.result_defer.callback(True)
Exemple #21
0
 def doReportTimeOut(self, *args, **kwargs):
     """
     Action method.
     """
     if None in self.callbacks:
         for cb in self.callbacks[None]:
             cb(self)
     if _Debug:
         lg.out(4,
                '\033[2;49;90mTIMEOUT %s(%s) sending to %s\033[0m' %
                (self.outpacket.Command, self.outpacket.PacketID,
                 global_id.UrlToGlobalID(self.remote_idurl)),
                log_name='packet')
Exemple #22
0
 def _do_request_supplier_service(self, ecc_map, family_position,
                                  family_snapshot):
     if _Debug:
         lg.args(_DebugLevel,
                 supplier_idurl=self.supplier_idurl,
                 ecc_map=ecc_map,
                 family_position=family_position,
                 family_snapshot=family_snapshot)
     if not self.supplier_idurl:
         lg.warn(
             'supplier idurl is empty, SKIP sending supplier_service request'
         )
         return
     service_info = {
         'needed_bytes': self.needed_bytes,
         'customer_id': global_id.UrlToGlobalID(self.customer_idurl),
     }
     my_customer_key_id = my_id.getGlobalID(key_alias='customer')
     if my_keys.is_key_registered(my_customer_key_id):
         service_info['customer_public_key'] = my_keys.get_key_info(
             key_id=my_customer_key_id,
             include_private=False,
             include_signature=True,
             generate_signature=True,
         )
     else:
         lg.warn('my own customer key is not registered: %r' %
                 my_customer_key_id)
     if self.key_id:
         service_info['key_id'] = self.key_id
     self._last_known_ecc_map = ecc_map
     if self._last_known_ecc_map is not None:
         service_info['ecc_map'] = self._last_known_ecc_map
     self._last_known_family_position = family_position
     if self._last_known_family_position is not None:
         service_info['position'] = self._last_known_family_position
     self._last_known_family_snapshot = family_snapshot
     if self._last_known_family_snapshot is not None:
         service_info['family_snapshot'] = id_url.to_bin_list(
             self._last_known_family_snapshot)
     request = p2p_service.SendRequestService(
         remote_idurl=self.supplier_idurl,
         service_name='service_supplier',
         json_payload=service_info,
         callbacks={
             commands.Ack(): self._supplier_service_acked,
             commands.Fail(): self._supplier_service_failed,
         },
     )
     self.request_packet_id = request.PacketID
Exemple #23
0
 def _on_identity_url_changed(self, evt):
     from twisted.internet import reactor  # @UnresolvedImport
     from logs import lg
     from userid import id_url
     from userid import global_id
     from p2p import online_status
     for idurl, inst in online_status.online_statuses().items():
         if idurl == id_url.field(evt.data['old_idurl']):
             idurl.refresh(replace_original=True)
             inst.idurl.refresh(replace_original=True)
             inst.name = 'online_%s' % global_id.UrlToGlobalID(idurl)
             inst.automat('shook-up-hands')
             reactor.callLater(0, inst.automat, 'ping-now')  # @UndefinedVariable
             lg.info('found %r with rotated identity and refreshed: %r' % (inst, idurl, ))
Exemple #24
0
def save_suppliers(path=None, customer_idurl=None):
    """
    Write current suppliers list on the disk, ``path`` is a file path to save.
    """
    if not customer_idurl:
        customer_idurl = my_id.getLocalID()
    if path is None:
        path = os.path.join(
            settings.SuppliersDir(),
            global_id.UrlToGlobalID(customer_idurl),
            'supplierids',
        )
    bpio._write_list(path, suppliers(customer_idurl=customer_idurl))
    return True
Exemple #25
0
 def _on_request_service_ack(self, response, info):
     self.router_connection_info = None
     if response.PacketID not in self.request_service_packet_id:
         lg.warn('wrong PacketID in response: %s, but outgoing was : %s' % (
             response.PacketID, str(self.request_service_packet_id)))
         self.automat('service-refused', (response, info))
         return
     if response.PacketID in self.request_service_packet_id:
         self.request_service_packet_id.remove(response.PacketID)
     else:
         lg.warn('%s was not found in pending requests: %s' % (response.PacketID, self.request_service_packet_id))
     if _Debug:
         lg.out(_DebugLevel, 'proxy_receiver._on_request_service_ack : %s' % str(response.Payload))
     if self.router_idurl != response.CreatorID:
         lg.err('received unexpected response from another node: %r ~ %r' % (self.router_idurl, response.CreatorID, ))
         self.automat('service-refused', (response, info))
         return
     service_ack_info = strng.to_text(response.Payload)
     if service_ack_info.startswith('rejected'):
         self.automat('service-refused', (response, info))
         return
     active_router_sessions = gateway.find_active_session(info.proto, host=info.host)
     if not active_router_sessions:
         active_router_sessions = gateway.find_active_session(info.proto, idurl=id_url.to_bin(response.CreatorID))
     if not active_router_sessions:
         lg.err('active connection with proxy router at %s:%s was not found' % (info.proto, info.host, ))
         if _Debug:
             lg.args(_DebugLevel, router_idurl=self.router_idurl, ack_packet=info, active_sessions=gateway.list_active_sessions(info.proto))
         self.automat('service-refused', (response, info))
         return
     self.router_connection_info = {
         'id': active_router_sessions[0].id,
         'index': active_router_sessions[0].index,
         'repr': repr(active_router_sessions[0]),
         'proto': info.proto,
         'host': info.host,
         'idurl': self.router_idurl,
         'global_id': global_id.UrlToGlobalID(self.router_idurl),
     }
     active_router_session_machine = automat.by_index(self.router_connection_info['index'])
     if active_router_session_machine is None:
         lg.err('did not found proxy router session state machine instance: %s' % self.router_connection_info)
         self.router_connection_info = None
         if _Debug:
             lg.args(_DebugLevel, automats=automat.objects())
         self.automat('service-refused', (response, info))
         return
     lg.info('found active session for proxy router: %s' % active_router_session_machine)
     self.automat('service-accepted', (response, info, active_router_session_machine))
Exemple #26
0
def load_suppliers(path=None, customer_idurl=None, all_customers=False):
    """
    Load suppliers list from disk.
    """
    if all_customers:
        list_local_customers = list(os.listdir(settings.SuppliersDir()))
        if _Debug:
            lg.out(_DebugLevel, 'contactsdb.load_suppliers %d known customers' % len(list_local_customers))
        for customer_id in list_local_customers:
            if not global_id.IsValidGlobalUser(customer_id):
                lg.warn('invalid customer record %s found in %s' % (customer_id, settings.SuppliersDir()))
                continue
            path = os.path.join(settings.SuppliersDir(), customer_id, 'supplierids')
            lst = bpio._read_list(path)
            if lst is None:
                lg.warn('did not found suppliers ids at %s' % path)
                continue
            one_customer_idurl = global_id.GlobalUserToIDURL(customer_id)
            if not id_url.is_cached(one_customer_idurl):
                lg.warn('customer identity %r not cached yet' % one_customer_idurl)
                continue
            if not one_customer_idurl.is_latest():
                latest_customer_path = os.path.join(settings.SuppliersDir(), one_customer_idurl.to_id())
                old_customer_path = os.path.join(settings.SuppliersDir(), customer_id)
                if not os.path.exists(latest_customer_path):
                    os.rename(old_customer_path, latest_customer_path)
                    lg.info('detected and processed idurl rotate when loading suppliers for customer : %r -> %r' % (customer_id, one_customer_idurl.to_id()))
                else:
                    bpio._dir_remove(old_customer_path)
                    lg.warn('found old customer dir %r and removed' % old_customer_path)
                    continue
            lst = list(map(lambda i: i if id_url.is_cached(i) else b'', lst))
            set_suppliers(lst, customer_idurl=one_customer_idurl)
            if _Debug:
                lg.out(_DebugLevel, '    loaded %d known suppliers for customer %r' % (len(lst), one_customer_idurl))
        return True
    if not customer_idurl:
        customer_idurl = my_id.getLocalID()
    customer_idurl = id_url.field(customer_idurl)
    if path is None:
        path = os.path.join(settings.SuppliersDir(), global_id.UrlToGlobalID(customer_idurl), 'supplierids')
    lst = bpio._read_list(path)
    if lst is None:
        lst = list()
    lst = list(map(lambda i: i if id_url.is_cached(i) else b'', lst))
    set_suppliers(lst, customer_idurl=customer_idurl)
    if _Debug:
        lg.out(_DebugLevel, 'contactsdb.load_suppliers %d items from %s' % (len(lst), path))
    return True
Exemple #27
0
 def doReportDoneNoAck(self, *args, **kwargs):
     """
     Action method.
     """
     callback.run_queue_item_status_callbacks(self, 'finished',
                                              'unanswered')
     if _Debug:
         lg.out(
             2,
             '\033[0;49;95mOUT %s(%s) with %s bytes to %s TID:%r\033[0m' %
             (self.outpacket.Command, self.outpacket.PacketID, self.filesize
              or '?', global_id.UrlToGlobalID(
                  self.remote_idurl), [i.transfer_id
                                       for i in self.results]),
             log_name='packet')
Exemple #28
0
def getGlobalID(key_alias=None):
    """
    Returns my global user id - according to my current IDURL.
    """
    global _LocalID
    if not key_alias and _LocalID is not None:
        return _LocalID
    if key_alias == 'master' and _LocalID is not None:
        return strng.to_text('{}${}'.format(key_alias, _LocalID))
    from userid import global_id
    glob_id = global_id.UrlToGlobalID(getIDURL())
    if not glob_id:
        return glob_id
    if key_alias:
        glob_id = strng.to_text('{}${}'.format(key_alias, glob_id))
    return glob_id
Exemple #29
0
 def __init__(self, customer_idurl, new_data=None, publish=False,
              limit_lookups=100, max_misses_in_row=3, prefix='customer_supplier'):
     self.customer_idurl = customer_idurl
     self.customer_id = global_id.UrlToGlobalID(self.customer_idurl)
     self._result_defer = Deferred()
     self._new_data = new_data
     self._publish = publish
     self._limit_lookups = limit_lookups
     self._index = 0
     self._last_success_index = -1
     self._last_missed_index = -1
     self._max_misses_in_row = max_misses_in_row
     self._prefix = prefix
     self._misses_in_row = 0
     self._missed = 0
     self._result = {}
     self._meta = {}
Exemple #30
0
 def doReportFailed(self, event, *args, **kwargs):
     """
     Action method.
     """
     lg.warn('share group key [%s] with %s failed: %s' % (self.group_key_id, self.remote_idurl, args, ))
     reason = 'group key transfer failed with unknown reason'
     if args and args[0]:
         reason = args[0]
     else:
         if event.count('timer-'):
             reason = 'group key transfer failed because of network connection timeout'
     events.send('group-key-share-failed', data=dict(
         global_id=global_id.UrlToGlobalID(self.remote_idurl),
         remote_idurl=self.remote_idurl,
         group_key_id=self.group_key_id,
         reason=reason,
     ))
     if self.result_defer:
         self.result_defer.errback(Exception(reason))