Example #1
0
 def _recover():
     from system import bpio
     from lib import nameurl
     if len(args) < 3:
         return 2
     src = bpio.ReadBinaryFile(args[2])
     if len(src) > 1024 * 10:
         print_text('file is too big for private key')
         return 0
     try:
         lines = src.split('\n')
         idurl = lines[0]
         txt = '\n'.join(lines[1:])
         if idurl != nameurl.FilenameUrl(nameurl.UrlFilename(idurl)):
             idurl = ''
             txt = src
     except:
         idurl = ''
         txt = src
     if idurl == '' and len(args) > 3:
         idurl = args[3]
     if idurl == '':
         print_text('BitDust need to know your IDURL to recover your account\n')
         return 2
     from automats import automat
     from main import initializer
     initializer.A('run-cmd-line-recover', {'idurl': idurl, 'keysrc': txt})
     reactor.run()
     automat.objects().clear()
     if my_id.isLocalIdentityReady():
         print_text('your identity were restored:')
         print_text(my_id.getLocalIdentity().serialize())
     else:
         print_text('identity recovery FAILED')
     return 0
Example #2
0
def shutdown():
    from logs import lg
    from main import config
    from system import bpio
    lg.out(2, 'bpmain.shutdown')

    import shutdowner
    shutdowner.A('reactor-stopped')

    from automats import automat
    automat.objects().clear()
    if len(automat.index()) > 0:
        lg.warn('%d automats was not cleaned' % len(automat.index()))
        for a in automat.index().keys():
            lg.out(2, '    %r' % a)
    else:
        lg.out(2, 'bpmain.shutdown automat.objects().clear() SUCCESS, no state machines left in memory')

    config.conf().removeCallback('logs/debug-level')

    lg.out(2, 'bpmain.shutdown currently %d threads running:' % len(threading.enumerate()))
    for t in threading.enumerate():
        lg.out(2, '    ' + str(t))

    lg.out(2, 'bpmain.shutdown finishing and closing log file, EXIT')

    automat.CloseLogFile()

    lg.close_log_file()

    if bpio.Windows() and bpio.isFrozen():
        lg.stdout_stop_redirecting()

    return 0
Example #3
0
 def doDestroyMe(self, arg):
     """
     Remove all references to the state machine object to destroy it.
     """
     automat.objects().pop(self.index)
     global _ProxyReceiver
     del _ProxyReceiver
     _ProxyReceiver = None
 def doDestroyMe(self, arg):
     """
     Remove all references to the state machine object to destroy it.
     """
     automat.objects().pop(self.index)
     global _IndexSynchronizer
     del _IndexSynchronizer
     _IndexSynchronizer = None
Example #5
0
 def doDestroyMe(self, arg):
     """
     Action method.
     """
     callback.remove_inbox_callback(self._on_inbox_packet)
     automat.objects().pop(self.index)
     global _CoinsMiner
     del _CoinsMiner
     _CoinsMiner = None
 def doDestroyMe(self, arg):
     """
     Remove all references to the state machine object to destroy it.
     """
     callback.remove_inbox_callback(self._inbox_packet_received)
     automat.objects().pop(self.index)
     global _AccountantsFinder
     del _AccountantsFinder
     _AccountantsFinder = None
 def doDestroyMe(self, arg):
     """
     Remove all references to the state machine object to destroy it.
     """
     self.incoming_broadcast_message_callback = None
     callback.remove_inbox_callback(self._on_inbox_packet)
     automat.objects().pop(self.index)
     global _BroadcastListener
     del _BroadcastListener
     _BroadcastListener = None
Example #8
0
 def doDestroyMe(self, arg):
     """
     Action method.
     """
     global _Shutdowner
     del _Shutdowner
     _Shutdowner = None
     self.destroy()
     lg.out(2, 'shutdowner.doDestroyMe %d machines left in memory:\n        %s' % (
         len(automat.objects()), '\n        '.join(
             ['%d: %r' % (k, automat.objects()[k]) for k in automat.objects().keys()])))
 def doDestroyMe(self, arg):
     """
     Remove all references to the state machine object to destroy it.
     """
     self.pending_coins = None
     self.connected_accountants = None
     callback.remove_inbox_callback(self._on_inbox_packet)
     automat.objects().pop(self.index)
     global _AccountantNode
     del _AccountantNode
     _AccountantNode = None
