Ejemplo n.º 1
0
def main():
    """
    This should print a current identity or create a new one.
    """
    settings.init(base_dir='/tmp/fake_id')
    from userid import my_id
    my_id.loadLocalIdentity()
    if my_id.isLocalIdentityReady():
        my_id.getLocalIdentity().sign()
        print(my_id.getLocalIdentity().serialize())
        print('Valid is: ', my_id.getLocalIdentity().Valid())
    else:
        if not key.InitMyKey():
            key.GenerateNewKey()
        ipaddr = '127.0.0.1'
        if len(sys.argv) > 2:
            ipaddr = sys.argv[2]
        rev = 0
        if len(sys.argv) > 3:
            rev = int(sys.argv[3])
        idurls = []
        if len(sys.argv) > 4:
            idurls = sys.argv[4:]
        my_id.setLocalIdentity(
            my_id.buildDefaultIdentity(name=sys.argv[1],
                                       ip=ipaddr,
                                       idurls=idurls,
                                       revision=rev))
        my_id.saveLocalIdentity()
        print(my_id.getLocalIdentity().serialize())
        print('Valid is: ', my_id.getLocalIdentity().Valid())
        my_id._LocalIdentity = None
        my_id.loadLocalIdentity()
    settings.shutdown()
Ejemplo n.º 2
0
 def tearDown(self):
     backup_fs.shutdown()
     key.ForgetMyKey()
     my_id.forgetLocalIdentity()
     settings.shutdown()
     os.remove('/tmp/_some_priv_key')
     bpio.rmdir_recursive('/tmp/.bitdust_tmp')
Ejemplo n.º 3
0
def main():
    import logging
    logging.basicConfig(level=logging.DEBUG)
    from twisted.internet.defer import setDebugging
    setDebugging(True)
    # from twisted.python import log as twisted_log
    # twisted_log.startLogging(sys.stdout)
    lg.set_debug_level(20)
    settings.init()
    settings.update_proxy_settings()

    if sys.argv.count('receive'):
        start_receiving()
#         global _Contacts
#         _Contacts['http://p2p-id.ru/veselin.xml'] = ('127.0.0.1', 9122)

    elif sys.argv.count('send'):
        start_sending(port=int(sys.argv[2]))
        send_file(sys.argv[3], sys.argv[4])

    else:
        usage()
        return

    reactor.run()  # @UndefinedVariable
    settings.shutdown()
Ejemplo n.º 4
0
def main():
    import pprint
    lg.set_debug_level(14)
    if sys.argv.count('list'):
        maps = lst()
        for itm in maps:
            pprint.pprint(itm)
    elif sys.argv.count('info'):
        locip, extip, maps = info()
        pprint.pprint(locip)
        pprint.pprint(extip)
        for itm in maps:
            pprint.pprint(itm)
    elif sys.argv.count('add'):
        print(add(sys.argv[2], 'TCP'))
    elif sys.argv.count('del'):
        print(dlt(sys.argv[2], 'TCP'))
    elif sys.argv.count('update'):
        bpio.init()
        settings.init()
        init()
        pprint.pprint(update(sys.argv[2]))
        settings.shutdown()
    elif sys.argv.count('clear'):
        print(clear())
    else:
        print('usage:')
        print('run_upnpc.py info')
        print('run_upnpc.py list')
        print('run_upnpc.py add [port]')
        print('run_upnpc.py del [port]')
        print('run_upnpc.py update [port]')
        print('run_upnpc.py clear')
Ejemplo n.º 5
0
def shutdown():
    from logs import lg
    from main import config
    from system import bpio
    if _Debug:
        lg.out(_DebugLevel, 'bpmain.shutdown')

    if config.conf():
        config.conf().removeConfigNotifier('logs/debug-level')

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

    from main import listeners
    listeners.shutdown()

    from main import events
    events.shutdown()

    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():
            if _Debug:
                lg.out(_DebugLevel, '    %r' % a)
    else:
        if _Debug:
            lg.out(
                _DebugLevel,
                'bpmain.shutdown automat.objects().clear() SUCCESS, no state machines left in memory'
            )

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

    if _Debug:
        lg.out(_DebugLevel,
               'bpmain.shutdown finishing and closing log file, EXIT')

    # automat.CloseLogFile()
    automat.SetExceptionsHandler(None)
    automat.SetLogOutputHandler(None)

    lg.close_log_file()

    lg.close_intercepted_log_file()

    lg.stdout_stop_redirecting()
    lg.stderr_stop_redirecting()

    from main import settings
    settings.shutdown()

    return 0
