summaryrefslogtreecommitdiff
path: root/scripts/simplevpn.py
blob: 94adb36298ad0866711b382c376237a9266ecd67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python3
import argparse
import os, sys

import yaml

from jinja2 import Template

AUTH_METHODS = ["anon", "sip"]


class EIPConfig:
    def __init__(self):
        self.openvpn = dict()
        self.locations = dict()
        self.gateways = dict()
        self.provider = dict()
        self.auth = ""


def parseConfig(provider_config):
    with open(provider_config) as conf:
        config = yaml.load(conf.read())
    eip = EIPConfig()
    eip.openvpn.update(yamlListToDict(config['openvpn']))
    configureAuth(eip, config)

    for loc in config['locations']:
        eip.locations.update(yamlIdListToDict(loc))
    for gw in config['gateways']:
        eip.gateways.update(yamlIdListToDict(gw))
    eip.provider.update(yamlListToDict(config['provider']))
    return eip

def configureAuth(eip, config):
    auth = config.get('auth', 'anon')
    if auth not in AUTH_METHODS:
        print("ERROR: unknown auth method", auth)
        sys.exit(1)
    eip.auth = auth

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['iatMode'] = "0"
            options.append(opts)
    return config


def dictToStr(d):
    for k, v in d.items():
        d[k] = str(v)
    return d


def produceEipConfig(config, obfs4_state, template):
    config = parseConfig(os.path.abspath(config))

    if obfs4_state:
        obfs4_cert = open(
            obfs4_state + '/obfs4_cert.txt').read().rstrip()
    else:
        obfs4_cert = None
    patchObfs4Cert(config, obfs4_cert)

    t = Template(open(template).read())

    print(t.render(
        locations=config.locations,
        gateways=config.gateways,
        openvpn=dictToStr(config.openvpn),
        auth=config.auth))


def produceProviderConfig(config, template):
    config = parseConfig(os.path.abspath(config))
    t = Template(open(template).read())
    print(t.render(
        provider=config.provider))


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("-f", "--file")
    parser.add_argument("-c", "--config")
    parser.add_argument("-t", "--template")
    parser.add_argument("--obfs4_state")
    args = parser.parse_args()

    if args.file == "eip":
        produceEipConfig(args.config, args.obfs4_state,  args.template)
    elif args.file == "provider":
        produceProviderConfig(args.config, args.template)
    else:
        print("unknown type of file:", args.file)