123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- #! /usr/bin/env python3
-
- from __future__ import print_function
- import argparse
- import json
- import os
- import sys
- import vpngen
-
-
- def eprint(*args, **kwargs):
- print(*args, file=sys.stderr, **kwargs)
-
-
- def create_variables(variables, defaults):
- variables_set = defaults.copy()
- for variable in variables:
- if variable == 'name' or variable == 'client':
- continue
- default = variables_set[variable] if variable in variables_set else ''
- print("Enter a value for '%s' [%s]: " % (variable, default), end='', flush=True)
- value = sys.stdin.readline()[:-1]
- if value != '':
- variables_set[variable] = value
- return variables_set
-
-
- def main():
- parser = argparse.ArgumentParser(description='Manage OpenVPN VPNs')
- parser.add_argument('--vpn', help='The VPN to use', required=True)
- parser.add_argument('--config', dest='config', default='/etc/vpngen/vpngen.json', help='Configuration file path')
-
- parser.add_argument('--create', help='Create a VPN', action='store_true')
- parser.add_argument('--remove', help='Remove a VPN', action='store_true')
- parser.add_argument('--create-client', help='Create a client for the VPN', metavar='CLIENT')
- parser.add_argument('--remove-client', help='Remove a client for the VPN', metavar='CLIENT')
- parser.add_argument('--rebuild-clients', help='Rebuild clients configurations', action='store_true')
-
- args = parser.parse_args()
-
- with open(args.config, "r") as f:
- config = json.load(f)
-
- vpn_name = config['vpnPrefix'] + args.vpn + config['vpnSuffix']
- if args.create_client is not None:
- client_name = args.create_client
- elif args.remove_client is not None:
- client_name = args.remove_client
- else:
- client_name = None
- if client_name is not None:
- client_name = config['clientPrefix'] + client_name + config['clientSuffix']
-
- vpng = vpngen.VpnGen(config['defaultConfigPath'], config['ovpnConfigPath'])
-
- config_path = vpng.get_vpn_variables_path(vpn_name)
- if os.path.exists(config_path):
- with open(config_path, "r") as f:
- data = json.load(f)
- config['defaults'].update(data['variables'])
-
- if args.create:
- variables = create_variables(vpng.get_vpn_vars(), config['defaults'])
- res = vpng.create_vpn(vpn_name, variables)
- if res == vpngen.VpnGenError.Success:
- print("VPN %s created successfully" % vpn_name)
- else:
- eprint("Failed to create VPN %s: %s" % (vpn_name, res))
- exit(1)
- elif args.remove:
- res = vpng.remove_vpn(vpn_name)
- if res == vpngen.VpnGenError.Success:
- print("VPN %s removed successfully" % vpn_name)
- else:
- eprint("Failed to remove VPN %s: %s" % (vpn_name, res))
- exit(1)
- elif args.create_client:
- variables = create_variables(vpng.get_client_vars(vpn_name), config['defaults'])
- if variables is None:
- res = vpngen.VpnGenError.VpnDoesNotExists
- else:
- res = vpng.create_client(vpn_name, client_name, variables)
- if res == vpngen.VpnGenError.Success:
- print("Client %s created successfully on VPN %s" % (client_name, vpn_name))
- else:
- eprint("Failed to create client %s on VPN %s: %s" % (client_name, vpn_name, res))
- exit(1)
- elif args.remove_client:
- res = vpng.remove_client(vpn_name, client_name)
- if res == vpngen.VpnGenError.Success:
- print("Client %s removed successfully on VPN %s" % (client_name, vpn_name))
- else:
- eprint("Failed to remove client %s on VPN %s: %s" % (client_name, vpn_name, res))
- exit(1)
- elif args.rebuild_clients:
- res = vpng.rebuild_clients(vpn_name)
- if res == vpngen.VpnGenError.Success:
- print("Clients configurations rebuilt successfully on VPN %s" % vpn_name)
- else:
- eprint("Failed to rebuild clients configuration on VPN %s: %s" % (vpn_name, res))
- exit(1)
-
-
- main()
|