You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vpngen-cli.py 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #! /usr/bin/env python3
  2. from __future__ import print_function
  3. import argparse
  4. import json
  5. import os
  6. import sys
  7. import vpngen
  8. def eprint(*args, **kwargs):
  9. print(*args, file=sys.stderr, **kwargs)
  10. def create_variables(variables, defaults):
  11. variables_set = defaults.copy()
  12. for variable in variables:
  13. if variable == 'name' or variable == 'client':
  14. continue
  15. default = variables_set[variable] if variable in variables_set else ''
  16. print("Enter a value for '%s' [%s]: " % (variable, default), end='', flush=True)
  17. value = sys.stdin.readline()[:-1]
  18. if value != '' or variable not in variables_set:
  19. variables_set[variable] = value
  20. return variables_set
  21. def main():
  22. parser = argparse.ArgumentParser(description='Manage OpenVPN VPNs')
  23. parser.add_argument('--vpn', help='The VPN to use', required=True)
  24. parser.add_argument('--config', dest='config', default='/etc/vpngen/vpngen.json', help='Configuration file path')
  25. parser.add_argument('--create', help='Create a VPN', action='store_true')
  26. parser.add_argument('--remove', help='Remove a VPN', action='store_true')
  27. parser.add_argument('--create-client', help='Create a client for the VPN', metavar='CLIENT')
  28. parser.add_argument('--remove-client', help='Remove a client for the VPN', metavar='CLIENT')
  29. parser.add_argument('--rebuild-client', help='Rebuild a client configuration', metavar='CLIENT')
  30. parser.add_argument('--rebuild-clients', help='Rebuild clients configurations', action='store_true')
  31. args = parser.parse_args()
  32. with open(args.config, "r") as f:
  33. config = json.load(f)
  34. vpn_name = config['vpnPrefix'] + args.vpn + config['vpnSuffix']
  35. if args.create_client is not None:
  36. client_name = args.create_client
  37. elif args.remove_client is not None:
  38. client_name = args.remove_client
  39. elif args.rebuild_client is not None:
  40. client_name = args.rebuild_client
  41. else:
  42. client_name = None
  43. if client_name is not None:
  44. client_name = config['clientPrefix'] + client_name + config['clientSuffix']
  45. vpng = vpngen.VpnGen(config['defaultConfigPath'], config['ovpnConfigPath'])
  46. config_path = vpng.get_vpn_variables_path(vpn_name)
  47. if os.path.exists(config_path):
  48. with open(config_path, "r") as f:
  49. data = json.load(f)
  50. config['defaults'].update(data['variables'])
  51. if args.create:
  52. default_variables = config['defaults'].copy()
  53. variables = create_variables(vpng.get_vpn_vars(), default_variables)
  54. res = vpng.create_vpn(vpn_name, variables)
  55. if res == vpngen.VpnGenError.Success:
  56. print("VPN %s created successfully" % vpn_name)
  57. else:
  58. eprint("Failed to create VPN %s: %s" % (vpn_name, res))
  59. exit(1)
  60. elif args.remove:
  61. res = vpng.remove_vpn(vpn_name)
  62. if res == vpngen.VpnGenError.Success:
  63. print("VPN %s removed successfully" % vpn_name)
  64. else:
  65. eprint("Failed to remove VPN %s: %s" % (vpn_name, res))
  66. exit(1)
  67. elif args.create_client:
  68. default_variables = config['defaults'].copy()
  69. default_variables.update(vpng.get_server_variables(vpn_name))
  70. variables = create_variables(vpng.get_client_vars(vpn_name), default_variables)
  71. if variables is None:
  72. res = vpngen.VpnGenError.VpnDoesNotExists
  73. else:
  74. res = vpng.create_client(vpn_name, client_name, variables)
  75. if res == vpngen.VpnGenError.Success:
  76. print("Client %s created successfully on VPN %s" % (client_name, vpn_name))
  77. else:
  78. eprint("Failed to create client %s on VPN %s: %s" % (client_name, vpn_name, res))
  79. exit(1)
  80. elif args.remove_client:
  81. res = vpng.remove_client(vpn_name, client_name)
  82. if res == vpngen.VpnGenError.Success:
  83. print("Client %s removed successfully on VPN %s" % (client_name, vpn_name))
  84. else:
  85. eprint("Failed to remove client %s on VPN %s: %s" % (client_name, vpn_name, res))
  86. exit(1)
  87. elif args.rebuild_client:
  88. default_variables = config['defaults'].copy()
  89. default_variables.update(vpng.get_server_variables(vpn_name))
  90. default_variables.update(vpng.get_client_variables(vpn_name, client_name))
  91. res = vpng.rebuild_client(vpn_name, client_name, default_variables)
  92. if res == vpngen.VpnGenError.Success:
  93. print("Client %s configuration rebuilt successfully on VPN %s" % (client_name, vpn_name))
  94. else:
  95. eprint("Failed to rebuild client %s configuration on VPN %s: %s" % (client_name, vpn_name, res))
  96. exit(1)
  97. elif args.rebuild_clients:
  98. default_variables = config['defaults'].copy()
  99. default_variables.update(vpng.get_server_variables(vpn_name))
  100. res = vpng.rebuild_clients(vpn_name)
  101. if res == vpngen.VpnGenError.Success:
  102. print("Clients configurations rebuilt successfully on VPN %s" % vpn_name)
  103. else:
  104. eprint("Failed to rebuild clients configuration on VPN %s: %s" % (vpn_name, res))
  105. exit(1)
  106. main()