Example #10
0
 def doDestroyMe(self, arg):
     """
     Remove all references to the state machine object to destroy it.
     """
     # gateway.remove_transport_state_changed_callback(self._on_transport_state_changed)
     network_connector.A().removeStateChangedCallback(self._on_network_connector_state_changed)
     callback.remove_inbox_callback(self._on_inbox_packet_received)
     callback.remove_finish_file_sending_callback(self._on_finish_file_sending)
     automat.objects().pop(self.index)
     global _ProxyRouter
     del _ProxyRouter
     _ProxyRouter = None
def stop_all():
    """
    
    """
    for a in automat.objects().values():
        if isinstance(a, NicknameObserver):
            lg.out(12, 'nickname_observer.stop_all sends "stop" to %r' % a)
            a.automat('stop')
Example #12
0
def cmd_register(opts, args, overDict):
    if len(args) < 2:
        return 2
    if len(args) >= 3:
        from main import settings
        config.conf().setData('backup.private-key-size', str(args[2]))
        
    from automats import automat
    from main import initializer
    from main import shutdowner
    if len(args) >= 4:
        regargs = (args[1], args[3])
    else:
        regargs = (args[1],)
    initializer.A('run-cmd-line-register', regargs)
    reactor.run()
    # shutdowner.A('reactor-stopped')
    automat.objects().clear()
    print
    return 0
Example #13
0
def _debuginfo(params):
    result = {}
    result['debug'] = lg.get_debug_level()
    result['automats'] = []
    from automats import automat
    for index, A in automat.objects().items():
        result['automats'].append({
            'index': index,
            'id': A.id,
            'name': A.name,
            'state': A.state, })
    return {'result': result, }
Example #14
0
def _debuginfo(params):
    result = {}
    result['debug'] = lg.get_debug_level()
    result['automats'] = []
    from automats import automat
    for index, A in automat.objects().items():
        result['automats'].append({
            'index': index,
            'id': A.id,
            'name': A.name,
            'state': A.state,
        })
    return {
        'result': result,
    }
Example #15
0
 def _register():
     if len(args) <= 2:
         return 2
     pksize = settings.getPrivateKeySize()
     if len(args) > 3:
         try:
             pksize = int(args[3])
         except:
             print_text('incorrect private key size\n')
             return 0
     from automats import automat
     from main import initializer
     from lib import misc
     if not misc.ValidUserName(args[2]):
         return 0
     initializer.A('run-cmd-line-register', {'username': args[2], 'pksize': pksize})
     reactor.run()
     automat.objects().clear()
     if my_id.isLocalIdentityReady():
         print_text('new identity created:')
         print_text(my_id.getLocalIdentity().serialize())
     else:
         print_text('identity creation FAILED')
     return 0
Example #16
0
def shutdown(x=None):
    """
    This is a top level method which control the process of finishing the
    program.

    Calls method ``shutdown()`` in other modules.
    """
    lg.out(2, "shutdowner.shutdown " + str(x))
    from services import driver
    from main import settings
    from main import events
    from logs import weblog
    from logs import webtraffic
    from system import tmpfile
    from system import run_upnpc
    from raid import eccmap
    from lib import net_misc
    from updates import git_proc
    from interface import api_jsonrpc_server
    from interface import api_rest_http_server
    from interface import ftp_server
    from userid import my_id
    from crypt import my_keys
    dl = []
    my_keys.shutdown()
    my_id.shutdown()
    ftp_server.shutdown()
    api_jsonrpc_server.shutdown()
    api_rest_http_server.shutdown()
    driver.shutdown()
    eccmap.shutdown()
    run_upnpc.shutdown()
    net_misc.shutdown()
    git_proc.shutdown()
    events.clear_subscribers()
    tmpfile.shutdown()
    if settings.NewWebGUI():
        from web import control
        dl.append(control.shutdown())
    else:
        from web import webcontrol
        dl.append(webcontrol.shutdown())
    weblog.shutdown()
    webtraffic.shutdown()
    for a in automat.objects().values():
        if a.name != 'shutdowner':
            a.event('shutdown')
    return DeferredList(dl)
