Exemple #1
0
async def main():
    """
    Generate a bunch of networks on a ZeroTier controller node.
    """
    async with aiohttp.ClientSession() as session:
        ZT_API = get_token()
        client = ZeroTier(ZT_API, loop, session)

        try:
            await client.get_data('controller/network')
            print('{} networks found'.format(len(client.data)))
            net_list = client.data

            # delete all networks
            if net_list:
                print('Deleting networks')
                for net_id in net_list:
                    endpoint = 'controller/network/{}'.format(net_id)
                    await client.delete_thing(endpoint)

            # await client.get_data('network/{}'.format(my_id))
            # print(network.get('allowGlobal'))

            # get/display all available network data
            await client.get_data('controller/network')
            print('{} networks found'.format(len(client.data)))
            # pprint(client.data)

        except ZeroTierConnectionError as exc:
            print(str(exc))
            pass
Exemple #2
0
async def main():
    """
    Generate a bunch of networks on a ZeroTier controller node.
    """
    async with aiohttp.ClientSession() as session:
        ZT_API = get_token()
        client = ZeroTier(ZT_API, loop, session)

        try:
            await client.get_data('status')
            node_data = client.data
            ctlr_id = node_data.get('address')

            # create some networks
            endpoint = 'controller/network/{}'.format(ctlr_id + '______')
            for N in range(1000):
                net_name = name_generator()
                await client.set_value('name', net_name, endpoint)

            # await client.get_data('network/{}'.format(my_id))
            # print(network.get('allowGlobal'))

            # get/display all available network data
            await client.get_data('controller/network')
            print('{} networks found'.format(len(client.data)))
            # pprint(client.data)

        except ZeroTierConnectionError as exc:
            print(str(exc))
            pass
Exemple #3
0
async def main():
    """Example code to retrieve data from a ZeroTier node."""
    async with aiohttp.ClientSession() as session:
        ZT_API = get_token()
        client = ZeroTier(ZT_API, loop, session)

        try:
            # get status details of the local node
            await client.get_data('status')
            dump_json('status', client.data)
            status_data = load_json('status')
            if verbose:
                pprint(status_data)

            # get status details of the node peers
            await client.get_data('peer')
            dump_json('peer', client.data)
            peer_data = load_json('peer')
            if verbose:
                pprint(peer_data)

            # get/display all available network data
            await client.get_data('network')
            dump_json('network', client.data)
            net_data = load_json('network')
            if verbose:
                pprint(net_data)

        except ZeroTierConnectionError as exc:
            print(str(exc))
            pass
async def main():
    """Retrieve data from and manipulate a ZeroTier controller node."""
    async with aiohttp.ClientSession() as session:
        ZT_API = get_token()
        client = ZeroTier(ZT_API, loop, session)

        try:
            # get status details of the controller node
            await client.get_data('status')
            print('Controller ID:')
            # pprint(client.data)
            node_id = handle_node_status(client.data, cache)
            print(node_id)

            # get/display all available network data
            await get_network_object_ids(client)
            print('{} networks found'.format(len(client.data)))
            if len(client.data) == 0:
                await add_network_object(client, ctlr_id=node_id)
                await get_network_object_ids(client)
            net_list = client.data
            net_data = []
            for net_id in net_list:
                print(net_id)
                # Get details about each network
                await get_network_object_data(client, net_id)
                pprint(client.data)
                net_data.append(client.data)
                await get_network_object_ids(client, net_id)
                # pprint(client.data)
                print('{} members found'.format(len(client.data)))
                if len(client.data) == 0:
                    await add_network_object(client, net_id, exit_node)
                    await get_network_object_ids(client, net_id)
                member_dict = client.data
                for mbr_id in member_dict.keys():
                    await get_network_object_data(client, net_id, mbr_id)
                    pprint(client.data)

        except Exception as exc:
            # print(str(exc))
            raise exc
Exemple #5
0
async def main():
    """Retrieve data from and manipulate a ZeroTier controller node."""
    async with aiohttp.ClientSession() as session:
        ZT_API = get_token()
        client = ZeroTier(ZT_API, loop, session)

        try:
            # get status details of the controller node
            # await client.get_data('controller')
            # print('Controller status:')
            # pprint(client.data)
            # print(client.data.get('controller'))

            # get status details of the node peers
            await client.get_data('peer')
            # print('Peers found:')
            # pprint(client.data)
            peer_data = client.data
            peer_keys = find_keys(cache, 'peer')
            print('Returned peer keys: {}'.format(peer_keys))
            load_cache_by_type(cache, peer_data, 'peer')

            # get/display all available network data
            await client.get_data('controller/network')
            print('{} networks found'.format(len(client.data)))
            net_list = client.data
            net_data = []
            for net_id in net_list:
                # print(net_id)
                # Get details about each network
                await client.get_data('controller/network/{}'.format(net_id))
                # pprint(client.data)
                net_data.append(client.data)

            # load_cache_by_type(cache, net_data, 'net')
            # net_keys = find_keys(cache, 'net')
            # print('{} network keys found'.format(len(net_list)))
            # pprint(net_data)

        except Exception as exc:
            # print(str(exc))
            raise exc
