1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- from enum import Enum
- import os
- import os.path
- import re
- import shutil
-
-
- 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 = default_config_path
- self.default_config_file = "%s.conf" % default_config_path
- self.ovpn_config_path = 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)
-
-
-
- return VpnGenError.Success
-
- def remove_vpn(self, vpn_name):
- return VpnGenError.ClientDoesNotExists
-
- 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
|