forked from huixhzheng/WxBot-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWxBot.java
586 lines (532 loc) · 19.5 KB
/
WxBot.java
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
import java.awt.Desktop;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.net.ssl.SSLContext;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.http.util.TextUtils;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class WxBot {
private CloseableHttpClient httpClient;
private File qrCodeFile = new File("qrcode.jpeg");
private String uuid;
private String redirectUri;
private String baseUrl;
private String skey;
private String wxsid;
private String wxuin;
private String passTicket;
private String deviceId;
private String userName;
private int retcode;
private int selector;
private JsonParser jsonParser = new JsonParser();
private JsonObject syncKeyJsonObject;
// 特殊账号
private List<String> specialUsers = Arrays.asList("newsapp", "fmessage", "filehelper", "weibo", "qqmail",
"fmessage", "tmessage", "qmessage", "qqsync", "floatbottle", "lbsapp", "shakeapp", "medianote", "qqfriend",
"readerapp", "blogapp", "facebookapp", "masssendapp", "meishiapp", "feedsapp", "voip", "blogappweixin",
"weixin", "brandsessionholder", "weixinreminder", "wxid_novlwrv3lqwv11", "gh_22b87fa7cb3c",
"officialaccounts", "notification_messages", "wxid_novlwrv3lqwv11", "gh_22b87fa7cb3c", "wxitil",
"userexperience_alarm", "notification_messages");
public WxBot() {
System.setProperty("jsse.enableSNIExtension", "false");
System.setProperty("https.protocols", "TLSv1");
try {
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(SSLContext.getDefault(),
new String[] { "TLSv1" }, null, SSLConnectionSocketFactory.getDefaultHostnameVerifier());
httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
public void start() {
this.getUuid();// 获取uuid
if (!TextUtils.isBlank(uuid)) {
this.downQrCode();// 下载二维码图片
this.showQrCode();// 显示二维码图片
}
this.login();// 登录操作
if (!TextUtils.isBlank(redirectUri)) {// 跳转到登录后页面
this.wxNewLoginPage();
}
if (!TextUtils.isBlank(skey)) {// 初始化微信
this.wxInit();
}
if (syncKeyJsonObject != null) {// 开启微信状态通知
this.wxStatusNotify();
this.listenMsg();
}
}
private void getUuid() {
String url = "https://login.weixin.qq.com/jslogin";
CloseableHttpResponse response = null;
try {
List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("appid", "wx782c26e4c19acffb"));
list.add(new BasicNameValuePair("fun", "new"));
list.add(new BasicNameValuePair("lang", "zh_CN"));
list.add(new BasicNameValuePair("_", Long.toString(System.currentTimeMillis())));
URIBuilder urlBuilder = new URIBuilder();
urlBuilder.setPath(url);
urlBuilder.setParameters(list);
URI uri = urlBuilder.build();
HttpGet httpGet = new HttpGet(uri);
System.out.println("获取uuid");
System.out.println(httpGet.getRequestLine());
response = httpClient.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
String responseContent = EntityUtils.toString(entity, "UTF-8");
System.out.println(responseContent);
if (!TextUtils.isBlank(responseContent)) {
String code = this.findStr("window.QRLogin.code = (\\d+);", responseContent);
if (!TextUtils.isEmpty(code) && code.equals("200")) {
this.uuid = this.findStr("window.QRLogin.uuid = \"(.*)\";", responseContent);
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void downQrCode() {
String url = "https://login.weixin.qq.com/qrcode/" + this.uuid;
CloseableHttpResponse response = null;
try {
HttpGet httpGet = new HttpGet(url);
System.out.println("下载二维码图片");
System.out.println(httpGet.getRequestLine());
response = httpClient.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
FileOutputStream fos = new FileOutputStream(qrCodeFile);
int l = -1;
byte[] b = new byte[1024];
while ((l = is.read(b)) != -1) {
fos.write(b, 0, l);
}
fos.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void showQrCode() {
try {
if (Desktop.isDesktopSupported()) {
Desktop desktop = Desktop.getDesktop();
if (desktop.isSupported(java.awt.Desktop.Action.BROWSE)) {
desktop.browse(qrCodeFile.toURI());
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void login() {
String tip = "1";// 首次时为1
long currentTimeMillis = System.currentTimeMillis();// 首次时为系统时间,之后每次请求加1
while (!this.checkLogin(tip, currentTimeMillis)) {
tip = "0";
currentTimeMillis++;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private boolean checkLogin(String tip, long currentTimeMillis) {
String url = "https://login.wx.qq.com/cgi-bin/mmwebwx-bin/login";
CloseableHttpResponse response = null;
try {
List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("loginicon", "true"));
list.add(new BasicNameValuePair("uuid", this.uuid));
list.add(new BasicNameValuePair("tip", tip));
list.add(new BasicNameValuePair("r", this.r()));
list.add(new BasicNameValuePair("_", Long.toString(currentTimeMillis)));
URIBuilder urlBuilder = new URIBuilder();
urlBuilder.setPath(url);
urlBuilder.setParameters(list);
URI uri = urlBuilder.build();
HttpGet httpGet = new HttpGet(uri);
System.out.println("检测登录");
System.out.println(httpGet.getRequestLine());
response = httpClient.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
String responseContent = EntityUtils.toString(entity, "UTF-8");
System.out.println(responseContent);
if (!TextUtils.isBlank(responseContent)) {
String code = this.findStr("window.code=(\\d+);", responseContent);
if (TextUtils.isEmpty(code)) {
} else if (code.equals("200")) {// 点击登录后执行
redirectUri = this.findStr("window.redirect_uri=\"(\\S+?)\";", responseContent);
baseUrl = redirectUri.substring(0, redirectUri.lastIndexOf("/"));
System.out.println("登录成功");
return true;
} else if (code.equals("201")) {// 扫描成功,还未点击登录
System.out.println("扫描成功,请点击登录");
} else if (code.equals("408")) {// 二维码未扫描,登录超时
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
private void wxNewLoginPage() {
CloseableHttpResponse response = null;
try {
HttpGet httpGet = new HttpGet(redirectUri + "&fun=new&version=v2");
System.out.println("跳转到新的登录页面");
System.out.println(httpGet.getRequestLine());
response = httpClient.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
String responseContent = EntityUtils.toString(entity, "UTF-8");
System.out.println(responseContent);
skey = this.findStr("<skey>(\\S+)</skey>", responseContent);
wxsid = this.findStr("<wxsid>(\\S+)</wxsid>", responseContent);
wxuin = this.findStr("<wxuin>(\\S+)</wxuin>", responseContent);
passTicket = this.findStr("<pass_ticket>(\\S+)</pass_ticket>", responseContent);
// List<Cookie> cookies = cookieStore.getCookies();
// if (cookies.isEmpty()) {
// System.out.println("获取Cookie失败");
// } else {
// System.out.println("获取并保存Cookie");
// for (int i = 0; i < cookies.size(); i++) {
// System.out.println(cookies.get(i).toString());
// }
// }
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void wxInit() {
String url = baseUrl + "/webwxinit";
CloseableHttpResponse response = null;
try {
List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("r", this.r()));
list.add(new BasicNameValuePair("lang", "zh_CN"));
list.add(new BasicNameValuePair("pass_ticket", passTicket));
URIBuilder urlBuilder = new URIBuilder();
urlBuilder.setPath(url);
urlBuilder.setParameters(list);
URI uri = urlBuilder.build();
deviceId = "e" + System.currentTimeMillis() + wxuin.substring(0, 2);// 生成15位随机数
String json = "{\"BaseRequest\":{\"Uin\":\"" + wxuin + "\",\"Sid\":\"" + wxsid + "\"," + "\"Skey\":\""
+ skey + "\",\"DeviceID\":\"" + deviceId + "\"}}";
StringEntity stringEntity = new StringEntity(json, ContentType.APPLICATION_JSON);
HttpPost httpPost = new HttpPost(uri);
httpPost.setEntity(stringEntity);
System.out.println("微信初始化");
System.out.println(httpPost.getRequestLine());
response = httpClient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
String responseContent = EntityUtils.toString(entity, "UTF-8");
JsonObject jsonObject = jsonParser.parse(responseContent).getAsJsonObject();
syncKeyJsonObject = jsonObject.getAsJsonObject("SyncKey");
System.out.println(syncKeyJsonObject);
userName = jsonObject.getAsJsonObject("User").get("UserName").getAsString();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void wxStatusNotify() {
String url = baseUrl + "/webwxstatusnotify";
CloseableHttpResponse response = null;
try {
String json = "{\"BaseRequest\":{\"Uin\":\"" + wxuin + "\",\"Sid\":\"" + wxsid + "\"," + "\"Skey\":\""
+ skey + "\",\"DeviceID\":\"" + deviceId + "\"},\"Code\":3," + "\"FromUserName\":" + userName
+ ",\"ToUserName\":" + userName + ",\"ClientMsgId\":"
+ System.currentTimeMillis() + "}";
StringEntity stringEntity = new StringEntity(json, ContentType.APPLICATION_JSON);
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(stringEntity);
System.out.println("开始微信状态通知");
System.out.println(httpPost.getRequestLine());
response = httpClient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
String responseContent = EntityUtils.toString(entity, "UTF-8");
JsonObject jsonObject = jsonParser.parse(responseContent).getAsJsonObject();
int ret = jsonObject.getAsJsonObject("BaseResponse").get("Ret").getAsInt();
if(ret == 0){
System.out.println("开始微信状态通知: 成功");
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void syncCheck() {
String url = "https://webpush." + baseUrl.substring(8, baseUrl.length()) + "/synccheck";
CloseableHttpResponse response = null;
try {
List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("r", this.r()));
list.add(new BasicNameValuePair("skey", skey));
list.add(new BasicNameValuePair("sid", wxsid));
list.add(new BasicNameValuePair("uin", wxuin));
list.add(new BasicNameValuePair("deviceid", deviceId));
list.add(new BasicNameValuePair("synckey", this.getSyncKeyStr()));
list.add(new BasicNameValuePair("_", Long.toString(System.currentTimeMillis())));
URIBuilder urlBuilder = new URIBuilder();
urlBuilder.setPath(url);
urlBuilder.setParameters(list);
URI uri = urlBuilder.build();
HttpGet httpGet = new HttpGet(uri);
System.out.println("消息检查");
System.out.println(httpGet.getRequestLine());
response = httpClient.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
String responseContent = EntityUtils.toString(entity, "UTF-8");
System.out.println(responseContent);
if (!TextUtils.isBlank(responseContent)) {
String retcodeStr = this.findStr("retcode:\"(\\d+)\"", responseContent);
String selectorStr = this.findStr("selector:\"(\\d+)\"}", responseContent);
if (!TextUtils.isBlank(retcodeStr) && !TextUtils.isBlank(selectorStr)) {
retcode = Integer.valueOf(retcodeStr);
selector = Integer.valueOf(selectorStr);
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void listenMsg() {
while (true) {
this.syncCheck();
if (retcode == 0) {
if (selector == 0) {// 0为正常
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else if (selector == 2) {// 新消息
this.wxSync();
} else if (selector == 6) {
} else if (selector == 7) {
}
} else if (retcode == 1100) {// 1100暂不处理,再请求一次同步
System.out.println(1100);
}
}
}
private void wxSync() {
String url = baseUrl + "/webwxsync";
CloseableHttpResponse response = null;
try {
List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("sid", wxsid));
list.add(new BasicNameValuePair("skey", skey));
URIBuilder urlBuilder = new URIBuilder();
urlBuilder.setPath(url);
urlBuilder.setParameters(list);
URI uri = urlBuilder.build();
String json = "{\"BaseRequest\":{\"Uin\":\"" + wxuin + "\",\"Sid\":\"" + wxsid + "\",\"Skey\":\""
+ skey + "\",\"DeviceID\":\"" + deviceId + "\"},\"SyncKey\":" + syncKeyJsonObject.toString()
+ ",\"rr\":" + this.r() + "}";
StringEntity stringEntity = new StringEntity(json, ContentType.APPLICATION_JSON);
HttpPost httpPost = new HttpPost(uri);
httpPost.setEntity(stringEntity);
System.out.println("获取最新消息");
System.out.println(httpPost.getRequestLine());
response = httpClient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
String responseContent = EntityUtils.toString(entity, "UTF-8");
// System.out.println(responseContent);
if (!TextUtils.isBlank(responseContent)) {
JsonObject jsonObject = jsonParser.parse(responseContent).getAsJsonObject();
syncKeyJsonObject = jsonObject.getAsJsonObject("SyncKey");
JsonArray jsonArray = jsonObject.getAsJsonArray("AddMsgList");
for (int i = 0; i < jsonArray.size(); i++) {
JsonObject obj = (JsonObject) jsonArray.get(i);
int msgType = obj.get("MsgType").getAsInt();
String fromUserName = obj.get("FromUserName").getAsString();
String toUserName = obj.get("ToUserName").getAsString();
if (msgType == 1) {// 普通消息
if (specialUsers.contains(toUserName)) {// 如果是特殊账号
continue;
}
if (toUserName.indexOf("@@") != -1) {// 群消息
continue;
}
if (toUserName.equals(userName)) {// 必须是发给我的,我才回复
String msg = this.getMsg(fromUserName, obj.get("Content").getAsString());
this.wxSendMsg(fromUserName, msg);
}
} else if (msgType == 3) {// 图片
} else if (msgType == 34) {// 语音
} else if (msgType == 42) {// 名片
} else if (msgType == 51) {// 微信初始化消息
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void wxSendMsg(String toUserName, String msg) {
if (TextUtils.isBlank(toUserName) || TextUtils.isBlank(msg)) {
return;
}
String url = baseUrl + "/webwxsendmsg";
CloseableHttpResponse response = null;
try {
String randomId = String.valueOf(System.currentTimeMillis());
Random random = new Random();
for (int i = 0; i < 4; i++) {
randomId += random.nextInt(10);
}
String json = "{\"BaseRequest\":{\"Uin\":\"" + wxuin + "\",\"Sid\":\"" + wxsid + "\"," + "\"Skey\":\""
+ skey + "\",\"DeviceID\":\"" + deviceId + "\"},\"Msg\":{\"Type\":1,\"Content\":\"" + msg
+ "\",\"FromUserName\":\"" + userName + "\",\"ToUserName\":\"" + toUserName + "\",\"LocalID\":\""
+ randomId + "\",\"ClientMsgId\":\"" + randomId + "\"},\"Scene\":0}";
StringEntity stringEntity = new StringEntity(json, ContentType.APPLICATION_JSON);
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(stringEntity);
// System.out.println("发送消息:" + msg);
System.out.println(httpPost.getRequestLine());
response = httpClient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
// HttpEntity entity = response.getEntity();
// String responseContent = EntityUtils.toString(entity, "UTF-8");
// System.out.println(responseContent);
System.out.println("发送成功");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 如果有需要发送的消息,重写此方法
private String getMsg(String fromUserName, String content) {
return null;
}
private String findStr(String regex, String str) {
Matcher matcher = Pattern.compile(regex).matcher(str);
if (matcher.find()) {
return matcher.group(1);
}
return null;
}
private String r() {
return Integer.toString(~((int) System.currentTimeMillis()));// 转换成int后取反,因为js位运算只支持32位
}
private String getSyncKeyStr() {
String syncKeyTmp = "";
JsonArray jsonArray = syncKeyJsonObject.getAsJsonArray("List");
for (int i = 0; i < jsonArray.size(); i++) {
JsonObject obj = (JsonObject) jsonArray.get(i);
syncKeyTmp += obj.get("Key") + "_" + obj.get("Val") + "|";
}
if (syncKeyTmp.length() > 0) {
syncKeyTmp = syncKeyTmp.substring(0, syncKeyTmp.length() - 1);
}
return syncKeyTmp;
}
public static void main(String[] args) {
new WxBot().start();
}
}