Beispiel #1
0
def wg_route_create(devname, localaddr, remoteaddr, routes):
    """Add a list of routes, via a remote tunnel address, on a give device
    """
    for route in routes:
        netdev.route_add(route,
                         devname=devname,
                         via=remoteaddr,
                         src=localaddr,
                         route_scope='global')
Beispiel #2
0
def _init_networks(state_dir, cidrs):
    """Initializes a CIDR for usage by the Warpgate server.
    """
    vips_path = os.path.join(state_dir, 'vips')
    sessions_path = os.path.join(state_dir, 'sessions')
    fs.mkdir_safe(sessions_path)

    # Clean up old sessions
    for old_session in os.listdir(sessions_path):
        _LOGGER.info('Cleaning up old session %r', old_session)
        os.unlink(os.path.join(sessions_path, old_session))

    # Clean up old interfaces
    for old_devname in _utils.wg_dev_list():
        _LOGGER.info('Cleaning up old device %r', old_devname)
        _utils.wg_dev_delete(old_devname)

    networks = {}
    for cidr in cidrs:
        _LOGGER.debug('Setting up %r', cidr)
        pool = vipfile.VipMgr(
            cidr=cidr,
            path=vips_path,
            owner_path=sessions_path
        )
        pool.initialize()
        wg_ip = pool.alloc('self')  # XXX: Point to self
        # blackhole all the managed network ranges
        try:
            netdev.route_add(cidr, rtype='blackhole')
        except netdev.subproc.CalledProcessError as err:
            if err.returncode == 2:  # route already exists
                pass
            else:
                raise
        networks[cidr] = {
            'gateway_ip': wg_ip,
            'pool': pool
        }

    return networks
Beispiel #3
0
    def test_route_add(self):
        """Test route definition.
        """
        netdev.route_add('1.2.3.4', via='bar')

        treadmill.subproc.check_call.assert_called_with([
            'ip',
            'route',
            'add',
            'unicast',
            '1.2.3.4',
            'via',
            'bar',
        ], )
        treadmill.subproc.check_call.reset_mock()

        netdev.route_add('1.2.3.4', devname='foo_dev')

        treadmill.subproc.check_call.assert_called_with([
            'ip',
            'route',
            'add',
            'unicast',
            '1.2.3.4',
            'dev',
            'foo_dev',
        ], )
        treadmill.subproc.check_call.reset_mock()

        netdev.route_add('1.2.3.4', via='bar', src='baz', route_scope='local')

        treadmill.subproc.check_call.assert_called_with([
            'ip',
            'route',
            'add',
            'unicast',
            '1.2.3.4',
            'via',
            'bar',
            'src',
            'baz',
            'scope',
            'local',
        ], )