This repository has been archived by the owner on Jun 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
submit.py
292 lines (248 loc) · 10.3 KB
/
submit.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
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
import base64
import time
from datetime import date, timedelta
import json
import requests
from hoshino import logger
# 加密密码
async def encryptPassword(password: str, key: str) -> str:
"""Encrypt password
Encrypt the password in ECB mode, PKCS7 padding, then Base64 encode the password
Args:
password:
The password to encrypt
key:
The encrypt key for encryption
Return:
encryptedPassword:
Encrypted password
"""
# add padding
blockSize = len(key)
padAmount = blockSize - len(password) % blockSize
padding = chr(padAmount) * padAmount
encryptedPassword = password + padding
# encrypt password in ECB mode
aesEncryptor = Cipher(algorithms.AES(key.encode('utf-8')),
modes.ECB(),
backend=default_backend()).encryptor()
encryptedPassword = aesEncryptor.update(
encryptedPassword.encode('utf-8')) + aesEncryptor.finalize()
# base64 encode
encryptedPassword = base64.b64encode(encryptedPassword)
return encryptedPassword.decode('utf-8')
# 登录并提交
async def login_submit(username: str, password: str, location: str, region: str):
#### 开始登录
requestSession = requests.session()
requestSession.headers.update({
'User-Agent':
'Mozilla/5.0 (Linux; Android 12; 114514FUCK) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.73 Mobile Safari/537.36',
'Accept':
'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9'
})
# get cookie: SESSION
ignore = requestSession.get('https://cas.hfut.edu.cn/cas/login')
ignore.raise_for_status()
# get cookie: JSESSIONID
ignore = requestSession.get('https://cas.hfut.edu.cn/cas/vercode')
ignore.raise_for_status()
# get encryption key
timeInMillisecond = round(time.time_ns() / 100000)
responseForKey = requestSession.get(
url='https://cas.hfut.edu.cn/cas/checkInitVercode',
params={'_': timeInMillisecond})
responseForKey.raise_for_status()
encryptionKey = responseForKey.cookies['LOGIN_FLAVORING']
# check if verification code is required
if responseForKey.json():
logger.info('需要验证码,过一会再试试吧。')
return False, '需要验证码,过一会再试试吧。'
# try to login
encryptedPassword = await encryptPassword(password, encryptionKey)
checkIdResponse = requestSession.get(
url='https://cas.hfut.edu.cn/cas/policy/checkUserIdenty',
params={
'_': (timeInMillisecond + 1),
'username': username,
'password': encryptedPassword
})
checkIdResponse.raise_for_status()
checkIdResponseJson = checkIdResponse.json()
if checkIdResponseJson['msg'] != 'success':
# login failed
if checkIdResponseJson['data']['mailRequired'] or checkIdResponseJson['data']['phoneRequired']:
# the problem may be solved manually
logger.info('需要进行手机或邮箱认证,移步: https://cas.hfut.edu.cn/')
return False, '需要进行手机或邮箱认证'
logger.info(f'处理checkUserIdenty时出现错误:{checkIdResponseJson["msg"]}')
return False, f'处理checkUserIdenty时出现错误:{checkIdResponseJson["msg"]}'
requestSession.headers.update(
{'Content-Type': 'application/x-www-form-urlencoded'})
loginResponse = requestSession.post(
url='https://cas.hfut.edu.cn/cas/login',
data={
'username': username,
'capcha': '',
'execution': 'e1s1',
'_eventId': 'submit',
'password': encryptedPassword,
'geolocation': "",
'submit': "登录"
})
loginResponse.raise_for_status()
requestSession.headers.pop('Content-Type')
if 'cas协议登录成功跳转页面。' not in loginResponse.text:
# log in failed
logger.info('登录失败')
return False, '未知原因,登录失败'
# log in success
logger.info('登录成功')
#### 开始提交
ignore = requestSession.get(
url='http://stu.hfut.edu.cn/xsfw/sys/swmjbxxapp/*default/index.do')
# always 502, ignore this
#ignore.raise_for_status()
requestSession.headers.update({
'Content-Type': 'application/x-www-form-urlencoded',
'X-Requested-With': 'XMLHttpRequest'
})
ignore = requestSession.post(
url='http://stu.hfut.edu.cn/xsfw/sys/emapfunauth/welcomeAutoIndex.do')
ignore.raise_for_status()
requestSession.headers.pop('Content-Type')
requestSession.headers.pop('X-Requested-With')
ignore = requestSession.get(
url='http://stu.hfut.edu.cn/xsfw/sys/emapfunauth/casValidate.do',
params={'service': '/xsfw/sys/swmjbxxapp/*default/index.do'})
ignore.raise_for_status()
requestSession.headers.update({
'X-Requested-With':
'XMLHttpRequest',
'Referer':
'http://stu.hfut.edu.cn/xsfw/sys/swmjbxxapp/*default/index.do'
})
ignore = requestSession.get(
url=
'http://stu.hfut.edu.cn/xsfw/sys/emappagelog/config/swmxsyqxxsjapp.do')
ignore.raise_for_status()
# get role config
requestSession.headers.pop('X-Requested-With')
requestSession.headers.update(
{'Content-Type': 'application/x-www-form-urlencoded'})
configData = {
'data':
json.dumps({
'APPID': '5811260348942403',
'APPNAME': 'swmxsyqxxsjapp'
})
}
roleConfigResponse = requestSession.post(
url=
'http://stu.hfut.edu.cn/xsfw/sys/swpubapp/MobileCommon/getSelRoleConfig.do',
data=configData)
roleConfigResponse.raise_for_status()
roleConfigJson = roleConfigResponse.json()
if roleConfigJson['code'] != '0':
# :(
logger.info(f'处理roleConfig时发生错误:{roleConfigJson["msg"]}')
return False, f'处理roleConfig时发生错误:{roleConfigJson["msg"]}'
# get menu info
menuInfoResponse = requestSession.post(
url= 'http://stu.hfut.edu.cn/xsfw/sys/swpubapp/MobileCommon/getMenuInfo.do',
data=configData)
menuInfoResponse.raise_for_status()
menuInfoJson = menuInfoResponse.json()
if menuInfoJson['code'] != '0':
# :(
logger.info(f'处理menuInfo时发生错误:{menuInfoJson["msg"]}')
return False, f'处理menuInfo时发生错误:{menuInfoJson["msg"]}'
todayDateStr = "%.2d-%.2d-%.2d" % time.localtime()[:3]
# if submitted
ifSubmitted = requestSession.post(
url=
'http://stu.hfut.edu.cn/xsfw/sys/swmxsyqxxsjapp/modules/mrbpa/judgeTodayHasData.do',
data={'data': json.dumps({'TBSJ': todayDateStr})})
ifSubmittedJson = ifSubmitted.json()
if len(ifSubmittedJson['data']) == 1:
logger.info('今天已经打过卡了,处理结束')
return 'have_done', ''
# get setting... for what?
requestSession.headers.pop('Content-Type')
settingResponse = requestSession.get(
url=
'http://stu.hfut.edu.cn/xsfw/sys/swmxsyqxxsjapp/modules/mrbpa/getSetting.do',
data={'data': ''})
settingResponse.raise_for_status()
settingJson = settingResponse.json()
# get the form submitted last time
requestSession.headers.update(
{'Content-Type': 'application/x-www-form-urlencoded'})
lastSubmittedResponse = requestSession.post(
url=
'http://stu.hfut.edu.cn/xsfw/sys/swmxsyqxxsjapp/modules/mrbpa/getStuXx.do',
data={'data': json.dumps({'TBSJ': todayDateStr})})
lastSubmittedResponse.raise_for_status()
lastSubmittedJson = lastSubmittedResponse.json()
if lastSubmittedJson['code'] != '0':
# something wrong with the form submitted last time
logger.info('上次填报提交的信息出现了问题,本次最好手动填报提交。')
return 'need_self', ''
# get the form submitted yesterday
todayDateStr_tmp = (date.today() + timedelta(days = -1)).strftime("%Y-%m-%d")
yes_DateStr = f'{todayDateStr_tmp}-{username}'
requestSession.headers.update(
{'Content-Type': 'application/x-www-form-urlencoded'})
yes_SubmittedResponse = requestSession.post(
url=
'http://stu.hfut.edu.cn/xsfw/sys/swmxsyqxxsjapp/modules/mrbpa/getStuXx.do',
data={'data': json.dumps({'WID': yes_DateStr,'TBSJ': todayDateStr})})
yes_SubmittedResponse.raise_for_status()
yes_SubmittedJson = yes_SubmittedResponse.json()
studentKeyResponse = requestSession.post(
url=
'http://stu.hfut.edu.cn/xsfw/sys/swmxsyqxxsjapp/modules/mrbpa/studentKey.do',
data={})
studentKeyJson = studentKeyResponse.json()
# generate today's form to submit
submitDataToday = lastSubmittedJson['data']
submitDataToday.update({
'BY1': '1',
'DFHTJHBSJ': '',
'DZ_SFSB': '1',
'DZ_TBDZ': location,
'DZ_TBSJDZ': region,
"DZ_AKMSFYC_DISPLAY": "否",
"DZ_XCKSFYC_DISPLAY": "否",
"DZ_AKMSFYC": "0",
"DZ_XCKSFYC": "0",
"DZ_SCAKMJT": yes_SubmittedJson['data']['DZ_SCAKMJT'],
"DZ_SCXCKJT": yes_SubmittedJson['data']['DZ_SCXCKJT'],
'GCJSRQ': '',
'GCKSRQ': '',
'TBSJ': todayDateStr,
'studentKey': studentKeyJson['data']['studentKey']
})
paramKeyResponse = requestSession.post(
url=
'http://stu.hfut.edu.cn/xsfw/sys/swmxsyqxxsjapp/modules/mrbpa/setCode.do',
data={'data': json.dumps(submitDataToday)})
paramKeyJson = paramKeyResponse.json()
# try to submit
submitResponse = requestSession.post(
url=
'http://stu.hfut.edu.cn/xsfw/sys/swmxsyqxxsjapp/modules/mrbpa/saveStuXx.do',
data={'data': json.dumps(paramKeyJson['data'])})
submitResponse.raise_for_status()
submitResponseJson = submitResponse.json()
if submitResponseJson['code'] != '0':
# failed
logger.info(f'提交时出现错误:{submitResponseJson["msg"]}')
return False, f'提交时出现错误:{submitResponseJson["msg"]}'
# succeeded
logger.info('提交成功')
requestSession.headers.pop('Referer')
requestSession.headers.pop('Content-Type')
return 'success', ''