#! /usr/bin/env python3 from __future__ import print_function import argparse import json import sys import vpngen def eprint(*args, **kwargs): print(*args, file=sys.stderr, **kwargs) def remove_vpn(vpng, vpn_name, force): return 0 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() if args.create: res = vpng.create_vpn(vpn_name) 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: res = vpng.create_client(vpn_name, client_name) 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()