1
+ # -*- coding: utf-8 -*-
2
+ # Copyright (C) 2021 github.com/ItsChasa
3
+ #
4
+ # This source code has been released under the GNU Affero General Public
5
+ # License v3.0. A copy of this license is available at
6
+ # https://www.gnu.org/licenses/agpl-3.0.en.html
7
+
8
+ import base64
9
+ import re
10
+ import json
11
+
12
+ # browser data
13
+ user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36"
14
+ sec_ch_ua = '"Google Chrome";v="113", "Chromium";v="113", "Not-A.Brand";v="24"'
15
+ browser_version = "113.0.0.0"
16
+ request_client_identifier = "chrome_113" # tls_client uses this to determine what "Client Hello" to use
17
+
18
+ # discord data
19
+ from main import request_client
20
+
21
+ def get_client_build_number ():
22
+ client_request = request_client .get (f'https://discord.com/app' , headers = {'User-Agent' : 'Mozilla/5.0' }).text
23
+ jsFileRegex = re .compile (r'([a-zA-z0-9]+)\.js' , re .I )
24
+ for asset in jsFileRegex .findall (client_request )[::- 1 ]:
25
+ if asset != "invisible" :
26
+ break
27
+
28
+ assetFileRequest = request_client .get (f'https://discord.com/assets/{ asset } .js' , headers = {'User-Agent' : 'Mozilla/5.0' }).text
29
+ try :
30
+ build_info_regex = re .compile (r'Build Number: "\)\.concat\("([0-9]{4,8})"' )
31
+ build_info_strings = build_info_regex .findall (assetFileRequest )
32
+ build_num = build_info_strings [0 ]
33
+ except (RuntimeError , TypeError , NameError ):
34
+ raise Exception (f"couldn't fetch discord build num from { asset } .js" )
35
+
36
+ return int (build_num )
37
+
38
+ discord_build = get_client_build_number ()
39
+ super_properties = {
40
+ "os" : "Windows" ,
41
+ "browser" : "Chrome" ,
42
+ "device" : "" ,
43
+ "system_locale" : "en-US" ,
44
+ "browser_user_agent" : user_agent ,
45
+ "browser_version" : browser_version ,
46
+ "os_version" : "10" ,
47
+ "referrer" : "" ,
48
+ "referring_domain" : "" ,
49
+ "referrer_current" : "" ,
50
+ "referring_domain_current" : "" ,
51
+ "release_channel" : "stable" ,
52
+ "client_build_number" : discord_build ,
53
+ "client_event_source" : None
54
+ }
55
+ b64_super_properties = base64 .b64encode (json .dumps (super_properties , separators = (',' , ':' )).encode ()).decode ()
56
+
57
+
58
+ def build_headers (
59
+ method ,
60
+ superprop = False ,
61
+ debugoptions = False ,
62
+ discordlocale = False ,
63
+ authorization = False ,
64
+ origin = False ,
65
+ referer = "https://discord.com/channels/@me" ,
66
+ context = False ,
67
+ timezone = False
68
+ ):
69
+ headers = {
70
+ "Accept" : "*/*" ,
71
+ "Accept-Encoding" : "gzip, deflate, br" ,
72
+ "Accept-Language" : "en-US,en;q=0.9" ,
73
+ "Cookie" : "locale=en-GB" ,
74
+ "Referer" : referer ,
75
+ "Sec-Ch-Ua" : sec_ch_ua ,
76
+ "Sec-Ch-Ua-Mobile" : "?0" ,
77
+ "Sec-Ch-Ua-Platform" : '"Windows"' ,
78
+ "Sec-Fetch-Dest" : "empty" ,
79
+ "Sec-Fetch-Mode" : "cors" ,
80
+ "Sec-Fetch-Site" : "same-origin" ,
81
+ "User-Agent" : user_agent
82
+ }
83
+
84
+ if referer is False :
85
+ del headers ["Referer" ]
86
+
87
+ if method != "get" :
88
+ headers ["Content-Type" ] = "application/json"
89
+ if origin is not False :
90
+ headers ["Origin" ] = origin
91
+
92
+ if authorization is not False :
93
+ headers ["Authorization" ] = authorization
94
+ if debugoptions is True :
95
+ headers ["X-Debug-Options" ] = "bugReporterEnabled"
96
+ if discordlocale is True :
97
+ headers ["X-Discord-Locale" ] = "en-US"
98
+ if superprop is True :
99
+ headers ["X-Super-Properties" ] = b64_super_properties
100
+ if context is True :
101
+ headers ["X-Context-Properties" ] = context
102
+ if timezone is True :
103
+ headers ["X-Discord-Timezone" ] = "Europe/London"
104
+
105
+ keyssorted = sorted (headers .keys (), key = lambda x :x .lower ())
106
+ newheaders = {}
107
+ for key in keyssorted :
108
+ newheaders [key ] = headers [key ]
109
+
110
+ return headers
0 commit comments