From 64a0ff75248541a5ea054937734b8e5380175f0f Mon Sep 17 00:00:00 2001 From: Urho Tamminen Date: Thu, 20 Apr 2023 14:46:13 +0300 Subject: [PATCH] Add support for reading hosts from a file New parameter: --routes-file or -f Syntax is one host per line. Single-line or postfix comments are handled and ignored. For example: 1.2.3.4 host1.example.com # This is one more comment host2.example.com --- vpn_slice/__main__.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/vpn_slice/__main__.py b/vpn_slice/__main__.py index 04673c5..06cbacc 100755 --- a/vpn_slice/__main__.py +++ b/vpn_slice/__main__.py @@ -94,6 +94,11 @@ def net_or_host_param(s): except ValueError: return s +def file_path_param(path): + if isinstance(path, str) and os.path.exists(path): + return path + else: + raise ValueError("path not valid") def names_for(host, domains, short=True, long=True): if '.' in host: first, rest = host.split('.', 1) @@ -109,6 +114,22 @@ def names_for(host, domains, short=True, long=True): elif rest in domains: names.append(first) return names +def read_routes_file(filename): + hosts = [] + with open(filename) as f: + lines = [x.strip() for x in f.readlines()] + for line in lines: + # ignore empty lines + if not len(line.strip()): + continue + # ignore comment lines + if line.strip().startswith('#'): + continue + # ignore comments at the end of the line + host = line.split('#', 1)[0].strip() + hosts.append(host) + return hosts + ######################################## def do_pre_init(env, args): @@ -444,6 +465,7 @@ def parse_args_and_env(args=None, environ=os.environ): p = argparse.ArgumentParser() p.add_argument('routes', nargs='*', type=net_or_host_param, help='List of VPN-internal hostnames, included subnets (e.g. 192.168.0.0/24), excluded subnets (e.g. %%8.0.0.0/8), or aliases (e.g. host1=192.168.1.2) to add to routing and /etc/hosts.') g = p.add_argument_group('Subprocess options') + p.add_argument('-f', '--routes-file', default=None, type=file_path_param, help='Path to List of VPN-internal hostnames, subnets (e.g. 192.168.0.0/24), or aliases (e.g. host1=192.168.1.2) to add to routing and /etc/hosts.') g.add_argument('-k', '--kill', default=[], action='append', help='File containing PID to kill before disconnect (may be specified multiple times)') g.add_argument('-K', '--prevent-idle-timeout', action='store_true', help='Prevent idle timeout by doing random DNS lookups (interval set by $IDLE_TIMEOUT, defaulting to 10 minutes)') g = p.add_argument_group('Informational options') @@ -487,6 +509,14 @@ def parse_args_and_env(args=None, environ=os.environ): args.exc_subnets = [] args.hosts = [] args.aliases = {} + + # read routes from a file + if args.routes_file: + args.routes.extend([ + net_or_host_param(x) + for x in read_routes_file(args.routes_file) + ]) + for x in args.routes: if isinstance(x, str): args.hosts.append(x)