Ejemplo n.º 6
0
def main():
    """
    Read settings from 'config' file and prints values from your queries to stdout.
    """
    from logs import lg
    lg.set_debug_level(24)
    from main import settings
    settings.init()
    init(settings.ConfigDir())
    print(conf().listEntries(''))
    try:
        inp = sys.argv[1].rstrip('/')
    except:
        print('wrong input')
        return
    if not conf().exist(inp):
        print('not exist')
        return
    if not conf().hasChilds(inp):
        print(inp, conf().getData(inp))
        return
    for child in conf().listEntries(inp):
        if conf().hasChilds(child):
            print(child, conf().listEntries(child))
        else:
            print(child, conf().getData(child))
    settings.shutdown()
    return
Ejemplo n.º 7
0
def main():
    """
    Entry point.
    """
    if len(sys.argv) < 2:
        return
    bpio.init()
    lg.disable_logs()
    lg.disable_output()
    settings.init()
    lg.set_debug_level(0)
    id_url.init()
    commands = {
        'update_customers': UpdateCustomers,
        'validate': Validate,
        'space_time': SpaceTime,
    }
    cmd = commands.get(sys.argv[1], None)
    if not cmd:
        if _Debug:
            printlog('ERROR wrong command: %r' % sys.argv)
        return
    cmd()
    settings.shutdown()
    id_url.shutdown()
Ejemplo n.º 8
0
def main():
    lg.life_begins()
    bpio.init()
    settings.init()
    misc.init()
    my_id.init()
    identitycache.init()
    from crypt import key
    key.InitMyKey()
    (options, args) = parseCommandLine()
    settings.override('transport.transport-tcp.transport-tcp-port',
                      options.tcpport)
    settings.override('transport.transport-udp.transport-udp-port',
                      options.udpport)
    settings.override('network.network-dht-port', options.dhtport)
    lg.set_debug_level(options.debug)
    tmpfile.init()
    if True:
        import lib.udp
        lib.udp.listen(options.udpport)
        import dht.dht_service
        dht.dht_service.init(options.dhtport)
    reactor.addSystemEventTrigger('before', 'shutdown',
                                  shutdown)  # @UndefinedVariable
    init()
    start()
    globals()['num_in'] = 0

    def _in(a, b, c, d):
        lg.out(2, 'INBOX %d : %r' % (globals()['num_in'], a))
        globals()['num_in'] += 1
        return False

    callback.insert_inbox_callback(0, _in)
    if len(args) > 0:
        globals()['num_out'] = 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

        old_state_changed = transport('udp').state_changed

        def new_state_changed(oldstate, newstate, event, *args, **kwargs):
            old_state_changed(oldstate, newstate, event, *args, **kwargs)
            if newstate == 'LISTENING':
                reactor.callLater(1, _s)  # @UndefinedVariable

        transport('udp').state_changed = new_state_changed
        # t = task.LoopingCall(_s)
        # reactor.callLater(5, t.start, 60, True)
        # reactor.callLater(2, t.stop)

    reactor.run()  # @UndefinedVariable
    settings.shutdown()
Ejemplo n.º 9
0
def main():
    from main import settings
    lg.set_debug_level(20)
    settings.init()
    init()
    # print '\n'.join(_BootUpOrder)
    shutdown()
    settings.shutdown()
Ejemplo n.º 10
0
def main():
    from twisted.internet import reactor  # @UnresolvedImport
    lg.set_debug_level(24)
    settings.init()
    my_id.init()
    dht_service.init(settings.getDHTPort())
    reactor.callWhenRunning(A, 'init', sys.argv[1])  # @UndefinedVariable
    reactor.run()  # @UndefinedVariable
    settings.shutdown()
Ejemplo n.º 11
0
def test1():
    lg.set_debug_level(20)
    bpio.init()
    settings.init()
    update_sheduler()
    #SetLocalDir('c:\\Program Files\\\xc4 \xd8 \xcd')
    # download_and_replace_starter()
    reactor.run()  # @UndefinedVariable
    settings.shutdown()
