123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- from enum import Enum
- import os
- import os.path
- import re
- import shutil
- from subprocess import call
-
-
- class VpnGenError(Enum):
- Success = 0,
- VpnAlreadyExists = 1,
- VpnDoesNotExists = 2,
- ClientAlreadyExists = 3,
- ClientDoesNotExists = 4
-
-
- class VpnGen:
- default_config_base_dir = ""
- default_config_file = ""
- ovpn_config_path = ""
-
- def __init__(self, default_config_path, ovpn_config_path):
- self.default_config_base_dir = os.path.abspath(default_config_path)
- self.default_config_file = "%s.conf" % self.default_config_base_dir
- self.ovpn_config_path = os.path.abspath(ovpn_config_path)
-
- def f7(self, seq):
- seen = set()
- seen_add = seen.add
- return [x for x in seq if not (x in seen or seen_add(x))]
-
- def _find_vars(self, content):
- variables = re.findall('\$\{([^}]+)}', content)
- variables = self.f7(variables)
- return variables
-
- def get_vpn_vars(self):
- with open(self.default_config_file, "r") as f:
- default_config = f.read()
- variables = self._find_vars(default_config)
- variables += ["KEY_COUNTRY", "KEY_PROVINCE", "KEY_CITY", "KEY_ORG", "KEY_EMAIL"]
- return variables
-
- def create_vpn(self, vpn_name, variables):
- base_dir = "%s%s%s" % (self.ovpn_config_path, os.sep, vpn_name)
- conf_file = "%s.conf" % base_dir
- if os.path.exists(base_dir) or os.path.exists(conf_file):
- return VpnGenError.VpnAlreadyExists
-
- with open(self.default_config_file, "r") as f:
- default_config = f.read()
-
- variables['name'] = vpn_name
- for variable in variables:
- default_config = default_config.replace("${%s}" % variable, variables[variable])
-
- os.makedirs(base_dir)
- with open(conf_file, "w") as f:
- f.write(default_config)
-
- os.rmdir(base_dir)
- shutil.copytree(self.default_config_base_dir, base_dir)
-
- curdir = os.curdir
- easyrsadir = base_dir + os.sep + "easy-rsa" + os.sep
- pkitool = easyrsadir + "pkitool"
- os.chdir(easyrsadir)
-
- os.environ["KEY_COUNTRY"] = variables['KEY_COUNTRY']
- os.environ["KEY_PROVINCE"] = variables['KEY_PROVINCE']
- os.environ["KEY_CITY"] = variables['KEY_CITY']
- os.environ["KEY_ORG"] = variables['KEY_ORG']
- os.environ["KEY_OU"] = variables['KEY_ORG']
- os.environ["KEY_CN"] = variables['KEY_ORG']
- os.environ["KEY_NAME"] = variables['KEY_ORG']
- os.environ["KEY_EMAIL"] = variables['KEY_EMAIL']
- os.environ["KEY_SIZE"] = variables['KEY_SIZE']
- os.environ["CA_EXPIRE"] = variables['CA_EXPIRE']
- os.environ["KEY_EXPIRE"] = variables['KEY_EXPIRE']
-
- os.environ["EASY_RSA"] = easyrsadir
- os.environ["OPENSSL"] = "openssl"
- os.environ["PKCS11TOOL"] = "pkcs11-tool"
- os.environ["GREP"] = "grep"
- os.environ["KEY_CONFIG"] = easyrsadir + "openssl.cnf"
- os.environ["KEY_DIR"] = easyrsadir + "keys"
- os.environ["PKCS11_MODULE_PATH"] = "dummy"
- os.environ["PKCS11_PIN"] = "dummy"
-
- call(["./clean-all"])
- call([pkitool, "--initca"])
- call([pkitool, "server"])
- call(["./build-dh"])
-
- os.chdir(curdir)
-
- return VpnGenError.Success
-
- def remove_vpn(self, vpn_name):
- base_dir = "%s%s%s" % (self.ovpn_config_path, os.sep, vpn_name)
- conf_file = "%s.conf" % base_dir
- if not os.path.exists(base_dir) and not os.path.exists(conf_file):
- return VpnGenError.VpnDoesNotExists
- os.remove(conf_file)
- shutil.rmtree(base_dir)
- return VpnGenError.Success
-
- def create_client(self, vpn_name, client_name, variables):
- return VpnGenError.ClientDoesNotExists
-
- def remove_client(self, vpn_name, client_name):
- return VpnGenError.ClientDoesNotExists
-
- def rebuild_clients(self, vpn_name):
- return VpnGenError.ClientDoesNotExists
|