-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmessengerlogin.py
More file actions
1153 lines (1079 loc) · 56.7 KB
/
messengerlogin.py
File metadata and controls
1153 lines (1079 loc) · 56.7 KB
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
"""messenger login"""
# -*- coding:utf-8 -*-
import re
import traceback
from urllib import parse
from lxml import etree
import base64
import json
import websocket
import random
from commonbaby.helpers import helper_str, helper_time
from datacontract.idowndataset import Task
from .messengerbase import MessengerBase
class MessengerLogin(MessengerBase):
"""messenger login"""
# 最外层是__d("LSGraphqlInitialSyncQuery.graphql"
# params: {
# id: "3325130330947119",
# metadata: {},
# name: "LSGraphqlInitialSyncQuery",
# operationKind: "query",
# text: null
# }
_re_docid_LSGraphqlInitialSyncQuery = re.compile(
r'__d\("LSGraphqlInitialSyncQuery.graphql".*?params:\s*?(.*?name:\s*?"LSGraphqlInitialSyncQuery".*?\s*?})',
re.S)
def __init__(self, task: Task, appcfg, clientid):
MessengerBase.__init__(self, task, appcfg, clientid)
def _sms_login_(self) -> bool:
res: bool = False
try:
res = self.__sms_login()
if res and not (isinstance(self._username, str)
or self._username == ""):
res, msg = self._access_profile()
if not isinstance(self._host, str) or self._host == "":
self._host = 'facebook.com'
except Exception:
self._logger.error("Sms login error:%s" % traceback.format_exc())
return res
def _cookie_login_(self) -> bool:
"""用cookie登陆尝试"""
res: bool = False
try:
# self._ha._managedCookie.add_cookies(".facebook.com",
# self.task.cookie)
res, msg = self._refresh_neccessary_fields()
if not res:
self._logger.error("Cookie login failed: {}".format(msg))
return res
res, msg = self._access_profile()
if not res:
self._logger.error("Cookie login failed: {}".format(msg))
return res
if not isinstance(self._host, str) or self._host == "":
self._host = 'facebook.com'
msg = "登录成功"
res = True
except Exception:
self._logger.error(
"Cookie login error:%s" % traceback.format_exc())
return res
def __sms_login(self) -> bool:
"""拿手机号去登陆,看能不能拿到验证码?"""
res: bool = False
try:
if not isinstance(self.phone, str) or self.phone == "":
self._logger.error("Phone number '%s' is invalid" % self.phone)
return res
phone = self.phone
# 登陆首页拿参数
url = "https://www.messenger.com/login.php"
html = self._ha.getstring(
url,
headers="""
accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
accept-encoding: gzip, deflate
accept-language: zh-CN,zh;q=0.9
cache-control: no-cache
pragma: no-cache
upgrade-insecure-requests: 1
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36
""")
# "_js_datr","egJxW0NbHZJOq9uB6i8b165T"
# 加cookie
succ, _js_datr = helper_str.substringif(html, '"_js_datr","', '"')
if not succ:
raise Exception("_js_datr not found.")
self._ha._managedCookie.add_cookies("messenger.com",
"_js_datr=%s" % _js_datr)
self._ha._managedCookie.add_cookies("facebook.com",
"datr=%s" % _js_datr)
# "identifier":"f348eeb7776c1fa9c07e9e74ba25bece"
# post data
succ, identifier = helper_str.substringif(html, '"identifier":"',
'"')
if not succ:
raise Exception("identifier not found.")
# //"initialRequestID":"ASkMH7-Nr9L8BiOCVp8Uvsk"
# //post data
succ, initial_requestid = helper_str.substringif(
html, '"initialRequestID":"', '"')
if not succ:
raise Exception("initial_requestid not found.")
# //"pkg_cohort":"PHASED:messengerdotcom_pkg"
# //post data
succ, self._pc = helper_str.substringif(html, 'pkg_cohort":"', '"')
if not succ:
raise Exception("__pc not found.")
# //{"server_revision":4201737,
# //post data
succ, self._rev = helper_str.substringif(html, 'server_revision":',
',')
if not succ:
raise Exception("__rev not found.")
# //<input type="hidden" name="lsd" value="AVoa4_T_" autocomplete="off" />
# //post data
succ, self.lsd = helper_str.substringif(html, 'name="lsd" value="',
'"')
if not succ:
raise Exception("lsd not found.")
# 这个请求里设置了cookie
pnl_data2 = helper_str.base64str(
'{"a":"all_pagelets_displayed","c":"XMessengerDotComLoginPageController","b":false,"d":"/login/identify","e":["primer_no_pagetrans"]}'
)
self._ha._managedCookie.add_cookies("messenger.com",
"pnl_data2=%s" % pnl_data2)
url = "https://www.messenger.com/ajax/bz"
postdata = (
'__a=1&__be=-1&__pc=' + parse.quote_plus(self._pc) + '&__req='
+ parse.quote_plus(self._req.get_next()) + '&__rev=' +
parse.quote_plus(
self._rev) + '&__user=0&lsd=' + parse.quote_plus(self.lsd)
+ '&ph=C3&q=%5B%7B%22user%22%3A%220%22%2C%22page_id'
'%22%3A%22bkhrdy%22%2C%22posts%22%3A%5B%5B%22clic'
'k_ref_logger%22%2C%5B%220ZDU%22%2C1534132912147%'
'2C%22act%22%2C1534132912145%2C0%2C%22https%3A%2F'
'%2Fwww.facebook.com%2Flogin%2Fidentify%3Fctx%3Dr'
'ecover%22%2C%22click%22%2C%22click%22%2C%22-%22%'
'2C%22r%22%2C%22%2Flogin%2F%22%2C%7B%22ft%22%3A%7'
'B%7D%2C%22gt%22%3A%7B%7D%7D%2C0%2C0%2C0%2C0%2C%2'
'2bkhrdy%22%2C%22XMessengerDotComLoginPageControl'
'ler%22%5D%2C1534132912145.8%2C0%5D%5D%2C%22trigg'
'er%22%3A%22click_ref_logger%22%2C%22send_method%'
'22%3A%22ajax%22%7D%5D&ts=' + str(helper_time.ts_since_1970()))
# &__dyn=5V8WXBzamaUmgDxKS5k2m3miWGey8jrWo466EeAq2i5U4e2CEaUgxebkwy6UnGiidz9XDG4XzEa8iyA14zorx64oKjG2e5UC4bz8gxO1iyECUd8hxG1awxwxgeEtxK1fwLhob876u4rGUpCwCGm8xC784a3mbwExuazoK13x3yUWfxu8CwKwCzUR1zAz8bAu9xm3edBAyEOfBK6o-6UG6EO1pGFUaUvxucy8KV8zwEwFypUKUbUgzVU
html = self._ha.getstring(
url,
req_data=postdata,
headers="""
accept: */*
accept-encoding: gzip, deflate
accept-language: zh-CN,zh;q=0.9
cache-control: no-cache
content-type: application/x-www-form-urlencoded
origin: https://www.messenger.com
referer: https://www.messenger.com/login/""")
# 忘记密码页面,设置cookie sb=rwJxWwQgofG3h1HEQAuVRhAN;
url = "https://www.facebook.com/login/identify?ctx=recover"
self._ha._managedCookie.add_cookies(
"messenger.com", "fr=00UVPcWMVc9W2tUi2..BbcQJ8...1.0.BbcQJ8.")
html = self._ha.getstring(
url,
headers="""
accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
accept-encoding: gzip, deflate
accept-language: zh-CN,zh;q=0.9
cache-control: no-cache
referer: https://www.messenger.com/
upgrade-insecure-requests: 1""")
# <input type="hidden" name="lsd" value="AVoa4_T_" autocomplete="off" />
# post data
succ, self.lsd = helper_str.substringif(html, 'name="lsd" value="',
'"')
if not succ:
raise Exception("lsd not found.")
# {"server_revision":4201737,
# post data
succ, self._rev = helper_str.substringif(html,
'"server_revision":', ',')
if not succ:
raise Exception("__rev not found.")
# "pkg_cohort":"PHASED:messengerdotcom_pkg"
# post data
succ, self._pc = helper_str.substringif(html, 'pkg_cohort":"', '"')
if not succ:
raise Exception("__pc not found.")
# 点击搜索后,有个自动发送的,设置了很多cookie的
url = "https://www.facebook.com/cookie/consent/?dpr=1"
self._ha._managedCookie.add_cookies(
"facebook.com",
"_js_reg_ext_ref=https%3A%2F%2Fwww.messenger.com%2F;")
self._ha._managedCookie.add_cookies(
"facebook.com",
r"_js_reg_fb_ref=https%3A%2F%2Fwww.facebook.com%2Flogin%2Fidentify%3Fctx%3Drecover;"
)
self._ha._managedCookie.add_cookies(
"facebook.com",
r"_js_reg_fb_gate=https%3A%2F%2Fwww.facebook.com%2Flogin%2Fidentify%3Fctx%3Drecover;"
)
html = self._ha.getstring(
url,
headers="""
accept: */*
accept-encoding: gzip, deflate
accept-language: zh-CN,zh;q=0.9
cache-control: no-cache
content-type: application/x-www-form-urlencoded
origin: https://www.facebook.com
referer: https://www.facebook.com/login/identify?ctx=recover"""
)
# 检查账号是否存在
url = "https://www.facebook.com/ajax/login/help/identify.php?ctx=recover&dpr=1"
did_submit = "搜索"
postdata = (
r'lsd=' + parse.quote_plus(self.lsd) + r'&email=' +
parse.quote_plus(phone) + r'&did_submit=' +
parse.quote_plus(did_submit) + r'&__user=0&__a=1&__req=' +
self._req.get_next() + r'&__be=-1&__pc=' + parse.quote_plus(
self._pc) + r'&__rev=' + parse.quote_plus(self._rev))
html = self._ha.getstring(
url,
req_data=postdata,
headers="""
accept: */*
accept-encoding: gzip, deflate
accept-language: zh-CN,zh;q=0.9
cache-control: no-cache
content-type: application/x-www-form-urlencoded
origin: https://www.facebook.com
pragma: no-cache
referer: https://www.facebook.com/login/identify?ctx=recover"""
)
if html is None or html == "":
raise Exception("Check if account registered failed.")
accExists = False
# 你的搜索没有返回任何结果。请换用其他信息再试一次。
if r'\u4f60\u7684\u641c\u7d22\u6ca1\u6709\u8fd4\u56de\u4efb\u4f55\u7ed3\u679c\u3002\u8bf7\u6362\u7528\u5176\u4ed6\u4fe1\u606f\u518d\u8bd5\u4e00\u6b21\u3002\u003' in html:
# raise Exception("Account not exists: %s" % phone)
self._logger.info('%s %s 你的搜索没有返回任何结果。请换用其他信息再试一次' %
(self.task.taskid, self._username))
# 识别您的账户
elif r'\u8bc6\u522b\u60a8\u7684\u5e10\u6237\u003C' in html:
# raise Exception("Account not unique, need to select one: %s" % phone)
self._logger.info('%s %s 结果不止一个,请识别您的账户' % (self.task.taskid,
self._username))
else:
accExists = True
if not accExists:
return res
# "onload": [
# "window.location.href=\"\\\/recover\\\/initiate\\\/?ldata=AWfsYMWVOOUqIwhHc2GGQ9EwqHBZdawTEWgiu-s7AvwlvB-KV5HUau4AOMQ_vIT46Ug1aicvq_Q_ZbvGWckaey0lRPHIPI1hGV_qf0TkrS2lqS9Nqh4KNXHuWd9iyLwN1x1iTmnMibf_cS9UvVrK5Y7EQUMt6iX9x5vFllbXdjTYexBNzcvDIPwctWcxB5Z-uv84i8tM3WjxLITX8T6aV0aax4x_Yzj-2dF6Al_wEjDqMXxKmk_Alekb1fOD-78VqIg\""
# ],
succ, tmp = helper_str.substringif(html, 'onload":[', ']')
if not succ or tmp is None or tmp == '':
raise Exception(
"get check account registration redirection failed.")
tmp = tmp.replace(r'\\"', '"').replace('\\', '').replace(
'\\/', '/')
succ, reUrl = helper_str.substringif(tmp, 'window.location.href="',
'"')
if not succ or reUrl is None or reUrl == '':
raise Exception(
"get check account registration redirection failed.")
redir = "https://www.facebook.com/%s" % reUrl.strip('/')
# 检查账号成功的请求会设置cookie "sfiu",后面postdata好像会用到
if not self._ha._managedCookie.contains_cookie('sfiu'):
raise Exception("sfui not found.")
sfiu = self._ha._managedCookie.get_cookie_value("sfiu")
if sfiu is None or sfiu == None:
raise Exception("sfui not found.")
# 检查账号后 跳转链接
htmlSms = self._ha.getstring(
redir,
headers="""
accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
accept-encoding: gzip, deflate
accept-language: zh-CN,zh;q=0.9
cache-control: no-cache
referer: https://www.facebook.com/login/identify?ctx=recover
upgrade-insecure-requests: 1""")
if not 'class="inputtext"' in htmlSms:
raise Exception("Select sms verify type failed.")
# 有个值后面要POST用,也可能不用,而是验证码直接已经发送了,不用选择验证方式。
# <input type="radio" id="send_sms:AYiYdwu_ywchlQTBjq_uGE-rtBcz
# 8gUVGgwXsH8CDsB8Zpp16qGxIJ-MaL3cfEsaGZg" name="recover_metho
# d" value="send_sms:AYiYdwu_ywchlQTBjq_uGE-rtBcz8gUVGgwXsH8CD
# sB8Zpp16qGxIJ-MaL3cfEsaGZg" checked="1" class="uiInputLabelI
# nput uiInputLabelRadio" />
clickToSend = True
succ, recover_method = helper_str.substringif(
htmlSms, 'type="radio" id="', '"')
if not succ or recover_method is None or recover_method == '':
self._logger.debug("recover_method not found.")
clickToSend = False
hash_ = ""
if clickToSend:
# 点击 是我,继续 发送验证码
url = "https://www.facebook.com/ajax/recover/initiate/?dpr=1"
postdata = (
r'lsd=' + parse.quote_plus(self.lsd) +
r'&openid_provider_id=&openid_provider_name=&' +
r'recover_method=' + parse.quote_plus(recover_method) +
r'&reset_action=1&__user=0&__a=1&' + r'__req=' +
parse.quote_plus(self._req.get_next()) + r'&__be=-1&__pc='
+ parse.quote_plus(
self._pc) + r'&__rev=' + parse.quote_plus(self._rev))
html = self._ha.getstring(
url,
req_data=postdata,
headers="""
accept: */*
accept-encoding: gzip, deflate
accept-language: zh-CN,zh;q=0.9
content-type: application/x-www-form-urlencoded
origin: https://www.facebook.com
referer: %s""" % redir)
# 有个跳转链接,不知道是搞啥的
# "redirectPageTo",[],["https:\/\/www.facebook.com\/recover\/code\/?ce=1625022&rm=send_sms&hash=AUbXIrQtnKwFxe_P",false,false
succ, reDirPageTo = helper_str.substringif(
html, 'redirectPageTo",[],["', '"')
if not succ or reDirPageTo is None or reDirPageTo == "":
raise Exception("redirect page to failed.")
reDirPageTo = reDirPageTo.replace('\\', '')
# 有个hash
succ, hash_ = helper_str.substringif(html, 'hash=', '"')
if not succ or hash_ is None or hash_ == "":
raise Exception("hash not found.")
# 搞验证码
smscode: str = self._get_vercode()
# 进入验证码页面准备输入验证码
urlSendSms = 'https://www.facebook.com/recover/code/?ph[0]=%s&rm=send_sms&hash=%s' % (
parse.quote_plus(phone), hash_)
html = self._ha.getstring(
urlSendSms,
headers="""
accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
accept-encoding: gzip, deflate
accept-language: zh-CN,zh;q=0.9
referer: %s
upgrade-insecure-requests: 1""" % redir)
if not 'class="inputtext"' in html:
raise Exception("Send sms failed.")
# 点击继续,进入改密页面
url = "https://www.facebook.com/recover/code?ph[0]=%s&recover_method=send_email&wsr=0" % (
parse.quote_plus(phone))
postdata = 'lsd=' + parse.quote_plus(
self.lsd) + '&n=' + parse.quote_plus(
smscode) + '&reset_action=1'
html, reDirModifyPwd = self._ha.getstring_unredirect(
url,
req_data=postdata,
headers="""
accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
accept-encoding: gzip, deflate
accept-language: zh-CN,zh;q=0.9
content-type: application/x-www-form-urlencoded
origin: https://www.facebook.com
referer: %s
upgrade-insecure-requests: 1""" % urlSendSms)
if reDirModifyPwd is None or reDirModifyPwd == "":
raise Exception("Jump to password modify page1 failed.")
# 跳转链接里的u= 就是用户id
# https://www.facebook.com/recover/password/?u=xxx&n=599428&fl=default_recover&sih=0
succ, userid = helper_str.substringif(reDirModifyPwd, 'u=', '&')
if not succ or userid is None or userid == '':
raise Exception("Userid not found.")
self._userid = userid
# 继续跳转
html = self._ha.getstring(
reDirModifyPwd,
headers="""
accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
accept-encoding: gzip, deflate
accept-language: zh-CN,zh;q=0.9
content-type: application/x-www-form-urlencoded
origin: https://www.facebook.com
referer: %s
upgrade-insecure-requests: 1""" % urlSendSms)
if 'id="password_new"' not in html:
raise Exception("Jump to password modify page2 failed.")
# 点击跳过改密,直接登录,这个请求会有很多setcookie跳转到facebook
url = "https://www.facebook.com/recover/password/?u=%s&n=%s&by&bm&bd&fl=default_recover" % (
userid, smscode)
postdata = 'lsd=%s&password_new=&btn_skip=1' % self.lsd
html, reDirHome = self._ha.getstring_unredirect(
url,
req_data=postdata,
headers="""
accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
accept-encoding: gzip, deflate
accept-language: zh-CN,zh;q=0.9
content-type: application/x-www-form-urlencoded
origin: https://www.facebook.com
referer: %s
upgrade-insecure-requests: 1""" % reDirModifyPwd)
if reDirHome is None or reDirHome == "" or not "www.facebook.com" in reDirHome:
raise Exception("Jump to home page failed.")
succ, msg = self._refresh_neccessary_fields()
if not succ:
self._logger.info(msg)
return succ
succ = self._access_profile()
res = succ
except Exception:
self._logger.error("Sms login error:\ntaskid:%s\nerror:%s" %
(self.task.taskid, traceback.format_exc()))
return res
def _refresh_neccessary_fields(self) -> (bool, str):
"""refresh neccessary fields from homepage"""
succ: bool = False
msg: str = None
try:
# facebook
url = "https://www.facebook.com/"
homepage = self._ha.getstring(
url,
headers="""
accept: 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
accept-encoding: gzip, deflate
accept-language: en-US,en;q=0.9
cache-control: no-cache
content-type: application/x-www-form-urlencoded
pragma: no-cache
sec-fetch-mode: cors
sec-fetch-site: same-origin
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36""")
if homepage is None or homepage == "":
raise Exception("Get userid failed.")
self.homepage = homepage
# {"USER_ID":"100027859862248","ACCOUNT_ID":"100027859862248","NAME":"San Zhang","SHORT_NAME":"San Zhang","IS_MESSENGER_ONLY_USER":false,"IS_DEACTIVATED_ALLOWED_ON_MESSENGER":false}
succ, self._userid = helper_str.substringif(
homepage, 'USER_ID":"', '"')
if not succ or self._userid is None or self._userid == "":
raise Exception("Get userid failed")
if self._username is None or self._username == "":
succ, username = helper_str.substringif(
homepage, 'NAME":"', '')
if succ and not self._username is None and not self._username == "":
self._username = username
# "async_get_token":"Adz9YM4ErUVi1H0azTmDnBX6Md_LsWwifZoVLsZMMIUakA:Adw2zeZ-U8JV7IoOmqmOkppLJGU_FOCjIicjrtSGkHuS-g"}
succ, self.fb_dtsg_ag = helper_str.substringif(
homepage, 'async_get_token":"', '"')
if not succ or self.fb_dtsg_ag is None or self.fb_dtsg_ag == "":
raise Exception("fb_dtsg_ag not found")
# "__spin_r":4206568,
succ, self._spin_r = helper_str.substringif(
homepage, '__spin_r":', ',')
if not succ or self._spin_r is None or self._spin_r == "":
raise Exception("__spin_r not found. account may be locked")
# "__spin_r":4206568,"__spin_b":"trunk","__spin_t":1534242344,
succ, self._spin_t = helper_str.substringif(
homepage, '__spin_t":', ',')
if not succ or self._spin_t is None or self._spin_t == "":
raise Exception("__spin_t not found")
succ, self._spin_b = helper_str.substringif(
homepage, '__spin_b":', ',')
if not succ or self._spin_b is None or self._spin_b == "":
raise Exception("__spin_b not found")
self._spin_b = self._spin_b.strip().strip('"')
# "hsi":"6746747510353494316-0"
succ, self.hsi = helper_str.substringif(homepage, '"hsi":"', '"')
if not succ or self.hsi is None or self.hsi == "":
raise Exception("hsi not found")
# {"name":"fb_dtsg","value":"AQHRzELKyYTl:AQFKH4XWkHlv"}
succ, self.fb_dtsg = helper_str.substringif(homepage, '"name":"fb_dtsg","value":"', '"')
if not succ or self.fb_dtsg is None or self.fb_dtsg == "":
raise Exception("fb_dtsg not found")
# {"name":"jazoest","value":"22097"}
succ, self.jazoest = helper_str.substringif(
homepage, '"name":"jazoest","value":"', '"')
if not succ or self.jazoest is None or self.jazoest == "":
raise Exception("jazoest not found")
# 没有QuicklingConfig
# ,"pkg_cohort":"EXP2:comet_pkg","
succ, self._pc = helper_str.substringif(
homepage, '"pkg_cohort":"', '"')
if not succ or self._pc is None or self._pc == "":
raise Exception("_pc not found")
# "client_revision":1003006555,
succ, self._rev = helper_str.substringif(
homepage, '"client_revision":', ',')
if not succ or self._rev is None or self._rev == "":
raise Exception("_rev not found")
# 获取个人主页地址
html, redir = self._ha.getstring_unredirect('https://www.facebook.com/me',
headers="""
accept: 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
accept-encoding: gzip, deflate
accept-language: en-US,en;q=0.9
sec-fetch-dest: document
sec-fetch-mode: navigate
sec-fetch-site: none
sec-fetch-user: ?1
upgrade-insecure-requests: 1
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.75 Safari/537.36"""
)
if redir is None or redir == "":
raise Exception("进入个人主页失败")
self._host = redir
# 进入messenger页面(通过facebook的cookie进入,如果不是,url需要改)
# "deviceId":"39f79811-b3a7-447e-8483-7d2f59e35915","schemaVersion":"3696807697038235",
re_params = re.compile(r'"deviceId":"(.*?)","schemaVersion":"(.*?)",')
# {"epochId":"6754309714097997511"},5634]
re_epochid = re.compile(r'"epochId":"(.*?)"}')
# "fbid":"100054477585089","appID":219994525426954,
re_aid = re.compile(r'"fbid":".*?","appID":(.*?),')
# {"app_id":"772021112871879"}
re_appid = re.compile(r'\{"app_id":"(\d+)"\}')
url = 'https://www.facebook.com/messages'
html = self._ha.getstring(url, headers='''
accept: 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
accept-encoding: gzip, deflate
accept-language: zh-CN,zh;q=0.9,zh-TW;q=0.8,en-US;q=0.7,en;q=0.6
sec-fetch-dest: document
sec-fetch-mode: navigate
sec-fetch-site: none
sec-fetch-user: ?1
upgrade-insecure-requests: 1
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36
''')
m = re.search(re_params, html)
if m is None:
raise Exception("进入messenger页面失败")
self.device_id = m.group(1)
self.schema_version = m.group(2)
# epoch_id
m = re.search(re_epochid, html)
if m is None:
raise Exception("epoch id not found")
self.epoch_id = m.group(1)
# aid
m = re.search(re_aid, html)
if m is None:
raise Exception('aid not found')
self.aid = m.group(1)
# app_id
m = re.search(re_appid, html)
if m is None:
raise Exception("app id not found")
self.appid = m.group(1)
# if 'lightspeed_web_initial_sync_v2' in html:
# re_respnse_js = re.compile(r'"lightspeed_web_initial_sync_v2":\{.*?"response":\[("function.*?),\s*"function.*?\]\}\}\}')
# m = re_respnse_js.search(html)
# if m is None:
# raise Exception("Get js function fail")
# js_func = m.group(1)
# if js_func is None or js_func == '':
# raise Exception("Get js function fail")
# res = self._parse_init_js(js_func)
# if not res:
# raise Exception("处理初始消息js失败")
succ = True
msg = "Refresh neccessary fields succeed."
except Exception:
self._logger.error(
"Refresh neccessary fields from homepage error, taskid:%s\nphone:%s\nex:%s"
% (self.task.taskid, self.phone, traceback.format_exc()))
succ = False
msg = "Refresh neccessary fields failed."
return (succ, msg)
def _access_profile(self) -> (bool, str):
"""access simple profile"""
try:
url = "https://www.facebook.com/"
html = self._ha.getstring(
url,
headers="""
accept: 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
accept-encoding: gzip, deflate
accept-language: en-US,en;q=0.9
cache-control: no-cache
content-type: application/x-www-form-urlencoded
pragma: no-cache
sec-fetch-mode: cors
sec-fetch-site: same-origin
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"""
)
if html is None or html == "":
raise Exception("Get profile page failed.")
# {"USER_ID":"100027859862248","ACCOUNT_ID":"100027859862248",
# "NAME":"San Zhang","SHORT_NAME":"San Zhang","IS_MESSENGER_ON
# LY_USER":false,"IS_DEACTIVATED_ALLOWED_ON_MESSENGER":false}
succ, self._userid = helper_str.substringif(
html, 'USER_ID":"', '"')
if not succ or self._userid is None or self._userid == "":
succ, self._userid = helper_str.substringif(
html, 'user_id:"', '"')
if not succ or self._username is None or self._username == "":
msg = "访问个人信息失败"
return (succ, msg)
succ, self._username = helper_str.substringif(html, 'NAME":"', '"')
if not succ or self._username is None or self._username == "":
succ, self._username = helper_str.substringif(
html, 'name:"', '"')
if not succ or self._username is None or self._username == "":
msg = "访问个人信息失败"
return (succ, msg)
succ, self.is_messenger_only_user = helper_str.substringif(
html, 'IS_MESSENGER_ONLY_USER":', ',')
if succ and not self.is_messenger_only_user is None:
if self.is_messenger_only_user == "false":
self.is_messenger_only_user = False
else:
self.is_messenger_only_user = True
succ, self.is_deactived_allowed_on_messenger = helper_str.substringif(
html, 'IS_DEACTIVATED_ALLOWED_ON_MESSENGER":', ',')
if succ and not self.is_deactived_allowed_on_messenger is None:
if self.is_deactived_allowed_on_messenger == "false":
self.is_deactived_allowed_on_messenger = False
else:
self.is_deactived_allowed_on_messenger = True
succ = True
msg = "访问个人信息成功"
except Exception:
self._logger.error(
"Access profile error:%s" % traceback.format_exc())
succ = False
msg = "访问个人信息失败"
return (succ, msg)
def _get_js_resources(self) -> iter:
"""下载并迭代返回所有页面里的js资源,用于查找各种docid..."""
try:
with self._jspages_locker:
if self._jspages_listpage is None:
# 拿资源列表页面
url = (
'https://www.facebook.com/ajax/bootloader-endpoint/?' +
'modules=NotificationList.react%2CNotificationJewelL' +
'ist.react%2CNotificationAsyncWrapper%2CNotification' +
'Store%2CNotificationJewelController%2CMercuryJewel%' +
'2CMercuryThreadInformer%2CMessengerState.bs%2CMesse' +
'ngerGraphQLThreadlistFetcher.bs%2CMercuryServerRequ' +
'ests%2CMercuryJewelUnreadCount.bs&' + '__user=' +
parse.quote_plus(self._userid) + '&__a=1&' + '__req=' +
self._req.get_next() + '&__be=1&' +
'__pc=PHASED%3Aufi_home_page_pkg&dpr=1&' + '__rev=' +
parse.quote_plus(self._rev) + '&fb_dtsg_ag=' +
parse.quote_plus(self.fb_dtsg_ag) + '&jazoest=' +
self.jazoest + '&__spin_r=' + parse.quote_plus(
self._spin_r) + '&__spin_b=' + parse.quote_plus(
self._spin_b) + '&__spin_t=' +
parse.quote_plus(self._spin_t))
html = self._ha.getstring(
url,
headers="""
accept: */*
accept-encoding: gzip, deflate
accept-language: zh-CN,zh;q=0.9
referer: https://www.facebook.com/""")
if not isinstance(html, str) or html == "":
self._logger.error("Get docid js pages failed.")
return
self._jspages_listpage = html
if len(self._jspages_itemurls) < 1:
# 解析资源列表页面
matches = MessengerLogin.re_js_resoures.findall(html)
if matches is None or not any(matches):
raise Exception("Get js resources failed.")
for m in matches:
try:
if len(m) != 2:
continue
n = m[0]
u = m[1]
u = u.replace('\\', '')
if not self._jspages_itemurls.__contains__(n):
self._jspages_itemurls[n] = u
except Exception:
self._logger.trace(
"Get docid for contact parse item url error: {} {}"
.format(m, traceback.format_exc()))
self._logger.info(
"Got js resources list, {} count={}".format(
self.uname_str, len(self._jspages_itemurls)))
# fbcookie = self._ha._managedCookie.get_cookie_for_domain(
# "https://www.facebook.com/")
# self._ha._managedCookie.add_cookies(uridocid.netloc, fbcookie)
for jsurl in self._jspages_itemurls.items():
try:
if self._jspages.__contains__(jsurl[0]):
yield self._jspages[jsurl[0]]
else:
jspage = self._ha.getstring(
jsurl[1],
headers="""
Origin: https://www.facebook.com
Referer: https://www.facebook.com/""")
self._jspages[jsurl[0]] = (jsurl[1], jspage)
self._logger.debug("Got js resource: {} {}".format(
self.uname_str, jsurl[1]))
yield self._jspages[jsurl[0]]
except Exception:
self._logger.error(
"Download js resources error: {} {}".format(
self.uname_str, traceback.format_exc()))
except Exception:
self._logger.error("Get js resources error: {} {}".format(
self.uname_str, traceback.format_exc()))
#######################################
def _get_js_v1(self) -> iter:
try:
html = self._ha.getstring('https://www.facebook.com',
headers="""
accept: 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
accept-encoding: gzip, deflate
accept-language: en-US,en;q=0.9
cache-control: no-cache
content-type: application/x-www-form-urlencoded
pragma: no-cache
sec-fetch-mode: cors
sec-fetch-site: same-origin
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"""
)
# 先取script src直接能拿到部分
ehtml = etree.HTML(html)
src_list = ehtml.xpath('//script/@src')
self._logger.info(
"Got js resources list count={}".format(len(src_list)))
for jsurl in src_list:
if jsurl.startswith('https://'):
js = self._ha.getstring(jsurl, headers='''
Origin: https://www.facebook.com
Referer: https://www.facebook.com/
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36''')
yield js
except Exception:
self._logger.error(
"Get js src error: {}".format(traceback.format_exc()))
def _get_docid_init(self) -> bool:
""""""
if self.docid_init is not None:
return True
res: bool = False
try:
# LSGraphqlInitialSyncQuery
for js in self._get_js_v1():
try:
if helper_str.is_none_or_empty(js) or 'LSGraphqlInitialSyncQuery' not in js:
continue
m = MessengerLogin._re_docid_LSGraphqlInitialSyncQuery.search(js)
if m is not None:
m_docid = re.search(r'id:\s*?"(\d+)"', m.group(1))
if m_docid is not None:
self.docid_init = m_docid.group(1)
res = True
break
except Exception:
self._logger.debug(
"Parse init message docid error: {}".format(traceback.format_exc()))
except Exception:
self._logger.error(
"Get docid for init message error: {} {}".format(
self.uname_str, traceback.format_exc()))
return res
def _parse_init_js(self, js_func) -> iter:
"""处理初始消息js, 包含联系人、第一条发送消息等信息"""
res = False
try:
re_js_seq = re.search(r'return LS.seq\(\[(.*?)\]\)\}', js_func)
if re_js_seq is None:
self._logger.error('处理js代码失败')
req_js = re_js_seq.group(1)
re_js_sp = re.compile(r'_=>LS.sp\((.*?)\)')
m = re_js_sp.findall(req_js)
if m is None:
self._logger.error('处理js代码失败')
for js_one in m:
param_list = self._parse_js_one_v1(js_one)
dict_one = dict()
if param_list[0] == '"396"':
self.last_applied_cursor = param_list[4][1:-1]
# LSMailboxDeleteThenInsertThreadStoredProcedure
# 聊天通道
elif param_list[0] == '"130"':
dict_one['type'] = 'threads'
dict_one['thread_id'] = self.J(self._parse_n(param_list[8]))
self.messenger_thread_id.append(dict_one['thread_id'])
# function c(a) {
# if (b("bs_caml_int64").eq(a, b("MessagingThreadType.bs").group) || b("bs_caml_int64").eq(a, b("MessagingThreadType.bs").tincanGroupDisappearing))
# return !0;
# else
# return !1
# }
is_group = self._parse_n(param_list[10])
if is_group == [0, 2] or is_group == [0, 8]:
dict_one['is_group'] = 1
else:
dict_one['is_group'] = 0
dict_one['group_name'] = param_list[4][1:-1] if param_list[4] != 'undefined' else 'undefined'
# 头像
dict_one['pic_url'] = param_list[5][1:-1].replace('\\', '') if param_list[5] != 'undefined' else 'undefined'
# LSMailboxAddParticipantIdToGroupThreadStoredProcedure
# 成员
elif param_list[0] == '"40"':
dict_one['type'] = 'participants'
dict_one['thread_id'] = self.J(self._parse_n(param_list[1]))
dict_one['member_id'] = self.J(self._parse_n(param_list[2]))
# 联系人会话翻页,94和135都可以,但是参数和参数位置有变化
elif param_list[0] == '"94"':
if param_list[2] == 'n`2`' or param_list[3] == 'undefined':
self.threads_ranges_time = 0
self.threads_ranges_id = 0
else:
self.threads_ranges_time = self.J(self._parse_n(param_list[2]))
self.threads_ranges_id = self.J(self._parse_n(param_list[3]))
# LSMailboxUpsertMessageStoredProcedure
# 消息
elif param_list[0] == '"123"':
dict_one['type'] = 'messages'
thread_id = self._parse_n(param_list[4])
dict_one['thread_id'] = self.J(thread_id)
dict_one['time'] = self.J(self._parse_n(param_list[6])) # 6 or 7,还没找出差别
dict_one['content'] = param_list[1][1:-1]
dict_one['msg_id'] = param_list[9][1:-1] # mid.$cAAAAAJQIzuh9Ih48hV28Nj1p8EeN
dict_one['member_id'] = self.J(self._parse_n(param_list[11])) # 发送者的thread_id
dict_one['unknow'] = param_list[10][1:-1] # 6754333178547160973不知道有用没,和请求的epoch_id有点像
dict_one['from_sys'] = param_list[13] # 是否是系统消息
# LSMailboxInsertAttachmentStoredProcedure
elif param_list[0] == '"138"':
dict_one['type'] = 'attachments'
thread_id = self._parse_n(param_list[35])
dict_one['thread_id'] = self.J(thread_id)
dict_one['msg_id'] = param_list[38][1:-1]
# 如果是视频的话,第二个url是缩略图,如果是gif的话,第一个url是mp4,第二个url是gif
dict_one['atta_url1'] = param_list[8][1:-1].replace('\\', '') if param_list[8] != 'undefined' else 'undefined'
dict_one['rsc_type1'] = param_list[11][1:-1].replace('\\', '') if param_list[11] != 'undefined' else 'undefined'
dict_one['atta_url2'] = param_list[13][1:-1].replace('\\', '') if param_list[13] != 'undefined' else 'undefined'
dict_one['rsc_type2'] = param_list[16][1:-1].replace('\\', '') if param_list[16] != 'undefined' else 'undefined'
# LSContactVerifyContactRowExistsStoredProcedure
# 联系人或群员
elif param_list[0] == '"533"':
dict_one['type'] = 'contacts'
thread_id = self._parse_n(param_list[1])
dict_one['thread_id'] = self.J(thread_id)
# 头像
dict_one['pic_url'] = param_list[3][1:-1].replace('\\', '') if param_list[3] != 'undefined' else 'undefined'
# 名称或备注 4 or 9 ?
dict_one['nick_name'] = param_list[4][1:-1]
# 用来提取entity_id
# dict_one['url'] = param_list[6]
# dict_one['entity_id'] = helper_str.substring(dict_one['url'], 'entity_id=', '&')
if dict_one != {}:
self.js_res.append(dict_one)
res = True
except Exception:
self._logger.error("Parse contacts error: {}".format(
traceback.format_exc()))
return res
def _get_init_js_res(self):
succ = False
try:
# 拿联系人的资源docid
if not self._get_docid_init():
self._logger.error(
"Get docid for init messages failed: {}".format(
self.uname_str))
return succ
url = 'https://www.facebook.com/api/graphql/'
# 先抓15个联系人
variables = '{' + f'"deviceId":"{self.device_id}","schemaVersion":"{self.schema_version}","numThreads":15,"epochId":"{self.epoch_id}"' + '}'
postdata = f'av={self._userid}&__user={self._userid}&__a=1&__csr=&__beoa=0&__pc={parse.quote(self._pc)}&dpr=1&__ccg=EXCELLENT&__hsi={self.hsi}&__comet_req=1&fb_dtsg={parse.quote(self.fb_dtsg)}&jazoest={self.jazoest}&__spin_r={self._spin_r}&__spin_b={self._spin_b}&__spin_t={self._spin_t}&fb_api_caller_class=RelayModern&fb_api_req_friendly_name=LSGraphqlInitialSyncQuery&variables=' + parse.quote(
variables) + f'&doc_id={self.docid_init}'
html = self._ha.getstring(url, postdata,
headers="""
accept: */*
accept-encoding: gzip, deflate
accept-language: en-US,en;q=0.9,zh;q=0.8
content-length: {}
content-type: application/x-www-form-urlencoded
origin: https://www.facebook.com
sec-fetch-dest: empty
sec-fetch-mode: cors
sec-fetch-site: same-origin
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36
""".format(len(postdata)))
res = json.loads(html)
if 'data' not in res or \
'viewer' not in res['data'] or \
'lightspeed_web_initial_sync_v2' not in res['data']['viewer']:
self._logger.error("Get js function fail: {}".format(
self.uname_str))
return succ
js_func = res['data']['viewer']['lightspeed_web_initial_sync_v2']['response'][0]
if js_func is None or js_func == '':
self._logger.error("Get js function fail: {}".format(
self.uname_str))
return succ
res = self._parse_init_js(js_func)
if not res:
self._logger.error("处理初始消息js失败")
return succ
succ = True
except Exception:
self._logger.error("Get init js result failed: {}".format(
traceback.format_exc()))
return succ
def _get_wss_obj(self):
"""获取一个wss实例"""
try:
if self.ws is not None:
try:
# 收发一次消息判断连接是否断开
self.ws.send_binary('\xc0\x00')
self.ws.recv()
return self.ws
except:
pass
self.message_identifier = 1 # 一个递增的数字,每发送一次加1
self.request_id = 2 # ls_rep请求的递增id,从2开始
sid = random.randint(1000000000000000, 9999999999999999)
wss_uri = 'wss://edge-chat.facebook.com/chat?region=prn&sid={}&cid={}'.format(
sid, self.device_id
)
ws = websocket.create_connection(wss_uri,
timeout=10,
host='edge-chat.facebook.com',
origin='https://www.facebook.com',
# http_proxy_host='192.168.90.45',
# http_proxy_port=10809,
cookie=self.task.cookie)
# fst = f'\x10\x90\x02\x00\x06MQIsdp\x03\x82\x00\x0a\x00\x0cmqttwsclient\x00\xf4{{"u":"{self._userid}","s":{sid},"cp":3,"ecp":10,"chat_on":true,"fg":true,"d":"{self.device_id}","ct":"websocket","mqtt_sid":"","aid":{self.aid},"st":[],"pm":[],"dc":"","no_auto_fg":true,"gas":null,"pack":[]}}'
payload = f'{{"u":"{self._userid}","s":{sid},"cp":3,"ecp":10,"chat_on":true,"fg":true,"d":"{self.device_id}","ct":"websocket","mqtt_sid":"","aid":{self.aid},"st":[],"pm":[],"dc":"","no_auto_fg":true,"gas":null,"pack":[]}}'
msg1 = self._build_connect_request(payload)
ws.send_binary(msg1)
res = ws.recv()
if res != b' \x02\x00\x00':
self._logger.error('websocket connect failed!')