-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
113 lines (94 loc) · 3.58 KB
/
main.py
File metadata and controls
113 lines (94 loc) · 3.58 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
# written by cobbdzon@github.com
# TIP wifi automatic connector (omygyat)
# hopefully this aint illegal cuh
import requests
import time
import os
from dotenv import load_dotenv
import termcolor
load_dotenv()
LOGIN_URL = "https://172.30.0.7:8443/login"
INTERNET_CHECK_TIMEOUT = float(os.getenv("INTERNET_CHECK_TIMEOUT") or 5)
LOGIN_INTERVAL = float(os.getenv("WIFI_LOGIN_INTERVAL") or 2) # in seconds
DATA = {
"username": os.getenv("WIFI_USERNAME"),
"password": os.getenv("WIFI_PASSWORD"),
"RedirectUrl": "",
"anonymous": "DISABLE",
"anonymousurl": "",
"accesscode": "",
"accesscode1": "DISABLE",
"checkbox": "on",
"checkbox1": "on"
}
HEADERS = {
"Content-Type": "application/x-www-form-urlencoded"
}
requests.packages.urllib3.disable_warnings(requests.packages.urllib3.exceptions.InsecureRequestWarning)
def isConnectedToTIPWifi():
try:
response = requests.get("https://172.30.0.7:8443/index.html", timeout=5, verify=False)
return True
except requests.ConnectionError:
return False
def hasInternetConnection():
try:
response = requests.get("https://www.speedtest.net", timeout=5)
return True
except requests.Timeout:
return False
except requests.ConnectionError:
return False
def dataToRawForm():
requestString = ""
i = 0
for key in DATA:
i = i + 1
suffix = ""
if i < len(DATA):
suffix = "&"
requestString = requestString + key + "=" + DATA[key] + suffix
return requestString
def requestLogin():
response = requests.post(LOGIN_URL, data=dataToRawForm(), headers=HEADERS, verify=False)
if response.status_code == 200:
print(termcolor.colored("Connected to wifi sucessfully!", "green"))
elif response.status_code == 404:
print(termcolor.colored("May already be connected to the wifi, checking status...", "yellow"))
if hasInternetConnection():
print(termcolor.colored("Status: Connected to internet", "green"))
else:
print(termcolor.colored("Status: Not connected to the internet", "red"))
elif response.status_code == 401:
print(termcolor.colored("Login details is wrong, please check if correct", "red"))
print(termcolor.colored("Welcome to Wi-Fi auto-login, starting loop...", "magenta"))
lastConnectedToTIP = None
lastInternetStatus = None
firstInternetCheck = True
lastStatusCode = 0
while True:
connectedToTIP = isConnectedToTIPWifi()
if lastConnectedToTIP != connectedToTIP:
if connectedToTIP:
print(termcolor.colored("Connected to TIP Wifi!", "green"))
else:
print(termcolor.colored("Not connected to TIP Wifi! Waiting until connected...", "yellow"))
if connectedToTIP:
if firstInternetCheck:
print(termcolor.colored("First time checking internet connection...", "yellow"))
internetConnected = hasInternetConnection()
internetStatusChanged = lastInternetStatus != internetConnected
if internetConnected:
if firstInternetCheck:
print(termcolor.colored("Already connected to the internet", "green"))
else:
if internetStatusChanged:
print(termcolor.colored("Lost connection, attempting login...", "yellow"))
else:
print(termcolor.colored("No connection, attempting login...", "yellow"))
requestLogin()
firstInternetCheck = False
lastInternetStatus = internetConnected
lastConnectedToTIP = connectedToTIP
if LOGIN_INTERVAL > 0:
time.sleep(LOGIN_INTERVAL)