Example #17
0
 def doShutdown(self, arg):
     lg.out(2, 'shutdowner.doShutdown %d machines currently' % len(automat.objects()))
     param = arg
     if self.shutdown_param is not None:
         param = self.shutdown_param
     if arg is None:
         param = 'exit'
     elif isinstance(arg, six.string_types):
         param = arg
     if param not in ['exit', 'restart', 'restartnshow']:
         param = 'exit'
     if param == 'exit':
         self._shutdown_exit()
     elif param == 'restart':
         self._shutdown_restart()
     elif param == 'restartnshow':
         self._shutdown_restart('show')
Example #18
0
 def doShutdown(self, arg):
     lg.out(2, 'shutdowner.doShutdown %d machines currently' % len(automat.objects()))
     param = arg
     if self.shutdown_param is not None:
         param = self.shutdown_param
     if arg is None:
         param = 'exit'
     elif isinstance(arg, str):
         param = arg
     if param not in ['exit', 'restart', 'restartnshow']:
         param = 'exit'
     if param == 'exit':
         self._shutdown_exit()
     elif param == 'restart':
         self._shutdown_restart()
     elif param == 'restartnshow':
         self._shutdown_restart('show')
Example #19
0
 def doStopListening(self, *args, **kwargs):
     """
     Action method.
     """
     if online_status.isKnown(self.router_idurl):
         online_status.remove_online_status_listener_callback(
             idurl=self.router_idurl,
             callback_method=self._on_router_contact_status_connected,
         )
         online_status.remove_online_status_listener_callback(
             idurl=self.router_idurl,
             callback_method=self._on_router_contact_status_offline,
         )
     active_router_session_machine_index = self.router_connection_info.get(
         'index', None)
     if active_router_session_machine_index is not None:
         active_router_session_machine = automat.objects().get(
             active_router_session_machine_index, None)
         if active_router_session_machine is not None:
             active_router_session_machine.removeStateChangedCallback(
                 self._on_router_session_disconnected)
             lg.info('removed callback from router active session: %r' %
                     active_router_session_machine)
         else:
             lg.err(
                 'did not found active router session state machine with index %s'
                 % active_router_session_machine_index)
     # if contact_status.isKnown(self.router_idurl):
     #     contact_status.A(self.router_idurl).removeStateChangedCallback(self._on_router_contact_status_connected)
     #     contact_status.A(self.router_idurl).removeStateChangedCallback(self._on_router_contact_status_offline)
     WriteMyOriginalIdentitySource('')
     config.conf().setString('services/proxy-transport/current-router', '')
     callback.remove_inbox_callback(self._on_inbox_packet_received)
     self.router_identity = None
     self.router_idurl = None
     self.router_id = ''
     self.router_proto_host = None
     self.request_service_packet_id = []
     self.router_connection_info = None
     my_id.rebuildLocalIdentity()
     if _Debug:
         lg.out(_DebugLevel, 'proxy_receiver.doStopListening')
Example #20
0
    def doShutdown(self, *args, **kwargs):
        if _Debug:
            lg.out(
                _DebugLevel, 'shutdowner.doShutdown %d machines currently' %
                len(automat.objects()))

        param = args[0]
        if self.shutdown_param is not None:
            param = self.shutdown_param
        if not args or args[0] is None:
            param = 'exit'
        elif isinstance(args[0], six.string_types):
            param = args[0]
        if param not in ['exit', 'restart', 'restartnshow']:
            param = 'exit'
        if param == 'exit':
            self._shutdown_exit()
        elif param == 'restart':
            self._shutdown_restart()
        elif param == 'restartnshow':
            self._shutdown_restart('show')
Example #21
0
def shutdown(x=None):
    """
    This is a top level method which control the process of finishing the
    program.

    Calls method ``shutdown()`` in other modules.
    """
    lg.out(2, "shutdowner.shutdown " + str(x))
    from services import driver
    from main import settings
    from logs import weblog
    from logs import webtraffic
    from system import tmpfile
    from system import run_upnpc
    from raid import eccmap
    from lib import net_misc
    from updates import git_proc
    from automats import automat
    dl = []
    driver.shutdown()
    eccmap.shutdown()
    run_upnpc.shutdown()
    net_misc.shutdown()
    git_proc.shutdown()
    if settings.NewWebGUI():
        from web import control
        dl.append(control.shutdown())
    else:
        from web import webcontrol
        dl.append(webcontrol.shutdown())
    weblog.shutdown()
    webtraffic.shutdown()
    for a in automat.objects().values():
        if a.name != 'shutdowner':
            a.event('shutdown')
    return DeferredList(dl)
