forked from hex-ci/smzdm_script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
254 lines (207 loc) · 7.05 KB
/
bot.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
const crypto = require('crypto');
const got = require('got');
// ------------------------------------
const APP_VERSION = '10.4.26';
const APP_VERSION_REV = '866';
const DEFAULT_USER_AGENT_APP = `smzdm_android_V${APP_VERSION} rv:${APP_VERSION_REV} (Redmi Note 3;Android10.0;zh)smzdmapp`;
const DEFAULT_USER_AGENT_WEB = `Mozilla/5.0 (Linux; Android 10.0; Redmi Build/Redmi Note 3; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/95.0.4638.74 Mobile Safari/537.36 smzdm_android_V${APP_VERSION} rv:${APP_VERSION_REV} (Redmi;Android10.0;zh) jsbv_1.0.0 webv_2.0 smzdmapp`;
const SIGN_KEY = 'apr1$AwP!wRRT$gJ/q.X24poeBInlUJC';
// ------------------------------------
const reVersion = /(smzdm_android_V|smzdm\s|iphone_smzdmapp\/)([\d.]+)/i;
const reRev = /rv:([\d.]+)/i;
const randomStr = (len = 18) => {
const char = '0123456789';
let str = '';
for (let i = 0; i < len; i++) {
str += char.charAt(Math.floor(Math.random() * char.length));
}
return str;
};
const parseJSON = (str) => {
try {
return JSON.parse(str);
}
catch (e) {
return {};
}
};
const removeTags = (str) => str.replace(/<[^<]+?>/g, '');
// 添加公共参数并签名数据
const signFormData = (data) => {
const newData = {
weixin: 1,
basic_v: 0,
f: 'android',
v: APP_VERSION,
time: `${Math.round(new Date().getTime() / 1000)}000`,
...data
};
const keys = Object.keys(newData).filter(key => newData[key] !== '').sort();
const signData = keys.map(key => `${key}=${String(newData[key]).replace(/\s+/, '')}`).join('&');
const sign = crypto.createHash('md5').update(`${signData}&key=${SIGN_KEY}`).digest('hex').toUpperCase();
return {
...newData,
sign
};
};
// 公共请求函数
const requestApi = async (url, inputOptions = {}) => {
const options = { ...inputOptions };
if (!options.method) {
options.method = 'get';
}
if (!options.data) {
options.data = {};
}
Object.keys(options.data).forEach(key => options.data[key] === undefined && delete options.data[key]);
if (options.sign !== false) {
options.data = signFormData(options.data);
}
const gotOptions = {
method: options.method.toUpperCase(),
headers: options.headers,
retry: {
limit: 2,
methods: [
'GET',
'POST'
],
statusCodes: [
],
errorCodes: [
'ECONNRESET',
'EAI_AGAIN'
]
}
};
if (options.method === 'get') {
gotOptions.searchParams = options.data;
}
else {
gotOptions.form = options.data;
}
return got(url, gotOptions).then((response) => {
const data = options.parseJSON === false ? response.body : parseJSON(response.body);
if (options.debug) {
console.log('------------------------');
console.log(url);
console.log('------------------------');
console.log(JSON.stringify(gotOptions, null, 2));
console.log('------------------------');
console.log(options.parseJSON === false ? response.body : JSON.stringify(data, null, 2));
console.log('------------------------');
}
return {
isSuccess: options.parseJSON === false ? true : (data.error_code == '0'),
response: options.parseJSON === false ? response.body : JSON.stringify(data, null, 2),
data
};
}).catch((error) => {
if (options.debug) {
console.log('------------------------');
console.log(url);
console.log('------------------------');
console.log(JSON.stringify(gotOptions, null, 2));
console.log('------------------------');
console.log(error);
console.log('------------------------');
}
return {
isSuccess: false,
response: error,
data: error
};
})
};
const updateCookie = (cookie, name, value) => {
const re = new RegExp(`(^|;)${name}=[^;]+;`, 'ig');
return cookie.replace(re, `$1${name}=${encodeURIComponent(value)};`);
};
const getEnvCookies = () => {
let cookies = [];
// 判断环境变量里面是否有 cookie
if (process.env.SMZDM_COOKIE) {
if (process.env.SMZDM_COOKIE.indexOf('&') > -1) {
cookies = process.env.SMZDM_COOKIE.split('&');
}
else if (process.env.SMZDM_COOKIE.indexOf('\n') > -1) {
cookies = process.env.SMZDM_COOKIE.split('\n');
}
else {
cookies = [process.env.SMZDM_COOKIE];
}
}
return cookies[0] ? cookies : false;
};
const randomDecimal = (min, max, decimal) => {
const rand = Math.random() * (max - min + 1) + min;
return Math.floor(rand * decimal) / decimal;
};
const wait = (minSecond, maxSecond) => {
// 生成随机小数秒数
const randomSecond = randomDecimal(minSecond, maxSecond, 1000);
console.log(`等候 ${minSecond}-${maxSecond}(${randomSecond}) 秒`);
return new Promise(resolve => setTimeout(resolve, randomSecond * 1000));
};
// ------------------------------------
class SmzdmBot {
constructor(cookie) {
this.cookie = cookie.trim();
const match = this.cookie.match(/sess=(.*?);/);
this.token = match ? match[1] : '';
// 处理 cookie
this.androidCookie = this.cookie.replace('iphone', 'android').replace('iPhone', 'Android');
this.androidCookie = updateCookie(this.androidCookie, 'smzdm_version', APP_VERSION);
this.androidCookie = updateCookie(this.androidCookie, 'device_smzdm_version', APP_VERSION);
this.androidCookie = updateCookie(this.androidCookie, 'v', APP_VERSION);
this.androidCookie = updateCookie(this.androidCookie, 'device_smzdm_version_code', APP_VERSION_REV);
this.androidCookie = updateCookie(this.androidCookie, 'device_system_version', '10.0');
this.androidCookie = updateCookie(this.androidCookie, 'apk_partner_name', 'smzdm_download');
this.androidCookie = updateCookie(this.androidCookie, 'partner_name', 'smzdm_download');
this.androidCookie = updateCookie(this.androidCookie, 'device_type', 'Android');
this.androidCookie = updateCookie(this.androidCookie, 'device_smzdm', 'android');
this.androidCookie = updateCookie(this.androidCookie, 'device_name', 'Android');
}
getHeaders() {
let userAgent = DEFAULT_USER_AGENT_APP;
if (process.env.SMZDM_USER_AGENT_APP) {
userAgent = process.env.SMZDM_USER_AGENT_APP
.replace(reVersion, `$1${APP_VERSION}`)
.replace(reRev, `rv:${APP_VERSION_REV}`);
}
return {
Accept: '*/*',
'Accept-Language': 'zh-Hans-CN;q=1',
'Accept-Encoding': 'gzip',
'request_key': randomStr(18),
'User-Agent': userAgent,
Cookie: this.androidCookie
};
}
getHeadersForWeb() {
let userAgent = DEFAULT_USER_AGENT_WEB;
if (process.env.SMZDM_USER_AGENT_WEB) {
userAgent = process.env.SMZDM_USER_AGENT_WEB
.replace(reVersion, `$1${APP_VERSION}`)
.replace(reRev, `rv:${APP_VERSION_REV}`);
}
return {
Accept: '*/*',
'Accept-Language': 'zh-CN,zh-Hans;q=0.9',
'Accept-Encoding': 'gzip',
'User-Agent': userAgent,
Cookie: this.androidCookie
};
}
getOneByRandom(listing) {
return listing[Math.floor(Math.random() * listing.length)];
}
}
module.exports = {
SmzdmBot,
requestApi,
removeTags,
parseJSON,
getEnvCookies,
wait
};