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 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 != '':
  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-clients', help='Rebuild clients configurations', action='store_true')
  30. args = parser.parse_args()
  31. with open(args.config, "r") as f:
  32. config = json.load(f)
  33. vpn_name = config['vpnPrefix'] + args.vpn + config['vpnSuffix']
  34. if args.create_client is not None:
  35. client_name = args.create_client
  36. elif args.remove_client is not None:
  37. client_name = args.remove_client
  38. else:
  39. client_name = None
  40. if client_name is not None:
  41. client_name = config['clientPrefix'] + client_name + config['clientSuffix']
  42. vpng = vpngen.VpnGen(config['defaultConfigPath'], config['ovpnConfigPath'])
  43. config_path = vpng.get_vpn_variables_path(vpn_name)
  44. if os.path.exists(config_path):
  45. with open(config_path, "r") as f:
  46. data = json.load(f)
  47. config['defaults'].update(data['variables'])
  48. if args.create:
  49. variables = create_variables(vpng.get_vpn_vars(), config['defaults'])
  50. res = vpng.create_vpn(vpn_name, variables)
  51. if res == vpngen.VpnGenError.Success:
  52. print("VPN %s created successfully" % vpn_name)
  53. else:
  54. eprint("Failed to create VPN %s: %s" % (vpn_name, res))
  55. exit(1)
  56. elif args.remove:
  57. res = vpng.remove_vpn(vpn_name)
  58. if res == vpngen.VpnGenError.Success:
  59. print("VPN %s removed successfully" % vpn_name)
  60. else:
  61. eprint("Failed to remove VPN %s: %s" % (vpn_name, res))
  62. exit(1)
  63. elif args.create_client:
  64. variables = create_variables(vpng.get_client_vars(vpn_name), config['defaults'])
  65. if variables is None:
  66. res = vpngen.VpnGenError.VpnDoesNotExists
  67. else:
  68. res = vpng.create_client(vpn_name, client_name, variables)
  69. if res == vpngen.VpnGenError.Success:
  70. print("Client %s created successfully on VPN %s" % (client_name, vpn_name))
  71. else:
  72. eprint("Failed to create client %s on VPN %s: %s" % (client_name, vpn_name, res))
  73. exit(1)
  74. elif args.remove_client:
  75. res = vpng.remove_client(vpn_name, client_name)
  76. if res == vpngen.VpnGenError.Success:
  77. print("Client %s removed successfully on VPN %s" % (client_name, vpn_name))
  78. else:
  79. eprint("Failed to remove client %s on VPN %s: %s" % (client_name, vpn_name, res))
  80. exit(1)
  81. elif args.rebuild_clients:
  82. res = vpng.rebuild_clients(vpn_name)
  83. if res == vpngen.VpnGenError.Success:
  84. print("Clients configurations rebuilt successfully on VPN %s" % vpn_name)
  85. else:
  86. eprint("Failed to rebuild clients configuration on VPN %s: %s" % (vpn_name, res))
  87. exit(1)
  88. main()