-
Notifications
You must be signed in to change notification settings - Fork 6
/
wechatapi.go
1266 lines (1219 loc) · 45.3 KB
/
wechatapi.go
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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package wechatapi
import (
"fmt"
"io/ioutil"
"net/http"
"html"
"regexp"
"time"
"github.com/skip2/go-qrcode"
"github.com/buger/jsonparser"
"strings"
"encoding/xml"
"encoding/json"
"math/rand"
"strconv"
)
type WechatData struct {
Skey string `xml:"skey"`
Sid string `xml:"wxsid"`
Uin string `xml:"wxuin"`
PassTicket string `xml:"pass_ticket"`
Synckey []byte
DeviceID string
MsgID string
Webwx_data_ticket string
}
type MySelf struct {
UserName string `json:"UserName"`
NickName string `json:"NickName"`
HeadImgUrl string `json:"HeadImgUrl"`
Sex int64 `json:"Sex"`
Signature string `json:"Signature"`
}
type MsgData struct {
Srctype int64 `json:"Srctype"`
Srcname string `json:"Srcname"`
Msgtype int64 `json:"Msgtype"`
Msg string `json:"Msg"`
Fromuser string `json:"Fromuser"`
Usernick string `json:"Usernick"`
Createtime int64 `json:"Createtime"`
}
var (
webwx WechatData
myself MySelf
chatrooms []byte
contactlist []byte
officiallist []byte
wxurl = "https://wx.qq.com"
webpushurl = "https://webpush.wx.qq.com"
extspam = "Go8FCIkFEokFCggwMDAwMDAwMRAGGvAESySibk50w5Wb3uTl2c2h64jVVrV7"+
"gNs06GFlWplHQbY/5FfiO++1yH4ykCyNPWKXmco+wfQzK5R98D3so7rJ5LmGFvBLjGceleySrc3SOf2Pc1gVeh"+
"zJgODeS0lDL3/I/0S2SSE98YgKleq6Uqx6ndTy9yaL9qFxJL7eiA/R3SEfTaW1SBoSITIu+EEkXff+Pv8NHOk7"+
"N57rcGk1w0ZzRrQDkXTOXFN2iHYIzAAZPIOY45Lsh+A4slpgnDiaOvRtlQYCt97nmPLuTipOJ8Qc5pM7ZsOsAP"+
"PrCQL7nK0I7aPrFDF0q4ziUUKettzW8MrAaiVfmbD1/VkmLNVqqZVvBCtRblXb5FHmtS8FxnqCzYP4WFvz3T0T"+
"crOqwLX1M/DQvcHaGGw0B0y4bZMs7lVScGBFxMj3vbFi2SRKbKhaitxHfYHAOAa0X7/MSS0RNAjdwoyGHeOepX"+
"OKY+h3iHeqCvgOH6LOifdHf/1aaZNwSkGotYnYScW8Yx63LnSwba7+hESrtPa/huRmB9KWvMCKbDThL/nne14h"+
"nL277EDCSocPu3rOSYjuB9gKSOdVmWsj9Dxb/iZIe+S6AiG29Esm+/eUacSba0k8wn5HhHg9d4tIcixrxveflc"+
"8vi2/wNQGVFNsGO6tB5WF0xf/plngOvQ1/ivGV/C1Qpdhzznh0ExAVJ6dwzNg7qIEBaw+BzTJTUuRcPk92Sn6Q"+
"Dn2Pu3mpONaEumacjW4w6ipPnPw+g2TfywJjeEcpSZaP4Q3YV5HG8D6UjWA4GSkBKculWpdCMadx0usMomsSS/"+
"74QgpYqcPkmamB4nVv1JxczYITIqItIKjD35IGKAUwAA=="
)
/** 获取自己的信息
* @return MySelf 返回自己的信息
*/
func GetMySelf() MySelf {
return myself
}
/** 获取所有群聊信息
* @return int64 群聊数量
* @return []string 返回所有群聊
*/
func GetChatrooms() (int64, []byte) {
var count int64 = 0
jsonparser.ArrayEach(chatrooms, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
if err != nil {
return
}
count++
})
return count, chatrooms
}
/** 根据群昵称获取群UserName
* @param string 群昵称
* @return string 群UserName
*/
func getChatroomName(nickname string) string {
name := ""
jsonparser.ArrayEach(chatrooms, func(value []byte, dataType jsonparser.ValueType, offset int, err error){
if err != nil {
return
}
nick, _ := jsonparser.GetString(value,"NickName")
if nickname == nick {
name, _ = jsonparser.GetString(value,"UserName")
}
})
return name
}
/** 获取所有联系人信息
* @return int64 联系人数量
* @return []byte 返回所有联系人
*/
func GetContactlist() (int64, []byte) {
var count int64 = 0
jsonparser.ArrayEach(contactlist, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
if err != nil {
return
}
count++
})
return count, contactlist
}
/** 获取所有公众号信息
* @return int64 公众号数量
* @return []byte 返回所有公众号
*/
func GetOfficiallist() (int64, []byte) {
var count int64 = 0
jsonparser.ArrayEach(officiallist, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
if err != nil {
return
}
count++
})
return count, officiallist
}
/**
* 直接在终端输出二维码
* 使用github.com/skip2/go-qrcode库
* @param info 要显示的字符串
* @return error 错误信息
*/
func PrintQRCode(info string) error {
// 生成QR码
qr, err := qrcode.New(info, qrcode.Highest)
if err != nil {
return err
}
// 获取 QR 码图像
size := 5
img := qr.Image(size)
// 遍历图像的每个像素点
bounds := img.Bounds()
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
// 获取像素的颜色值
r, g, b, _ := img.At(x, y).RGBA()
_, _, _, alpha := img.At(x, y).RGBA()
// 假设黑色像素为 QR 码的黑色部分,白色或透明像素为白色部分
if alpha == 0xFFFF && (r == 0 && g == 0 && b == 0) {
fmt.Print("██")
} else {
fmt.Print(" ")
}
}
fmt.Println()
}
return nil
}
/**
* 获取微信登录二维码UUID值,用于后续登录
* @return string 返回UUID
*/
func getQRCodeuuid() string {
// 请求微信登录页面获取响应中的值
resp, err := http.Get(wxurl + "/jslogin?appid=wx782c26e4c19acffb&redirect_uri=https%3A%2F%2Fwx.qq.com%2Fcgi-bin%2Fmmwebwx-bin%2Fwebwxnewloginpage%3Fmod%3Ddesktop&fun=new")
if err != nil {
return ""
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return ""
}
re := regexp.MustCompile(`window\.QRLogin\.uuid\s*=\s*"([^"]+)"`)
uuid := re.FindStringSubmatch(string(body))
if uuid == nil {
return ""
}
return uuid[1]
}
/**
* 检查是否登录,如果登录则返回 redirect_uri,否则返回错误信息
* @param uuid 登录二维码UUID
* @return string 返回 redirect_uri
* @return error 错误信息
*/
func checkLogin(uuid string) (string, error) {
p := time.Now().Unix()
r := int(p * 11)
loginURL := fmt.Sprintf(wxurl + "/cgi-bin/mmwebwx-bin/login?loginicon=true&uuid=%s&tip=0&r=%d&_=%d", uuid, r, p)
resp, err := http.Get(loginURL)
if err != nil {
return "", fmt.Errorf("E00001: Please check your network connection.")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("E00002: Failed to read response body.")
}
codeRegex := regexp.MustCompile(`window\.code=(\d+);`)
redirectURIRegex := regexp.MustCompile(`window\.redirect_uri\s*=\s*"([^"]+)";`)
codeMatch := codeRegex.FindStringSubmatch(string(body))
if codeMatch != nil {
// 二维码过期
if codeMatch[1] == "400" {
return "400", fmt.Errorf("E00003: The QR code has expired.")
}
// 登录成功
if codeMatch[1] == "200" {
redirectURIMatch := redirectURIRegex.FindStringSubmatch(string(body))
if redirectURIMatch == nil {
return "", fmt.Errorf("E00004: window.redirect_uri not found.")
}
// 返回提取到的 redirect_uri
return redirectURIMatch[1], nil
}
return "", fmt.Errorf("")
}else {
return "", fmt.Errorf("E00005: window.code is not exist")
}
}
/**
* 输出登录二维码,并检查登录结果,登录成功后初始化数据
* @return error 错误信息
*/
func Login() error {
uuid := getQRCodeuuid()
if uuid == "" {
return fmt.Errorf("[!] Cound not find uuid.")
} else {
fmt.Println("[*] 请扫描二维码登录微信")
fmt.Println("[*] https://api.pwmqr.com/qrcode/create/?url=" + "https://login.weixin.qq.com/l/" + uuid)
fmt.Println("[*] https://api.isoyu.com/qr/?m=1&e=L&p=20&url=" + "https://login.weixin.qq.com/l/" + uuid)
PrintQRCode("https://login.weixin.qq.com/l/" + uuid)
// 轮询检查是否已经扫码登录
for {
redirectURI, err := checkLogin(uuid)
if err != nil {
if redirectURI == "400" {
uuid = getQRCodeuuid()
fmt.Println("[*] 二维码过期,请重新扫描二维码登录微信")
fmt.Println("[*] https://api.pwmqr.com/qrcode/create/?url=" + "https://login.weixin.qq.com/l/" + uuid)
fmt.Println("[*] https://api.isoyu.com/qr/?m=1&e=L&p=20&url=" + "https://login.weixin.qq.com/l/" + uuid)
PrintQRCode("https://login.weixin.qq.com/l/" + uuid)
continue
}
if strings.Contains(err.Error(), "E00001") {
fmt.Println("[!] " + err.Error())
return err
}
} else {
fmt.Println("[+] 登录成功!")
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
// 返回错误以阻止自动跟随重定向
return http.ErrUseLastResponse
},
}
req, _ := http.NewRequest("GET",redirectURI + "&fun=new&version=v2&mod=desktop",nil)
//fmt.Println(redirectURI + "&fun=new&version=v2&mod=desktop")
req.Header.Add("Referer","https://wx.qq.com/?target=t")
req.Header.Add("client-version","2.0.0")
req.Header.Add("extspam",extspam)
resp, err := client.Do(req)
if err != nil {
fmt.Println("[!] E00001: Please check your network connection.")
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("[!] E00002: Failed to read response body.")
}
cookies := resp.Header["Set-Cookie"]
webwx_data_ticket := ""
for _, cookie := range cookies {
if strings.HasPrefix(cookie, "webwx_data_ticket="){
parts := strings.Split(cookie, ";")
for _, part := range parts {
part = strings.TrimSpace(part)
if strings.HasPrefix(part, "webwx_data_ticket="){
webwx_data_ticket = part[len("webwx_data_ticket="):]
break
}
}
if webwx_data_ticket != "" {
webwx.Webwx_data_ticket = webwx_data_ticket
break
}
}
}
err = xml.Unmarshal([]byte(body), &webwx)
if err != nil {
fmt.Println("[!] E00006: You need real name authentication and WeChat payment activation.")
}
fmt.Println("[*] 正在初始化数据...")
webInit()
fmt.Println("[*] 初始化数据成功!")
return nil
}
time.Sleep(2 * time.Second)
}
}
}
/**
* 获取webInit数据,初始化数据
*/
func webInit() {
r := time.Now().Unix()
rand.Seed(time.Now().UnixNano())
const count = 15
var randomString string
for i := 0; i < count; i++ {
randomDigit := rand.Intn(10)
randomString += fmt.Sprint(randomDigit)
}
webwx.DeviceID = "e" + randomString
url := fmt.Sprintf(wxurl + "/cgi-bin/mmwebwx-bin/webwxinit?r=%d&pass_ticket=%s", r, webwx.PassTicket)
data := fmt.Sprintf(`{"BaseRequest":{"Uin":"%s","Sid":"%s","Skey":"%s","DeviceID":"%s"}}`, webwx.Uin, webwx.Sid, webwx.Skey, webwx.DeviceID)
resp, err := http.Post(url, "application/json;charset=utf-8", strings.NewReader(data))
if err != nil {
fmt.Println("[!] E00001: Please check your network connection.")
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("[!] E00002: Failed to read response body.")
return
}
SyncKey, _, _, err := jsonparser.Get(body,"SyncKey")
if err != nil {
fmt.Println("[!] E00007: %s",err)
return
}
webwx.Synckey = SyncKey
err = setMySelf(body)
if err != nil {
fmt.Println(err)
}
fmt.Println("[*] 正在获取联系人,请稍后...")
err = getAllContact()
if err != nil {
fmt.Println(err)
}
fmt.Println("[*] 获取联系人成功,正在获取微信群,请稍后...")
_ = getStatusNotify()
getChatRoomFromWebwxsync()
fmt.Println("[*] 获取微信群成功")
}
/**
* 获取自己的信息,并存入myself结构体实例
* @param jsondata []byte 服务器返回的JSON数据
* @return error 错误信息
*/
func setMySelf(jsondata []byte) error {
username, err := jsonparser.GetString(jsondata, "User", "UserName")
if err != nil {
return fmt.Errorf("E00007: %s",err)
}
myself.UserName = username
nickname, err := jsonparser.GetString(jsondata, "User", "NickName")
if err != nil {
return fmt.Errorf("E00007: %s",err)
}
myself.NickName = nickname
headimgurl, err := jsonparser.GetString(jsondata, "User", "HeadImgUrl")
if err != nil {
return fmt.Errorf("E00007: %s",err)
}
myself.HeadImgUrl = headimgurl
if value, err := jsonparser.GetInt(jsondata, "User", "Sex"); err == nil {
myself.Sex = value
}
signature, err := jsonparser.GetString(jsondata, "User", "Signature")
if err != nil {
return fmt.Errorf("E00007: %s",err)
}
myself.Signature = signature
return nil
}
/**
* 获取微信状态通知
* @return error 错误信息
*/
func getStatusNotify() error {
url := fmt.Sprintf(wxurl + "/cgi-bin/mmwebwx-bin/webwxstatusnotify?pass_ticket=%s",webwx.PassTicket)
data := fmt.Sprintf(`{"BaseRequest":{"Uin":%s,"Sid":"%s","Skey":"%s","DeviceID":"%s"},"Code":3,"FromUserName":"%s","ToUserName":"%s","ClientMsgId":"%d"}`,webwx.Uin,webwx.Sid,webwx.Skey,webwx.DeviceID,myself.UserName,myself.UserName,time.Now().Unix())
resp, err := http.Post(url, "application/json;charset=utf-8", strings.NewReader(data))
if err != nil {
return fmt.Errorf("E00001: Please check your network connection.")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("E00002: Failed to read response body.")
}
msgID, err := jsonparser.GetString(body,"MsgID")
if err != nil {
return fmt.Errorf("E00007: %s",err)
}
webwx.MsgID = msgID
return nil
}
/**
* 获取SyncKey
* @param SyncKey []byte Json数据
* @return string 返回格式化后的synckey数据
* @return error 错误信息
*/
func getSyncKey(SyncKey []byte) (string, error) {
SyncKeyList, _, _, err := jsonparser.Get(SyncKey,"List")
if err != nil {
return "", fmt.Errorf("E00007: %s",err)
}
// Initialize an empty string for the result
result := ""
// Iterate over the array elements
jsonparser.ArrayEach(SyncKeyList, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
if err != nil {
return
}
// Use jsonparser to get the "Key" and "Val" values
key, _ := jsonparser.GetInt(value, "Key")
val, _ := jsonparser.GetInt(value, "Val")
// Format the current element as "key_val" and add it to the result string
if len(result) > 0 {
result += "%7C" // Add separator (URL-encoded pipe symbol)
}
result += fmt.Sprintf("%d_%d", key, val)
})
return result, nil
}
/** 通过联系人备注名获取联系人信息
* @param remarkname string 联系人备注名
* @return []byte 返回联系人信息
*/
func GetContactByRemarkName(remarkname string) []byte {
var contact []byte
jsonparser.ArrayEach(contactlist, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
if err != nil {
return
}
remarkName, _ := jsonparser.GetString(value, "RemarkName")
if remarkName == remarkname {
contact = value
}
})
return contact
}
/** 通过联系人昵称获取联系人信息
* @param nickname string 联系人昵称
* @return []byte 返回联系人信息
*/
func GetContactByNickName(nickname string) []byte {
var contact []byte
jsonparser.ArrayEach(contactlist, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
if err != nil {
return
}
nickName, _ := jsonparser.GetString(value, "NickName")
if nickName == nickname {
contact = value
}
})
return contact
}
/** 获取指定省份、城市、性别的联系人
* @param args ...string 省份、城市、性别
* @return int64 符合条件的联系人数量
* @return []byte 符合条件的联系人信息
*/
func GetContacts(args ...string) (int64, []byte) {
if len(args) != 3 {
fmt.Println("[!] E00008: 3 parameters are required.")
return 0,[]byte{}
}
var count int64
var contactarray []map[string]interface{}
var contactslist []byte
jsonparser.ArrayEach(contactlist, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
contact := make(map[string]interface{})
_ = json.Unmarshal(value, &contact)
if args[0] != "" {
Province, _ := jsonparser.GetString(value, "Province")
if args[0] == Province {
contactarray = append(contactarray,contact)
count ++
}
}
if args[1] != "" {
City, _ := jsonparser.GetString(value, "City")
if args[1] == City {
contactarray = append(contactarray,contact)
count ++
}
}
if args[2] != "" {
Sexint, _ := strconv.ParseInt(args[2], 10, 64)
Sex, _ := jsonparser.GetInt(value, "Sex")
if Sexint == Sex {
contactarray = append(contactarray,contact)
count ++
}
}
})
contactslist,_ = json.Marshal(contactarray)
return count, contactslist
}
/**
* 获取所有联系人,包括公众号
* @return error 错误信息
*/
func getAllContact() error {
var Seq, MemberCount int64 = 0,0
var memberArray []map[string]interface{}
for {
url := fmt.Sprintf(wxurl + "/cgi-bin/mmwebwx-bin/webwxgetcontact?pass_ticket=%s&r=%d&seq=%d&skey=%s",webwx.PassTicket,time.Now().Unix(),Seq,webwx.Skey)
req, err := http.NewRequest("GET",url,nil)
if err != nil {
return err
}
req.Header.Add("Cookie","wxuin="+webwx.Uin+";wxsid="+webwx.Sid)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("E00001: Please check your network connection.")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("E00002: Failed to read response body.")
}
if value,err := jsonparser.GetInt(body, "Seq"); err == nil {
Seq = value
}
if value,err := jsonparser.GetInt(body, "MemberCount"); err == nil {
MemberCount += value
}
memberLists, _, _, err := jsonparser.Get(body,"MemberList")
if err != nil {
return fmt.Errorf("E00007: %s",err)
}
var temp []map[string]interface{}
err = json.Unmarshal(memberLists, &temp)
if err != nil {
return err
}
memberArray = append(memberArray, temp...)
if Seq == 0 {
break
}
}
memberList, err := json.Marshal(memberArray)
if err != nil {
return fmt.Errorf("E00009: error marshalling merged JSON array: %v", err)
}
// 保留指定字段
var contactarray []map[string]interface{}
var officialarray []map[string]interface{}
jsonparser.ArrayEach(memberList, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
if err != nil {
fmt.Println("Error:", err)
return
}
contact := make(map[string] interface{})
username, _ := jsonparser.GetString(value, "UserName")
nickname, _ := jsonparser.GetString(value, "NickName")
headimgurl, _ := jsonparser.GetString(value, "HeadImgUrl")
remarkname, _ := jsonparser.GetString(value, "RemarkName")
sex, _ := jsonparser.GetInt(value, "Sex")
signature, _ := jsonparser.GetString(value, "Signature")
province, _ := jsonparser.GetString(value, "Province")
city, _ := jsonparser.GetString(value, "City")
snsflag, _ := jsonparser.GetInt(value, "SnsFlag")
contact["UserName"] = username
contact["NickName"] = nickname
contact["HeadImgUrl"] = headimgurl
contact["RemarkName"] = remarkname
contact["Sex"] = sex
contact["Signature"] = signature
contact["Province"] = province
contact["City"] = city
contact["SnsFlag"] = snsflag
if snsflag == 0 {
officialarray = append(officialarray,contact)
officiallist,_ = json.Marshal(officialarray)
} else {
contactarray = append(contactarray,contact)
contactlist,_ = json.Marshal(contactarray)
}
})
return nil
}
/**
* 批量获取联系人
* @param username ...string 用户名
*/
func getBatchContact(username ...string) []byte {
count := int64(len(username))
users := make([]map[string]string, count)
for i, user := range username {
users[i] = map[string]string{
"UserName": user,
"EncryChatRoomId": "",
}
}
userList, err := json.Marshal(users)
if err != nil {
return []byte{}
}
r := time.Now().Unix()
url := fmt.Sprintf(wxurl + "/cgi-bin/mmwebwx-bin/webwxbatchgetcontact?type=ex&r=%d&pass_ticket=%s",r, webwx.PassTicket)
data := fmt.Sprintf(
`{
"BaseRequest":{
"Uin":%s,
"Sid":"%s",
"Skey":"%s",
"DeviceID":"%s"
},
"Count":%d,
"List":%s
}`,
webwx.Uin,webwx.Sid,webwx.Skey,webwx.DeviceID,count,string(userList))
req, err := http.NewRequest("POST",url,strings.NewReader(data))
if err != nil {
return []byte{}
}
req.Header.Add("Cookie","wxuin="+webwx.Uin+";wxsid="+webwx.Sid)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return []byte{}
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return []byte{}
}
contactList, _, _, err := jsonparser.Get(body,"ContactList")
if err != nil {
return []byte{}
}
return contactList
}
/**
* 检查消息
* @return int64 返回retcode的值
* @return int64 返回selector的值,为0表示无消息
*/
func syncCheck() (int64, int64) {
r := time.Now().Unix()
synckey, _ := getSyncKey(webwx.Synckey)
url := fmt.Sprintf(webpushurl + "/cgi-bin/mmwebwx-bin/synccheck?r=%d&skey=%s&sid=%s&uin=%s&deviceid=%s&synckey=%s&_=%d",r,webwx.Skey,webwx.Sid,webwx.Uin,webwx.DeviceID,synckey,r)
req, err := http.NewRequest("GET",url,nil)
if err != nil {
return 0, 0
}
req.Header.Add("Cookie","wxuin="+webwx.Uin+";webwx_data_ticket="+webwx.Webwx_data_ticket)
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
return 0, 0
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
re1 := regexp.MustCompile(`retcode:"([^"]+)"`)
re2 := regexp.MustCompile(`selector:"([^"]+)"`)
retcode := re1.FindStringSubmatch(string(body))
if len(retcode) < 2 {
return 0, 0
}
retcodeInt, _ := strconv.ParseInt(retcode[1], 10, 64)
selector := re2.FindStringSubmatch(string(body))
if len(selector) < 2 {
return 0, 0
}
selectorInt, _ := strconv.ParseInt(selector[1], 10, 64)
//fmt.Println(retcodeInt, selectorInt)
return retcodeInt, selectorInt
}
/** 检查用户名是否在联系人列表中
* @param slice []byte 联系人列表
* @param str string 要检查的用户名
* @return bool 如果用户名在列表中,返回 true;否则返回 false
*/
func contains(slice []byte, str string) bool {
res := false
jsonparser.ArrayEach(slice, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
if err != nil {
return
}
username, _ := jsonparser.GetString(value, "UserName")
if username == str {
res = true
}
})
return res
}
/** 获取Webwxsync数据
* @return []byte webwxsync的响应body
* @return error 错误信息
*/
func getWebwxsync() ([]byte, error) {
url := fmt.Sprintf(wxurl + "/cgi-bin/mmwebwx-bin/webwxsync?sid=%s&skey=%s&pass_ticket=%s",webwx.Sid, webwx.Skey, webwx.PassTicket)
data := fmt.Sprintf(
`{
"BaseRequest":{
"Uin":%s,
"Sid":"%s",
"Skey":"%s",
"DeviceID":"%s"
},
"SyncKey":%s,
"rr":%s
}`,
webwx.Uin,webwx.Sid,webwx.Skey,webwx.DeviceID,webwx.Synckey,webwx.Uin)
req, err := http.NewRequest("POST",url,strings.NewReader(data))
if err != nil {
return []byte{}, err
}
req.Header.Add("Cookie","wxuin="+webwx.Uin+";wxsid="+webwx.Sid)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return []byte{}, fmt.Errorf("E00001: Please check your network connection.")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return []byte{}, fmt.Errorf("E00002: Failed to read response body.")
}
// 更新Synckey
SyncKey, _, _, err := jsonparser.Get(body,"SyncKey")
if err != nil {
return []byte{}, fmt.Errorf("E00007: %s",err)
}
webwx.Synckey = SyncKey
return body, nil
}
/** 获取Webwxsync中的群聊
*/
func getChatRoomFromWebwxsync() {
StatusNotifyUserName := ""
for i := 0; i < 20; i++ {
webwxsync, err := getWebwxsync()
if err != nil {
continue
}
AddMsgList, _, _, err := jsonparser.Get(webwxsync, "AddMsgList")
if err != nil {
continue
}
// 遍历 AddMsgList 数组中的每个元素
jsonparser.ArrayEach(AddMsgList, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
if err != nil {
return
}
StatusNotifyUserName, _ = jsonparser.GetString(value, "StatusNotifyUserName")
})
if StatusNotifyUserName != "" {
break
}
}
if StatusNotifyUserName == "" {
fmt.Println("[!] 未获取到群聊")
return
}
var chatroomarray []map[string]interface{}
parts := strings.Split(StatusNotifyUserName, ",")
for _, part := range parts {
trimmedPart := strings.TrimSpace(part)
if strings.HasPrefix(trimmedPart, "@@") && !contains(chatrooms, trimmedPart) {
ContactList := getBatchContact(trimmedPart)
tempnick := ""
var tempcount int64
jsonparser.ArrayEach(ContactList, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
if err != nil {
return
}
tempnick,_ = jsonparser.GetString(value,"NickName")
tempcount,_ = jsonparser.GetInt(value,"MemberCount")
})
data := make(map[string] interface{})
data["UserName"] = trimmedPart
data["NickName"] = tempnick
data["MemberCount"] = tempcount
chatroomarray = append(chatroomarray, data)
}
}
chatrooms,_ = json.Marshal(chatroomarray)
}
/**
* 获取消息
*/
func GetMsg() MsgData {
var createtime,srctype,msgtype int64
fromuser := ""
srcname := ""
srcuser := ""
msg := ""
for {
retcode, selector := syncCheck()
if retcode != 0 {
return MsgData{Srctype: retcode,Srcname: srcname,Msgtype: msgtype,Fromuser: fromuser,Usernick: srcuser,Msg: msg,Createtime: createtime}
}
if selector == 0 {
continue
} else {
break
}
}
body, err := getWebwxsync()
if err != nil {
fmt.Println("[!]",err)
return MsgData{Srctype: srctype,Srcname: srcname,Msgtype: msgtype,Fromuser: fromuser,Usernick: srcuser,Msg: msg,Createtime: createtime}
}
// 获取消息
count, _ := jsonparser.GetInt(body,"AddMsgCount")
if count == 0 {
return MsgData{Srctype: srctype,Srcname: srcname,Msgtype: msgtype,Fromuser: fromuser,Usernick: srcuser,Msg: msg,Createtime: createtime}
}
msglist, _, _, err := jsonparser.Get(body,"AddMsgList")
jsonparser.ArrayEach(msglist, func(value []byte, dataType jsonparser.ValueType, offset int, err error){
if err != nil {
return
}
fromuser, _ = jsonparser.GetString(value,"FromUserName")
if fromuser == myself.UserName {
return
}
createtime, _ = jsonparser.GetInt(value,"CreateTime")
msgtype,_ = jsonparser.GetInt(value,"MsgType")
if msgtype == 51 {
return
}
content,_ := jsonparser.GetString(value,"Content")
appmsgtype,_ := jsonparser.GetInt(value,"AppMsgType")
// 群聊消息
if strings.HasPrefix(fromuser, "@@") {
srctype = 1
user := strings.Split(content, ":")
if len(user) < 2 {
srctype = 0
return
}
msg = user[1]
chatroominfo := getBatchContact(fromuser)
jsonparser.ArrayEach(chatroominfo, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
if err != nil {
return
}
srcname,_ = jsonparser.GetString(value,"NickName")
memberlist,_ ,_ ,err := jsonparser.Get(value,"MemberList")
if err != nil {
fmt.Println(err)
}
jsonparser.ArrayEach(memberlist, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
if err != nil {
return
}
membername,_ := jsonparser.GetString(value,"UserName")
if membername == user[0] {
srcuser,_ = jsonparser.GetString(value,"NickName")
}
})
})
switch msgtype {
case 1:
//fmt.Println("[+]",msgtime,"群 [",srcname,"] 成员 [",srcuser,"] 的文本消息:",msg)
return //MsgData{Srctype: srctype,Srcname: srcname,Msgtype: msgtype,Fromuser: fromuser,Usernick: srcuser,Msg: msg,Createtime: createtime}
case 3:
msgid,_ := jsonparser.GetString(value,"MsgId")
msg = wxurl + "/cgi-bin/mmwebwx-bin/webwxgetmsgimg?&MsgID=" + msgid + "&skey=" + webwx.Skey
//fmt.Println("[+]",msgtime,"群 [",srcname,"] 成员 [",srcuser,"] 的图片消息:",msg)
return //MsgData{Srctype: srctype,Srcname: srcname,Msgtype: msgtype,Fromuser: fromuser,Usernick: srcuser,Msg: msg,Createtime: createtime}
//case 34:
// fmt.Println("[+]",msgtime,"群 [",srcname,"] 成员 [",srcuser,"] 的语音消息暂不支持获取")
//case 43:
// fmt.Println("[+]",msgtime,"群 [",srcname,"] 成员 [",srcuser,"] 的视频消息暂不支持获取")
case 47:
re := regexp.MustCompile(`(?i)cdnurl\s*=\s*"(.*?)"`)
match := re.FindStringSubmatch(content)
if len(match) > 1 {
msg = html.UnescapeString(html.UnescapeString(match[1]))
//fmt.Println("[+]",msgtime,"群 [",srcname,"] 成员 [",srcuser,"] 的表情包消息:",msg)
return //MsgData{Srctype: srctype,Srcname: srcname,Msgtype: msgtype,Fromuser: fromuser,Usernick: srcuser,Msg: msg,Createtime: createtime}
}
srctype = 0
case 49:
switch appmsgtype {
case 5:
title,_ := jsonparser.GetString(value,"FileName")
msg,_ = jsonparser.GetString(value,"Url")
msg = title + html.UnescapeString(msg)
//fmt.Println("[+]",msgtime,"群 [",srcname,"] 成员 [",srcuser,"] 的公众号消息:",title + msg)
return //MsgData{Srctype: srctype,Srcname: srcname,Msgtype: msgtype,Fromuser: fromuser,Usernick: srcuser,Msg: msg,Createtime: createtime}
//case 6:
// fmt.Println("[+]",msgtime,"群 [",srcname,"] 成员 [",srcuser,"] 的文件消息,暂不支持获取")
//case 51:
// fmt.Println("[+]",msgtime,"群 [",srcname,"] 成员 [",srcuser,"] 的视频号消息,暂不支持获取")
default:
//fmt.Println("[+]",msgtime,"群 [",srcname,"] 成员 [",srcuser,"] 的其它类型消息,暂不支持获取")
return //MsgData{Srctype: srctype,Srcname: srcname,Msgtype: msgtype,Fromuser: fromuser,Usernick: srcuser,Msg: msg,Createtime: createtime}
}
case 10000:
return //MsgData{Srctype: srctype,Srcname: srcname,Msgtype: msgtype,Fromuser: fromuser,Usernick: srcuser,Msg: msg,Createtime: createtime}
default:
//fmt.Println("[+] 收到其他消息:",string(content))
return //MsgData{Srctype: srctype,Srcname: srcname,Msgtype: msgtype,Fromuser: fromuser,Usernick: srcuser,Msg: msg,Createtime: createtime}
}
} else {
srcuser = getNicknameByUsername(fromuser,"contactlist")
// 公众号消息
if appmsgtype == 5 {
srctype = 2
srcname = getNicknameByUsername(fromuser,"officiallist")
title,_ := jsonparser.GetString(value,"FileName")
msg_url,_ := jsonparser.GetString(value,"Url")
msg_url = html.UnescapeString(msg_url)
msg = title + msg_url
//fmt.Println("[+]",msgtime,"公众号 [",srcname,"] 的消息:",msg)
return //MsgData{Srctype: srctype,Srcname: srcname,Msgtype: msgtype,Fromuser: fromuser,Usernick: srcuser,Msg: msg,Createtime: createtime}
} else {// 联系人消息
srctype = 3
remarkname := getRemarkNameByUsername(fromuser)
if remarkname != "" {
srcuser = remarkname
}
//content,_ := jsonparser.GetString(value,"Content")
//msgtype,_ := jsonparser.GetInt(value,"MsgType")
switch msgtype {
case 1:
msg = content
//fmt.Println("[+]",msgtime,"[ ",srcuser,"] 的文本消息:",content)
return //MsgData{Srctype: srctype,Srcname: srcname,Msgtype: msgtype,Fromuser: fromuser,Usernick: srcuser,Msg: msg,Createtime: createtime}
case 3:
msgid,_ := jsonparser.GetString(value,"MsgId")
msg = wxurl + "/cgi-bin/mmwebwx-bin/webwxgetmsgimg?&MsgID=" + msgid + "&skey=" + webwx.Skey
//fmt.Println("[+]",msgtime,"[ ",srcuser,"] 的图片消息:",msg)
return //MsgData{Srctype: srctype,Srcname: srcname,Msgtype: msgtype,Fromuser: fromuser,Usernick: srcuser,Msg: msg,Createtime: createtime}
//case 34:
// fmt.Println("[+]",msgtime,"[ ",srcuser,"] 的语音消息暂不支持获取")
//case 43:
// fmt.Println("[+]",msgtime,"[ ",srcuser,"] 的视频消息暂不支持获取")
case 47:
re := regexp.MustCompile(`(?i)cdnurl\s*=\s*"(.*?)"`)
match := re.FindStringSubmatch(content)
if len(match) > 1 {
msg = html.UnescapeString(html.UnescapeString(match[1]))
//fmt.Println("[+]",msgtime,"[ ",srcuser,"] 的表情包消息:",msg)
return //MsgData{Srctype: srctype,Srcname: srcname,Msgtype: msgtype,Fromuser: fromuser,Usernick: srcuser,Msg: msg,Createtime: createtime}
}
srctype = 0
case 49:
appmsgtype,_ := jsonparser.GetInt(value,"AppMsgType")
switch appmsgtype {
case 5:
title,_ := jsonparser.GetString(value,"FileName")
msg,_ = jsonparser.GetString(value,"Url")
msg = title + html.UnescapeString(msg)