Exemple #6
0
async def main():
    """Example code to retrieve data from a ZeroTier node."""
    async with aiohttp.ClientSession() as session:
        ZT_API = get_token()
        client = ZeroTier(ZT_API, loop, session)

        try:
            # get status details of the local node
            await client.get_data('status')
            print('Node status:')
            pprint(client.data)
            print(client.data.get('online'))

            # get status details of the node peers
            await client.get_data('peer')
            print('Peers found:')
            pprint(client.data)

            # get/display all available network data
            await client.get_data('network')
            print('Networks found:')
            pprint(client.data)
            # for network in client.data:
                # my_id = network.get('id')
                # print(my_id)
                # # Get details about each network
                # await client.get_data('network/{}'.format(my_id))
                # pprint(client.data)

                # # Set a toggle for an existing network
                # await client.set_value({'allowGlobal': True},
                                        # 'network/{}'.format(my_id))
                # await client.get_data('network/{}'.format(my_id))
                # print(network.get('allowGlobal'))

        except ZeroTierConnectionError as exc:
            print(str(exc))
            pass
Exemple #7
0
async def main():
    """State cache updater to retrieve data from a local ZeroTier node."""
    async with aiohttp.ClientSession() as session:
        ZT_API = get_token()
        client = ZeroTier(ZT_API, loop, session)

        try:
            logger.debug('{} node(s) in offline queue: {}'.format(
                len(off_q), list(off_q)))
            if len(off_q) > 0:
                drain_msg_queue(off_q, addr='127.0.0.1', method='offline')

            logger.debug('{} node(s) in wedged queue: {}'.format(
                len(wdg_q), list(wdg_q)))
            if len(wdg_q) > 0:
                drain_msg_queue(wdg_q, addr='127.0.0.1', method='wedged')

            logger.debug('{} node(s) in reg queue: {}'.format(
                len(reg_q), list(reg_q)))
            logger.debug('{} node(s) in wait queue: {}'.format(
                len(wait_q), list(wait_q)))
            manage_incoming_nodes(node_q, reg_q, wait_q)
            if len(reg_q) > 0:
                drain_msg_queue(reg_q, pub_q, addr='127.0.0.1')

            # get status details of the local node and update state
            await client.get_data('status')
            node_id = handle_node_status(client.data, cache)

            # get status details of the node peers
            await client.get_data('peer')
            peer_data = client.data
            logger.info('Found {} peers'.format(len(peer_data)))
            peer_keys = find_keys(cache, 'peer')
            logger.debug('Returned peer keys: {}'.format(peer_keys))
            load_cache_by_type(cache, peer_data, 'peer')

            num_leaves = 0
            peerStatus = get_peer_status(cache)
            for peer in peerStatus:
                if peer['role'] == 'LEAF':
                    if peer['identity'] not in reg_q:
                        if peer['identity'] not in node_q:
                            node_q.append(peer['identity'])
                            logger.debug('Adding LEAF node id: {}'.format(
                                peer['identity']))
                    populate_leaf_list(node_q, wait_q, tmp_q, peer)
                    num_leaves = num_leaves + 1
            if num_leaves == 0 and st.leaf_nodes != []:
                st.leaf_nodes = []
            if st.leaf_nodes != []:
                logger.debug('Found {} leaf node(s)'.format(num_leaves))
            logger.debug('{} node(s) in node queue: {}'.format(
                len(node_q), list(node_q)))

            logger.debug('{} node(s) in reg queue: {}'.format(
                len(reg_q), list(reg_q)))
            logger.debug('{} node(s) in wait queue: {}'.format(
                len(wait_q), list(wait_q)))
            manage_incoming_nodes(node_q, reg_q, wait_q)
            if len(reg_q) > 0:
                drain_msg_queue(reg_q, pub_q, addr='127.0.0.1')

            logger.debug('{} node(s) in node queue: {}'.format(
                len(node_q), list(node_q)))
            logger.debug('{} node(s) in pub queue: {}'.format(
                len(pub_q), list(pub_q)))
            logger.debug('{} node(s) in active queue: {}'.format(
                len(cfg_q), list(cfg_q)))

        except Exception as exc:
            logger.error('peerstate exception was: {}'.format(exc))
            raise exc
