ungleich-cli/ungleich_vpn.py

87 lines
3.5 KiB
Python
Executable file

import argparse
import requests
import json
class ungleichVPN(object):
def __init__(self, parser, parents):
self.parser = parser
self.parser['vpn-create'] = self.parser['sub'].add_parser(
'vpn-create',
help="Create VPN",
parents=[parents])
self.parser['vpn-delete'] = self.parser['sub'].add_parser(
'vpn-delete',
help="Delete VPN",
parents=[parents])
self.parser['vpn-create'].set_defaults(func=ungleichVPN.create_vpn)
self.parser['vpn-create'].add_argument("--server_url", required=True,
dest="server_url")
self.parser['vpn-create'].add_argument("--name", required=True,
dest="name")
self.parser['vpn-create'].add_argument("--realm", required=True,
dest="realm")
self.parser['vpn-create'].add_argument("--seed", required=True,
dest="seed")
# email argument would be deleted once we can
# get email from OTP
self.parser['vpn-create'].add_argument("--email", required=True,
dest="email")
self.parser['vpn-create'].add_argument("--public_key", required=True,
dest="public_key")
self.parser['vpn-delete'].set_defaults(func=ungleichVPN.delete_vpn)
self.parser['vpn-delete'].add_argument("--server_url", required=True,
dest="server_url")
self.parser['vpn-delete'].add_argument("--name", required=True,
dest="name")
self.parser['vpn-delete'].add_argument("--realm", required=True,
dest="realm")
self.parser['vpn-delete'].add_argument("--seed", required=True,
dest="seed")
def create_vpn(args):
try:
# email argument from the below request would
# be deleted once we can get email from OTP
r = requests.post(args.server_url + "/create",
data={'name': args.name,
'realm': args.realm,
'seed': args.seed,
'email': args.email,
'public_key': args.public_key
})
except Exception as e:
print(e)
return None
if r.status_code == 200:
response = r.content.decode("utf-8")
with open("wg0.conf", "w") as f:
f.write(response)
print("""VPN successfully created. VPN Configuration \
file is saved as wg0.conf""")
else:
response = r.json()
print(response.get("message"))
def delete_vpn(args):
try:
# email argument from the below request would
# be deleted once we can get email from OTP
r = requests.post(args.server_url + "/delete",
data={'name': args.name,
'realm': args.realm,
'seed': args.seed
})
except Exception as e:
print(e)
return None
response = r.json()
print(response.get("message"))