Ejemplo n.º 1
0
def safe_stun(udp_port=None, dht_port=None, ):
    from twisted.internet.defer import Deferred
    result = Deferred()
    try:
        settings.init()
        dht_port = dht_port or settings.getDHTPort()
        udp_port = udp_port or settings.getUDPPort()
        dht_service.init(dht_port)
        dht_service.connect()
        udp.listen(udp_port)

        def _cb(cod, typ, ip, details):
            # A('shutdown')
            result.callback({
                'result': cod,  # 'stun-success' or 'stun-failed'
                'type': typ,
                'ip': ip,
                'details': details,
            })

        A('init', (udp_port))
        A('start', _cb)
    except Exception as exc:
        lg.exc()
        result.callback(dict(ip='127.0.0.1', errors=[str(exc), ]))
        return result
    return result
Ejemplo n.º 2
0
def connected(nodes, seeds=[]):
    print('connected:', nodes, seeds)
    if options.layer != 0:
        dht_service.connect(seeds,
                            layer_id=options.layer).addBoth(layer_connected)
    else:
        run()
Ejemplo n.º 3
0
 def start(self):
     from dht import dht_service
     from main import settings
     from main.config import conf
     dht_service.init(settings.getDHTPort(), settings.DHTDBFile())
     dht_service.connect()
     conf().addCallback('services/entangled-dht/udp-port',
                        self._on_udp_port_modified)
     return True
 def start(self):
     from dht import dht_service
     from main import settings
     from main.config import conf
     dht_service.init(settings.getDHTPort(), settings.DHTDBFile())
     dht_service.connect()
     conf().addCallback('services/entangled-dht/udp-port',
                        self._on_udp_port_modified)
     return True
Ejemplo n.º 5
0
def main():
    from twisted.internet import reactor
    lg.set_debug_level(24)
    bpio.init()
    settings.init()
    dht_service.init(settings.getDHTPort())
    dht_service.connect()
    udp.listen(settings.getUDPPort())
    A('start', settings.getUDPPort())
    reactor.run()
Ejemplo n.º 6
0
def main():
    from twisted.internet import reactor
    lg.set_debug_level(24)
    bpio.init()
    settings.init()
    dht_service.init(settings.getDHTPort())
    dht_service.connect()
    udp.listen(settings.getUDPPort())
    A('start', settings.getUDPPort())
    reactor.run()
Ejemplo n.º 7
0
def main():
    settings.init()

    lg.set_debug_level(12)

    dht_service.init(udp_port=14441, db_file_path=settings.DHTDBFile())

    seeds = []

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

    dht_service.connect(seeds).addBoth(run)
    reactor.run()
Ejemplo n.º 8
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()
 def start(self):
     from twisted.internet.defer import Deferred, succeed
     from logs import lg
     from dht import dht_records
     from dht import dht_service
     from dht import known_nodes
     from main import settings
     from main.config import conf
     conf().addConfigNotifier('services/entangled-dht/udp-port', self._on_udp_port_modified)
     known_seeds = known_nodes.nodes()
     dht_layers = list(dht_records.LAYERS_REGISTRY.keys())
     dht_service.init(
         udp_port=settings.getDHTPort(),
         dht_dir_path=settings.ServiceDir('service_entangled_dht'),
         open_layers=dht_layers,
     )
     lg.info('DHT known seed nodes are : %r   DHT layers are : %r' % (known_seeds, dht_layers, ))
     self.starting_deferred = Deferred()
     d = dht_service.connect(
         seed_nodes=known_seeds,
         layer_id=0,
         attach=True,
     )
     d.addCallback(self._on_connected)
     d.addErrback(self._on_connect_failed)
     return self.starting_deferred or succeed(True)
Ejemplo n.º 10
0
def main():
    from twisted.internet import reactor
    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()
    A('init', (udp_port))
    A('start', _cb)
    reactor.run()
Ejemplo n.º 11
0
 def start(self):
     from dht import dht_service
     from dht import known_nodes
     from main import settings
     from main.config import conf
     conf().addCallback('services/entangled-dht/udp-port', self._on_udp_port_modified)
     dht_service.init(udp_port=settings.getDHTPort(), db_file_path=settings.DHTDBFile())
     known_seeds = known_nodes.default_nodes()
     d = dht_service.connect(seed_nodes=known_seeds)
     d.addCallback(self._on_connected)
     d.addErrback(self._on_connect_failed)
     return d
Ejemplo n.º 12
0
 def _do_connect_proxy_routers_dht_layer(self):
     from logs import lg
     from dht import dht_service
     from dht import dht_records
     from dht import known_nodes
     known_seeds = known_nodes.nodes()
     d = dht_service.connect(
         seed_nodes=known_seeds,
         layer_id=dht_records.LAYER_PROXY_ROUTERS,
         attach=True,
     )
     d.addCallback(self._on_dht_proxy_routers_layer_connected)
     d.addErrback(lambda *args: lg.err(str(args)))