Example #22
0
def shutdown(x=None):
    """
    This is a top level method which control the process of finishing the
    program.

    Calls method ``shutdown()`` in other modules.
    """
    if _Debug:
        lg.out(_DebugLevel, "shutdowner.shutdown " + str(x))
    dl = []
    try:
        from services import driver
        from main import control
        from main import events
        from main import listeners
        from logs import weblog
        from logs import webtraffic
        from system import tmpfile
        from system import run_upnpc
        from raid import eccmap
        from lib import net_misc
        from updates import git_proc
        from interface import api_rest_http_server
        from interface import api_web_socket
        # from interface import ftp_server
        from contacts import identitydb
        from crypt import my_keys
        from userid import id_url
        from userid import my_id
        my_keys.shutdown()
        my_id.shutdown()
        identitydb.shutdown()
        # ftp_server.shutdown()
        api_rest_http_server.shutdown()
        api_web_socket.shutdown()
        driver.shutdown()
        eccmap.shutdown()
        run_upnpc.shutdown()
        net_misc.shutdown()
        git_proc.shutdown()
        listeners.shutdown()
        events.shutdown()
        tmpfile.shutdown()
        control.shutdown()
        try:
            weblog.shutdown()
        except:
            lg.exc()
        try:
            webtraffic.shutdown()
        except:
            lg.exc()
        survived_automats = list(automat.objects().values())
        if survived_automats:
            lg.warn(
                'found %d survived state machines, sending "shutdown" event to them all'
                % len(survived_automats))
            for a in survived_automats:
                if a.name != 'shutdowner':
                    a.event('shutdown')
        survived_automats = list(automat.objects().values())
        if survived_automats:
            lg.warn(
                'still found %d survived state machines, executing "destroy()" method to them all'
                % len(survived_automats))
            for a in survived_automats:
                if a.name != 'shutdowner':
                    a.destroy()
        settings.shutdown()
        id_url.shutdown()
    except:
        lg.exc()
    # TODO: rework all shutdown() methods to return deferred objects
    return DeferredList(dl)
