forked from zcdddddge/covid19-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHealthyPunch.js
322 lines (321 loc) · 10.6 KB
/
HealthyPunch.js
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
const { readFile } = require('fs');
const { HttpRequest } = require('./node/HttpRequest');
const { USERNAME, PASSWORD, SCKEY } = process.env;
/* 身份验证信息 */
let Cookie = 'languageIndex=0; ',
__token__ = '',
authorization = 'Bearer ';
/* 请求头 */
const
YQFK_HOST = 'https://yqfk.dgut.edu.cn',
CAS_HOST = 'https://cas.dgut.edu.cn',
LOGIN_PAGE = CAS_HOST + '/home/Oauth/getToken/appid/illnessProtectionHome/state/home.html',
Headers = {
CHROME: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36',
HTML: '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',
JSON: 'application/json;charset=utf-8',
URL_ENCODE: 'application/x-www-form-urlencoded; charset=utf-8'
};
const API = {
/**
* 初始化登录信息
* @returns {Promise<string>}
*/
initSignIn() {
return new Promise((resolve, reject) => {
HttpRequest({
type: 'GET',
_url: LOGIN_PAGE,
headers: {
Referer: YQFK_HOST,
'User-Agent': Headers.CHROME,
Accept: Headers.HTML,
},
success: chunk => {
const _Cookies = chunk.header['set-cookie'];
let _Cookie = [];
_Cookies.forEach(cookie => {
_Cookie.push(cookie.split('; ')[0]);
});
Cookie += _Cookie.join('; ');
const token = /(?<=token\s*=\s*")\S*(?=")/.exec(chunk.body);
if (token === null) reject();
__token__ = token[0];
resolve('初始化成功')
},
error: err => {
reject(`未初始化成功\n\n${err}`);
}
})
})
},
/**
* 成功登录跳转链接
* @param {string} username
* @param {string} password
* @returns {Promise<string>}
*/
getLink(username, password) {
return new Promise((resolve, reject) => {
HttpRequest({
type: 'POST',
_url: LOGIN_PAGE,
contents: {
username,
password,
__token__,
wechat_verify: ''
},
headers: {
'User-Agent': Headers.CHROME,
Referer: LOGIN_PAGE,
Origin: CAS_HOST,
Accept: Headers.JSON,
'Content-Type': Headers.URL_ENCODE,
Cookie,
},
success: chunk => {
const data = JSON.parse(chunk.body);
if (data.code === 1) {
resolve(data.info)
} else {
reject('未登陆成功')
}
},
error: err => {
reject(`未登陆成功\n\n${err}`)
}
})
});
},
/**
* 设置授权信息
* @param {string} link
* @returns {Promise<string>}
*/
setAuthorization(link) {
return new Promise((resolve, reject) => {
HttpRequest({
type: 'GET',
_url: link,
headers: {
'User-Agent': Headers.CHROME,
Referer: CAS_HOST,
Accept: Headers.HTML,
},
success: chunk => {
const location = chunk.header['location'];
const access_token = /(?<=_token=)\S*$/.exec(location);
if (access_token === null) {
reject('未获取到access_token')
} else {
authorization += access_token[0];
resolve(`成功获取到access_token`)
}
},
error: err => {
reject(`未获取到access_token\n\n${err}`)
}
})
});
},
/**
* 获取基础信息
* @returns {Promise<JSON>}
*/
getBaseInfo() {
return new Promise((resolve, reject) => {
HttpRequest({
type: 'GET',
_url: YQFK_HOST + '/home/base_info/getBaseInfo',
headers: {
Referer: YQFK_HOST + '/main',
'User-Agent': Headers.CHROME,
Accept: Headers.JSON,
Cookie,
authorization,
},
success: chunk => {
const res = JSON.parse(chunk.body);
if (res.code === 200) {
resolve(res.info);
} else {
reject(`获取基本信息失败\n\n${res}`);
}
},
error: err => {
reject(`获取基本信息失败\n\n${err}`)
}
})
});
},
/**
* 提交信息
* @param {{[key: string]: any}} info
* @returns {Promise<void>}
*/
submit(info) {
return new Promise((resolve, reject) => {
HttpRequest({
type: 'POST',
_url: YQFK_HOST + '/home/base_info/addBaseInfo',
contents: info,
headers: {
Referer: YQFK_HOST + '/main',
Origin: YQFK_HOST,
'User-Agent': Headers.CHROME,
Accept: Headers.JSON,
'Content-Type': Headers.JSON,
Cookie,
authorization,
},
success: chunk => {
const res = JSON.parse(chunk.body);
if (res.code === 200) {
resolve(res.message)
} else {
reject(res.message)
}
},
error: err => {
reject(`提交失败\n\n${err}`)
}
})
});
},
/**
* Server酱推送
* @param {string} text
* @param {string} desp
* @return {Promise<string>}
*/
sendToMe(text, desp) {
if (!SCKEY) return Promise.reject('未在环境变量里设置SCKEY, 取消Server酱推送')
return new Promise((resolve, reject) => {
HttpRequest({
type: 'GET',
_url: `https://sc.ftqq.com/${SCKEY}.send`,
_query_string: {
text,
desp
},
headers: {
'User-Agent': Headers.CHROME,
Accept: Headers.JSON,
},
success: chunk => {
const res = JSON.parse(chunk.body);
if (res.errno === 0) {
resolve('Server酱推送成功')
} else {
reject('Server酱推送失败')
}
},
error: err => {
reject(`Server酱推送失败\n\n${err}`)
}
})
});
}
};
const fn = {
/**
* 获取要发送的信息
* @param {JSON} baseinfo
* @returns {Promise<{}>}
*/
filterSendInfo(baseinfo) {
return new Promise((resolve, reject) => {
readFile('Keys.json', (err, data) => {
if (err) {
reject('打开文件失败');
} else {
let sendinfo = {};
const { keys } = JSON.parse(data);
keys.forEach(key => {
const value = baseinfo[key];
if (typeof value === 'undefined') {
if (key === "confirm") {
sendinfo[key] = 1;
} else {
reject(`缺少键${key}`);
return;
}
} else if (value instanceof Array && value.length === 0) {
sendinfo[key] = null;
} else {
sendinfo[key] = value;
}
});
resolve(sendinfo)
}
})
});
},
/**
* 转换为Server酱所需正文格式
* @param {string} text
* @returns {string}
*/
toMarkDown(text) {
console.log(text);
return text + '\n\n'
},
/**
* 隐私化
* @param {string} text
*/
hideSecret(text) {
return text.replace(/./g, '*');//.split('').map((it, n) => n % 2 ? '*' : it).join('')
}
};
(() => {
let desp = '';
const md = fn.toMarkDown;
desp += md('开始自动登录');
API.initSignIn()
.then(data => {
desp += md(data);
desp += md(`成功获取到Cookie:\n\n\`${fn.hideSecret(Cookie)}\``);
desp += md(`成功获取到token:\n\n\`${fn.hideSecret(__token__)}\``);
if (USERNAME && PASSWORD) {
return API.getLink(USERNAME, PASSWORD);
} else {
return Promise.reject('未在环境变量里设置USERNAME, PASSWORD')
}
})
.then(data => {
desp += md('登陆成功');
desp += md(`成功获取到跳转链接:\n\n\`${fn.hideSecret(data)}\``);
return API.setAuthorization(data);
})
.then(data => {
desp += md(data);
desp += md('设置授权头');
return API.getBaseInfo();
})
.then(data => {
desp += md('获取基本信息成功');
desp += md(data.msg);
return fn.filterSendInfo(data);
})
.then(data => {
desp += md('要发送的信息筛选完毕');
desp += md('提交健康日报表');
return API.submit(data);
})
.then(data => {
desp += md(data)
return Promise.resolve();
})
.then(() => {
return API.sendToMe('健康打卡ok', desp);
})
.catch(err => {
desp += fn.toMarkDown(err)
return API.sendToMe('健康打卡出错', desp);
})
.catch(err => {
console.log(err);
})
})()