Ejemplo n.º 12
0
 def tearDown(self):
     automat.CloseLogFile()
     tmpfile.shutdown()
     key.ForgetMyKey()
     my_id.forgetLocalIdentity()
     settings.shutdown()
     os.remove('/tmp/_some_priv_key')
     bpio.rmdir_recursive('/tmp/.bitdust_tmp')
     bpio.rmdir_recursive('/tmp/_some_folder')
     os.remove('/tmp/random_file')
Ejemplo n.º 13
0
def main():
    from system import bpio
    bpio.init()
    settings.init()
    lg.set_debug_level(20)
    from twisted.internet import reactor  # @UnresolvedImport
    ir = IdRotator()
    ir.addStateChangedCallback(lambda *a: reactor.stop(), oldstate=None, newstate='DONE')  # @UndefinedVariable
    ir.addStateChangedCallback(lambda *a: reactor.stop(), oldstate=None, newstate='FAILED')  # @UndefinedVariable
    reactor.callWhenRunning(ir.automat, 'run')  # @UndefinedVariable
    reactor.run()  # @UndefinedVariable
    settings.shutdown()
Ejemplo n.º 14
0
 def do_restart(param):
     from lib import misc
     from system import bpio
     settings.init()
     # appdata = settings.BaseDir()
     detach = False
     if bpio.Windows():
         detach = True
     misc.DoRestart(
         param,
         detach=detach,
         # std_out=os.path.join(appdata, 'logs', 'stdout.log'),
         # std_err=os.path.join(appdata, 'logs', 'stderr.log'),
     )
     settings.shutdown()
Ejemplo n.º 15
0
def update():
    """
    A good way to check all things - load and sign again.
    Also will test rebuilding of the identity
    """
    from userid import my_id
    bpio.init()
    settings.init()
    src = bpio.ReadTextFile(settings.LocalIdentityFilename())
    print(src)
    my_id.setLocalIdentity(identity(xmlsrc=src))
    my_id.getLocalIdentity().sign()
    my_id.saveLocalIdentity()
    print(my_id.getLocalIdentity().serialize())
    print(my_id.rebuildLocalIdentity(revision_up=True))
    settings.shutdown()
Ejemplo n.º 16
0
def main():
    settings.init()
    lg.set_debug_level(12)
    connect_layers = []
    if options.layer != 0:
        connect_layers.append(options.layer)
    dht_service.init(udp_port=14441, open_layers=connect_layers)
    seeds = []

    for seed_env in os.environ.get('DHT_SEEDS').split(','):
        seed = seed_env.split(':')
        seeds.append((seed[0], int(seed[1])))

    dht_service.connect(seeds).addBoth(connected, seeds=seeds)
    reactor.run()  # @UndefinedVariable
    settings.shutdown()
Ejemplo n.º 17
0
def main():
    bpio.init()
    settings.init()
    lg.set_debug_level(20)
    from twisted.internet import reactor  # @UnresolvedImport
    if len(sys.argv) > 2:
        args = (sys.argv[1], sys.argv[2])
    else:
        args = (sys.argv[1])
    A().addStateChangedCallback(lambda *a: reactor.stop(),
                                oldstate=None,
                                newstate='DONE')  # @UndefinedVariable
    A().addStateChangedCallback(lambda *a: reactor.stop(),
                                oldstate=None,
                                newstate='FAILED')  # @UndefinedVariable
    reactor.callWhenRunning(A, 'start', args)  # @UndefinedVariable
    reactor.run()  # @UndefinedVariable
    settings.shutdown()
Ejemplo n.º 18
0
def main():
    def _ok(x):
        print('ok', x)
        reactor.stop()  # @UndefinedVariable

    def _fail(x):
        print('fail', x)
        reactor.stop()  # @UndefinedVariable

    from lib import net_misc
    from main import settings
    settings.init()
    settings.update_proxy_settings()
    url = 'http://localhost:8084'
    r = net_misc.getPageTwisted(url)
    r.addCallback(_ok)
    r.addErrback(_fail)
    reactor.run()  # @UndefinedVariable
    settings.shutdown()