Exemple #8
0
async def main():
    """State cache updater to retrieve data from a local ZeroTier node."""
    async with aiohttp.ClientSession() as session:
        ZT_API = get_token()
        client = ZeroTier(ZT_API, loop, session)
        nsState = AttrDict.from_nested_dict(st.fpnState)
        net_wait = st.wait_cache

        try:
            # get status details of the local node and update state
            await client.get_data('status')
            node_id = handle_node_status(client.data, cache)

            if NODE_SETTINGS['mode'] == 'peer':
                # get status details of the node peers
                await client.get_data('peer')
                peer_data = client.data
                logger.info('Found {} peers'.format(len(peer_data)))
                peer_keys = find_keys(cache, 'peer')
                logger.debug('Returned peer keys: {}'.format(peer_keys))
                load_cache_by_type(cache, peer_data, 'peer')

                # check for moon data (only exists for moons we orbit)
                if not nsState.moon_id0:
                    moon_data = run_ztcli_cmd(action='listmoons')
                    if moon_data:
                        load_cache_by_type(cache, moon_data, 'moon')

                    moonStatus = []
                    fpn_moons = NODE_SETTINGS['moon_list']
                    peerStatus = get_peer_status(cache)
                    for peer in peerStatus:
                        if peer['role'] == 'MOON' and peer['identity'] in fpn_moons:
                            moonStatus.append(peer)
                            break
                    logger.debug('Got moon state: {}'.format(moonStatus))
                    load_cache_by_type(cache, moonStatus, 'mstate')

            # get all available network data
            await client.get_data('network')
            net_data = client.data
            logger.info('Found {} networks'.format(len(net_data)))

            if NODE_SETTINGS['mode'] == 'peer':
                wait_for_nets = net_wait.get('offline_wait')
                if len(net_data) == 0 and not nsState.cfg_ref:
                    send_cfg_handler()
                    put_state_msg('WAITING')
                elif len(net_data) == 0 and nsState.cfg_ref and not wait_for_nets:
                    put_state_msg('ERROR')

            net_keys = find_keys(cache, 'net')
            logger.debug('Returned network keys: {}'.format(net_keys))
            load_cache_by_type(cache, net_data, 'net')

            netStatus = get_net_status(cache)
            logger.debug('Got net state: {}'.format(netStatus))
            load_cache_by_type(cache, netStatus, 'istate')

            if NODE_SETTINGS['mode'] == 'peer':
                # check for reconfiguration events
                for net in netStatus:
                    if net['status'] == 'NOT_FOUND' or net['status'] == 'ACCESS_DENIED':
                        # if net['ztaddress'] != net['gateway']:
                        #     do_net_cmd(get_net_cmds(NODE_SETTINGS['home_dir'], 'fpn0'))
                        run_ztcli_cmd(action='leave', extra=net['identity'])
                        net_id_handler(None, net['identity'], old=True)
                        st.fpnState['cfg_ref'] = None
                        net_wait.set('offline_wait', True, 75)
                if len(net_data) < 2 and not nsState.cfg_ref:
                    send_cfg_handler()
                    put_state_msg('WAITING')

                # check the state of exit network/route
                exit_id = get_ztnwid('fpn0', 'fpn_id0', nsState)
                if exit_id is not None:
                    for net in netStatus:
                        if net['identity'] == exit_id:
                            ztaddr = net['ztaddress']
                            break
                    exit_state, _, _ = do_peer_check(ztaddr)
                    logger.debug('HEALTH: peer state is {}'.format(exit_state))

                    wait_for_nets = net_wait.get('offline_wait')
                    logger.debug('HEALTH: network route state is {}'.format(nsState.route))
                    if nsState.route is False:
                        if not st.fpnState['wdg_ref'] and not wait_for_nets:
                            # logger.error('HEALTH: net_health state is {}'.format(nsState.route))
                            reply = send_wedged_msg()
                            if 'result' in reply[0]:
                                st.fpnState['wdg_ref'] = True
                            logger.error('HEALTH: network is unreachable!!')
                            put_state_msg('ERROR')
                    else:
                        logger.debug('HEALTH: wait_for_nets is {}'.format(wait_for_nets))

            elif NODE_SETTINGS['mode'] == 'adhoc':
                if not NODE_SETTINGS['nwid']:
                    logger.warning('ADHOC: network ID not set {}'.format(NODE_SETTINGS['nwid']))
                else:
                    logger.debug('ADHOC: found network ID {}'.format(NODE_SETTINGS['nwid']))
                if netStatus != []:
                    nwid = netStatus[0]['identity']
                    addr = netStatus[0]['ztaddress']
                    nwstat = netStatus[0]['status']
                    logger.debug('ADHOC: found network with ID {}'.format(nwid))
                    logger.debug('ADHOC: network status is {}'.format(nwstat))
                    if addr:
                        res = do_peer_check(addr)

                # elif NODE_SETTINGS['nwid']:
                #     run_ztcli_cmd(action='join', extra=NODE_SETTINGS['nwid'])

        except Exception as exc:
            logger.error('nodestate exception was: {}'.format(exc))
            raise exc
