-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeepalive.py
More file actions
125 lines (105 loc) · 3.73 KB
/
keepalive.py
File metadata and controls
125 lines (105 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/python3
import base64
import datetime
import getpass
import random
import time
from time import sleep
from urllib.parse import urlencode, quote_plus
import requests
VCLOUD_ORG = "Defsec"
# https://vcloud.ialab.dsu.edu/tenant/DefSec/vdcs/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/vapp/vapp-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/vcd-vapp-vms
VAPP_IDS = [
"vapp-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", # Some example vapp
]
PLAIN_USERNAME: str = input("Username:")
PLAIN_PASSWORD: str = getpass.getpass("Password:")
# This is not good encryption, but it makes it significantly more difficult to grab password.
# You are now unlikely to get it if you just strings the memory of the program.
PWD_XOR_KEY: bytes = random.randbytes(len(PLAIN_PASSWORD))
DSU_PASSWORD: list[int] = [ord(a) ^ PWD_XOR_KEY[i] for i, a in enumerate(PLAIN_PASSWORD)]
USER_XOR_KEY: bytes = random.randbytes(len(PLAIN_USERNAME))
DSU_USERNAME: list[int] = [ord(a) ^ USER_XOR_KEY[i] for i, a in enumerate(PLAIN_USERNAME)]
del PLAIN_PASSWORD
del PLAIN_USERNAME
FIRST_RUN: bool = True
LAST = 0
print("Initializing...")
def do_auth() -> bool:
usr = "".join(chr(a ^ USER_XOR_KEY[i]) for i, a in enumerate(DSU_USERNAME))
pwd = "".join(chr(a ^ PWD_XOR_KEY[i]) for i, a in enumerate(DSU_PASSWORD))
# Get internet
try:
requests.post(
"https://captiveportal.ialab.dsu.edu:6082/php/uid.php?vsys=1&rule=2",
data=urlencode(
{
"inputStr": "",
"escapeUser": "",
"preauthid": "",
"user": usr,
"passwd": pwd,
"ok": "Login",
},
quote_via=quote_plus,
),
)
except requests.exceptions.ConnectionError:
print("Could not auth with captive portal")
del pwd
del usr
return False
sleep(5)
# Authenticate
try:
resp = requests.post(
url="https://vcloud.ialab.dsu.edu/api/sessions",
headers={
"Accept": "application/*+xml;version=37.2;",
"Authorization": "Basic {}".format(
base64.b64encode(("%s@%s:%s" % (usr, VCLOUD_ORG, pwd)).encode()).decode("utf-8")
),
},
)
except requests.exceptions.RequestException:
print("Could not connect to vcloud to reauthenticate")
return False
finally:
del pwd
del usr
try:
auth = resp.headers["x-vcloud-authorization"]
except KeyError:
print("Invalid Auth token from ialab")
return False
# Refresh lease
for vapp in VAPP_IDS:
try:
resp = requests.put(
"https://vcloud.ialab.dsu.edu/api/vApp/{}/leaseSettingsSection/".format(vapp),
# json={"deploymentLeaseInSeconds": "172800", "storageLeaseInSeconds": "0", "_type": "LeaseSettingsSectionType"},
json={"_type": "LeaseSettingsSectionType"},
headers={
"Content-Type": "application/*+json",
"Accept": "application/*+json;version=37.2;",
"x-vcloud-authorization": auth,
},
)
if resp.status_code != 202:
print(f"Could not refresh vapp: {vapp}")
except requests.exceptions.RequestException:
print(f"Could not refresh vapp: {vapp}")
return False
del auth
del resp
return True
if do_auth():
print("Auth successful. Autorenew started!")
else:
print("Auth failed!")
exit()
while True:
LAST = datetime.datetime.now()
do_auth()
next_run = datetime.datetime.now() + datetime.timedelta(hours=12)
time.sleep((next_run - LAST).seconds)