Ejemplo n.º 19
0
def main():
    from twisted.internet import reactor  # @UnresolvedImport
    lg.set_debug_level(24)
    bpio.init()
    settings.init()
    dht_port = settings.getDHTPort()
    if len(sys.argv) > 1:
        dht_port = int(sys.argv[1])
    udp_port = settings.getUDPPort()
    if len(sys.argv) > 2:
        udp_port = int(sys.argv[2])
    dht_service.init(dht_port)
    d = dht_service.connect()
    udp.listen(udp_port)

    def _go(live_nodes):
        A('start', udp_port)

    d.addCallback(_go)
    reactor.run()  # @UndefinedVariable
    settings.shutdown()
Ejemplo n.º 20
0
def main():
    if len(sys.argv) < 3:
        print(
            'usage: nickname_observer.py <"many"|"one"> <nickname> <attempts>')
        return
    from twisted.internet import reactor  # @UnresolvedImport
    lg.set_debug_level(24)
    settings.init()
    my_id.init()
    dht_service.init(settings.getDHTPort())

    def _result(result, nickname):
        print(result, nickname)
        if result == 'finished':
            reactor.stop()  # @UndefinedVariable

    if sys.argv[1] == 'many':
        observe_many(sys.argv[2], int(sys.argv[3]), results_callback=_result)
    else:
        find_one(sys.argv[2], int(sys.argv[3]), _result)
    reactor.run()  # @UndefinedVariable
    settings.shutdown()
Ejemplo n.º 21
0
    def test_signed_key(self):
        try:
            bpio.rmdir_recursive('/tmp/.bitdust_test_signed_key')
        except Exception:
            pass
        lg.set_debug_level(30)
        settings.init(base_dir='/tmp/.bitdust_test_signed_key')
        self.my_current_key = None
        try:
            os.makedirs('/tmp/.bitdust_test_signed_key/metadata/')
        except:
            pass
        fout = open('/tmp/_some_priv_key', 'w')
        fout.write(_some_priv_key)
        fout.close()
        fout = open(settings.LocalIdentityFilename(), 'w')
        fout.write(_some_identity_xml)
        fout.close()
        self.assertTrue(key.LoadMyKey(keyfilename='/tmp/_some_priv_key'))
        self.assertTrue(my_id.loadLocalIdentity())

        key_id = '[email protected]_8084'
        my_keys.erase_key(key_id, keys_folder='/tmp/')
        my_keys.register_key(key_id, _sample_private_key, keys_folder='/tmp/')
        is_valid = my_keys.validate_key(my_keys.key_obj(key_id))
        self.assertTrue(is_valid)
        my_keys.sign_key(key_id)
        signed_key_info = my_keys.get_key_info(key_id,
                                               include_private=True,
                                               include_signature=True)
        self.assertTrue(my_keys.verify_key_info_signature(signed_key_info))

        key.ForgetMyKey()
        my_id.forgetLocalIdentity()
        settings.shutdown()
        os.remove('/tmp/_some_priv_key')
        bpio.rmdir_recursive('/tmp/.bitdust_test_signed_key')
Ejemplo n.º 22
0
def main():
    bpio.init()
    settings.init()
    if len(sys.argv) > 1:
        web_port = int(sys.argv[1])
    else:
        web_port = settings.getIdServerWebPort()
    if len(sys.argv) > 2:
        tcp_port = int(sys.argv[2])
    else:
        tcp_port = settings.getIdServerTCPPort()
    lg.set_debug_level(20)
    lg.out(2, 'starting ID server ...')
    reactor.addSystemEventTrigger(
        'before',
        'shutdown',  # @UndefinedVariable
        A().automat,
        'shutdown')
    reactor.callWhenRunning(A, 'init',
                            (web_port, tcp_port))  # @UndefinedVariable
    reactor.callLater(0, A, 'start')  # @UndefinedVariable
    reactor.run()  # @UndefinedVariable
    settings.shutdown()
    lg.out(2, 'reactor stopped, EXIT')
Ejemplo n.º 23
0
def _test():
    import logging
    logging.basicConfig(level=logging.DEBUG)
    from twisted.internet import reactor  # @UnresolvedImport
    from twisted.internet.defer import setDebugging
    setDebugging(True)
    # from twisted.python import log as twisted_log
    # twisted_log.startLogging(sys.stdout)
    lg.set_debug_level(20)

    from main import settings
    settings.init()
    settings.update_proxy_settings()

    init()

    def _resp(src):
        print(src)
        reactor.stop()  # @UndefinedVariable

    immediatelyCaching(sys.argv[1]).addBoth(_resp)
    reactor.run()  # @UndefinedVariable
    shutdown()
    settings.shutdown()