Exemple #9
0
async def main():
    """State cache updater to retrieve data from a local ZeroTier node."""
    async with aiohttp.ClientSession() as session:
        ZT_API = get_token()
        client = ZeroTier(ZT_API, loop, session)

        try:
            # handle offline/wedged nodes
            handle_wedged_nodes(ct.net_trie, wdg_q, off_q)
            pre_off = list(off_q)
            logger.debug('{} nodes in offline queue: {}'.format(
                len(pre_off), pre_off))
            for node_id in pre_off:
                await offline_mbr_node(client, node_id)
            for node_id in [x for x in off_q if x in pre_off]:
                off_q.remove(node_id)
            logger.debug('{} nodes in offline queue: {}'.format(
                len(off_q), list(off_q)))

            # get ID and status details of ctlr node
            await client.get_data('status')
            ctlr_id = handle_node_status(client.data, cache)

            # update ctlr state tries
            await update_state_tries(client, ct.net_trie, ct.id_trie)
            logger.debug('net_trie has keys: {}'.format(list(ct.net_trie)))
            # for key in list(ct.net_trie):
            #     logger.debug('net key {} has paylod: {}'.format(key, ct.net_trie[key]))
            # for key in list(ct.id_trie):
            #     logger.debug('id key {} has payload: {}'.format(key, ct.id_trie[key]))
            logger.debug('id_trie has keys: {}'.format(list(ct.id_trie)))

            # handle node queues and publish messages
            logger.debug('{} nodes in node queue: {}'.format(
                len(node_q), list(node_q)))
            if len(node_q) > 0:
                handle_node_queues(node_q, staging_q)
                logger.debug('{} nodes in node queue: {}'.format(
                    len(node_q), list(node_q)))
            logger.debug('{} nodes in staging queue: {}'.format(
                len(staging_q), list(staging_q)))

            for mbr_id in [x for x in staging_q if x not in list(ct.id_trie)]:
                if is_exit_node(mbr_id):
                    await bootstrap_mbr_node(client,
                                             ctlr_id,
                                             mbr_id,
                                             netobj_q,
                                             ex=True)
                else:
                    await bootstrap_mbr_node(client, ctlr_id, mbr_id, netobj_q)
                st.wait_cache.set(mbr_id, True, 90)
                publish_cfg_msg(ct.id_trie, mbr_id, addr='127.0.0.1')

            for mbr_id in [x for x in staging_q if x in list(ct.id_trie)]:
                publish_cfg_msg(ct.id_trie, mbr_id, addr='127.0.0.1')
                staging_q.remove(mbr_id)
            logger.debug('{} nodes in staging queue: {}'.format(
                len(staging_q), list(staging_q)))

            # refresh ctlr state tries again
            await update_state_tries(client, ct.net_trie, ct.id_trie)

            node_list = get_active_nodes(ct.id_trie)
            logger.debug('{} nodes in node_list: {}'.format(
                len(node_list), node_list))
            if len(node_list) > 0:
                boot_list = get_bootstrap_list(ct.net_trie, ct.id_trie)
                logger.debug('{} nodes in boot_list: {}'.format(
                    len(boot_list), boot_list))

                if len(boot_list) != 0:
                    await close_mbr_net(client,
                                        node_list,
                                        boot_list,
                                        min_nodes=3)
                else:
                    await unwrap_mbr_net(client,
                                         node_list,
                                         boot_list,
                                         min_nodes=3)

        except Exception as exc:
            logger.error('netstate exception was: {}'.format(exc))
            await cleanup_orphans(client)
            if list(ct.net_trie) == [] and list(ct.id_trie) != []:
                ct.id_trie.clear()
            raise exc