-
Notifications
You must be signed in to change notification settings - Fork 2
/
wifirst-autoconnect.py
169 lines (122 loc) · 4.66 KB
/
wifirst-autoconnect.py
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Copyright (C) 2020 Lancelot H. (Azuxul)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import requests
import credentials
from bs4 import BeautifulSoup
LOGIN_HOST = "https://smartcampus.wifirst.net/"
LOGIN_PAGE_URL = "https://smartcampus.wifirst.net/sessions/new"
USER = credentials.LOGIN
PASSWORD = credentials.PASSWORD
session = requests.Session()
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36",
"Content-Type" : "application/x-www-form-urlencoded",
}
def getUserLoginInfo():
rep = session.get(LOGIN_PAGE_URL, headers=headers)
sigin = BeautifulSoup(rep.text, features="html.parser").find("form", {"id" : "signin-form"})
if sigin:
token_input = sigin.find("input", {"name" : "authenticity_token"})
if token_input:
return sigin["action"], token_input["value"]
def extractData(response):
data = BeautifulSoup(response.text, features="html.parser").find("form", {"name" : "log"})
if data:
URL = data["action"]
inputs = data.findAll("input")
login_data = []
if len(inputs) > 0:
for field in inputs:
name = field["name"]
value = field["value"]
login_data.append([name, value])
return URL, login_data
def getInternalLogin():
logInfo = getUserLoginInfo()
url = LOGIN_HOST + logInfo[0] if logInfo[0].startswith("/") else logInfo[0]
data = {
"utf8" : "✓",
"authenticity_token" : logInfo[1],
"login" : USER,
"password" : PASSWORD
}
rep = session.post(url, data=data, headers=headers)
if rep.status_code == 200:
new_url = BeautifulSoup(rep.text, features="html.parser").find("meta", {"http-equiv" : "refresh"})
if new_url:
start_index = new_url["content"].find("URL=") + 4
new_url = new_url["content"][start_index:]
rep = session.post(new_url, headers=headers)
if rep.status_code == 200:
return extractData(rep)
def login(withInternalLogin = True):
if withInternalLogin:
data = credentials.INTERNAL_LOGIN
else:
data = getInternalLogin()
post = {}
for elem in data[1]:
post[elem[0]] = elem[1]
rep = requests.post(data[0], data=post, headers=headers)
if rep.history[-1].url == post["success_url"]:
return True
import sys, getopt
def main(argv):
password = None
username = None
dump = False
try:
opts, args = getopt.getopt(argv,"adsu:p:")
except getopt.GetoptError:
print('wifirst-autoconnect.py')
print('-u <username> -p <password>')
print('-a Use direct connexion with saved info in credentials.py')
print('-s Use password and username in credentials.py')
print('-d Dump connexion info to save it into credentials.py')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('wifirst-autoconnect.py')
print('-u <username> -p <password>')
print('-a Use direct connexion with saved info in credentials.py')
print('-s Use password and username in credentials.py')
print('-d Dump connexion info to save it into credentials.py')
sys.exit()
elif opt == '-u':
username = arg
elif opt == '-p':
password = arg
elif opt == '-d':
dump = True
elif opt == '-a':
print('Start connexion with direct connexion info')
login()
return
elif opt == '-s':
print('Start connexion with saved username and password')
login(False)
return
if dump:
print(getInternalLogin())
return
if password is not None and username is not None:
PASSWORD = password
USER = username
print('Start connexion')
login(False)
return
if __name__ == "__main__":
main(sys.argv[1:])