-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathauto.py
331 lines (284 loc) · 12.2 KB
/
auto.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# -*- coding:utf-8 -*-
import requests
import urllib
#model name: pycryptodome
from Crypto.Cipher import AES
import base64
from Crypto.Util.Padding import pad
from bs4 import BeautifulSoup
import argparse
#Settings aera
#登陆模式
auth_mode = "PASSWORD" #1:PASSWORD 2:COOKIES
#统一身份认证账号密码,仅在“PASSWORD”认证模式下需要
stu_id = ""
stu_passwd = ""
#app.nwu.edu.cn认证Cookies,仅在“COOKIES”认证模式下需要
#可以通过浏览器直接获取
stu_varify_cookies = {
"UUkey":"",
"eai-sess":""
}
#调试信息开关
debug_mode = False
#是否在账号密码登陆成功后输出app.nwu.edu.cn认证Cookies(即“COOKIES”认证模式下输入参数)
is_print_cookies = False
#重试最大次数
retry_max = 3
#自定义填报参数
"""
custom_params中每个元素为一个长度为2的列表,如下:
[字段名,要覆盖的值]
eg.
custom_params = [
["sfzx","0"]
]
"""
"""
默认参数及解释如下:
params = {
"sfzx":"1", #是否在校
"tw":"1", #体温(list)(0-"Below 36";1-"36-36.5";2-"36.5-36.9";3-"36.9-37.3"; ... , i<=8)
"area":"陕西省 西安市 长安区",
"city":"西安市",
"province":"陕西省",
"address":"陕西省西安市长安区郭杜街道西北大学南校区学生公寓10号楼西北大学长安校区",
"geo_api_info":'{"type":"complete","info":"SUCCESS","status":1,"$Da":"jsonp_687452_","position":{"Q":34.14218,"R":108.87518999999998,"lng":108.87519,"lat":34.14218},"message":"Get ipLocation success.Get address success.","location_type":"ip","accuracy":null,"isConverted":true,"addressComponent":{"citycode":"029","adcode":"610116","businessAreas":[],"neighborhoodType":"","neighborhood":"","building":"","buildingType":"","street":"文苑南路","streetNumber":"11号","country":"中国","province":"陕西省","city":"西安市","district":"长安区","township":"郭杜街道"},"formattedAddress":"陕西省西安市长安区郭杜街道西北大学南校区学生公寓10号楼西北大学长安校区","roads":[],"crosses":[],"pois":[]}', #高德SDK返回值
"sfcyglq":"0", #是否隔离期
"sfyzz":"0", #是否有症状
"qtqk":"", #其他情况
"ymtys":"" #不明(可能是一码通颜色,暂无用)
}
"""
custom_params = [
]
#日志,供其他模块查阅
log = ""
#代理设置
use_proxy = False
proxy_detail = {
'http':'127.0.0.1:8080'
}
# costum padding for AES-128 (16 byte) (useless)
def add_to_16(text):
if len(text.encode('utf-8')) % 16:
add = 16 - (len(text.encode('utf-8')) % 16)
else:
add = 0
text = text + ('\0' * add)
return text.encode('utf-8')
# costum padding for AES (useless)
def add_to_n(text):
n = 64
if len(text.encode('utf-8')) % n:
add = n - (len(text.encode('utf-8')) % n)
else:
add = 0
text = text + ('\0' * add)
return text.encode('utf-8')
# AES-CBC-128 encrypt
def encrypt(text,key,iv):
mode = AES.MODE_CBC
#text = add_to_16(text)
#print(text)
cryptos = AES.new(key, mode, iv)
#cipher_text = cryptos.encrypt(text)
cipher_text = cryptos.encrypt(pad(text.encode('utf-8'), AES.block_size))
res = base64.encodebytes(cipher_text).decode()
res = res.replace("\n","")
return res
# Login functions
def get_cookies(username='2015000001',password='123456abc'):
#cookies_res = {}
global log
ncov_report_url = "https://app.nwu.edu.cn/site/ncov/dailyup"
auth_server_url = "http://authserver.nwu.edu.cn"
action_url = "http://authserver.nwu.edu.cn/authserver/login"
app_uc_login_url = "https://app.nwu.edu.cn/uc/wap/login"
app_cas_login_url = "https://app.nwu.edu.cn/a_nwu/api/sso/cas"
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36 Edg/83.0.478.58"
}
if(use_proxy==False):
action_1 = requests.get(auth_server_url,headers=headers)
else:
action_1 = requests.get(auth_server_url,headers=headers,proxies=proxy_detail)
cookies_init = requests.utils.dict_from_cookiejar(action_1.cookies)
if debug_mode:
print(cookies_init)
doc = BeautifulSoup(action_1.text, 'html.parser')
if debug_mode:
print(doc.title)
lt = doc.find('input',attrs={"name": "lt"})['value']
execution = doc.find('input',attrs={"name": "execution"})['value']
aes_key = doc.find_all('script')[1].string.split('"')[3].encode('utf-8')
#print(aes_key)
#return
# encrypt passwd ans create params
#aes_key = "6cYJKrJBZAQzCtr9".encode('utf-8')
aes_iv = b"6cYJKrJBZAQzCtr9"
#aes_random_fill = "6cYJKrJBZAQzCtr96cYJKrJBZAQzCtr96cYJKrJBZAQzCtr96cYJKrJBZAQzCtr9"
aes_random_fill = "abcdefghijklmnopabcdefghijklmnopabcdefghijklmnopabcdefghijklmnop"
salted_pwd = encrypt(aes_random_fill+password,aes_key,aes_iv)
if debug_mode:
print("Salt: "+aes_key.decode())
print("Salted password: "+salted_pwd)
#return
params = {
"username":username,
"password":salted_pwd,
#"rememberMe":"0",
"lt":lt,
"dllt":"userNamePasswordLogin",
"execution":execution,
"_eventId":"submit",
"rmShown":"1"
}
if debug_mode:
print(params)
#return
if(use_proxy==False):
action_2 = requests.post(action_url,params=params,cookies=cookies_init,headers=headers,allow_redirects=False)
else:
action_2 = requests.post(action_url,params=params,cookies=cookies_init,headers=headers,allow_redirects=False,proxies=proxy_detail)
#print(action_2.text)
doc = BeautifulSoup(action_2.text, 'html.parser')
fails = doc.find_all(attrs={"class":"auth_error"})
fail_flag = 0
if len(fails)>0:
for i in fails:
#print(i)
if i['style']=="display:none;":
pass
else:
print("登陆失败:",end=" ")
print(i.text)
log = log + "\n登陆失败:" + i.text
fail_flag = 1
if fail_flag==0:
#print(action_2.text)
print("账户密码登陆成功")
log = log + "\n账户密码登陆成功"
else:
#print("Terminated...")
return {}
cookies_action_2 = requests.utils.dict_from_cookiejar(action_2.cookies)
if debug_mode:
print(cookies_action_2)
#Get UUID and eai-sess
#finally return this cookies
params = {
"redirect":ncov_report_url
}
if(use_proxy==False):
action_3 = requests.get(app_uc_login_url,params=params,cookies=cookies_action_2,headers=headers,allow_redirects=False)
else:
action_3 = requests.get(app_uc_login_url,params=params,cookies=cookies_action_2,headers=headers,allow_redirects=False,proxies=proxy_detail)
cookies_action_3 = requests.utils.dict_from_cookiejar(action_3.cookies)
if debug_mode or is_print_cookies:
print("Cookies for app:",end=" ")
print(cookies_action_3)
#Login to app (CAS)
cookies_tmp = dict(cookies_action_2 , **cookies_action_3)
params = {
"redirect":ncov_report_url
}
if(use_proxy==False):
action_4 = requests.get(app_cas_login_url,params=params,cookies=cookies_tmp,headers=headers,allow_redirects=True)
else:
action_4 = requests.get(app_cas_login_url,params=params,cookies=cookies_tmp,headers=headers,allow_redirects=True,proxies=proxy_detail)
#cookies_action_4 = requests.utils.dict_from_cookiejar(action_4.cookies)
#print(cookies_action_4)
#print(action_4.content)
return cookies_action_3
def modify_report_params(params,change):
for line in change:
params[line[0]] = line[1]
return params
def sent_report(cookies):
global log
headers = {"Accept":"application/json, text/plain, */*","Content-Type":"application/x-www-form-urlencoded","X-Requested-With":"XMLHttpRequest"}
# cookies = {"UUkey":"","eai-sess":""}
params = {
"sfzx":"1", #是否在校
"tw":"1", #体温(list)(0-"Below 36";1-"36-36.5";2-"36.5-36.9";3-"36.9-37.3"; ... , i<=8)
"area":"陕西省 西安市 长安区",
"city":"西安市",
"province":"陕西省",
"address":"陕西省西安市长安区郭杜街道西北大学南校区学生公寓10号楼西北大学长安校区",
"geo_api_info":'{"type":"complete","info":"SUCCESS","status":1,"$Da":"jsonp_687452_","position":{"Q":34.14218,"R":108.87518999999998,"lng":108.87519,"lat":34.14218},"message":"Get ipLocation success.Get address success.","location_type":"ip","accuracy":null,"isConverted":true,"addressComponent":{"citycode":"029","adcode":"610116","businessAreas":[],"neighborhoodType":"","neighborhood":"","building":"","buildingType":"","street":"文苑南路","streetNumber":"11号","country":"中国","province":"陕西省","city":"西安市","district":"长安区","township":"郭杜街道"},"formattedAddress":"陕西省西安市长安区郭杜街道西北大学南校区学生公寓10号楼西北大学长安校区","roads":[],"crosses":[],"pois":[]}', #高德SDK返回值
"sfcyglq":"0", #是否隔离期
"sfyzz":"0", #是否有症状
"qtqk":"", #其他情况
"ymtys":"" #不明(可能是一码通颜色,暂无用)
}
params = modify_report_params(params,custom_params)
params = urllib.parse.urlencode(params, quote_via=urllib.parse.quote)
if(use_proxy==False):
res = requests.post("https://app.nwu.edu.cn/ncov/wap/open-report/save",headers=headers,cookies=cookies,data=params)
else:
res = requests.post("https://app.nwu.edu.cn/ncov/wap/open-report/save",headers=headers,cookies=cookies,data=params,proxies=proxy_detail)
#print(res.url)
#print(res.content.decode())
json_res = res.json()
print("填报返回结果:"+json_res['m'])
log = log + "\n" + "填报返回结果:"+json_res['m']
return json_res['m']
def main(username='',password=''):
cookies_res = {}
global retry_max
if auth_mode=="PASSWORD":
print("USE PASSWORD MODE")
try:
cookies_res = get_cookies(username=stu_id,password=stu_passwd)
except:
print("[ERROR] Login process crash")
elif auth_mode=="COOKIES":
print("USE COOKIES MODE")
cookies_res = stu_varify_cookies
else:
print("[ERROR] Unknow auth mode")
return "Unknow auth mode"
if len(cookies_res)<=0:
print("[ERROR] Terminated...")
return "Cookies invalid"
else:
res = ""
try:
res = sent_report(cookies=cookies_res)
except:
print("[ERROR] Send-report process crash")
if res=="操作成功":
print("\n[FINAL] 自动填报成功")
return res
elif res=="您已上报过" or res=="未到上报时间":
print("\n[FINAL] 还不用填报哦~")
return res
else:
if retry_max>0:
print("Retry "+str(retry_max)+":")
retry_max = retry_max-1
main()
else:
print("\n[ERROR] [FINAL] 超过最大重试次数,填报失败!")
if __name__ == "__main__":
#CLI
parser = argparse.ArgumentParser(description='Auto report CLI')
parser.add_argument('--cli', type=bool, default=False,help="是否使用命令行参数") #Is call by cli. If false, use settings at the begining of this file.
parser.add_argument('--auth_mode', type=str, default="PASSWORD",help="认证模式")
parser.add_argument('--username', type=str, default=None,help="学工号")
parser.add_argument('--password', type=str, default=None,help="统一身份认证密码")
parser.add_argument('--eai-sess', type=str, default=None,help="认证Cookies")
parser.add_argument('--UUkey', type=str, default=None,help="认证Cookies")
args = parser.parse_args()
#print("Inpute:", args)
if args.cli==True:
print("Load settings from CLI args...")
auth_mode = args.auth_mode
stu_id = args.username
stu_passwd = args.password
stu_varify_cookies['UUkey'] = args.UUkey
stu_varify_cookies['eai-sess'] = args.eai_sess
elif args.cli==False:
print("Load Setting in file...")
main()