From 1c72e34eecc69288b9211081400f4375b78b47e7 Mon Sep 17 00:00:00 2001 From: Max Moser Date: Sat, 3 Feb 2018 17:42:04 +0100 Subject: [PATCH] Add script that parses the interesting parts from a wireguard config b/c C string handling is hard --- src/readconfig.py | 68 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100755 src/readconfig.py diff --git a/src/readconfig.py b/src/readconfig.py new file mode 100755 index 0000000..fb68dd5 --- /dev/null +++ b/src/readconfig.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python3 +"""Carve the interesting parts out of a wireguard config file and print them to stdout such that it's easier to read them in C""" + +import argparse + +def is_ip4(string): + """Check if the given string is in IPv4 format""" + if string.count(".") == 3: + for s in string.split("."): + try: + int(s) + except: + return False + return True + + return False + + +def is_ip6(string): + """Check if the given string is in IPv6 format""" + if ":" in string: + return True + + return False + + +def parse_ips(string): + """Parse the IP4s and IP6s out of a comma-separated list of IPs""" + ip4 = [] + ip6 = [] + + ips = [s.strip() for s in string.split(",")] + for ip in ips: + if is_ip4(ip): + ip4.append(ip) + elif is_ip6(ip): + ip6.append(ip) + + return ip4, ip6 + + +parser = argparse.ArgumentParser() +parser.add_argument("config", metavar="CONFIG-FILE", help="The path of the configuration file") + +args = parser.parse_args() + +with open(args.config, "r") as config_file: + lines = config_file.readlines() + + local_section = False + for line in lines: + line = line.strip() + + if "[Interface]" in line: + local_section = True + + if line and not line.startswith("#"): + if line.startswith("Address"): + # this is the line where the local addresses are defined + split = line.split("=") + ip4, ip6 = parse_ips(split[1]) + + print("IPv4") + for ip in ip4: + print(ip) + print("IPv6") + for ip in ip6: + print(ip)