Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add config file support #42

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 32 additions & 10 deletions vpn_slice/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from enum import Enum
from itertools import chain
from ipaddress import ip_network, ip_address, IPv4Address, IPv4Network, IPv6Address, IPv6Network, IPv6Interface
from time import sleep
from time import sleep, time
from random import randint, choice, shuffle

try:
Expand Down Expand Up @@ -60,6 +60,13 @@ def net_or_host_param(s):
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)
else: first, rest = host, None
Expand Down Expand Up @@ -337,8 +344,9 @@ def parse_env(environ=os.environ):

# Parse command-line arguments and environment
def parse_args_and_env(args=None, environ=os.environ):
p = argparse.ArgumentParser()
p = argparse.ArgumentParser(fromfile_prefix_chars='@')
p.add_argument('routes', nargs='*', type=net_or_host_param, help='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.')
p.add_argument('-c', '--config', 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 = p.add_argument_group('Subprocess options')
p.add_argument('-k','--kill', default=[], action='append', help='File containing PID to kill before disconnect (may be specified multiple times)')
p.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)')
Expand All @@ -365,6 +373,16 @@ def parse_args_and_env(args=None, environ=os.environ):
env = parse_env(environ)
return p, args, env

def parse_routes_from_list(args, routes):
for x in routes:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parameter x need to parse by net_or_host_param() before checking.
Otherwise it will not handle the subnet and alias cases.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please check 2f0b464

if isinstance(x, (IPv4Network, IPv6Network)):
args.subnets.append(x)
elif isinstance(x, str):
args.hosts.append(x)
else:
hosts, ip = x
args.aliases.setdefault(ip, []).extend(hosts)

def finalize_args_and_env(args, env):
global providers

Expand All @@ -388,14 +406,18 @@ def finalize_args_and_env(args, env):
args.exc_subnets = []
args.hosts = []
args.aliases = {}
for x in args.routes:
if isinstance(x, (IPv4Network, IPv6Network)):
args.subnets.append(x)
elif isinstance(x, str):
args.hosts.append(x)
else:
hosts, ip = x
args.aliases.setdefault(ip, []).extend(hosts)
parse_routes_from_list(args, args.routes)
begin_time = time()
if args.config is not None:
print("got config file: {0}".format(args.config))
with open(args.config, 'r') as file:
routes_from_file = []
for line in file:
routes_from_file.append(net_or_host_param(line.rstrip()))
parse_routes_from_list(args, routes_from_file)
end_time = time()
print("elapsed time: {0} s; number lines: {1}".format(end_time-begin_time, len(args.subnets) + len(args.hosts)))

if args.route_internal:
if env.network: args.subnets.append(env.network)
if env.network6: args.subnets.append(env.network6)
Expand Down