Ejemplo n.º 24
0
def main():
    from twisted.internet import reactor  # @UnresolvedImport
    from dht import dht_service
    settings.init()
    lg.set_debug_level(30)
    dht_port = settings.getDHTPort()
    udp_port = settings.getUDPPort()
    if len(sys.argv) > 1:
        dht_port = int(sys.argv[1])
    if len(sys.argv) > 2:
        udp_port = int(sys.argv[2])
    dht_service.init(dht_port)
    dht_service.connect()
    udp.listen(udp_port)

    def _cb(result, typ, ip, details):
        print(result, typ, ip, details)
        A('shutdown')
        reactor.stop()  # @UndefinedVariable

    A('init', (udp_port))
    A('start', _cb)
    reactor.run()  # @UndefinedVariable
    settings.shutdown()
Ejemplo n.º 25
0
 def tearDown(self):
     settings.shutdown()
     bpio.rmdir_recursive('/tmp/.bitdust_tmp')
Ejemplo n.º 26
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)
Ejemplo n.º 27
0
def main():
    from system import bpio
    from storage import backup_tar

    bpio.init()
    settings.init()

    lg.set_debug_level(24)
    lg.life_begins()

    automat.LifeBegins(lg.when_life_begins())
    automat.OpenLogFile(settings.AutomatsLog())

    key.InitMyKey()
    my_id.init()

    sourcePath = sys.argv[1]
    compress_mode = 'none'  # 'gz'
    backupID = sys.argv[2]
    raid_worker.A('init')

    if bpio.pathIsDir(sourcePath):
        backupPipe = backup_tar.backuptardir_thread(sourcePath,
                                                    compress=compress_mode)
    else:
        backupPipe = backup_tar.backuptarfile_thread(sourcePath,
                                                     compress=compress_mode)

    def _bk_done(bid, result):
        from crypt import signed
        customer, remotePath = packetid.SplitPacketID(bid)
        try:
            os.mkdir(
                os.path.join(settings.getLocalBackupsDir(), customer,
                             remotePath + '.out'))
        except:
            pass
        for filename in os.listdir(
                os.path.join(settings.getLocalBackupsDir(), customer,
                             remotePath)):
            filepath = os.path.join(settings.getLocalBackupsDir(), customer,
                                    remotePath, filename)
            payld = 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(), customer,
                                       remotePath + '.out', filename)
            bpio.WriteBinaryFile(newfilepath, newpacket.Serialize())

    def _bk_closed(*args, **kwargs):
        # job.automat('fail')
        # del job
        reactor.stop()  # @UndefinedVariable

    def _bk_start():
        job = backup(backupID, backupPipe, blockSize=16 * 1024 * 1024)
        job.finishCallback = _bk_done  # lambda bid, result: _bk_done(bid, result, job)
        job.addStateChangedCallback(_bk_closed, oldstate=None, newstate='DONE')
        reactor.callLater(1, job.automat, 'start')  # @UndefinedVariable

    reactor.callLater(0, _bk_start)  # @UndefinedVariable
    reactor.run()  # @UndefinedVariable
    settings.shutdown()
Ejemplo n.º 28
0
        Signature = Sign(Hash(EncryptedData))
        packets.append((Data, len(Data), EncryptedSessionKey, EncryptedData, Signature))
        print('.', end=' ')
    print(time.time() - dt, 'seconds')

    dt = time.time()
    print('decrypt now')
    i = 0
    for Data, Length, EncryptedSessionKey, EncryptedData, Signature in packets:
        SessionKey = DecryptLocalPrivateKey(EncryptedSessionKey)
        paddedData = DecryptWithSessionKey(SessionKey, EncryptedData, session_key_type=SessionKeyType())
        newData = paddedData[:Length]
        if not VerifySignature(MyPublicKey(), Hash(EncryptedData), Signature):
            raise Exception()
        if newData != Data:
            raise Exception
        print('.', end=' ')
        # open(str(i), 'wb').write(EncryptedData)
        i += 1
    print(time.time() - dt, 'seconds')

#------------------------------------------------------------------------------

if __name__ == '__main__':
    bpio.init()
    lg.set_debug_level(18)
    settings.init()
    InitMyKey()
    SpeedTest()
    settings.shutdown()