Example #23
0
 def _do_process_request(self, *args, **kwargs):
     global _MaxRoutesNumber
     json_payload, request, info = args[0]
     user_id = request.CreatorID
     #--- commands.RequestService()
     if request.Command == commands.RequestService():
         if len(self.routes) >= _MaxRoutesNumber:
             if _Debug:
                 lg.out(
                     _DebugLevel,
                     'proxy_server.doProcessRequest RequestService rejected: too many routes'
                 )
                 lg.out(_DebugLevel, '    %r' % self.routes)
             p2p_service.SendAck(request, 'rejected', wide=True)
         else:
             try:
                 # idsrc = strng.to_bin(json_payload['identity'])
                 idsrc = json_payload['identity']
                 cached_id = identity.identity(xmlsrc=idsrc)
             except:
                 lg.out(_DebugLevel, 'payload: [%s]' % request.Payload)
                 lg.exc()
                 return
             if not cached_id.Valid():
                 lg.warn('incoming identity is not valid')
                 return
             if not cached_id.isCorrect():
                 lg.warn('incoming identity is not correct')
                 return
             if user_id != cached_id.getIDURL():
                 lg.warn(
                     'incoming identity is not belong to request packet creator'
                 )
                 return
             if contactsdb.is_supplier(user_id):
                 if _Debug:
                     lg.out(
                         _DebugLevel,
                         'proxy_server.doProcessRequest RequestService rejected: this user is my supplier'
                     )
                 p2p_service.SendAck(request, 'rejected', wide=True)
                 return
             oldnew = ''
             if user_id not in list(self.routes.keys()):
                 # accept new route
                 oldnew = 'NEW'
                 self.routes[user_id] = {}
             else:
                 # accept existing routed user
                 oldnew = 'OLD'
             if not self._is_my_contacts_present_in_identity(cached_id):
                 if _Debug:
                     lg.out(_DebugLevel,
                            '    DO OVERRIDE identity for %s' % user_id)
                 identitycache.OverrideIdentity(user_id,
                                                cached_id.serialize())
             else:
                 if _Debug:
                     lg.out(
                         _DebugLevel,
                         '        SKIP OVERRIDE identity for %s' % user_id)
             self.routes[user_id]['time'] = time.time()
             self.routes[user_id]['identity'] = cached_id.serialize(
                 as_text=True)
             self.routes[user_id]['publickey'] = strng.to_text(
                 cached_id.publickey)
             self.routes[user_id][
                 'contacts'] = cached_id.getContactsAsTuples(as_text=True)
             self.routes[user_id]['address'] = []
             self._write_route(user_id)
             active_user_sessions = gateway.find_active_session(
                 info.proto, info.host)
             if active_user_sessions:
                 user_connection_info = {
                     'id': active_user_sessions[0].id,
                     'index': active_user_sessions[0].index,
                     'proto': info.proto,
                     'host': info.host,
                     'idurl': user_id,
                 }
                 active_user_session_machine = automat.objects().get(
                     user_connection_info['index'], None)
                 if active_user_session_machine:
                     active_user_session_machine.addStateChangedCallback(
                         lambda o, n, e, a: self.
                         _on_user_session_disconnected(user_id, o, n, e, a),
                         oldstate='CONNECTED',
                     )
                     if _Debug:
                         lg.out(
                             _DebugLevel,
                             'proxy_server.doProcessRequest connected %s routed user, set active session: %s'
                             % (oldnew.capitalize(), user_connection_info))
                 else:
                     lg.err('not found session state machine: %s' %
                            user_connection_info['index'])
             else:
                 if _Debug:
                     lg.out(
                         _DebugLevel,
                         'proxy_server.doProcessRequest active connection with user %s at %s:%s not yet exist'
                         % (
                             user_id,
                             info.proto,
                             info.host,
                         ))
                     lg.out(
                         _DebugLevel, '    current active sessions: %d' %
                         len(gateway.list_active_sessions(info.proto)))
             self.acks.append(
                 p2p_service.SendAck(request, 'accepted', wide=True))
             if _Debug:
                 lg.out(
                     _DebugLevel,
                     'proxy_server.doProcessRequest !!!!!!! ACCEPTED %s ROUTE for %s  contacts=%s'
                     % (
                         oldnew.capitalize(),
                         user_id,
                         self.routes[user_id]['contacts'],
                     ))
     #--- commands.CancelService()
     elif request.Command == commands.CancelService():
         if user_id in self.routes:
             # cancel existing route
             self._remove_route(user_id)
             self.routes.pop(user_id)
             identitycache.StopOverridingIdentity(user_id)
             p2p_service.SendAck(request, 'accepted', wide=True)
             if _Debug:
                 lg.out(
                     _DebugLevel,
                     'proxy_server.doProcessRequest !!!!!!! CANCELLED ROUTE for %s'
                     % user_id)
         else:
             p2p_service.SendAck(request, 'rejected', wide=True)
             if _Debug:
                 lg.out(
                     _DebugLevel,
                     'proxy_server.doProcessRequest CancelService rejected : %s is not found in routes'
                     % user_id)
                 lg.out(_DebugLevel, '    %r' % self.routes)
     else:
         p2p_service.SendFail(request, 'rejected', wide=True)
Example #24
0
 def doStartListening(self, *args, **kwargs):
     """
     Action method.
     """
     try:
         _, info = args[0]
         self.router_proto_host = (info.proto, info.host)
     except:
         try:
             # TODO: move that setting to separate file
             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(as_text=True)
     previous_identity = ReadMyOriginalIdentitySource()
     if previous_identity:
         lg.warn('my original identity is not empty, SKIP overwriting')
         if _Debug:
             lg.out(
                 _DebugLevel,
                 '\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 online_status.isKnown(self.router_idurl):
         online_status.add_online_status_listener_callback(
             idurl=self.router_idurl,
             callback_method=self._on_router_contact_status_connected,
             newstate='CONNECTED',
         )
         online_status.add_online_status_listener_callback(
             idurl=self.router_idurl,
             callback_method=self._on_router_contact_status_offline,
             newstate='OFFLINE',
         )
         # 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 is not None:
             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(
             _DebugLevel,
             'proxy_receiver.doStartListening !!!!!!! router: %s at %s://%s'
             % (self.router_idurl, self.router_proto_host[0],
                self.router_proto_host[1]))