diff options
author | Kali Kaneko <kali@leap.se (leap communications)> | 2019-07-08 18:27:11 +0200 |
---|---|---|
committer | Kali Kaneko <kali@leap.se (leap communications)> | 2019-07-08 18:27:11 +0200 |
commit | 8d1f2ceadac382c6ea42ee3e29633cf99de1a58d (patch) | |
tree | b3b32da98ea2c27aa58fa17bf0a596bf5714b477 /scripts/simplevpn.py | |
parent | 638ff1ddac9d45ad442e78bd698a39fa3469eb6d (diff) |
generate eip-service.json
Diffstat (limited to 'scripts/simplevpn.py')
-rwxr-xr-x | scripts/simplevpn.py | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/scripts/simplevpn.py b/scripts/simplevpn.py new file mode 100755 index 0000000..7906e7c --- /dev/null +++ b/scripts/simplevpn.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python3 +import argparse +import os + +import yaml + +from jinja2 import Template + + +class EIPConfig: + def __init__(self): + self.openvpn = dict() + self.locations = dict() + self.gateways = dict() + self.obfs4_cert = "" + + +def parseConfig(provider_config): + with open(provider_config) as conf: + config = yaml.load(conf.read()) + eip = EIPConfig() + eip.openvpn.update(yamlListToDict(config['openvpn'])) + + for loc in config['locations']: + eip.locations.update(yamlIdListToDict(loc)) + for gw in config['gateways']: + eip.gateways.update(yamlIdListToDict(gw)) + return eip + + +def yamlListToDict(values): + vals = {} + for d in values: + for k, v in d.items(): + vals[k] = v + return vals + + +def yamlIdListToDict(data): + _d = {} + for identifier, values in data.items(): + _d[identifier] = yamlListToDict(values) + return _d + + +def patchObfs4Cert(config, cert): + for gw in config.gateways: + for options in config.gateways[gw]['transports']: + opts = {} + transport, _, _ = options + if transport == "obfs4": + opts['cert'] = cert + opts['iat-mode'] = 0 + options.append(opts) + return config + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument("provider_config") + parser.add_argument("eip_template") + parser.add_argument("--obfs4_state") + args = parser.parse_args() + + config = parseConfig(os.path.abspath(args.provider_config)) + + if args.obfs4_state: + obfs4_cert = open( + args.obfs4_state + '/obfs4_cert.txt').read().rstrip() + else: + obfs4_cert = None + patchObfs4Cert(config, obfs4_cert) + + t = Template(open(args.eip_template).read()) + + print(t.render( + locations=config.locations, + gateways=config.gateways, + openvpn=config.openvpn)) |