選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

vpngen.py 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. from enum import Enum
  2. import os
  3. import os.path
  4. import re
  5. import shutil
  6. from subprocess import call
  7. class VpnGenError(Enum):
  8. Success = 0,
  9. VpnAlreadyExists = 1,
  10. VpnDoesNotExists = 2,
  11. ClientAlreadyExists = 3,
  12. ClientDoesNotExists = 4
  13. class VpnGen:
  14. default_config_base_dir = ""
  15. default_config_file = ""
  16. ovpn_config_path = ""
  17. def __init__(self, default_config_path, ovpn_config_path):
  18. self.default_config_base_dir = os.path.abspath(default_config_path)
  19. self.default_config_file = "%s.conf" % self.default_config_base_dir
  20. self.ovpn_config_path = os.path.abspath(ovpn_config_path)
  21. def f7(self, seq):
  22. seen = set()
  23. seen_add = seen.add
  24. return [x for x in seq if not (x in seen or seen_add(x))]
  25. def _find_vars(self, content):
  26. variables = re.findall('\$\{([^}]+)}', content)
  27. variables = self.f7(variables)
  28. return variables
  29. def get_vpn_vars(self):
  30. with open(self.default_config_file, "r") as f:
  31. default_config = f.read()
  32. variables = self._find_vars(default_config)
  33. variables += ["KEY_COUNTRY", "KEY_PROVINCE", "KEY_CITY", "KEY_ORG", "KEY_EMAIL"]
  34. return variables
  35. def create_vpn(self, vpn_name, variables):
  36. base_dir = "%s%s%s" % (self.ovpn_config_path, os.sep, vpn_name)
  37. conf_file = "%s.conf" % base_dir
  38. if os.path.exists(base_dir) or os.path.exists(conf_file):
  39. return VpnGenError.VpnAlreadyExists
  40. with open(self.default_config_file, "r") as f:
  41. default_config = f.read()
  42. variables['name'] = vpn_name
  43. for variable in variables:
  44. default_config = default_config.replace("${%s}" % variable, variables[variable])
  45. os.makedirs(base_dir)
  46. with open(conf_file, "w") as f:
  47. f.write(default_config)
  48. os.rmdir(base_dir)
  49. shutil.copytree(self.default_config_base_dir, base_dir)
  50. curdir = os.curdir
  51. easyrsadir = base_dir + os.sep + "easy-rsa" + os.sep
  52. pkitool = easyrsadir + "pkitool"
  53. os.chdir(easyrsadir)
  54. os.environ["KEY_COUNTRY"] = variables['KEY_COUNTRY']
  55. os.environ["KEY_PROVINCE"] = variables['KEY_PROVINCE']
  56. os.environ["KEY_CITY"] = variables['KEY_CITY']
  57. os.environ["KEY_ORG"] = variables['KEY_ORG']
  58. os.environ["KEY_OU"] = variables['KEY_ORG']
  59. os.environ["KEY_CN"] = variables['KEY_ORG']
  60. os.environ["KEY_NAME"] = variables['KEY_ORG']
  61. os.environ["KEY_EMAIL"] = variables['KEY_EMAIL']
  62. os.environ["KEY_SIZE"] = variables['KEY_SIZE']
  63. os.environ["CA_EXPIRE"] = variables['CA_EXPIRE']
  64. os.environ["KEY_EXPIRE"] = variables['KEY_EXPIRE']
  65. os.environ["EASY_RSA"] = easyrsadir
  66. os.environ["OPENSSL"] = "openssl"
  67. os.environ["PKCS11TOOL"] = "pkcs11-tool"
  68. os.environ["GREP"] = "grep"
  69. os.environ["KEY_CONFIG"] = easyrsadir + "openssl.cnf"
  70. os.environ["KEY_DIR"] = easyrsadir + "keys"
  71. os.environ["PKCS11_MODULE_PATH"] = "dummy"
  72. os.environ["PKCS11_PIN"] = "dummy"
  73. call(["./clean-all"])
  74. call([pkitool, "--initca"])
  75. call([pkitool, "server"])
  76. call(["./build-dh"])
  77. os.chdir(curdir)
  78. return VpnGenError.Success
  79. def remove_vpn(self, vpn_name):
  80. base_dir = "%s%s%s" % (self.ovpn_config_path, os.sep, vpn_name)
  81. conf_file = "%s.conf" % base_dir
  82. if not os.path.exists(base_dir) and not os.path.exists(conf_file):
  83. return VpnGenError.VpnDoesNotExists
  84. os.remove(conf_file)
  85. shutil.rmtree(base_dir)
  86. return VpnGenError.Success
  87. def create_client(self, vpn_name, client_name, variables):
  88. return VpnGenError.ClientDoesNotExists
  89. def remove_client(self, vpn_name, client_name):
  90. return VpnGenError.ClientDoesNotExists
  91. def rebuild_clients(self, vpn_name):
  92. return VpnGenError.ClientDoesNotExists