Extend ungleich-cli to add create-vpn, delete-vpn

This commit is contained in:
ahmadbilalkhalid 2019-06-16 13:58:05 +05:00
parent 992bd6d11b
commit 2976e3a9fb
5 changed files with 121 additions and 15 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
__pycache__/ __pycache__/
.vscode/

18
Pipfile
View file

@ -1,17 +1,15 @@
[[source]] [[source]]
url = "https://pypi.python.org/simple"
verify_ssl = true
name = "pypi" name = "pypi"
url = "https://pypi.org/simple"
[packages] verify_ssl = true
requests = "*"
pyotp = "*"
[dev-packages] [dev-packages]
pep8 = "*"
[packages]
pyotp = "*"
requests = "*"
apixu-python = {git = "https://github.com/apixu/apixu-python.git",ref = "master"}
[requires] [requires]
python_version = "3.7" python_version = "3.7"
[packages.apixu]
git = "https://github.com/apixu/apixu-python.git"
ref = "master"

27
Pipfile.lock generated
View file

@ -1,7 +1,7 @@
{ {
"_meta": { "_meta": {
"hash": { "hash": {
"sha256": "ca9c2522bf07f03d1588afe76e9f6fc73bc1efec20d4f155d82b709efaf14a56" "sha256": "b35d443f952246f5828abb12b75c9f9d02f03d016921244c04538216f0944693"
}, },
"pipfile-spec": 6, "pipfile-spec": 6,
"requires": { "requires": {
@ -10,15 +10,15 @@
"sources": [ "sources": [
{ {
"name": "pypi", "name": "pypi",
"url": "https://pypi.python.org/simple", "url": "https://pypi.org/simple",
"verify_ssl": true "verify_ssl": true
} }
] ]
}, },
"default": { "default": {
"apixu": { "apixu-python": {
"git": "https://github.com/apixu/apixu-python.git", "git": "https://github.com/apixu/apixu-python.git",
"ref": "master" "ref": "4beb003c71c0213720e930350f46e5fa6af9ef12"
}, },
"certifi": { "certifi": {
"hashes": [ "hashes": [
@ -41,6 +41,14 @@
], ],
"version": "==2.8" "version": "==2.8"
}, },
"pyotp": {
"hashes": [
"sha256:1e3dc3d16919c4efac528d1dbecc17de1a97c4ecfdacb89d7726ed2c6645adff",
"sha256:be0ffeabddaa5ee53e7204e7740da842d070cf69168247a3d0c08541b84de602"
],
"index": "pypi",
"version": "==2.2.7"
},
"requests": { "requests": {
"hashes": [ "hashes": [
"sha256:11e007a8a2aa0323f5a921e9e6a2d7e4e67d9877e85773fba9ba6419025cbeb4", "sha256:11e007a8a2aa0323f5a921e9e6a2d7e4e67d9877e85773fba9ba6419025cbeb4",
@ -57,5 +65,14 @@
"version": "==1.25.3" "version": "==1.25.3"
} }
}, },
"develop": {} "develop": {
"pep8": {
"hashes": [
"sha256:b22cfae5db09833bb9bd7c8463b53e1a9c9b39f12e304a8d0bba729c501827ee",
"sha256:fe249b52e20498e59e0b5c5256aa52ee99fc295b26ec9eaa85776ffdb9fe6374"
],
"index": "pypi",
"version": "==1.7.1"
}
}
} }

View file

@ -8,9 +8,11 @@ from ungleich_account import Account_Create
from ungleich_weather import ungleichWeather from ungleich_weather import ungleichWeather
from ungleich_ssh_key import SSHKey from ungleich_ssh_key import SSHKey
from ungleich_config import Ungleich_Config from ungleich_config import Ungleich_Config
from ungleich_vpn import ungleichVPN
VERSION = "0.0.4" VERSION = "0.0.4"
class ungleichCLI(object): class ungleichCLI(object):
def __init__(self): def __init__(self):
self._init_parser() self._init_parser()
@ -22,6 +24,7 @@ class ungleichCLI(object):
config = Ungleich_Config(self.parser, self.parser_parents) config = Ungleich_Config(self.parser, self.parser_parents)
SSHKey(self.parser, self.parser_parents) SSHKey(self.parser, self.parser_parents)
ungleichWeather(self.parser, self.parser_parents) ungleichWeather(self.parser, self.parser_parents)
ungleichVPN(self.parser, self.parser_parents)
def _init_parser(self): def _init_parser(self):
self.parser = {} self.parser = {}

87
ungleich_vpn.py Normal file
View file

@ -0,0 +1,87 @@
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 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 e:
print(e)
return None
response = r.json()
print(response.get("message"))