Ejemplo n.º 13
0
def main():
    from twisted.internet import reactor  # @UnresolvedImport
    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
Ejemplo n.º 14
0
def main():
    from twisted.internet import reactor
    lg.set_debug_level(24)
    bpio.init()
    settings.init()
    dht_service.init(settings.getDHTPort())
    d = dht_service.connect()
    udp.listen(settings.getUDPPort())

    def _go(live_nodes):
        A('start', settings.getUDPPort())

    d.addCallback(_go)

    reactor.run()
Ejemplo n.º 15
0
def safe_stun(udp_port=None, dht_port=None, result_defer=None):
    from twisted.internet.defer import Deferred
    result = result_defer or Deferred()

    if driver.is_started('service_entangled_dht'):
        if dht_service.node()._joinDeferred and not dht_service.node()._joinDeferred.called:
            dht_service.node()._joinDeferred.addCallback(lambda ok: safe_stun(udp_port=udp_port, dht_port=dht_port, result_defer=result))
            dht_service.node()._joinDeferred.addErrback(result.errback)
            return result

    if not driver.is_on('service_entangled_dht'):
        result.errback(Exception('service_entangled_dht() is not started'))
        return result
    
    try:
        settings.init()
        dht_port = dht_port or settings.getDHTPort()
        udp_port = udp_port or settings.getUDPPort()
        if dht_port:
            dht_service.init(dht_port)
        d = dht_service.connect()
        if udp_port:
            udp.listen(udp_port)

        def _cb(cod, typ, ip, details):
            # A('shutdown')
            result.callback({
                'result': cod,  # 'stun-success' or 'stun-failed'
                'type': typ,
                'ip': ip,
                'details': details,
            })

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

        d.addCallback(_go)
        d.addErrback(lambda err: result.callback(dict(ip='127.0.0.1', errors=[str(err), ])))

    except Exception as exc:
        lg.exc()
        result.callback(dict(ip='127.0.0.1', errors=[str(exc), ]))
        return result
    return result
Ejemplo n.º 16
0
 def start(self):
     from logs import lg
     from dht import dht_service
     from dht import known_nodes
     from main import settings
     from main.config import conf
     from userid import my_id
     conf().addCallback('services/entangled-dht/udp-port',
                        self._on_udp_port_modified)
     dht_service.init(udp_port=settings.getDHTPort(),
                      db_file_path=settings.DHTDBFile())
     known_seeds = known_nodes.nodes()
     lg.info('known seed nodes are : %r' % known_seeds)
     d = dht_service.connect(seed_nodes=known_seeds)
     d.addCallback(self._on_connected)
     d.addErrback(self._on_connect_failed)
     if my_id.getLocalID():
         dht_service.set_node_data('idurl', my_id.getLocalID())
     return d
Ejemplo n.º 17
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.º 18
0
def udp_dht_stun(udp_port=None, dht_port=None, result_defer=None):
    if not driver.is_on('service_my_ip_port'):
        if _Debug:
            lg.out(
                _DebugLevel,
                'stun_client.udp_dht_stun   SKIP because service_my_ip_port() is not started'
            )
        if result_defer:
            result_defer.errback(
                Exception('service_my_ip_port() is not started'))
        return False

    from dht import dht_service

    if dht_service.node().connectingTask(
    ) and not dht_service.node().connectingTask().called:
        if _Debug:
            lg.out(
                _DebugLevel,
                'stun_client.udp_dht_stun   SKIP and run later because dht_service is still joining the network'
            )
        dht_service.node().connectingTask(
        ).addCallback(lambda ok: udp_dht_stun(
            udp_port=udp_port, dht_port=dht_port, result_defer=result_defer))
        if result_defer:
            dht_service.node().connectingTask().addErrback(
                result_defer.errback)
        return True

    dht_port = dht_port or settings.getDHTPort()
    udp_port = udp_port or settings.getUDPPort()
    if dht_port:
        dht_service.init(dht_port)
    d = dht_service.connect()
    if udp_port:
        udp.listen(udp_port)

    def _cb(cod, typ, ip, details):
        # A('shutdown')
        ret = {
            'result': cod,  # 'stun-success' or 'stun-failed'
            'type': typ,
            'ip': ip,
            'details': details,
        }
        if _Debug:
            lg.out(_DebugLevel, 'stun_client.udp_dht_stun   result : %r' % ret)
        result_defer.callback(ret)
        return None

    def _go(live_nodes):
        if _Debug:
            lg.out(_DebugLevel,
                   'stun_client.udp_dht_stun   GO with nodes: %r' % live_nodes)
        A('init', udp_port)
        A('start', _cb)
        return live_nodes

    d.addCallback(_go)
    if result_defer:
        d.addErrback(result_defer.errback)
        # d.addErrback(lambda err: result_defer.callback(dict(ip='127.0.0.1', errors=[str(err), ])))
    return True