-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheaders.go
352 lines (290 loc) · 7.53 KB
/
headers.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
package headers
import (
"crypto/rand"
"encoding/json"
"fmt"
"github.com/gorilla/websocket"
"io"
"math/big"
"net/http"
"net/url"
"regexp"
"strings"
"time"
"github.com/agnosto/fansly-scraper/config"
)
type FanslyHeaders struct {
AuthToken string
UserAgent string
DeviceID string
SessionID string
CheckKey string
Config *config.Config
}
const fallbackCheckKey = "qybZy9-fyszis-bybxyf"
func NewFanslyHeaders(cfg *config.Config) (*FanslyHeaders, error) {
headers := &FanslyHeaders{
AuthToken: cfg.Account.AuthToken,
UserAgent: cfg.Account.UserAgent,
DeviceID: cfg.SecurityHeaders.DeviceID,
SessionID: cfg.SecurityHeaders.SessionID,
CheckKey: cfg.SecurityHeaders.CheckKey,
Config: cfg,
}
var updated bool
if headers.DeviceID == "" {
deviceID, err := GetDeviceID()
if err != nil {
return nil, err
}
headers.DeviceID = deviceID
cfg.SecurityHeaders.DeviceID = deviceID
updated = true
}
if headers.SessionID == "" {
sessionID, err := GetSessionID(headers.AuthToken)
if err != nil {
return nil, err
}
headers.SessionID = sessionID
cfg.SecurityHeaders.SessionID = sessionID
updated = true
}
if headers.CheckKey == "" {
err := headers.SetCheckKey()
if err != nil {
return nil, err
}
cfg.SecurityHeaders.CheckKey = headers.CheckKey
updated = true
}
// Save the updated config
if updated {
err := config.SaveConfig(cfg)
if err != nil {
return nil, err
}
}
if time.Since(cfg.SecurityHeaders.LastUpdated) > 7*24*time.Hour {
if err := headers.RefreshSecurityHeaders(); err != nil {
return nil, err
}
}
return headers, nil
}
func (f *FanslyHeaders) GetBasicHeaders() map[string]string {
return map[string]string{
"Accept-Language": "en-US,en;q=0.9",
"Authorization": f.AuthToken,
"Origin": "https://fansly.com",
"Referer": "https://fansly.com/",
"User-Agent": f.UserAgent,
}
}
func (f *FanslyHeaders) GetFullHeaders(reqURL string) map[string]string {
headers := f.GetBasicHeaders()
headers["fansly-client-id"] = f.DeviceID
headers["fansly-client-ts"] = fmt.Sprintf("%d", getClientTimestamp())
headers["fansly-session-id"] = f.SessionID
headers["fansly-client-check"] = f.getFanslyClientCheck(reqURL)
return headers
}
func (f *FanslyHeaders) AddHeadersToRequest(req *http.Request, fullHeaders bool) {
var headers map[string]string
if fullHeaders {
headers = f.GetFullHeaders(req.URL.String())
} else {
headers = f.GetBasicHeaders()
}
for key, value := range headers {
req.Header.Add(key, value)
}
}
func getClientTimestamp() int64 {
now := time.Now().UnixNano() / int64(time.Millisecond)
randomValue, _ := rand.Int(rand.Reader, big.NewInt(10000))
return now + (5000 - randomValue.Int64())
}
func (f *FanslyHeaders) SetCheckKey() error {
mainJSPattern := `\ssrc\s*=\s*"(main\..*?\.js)"`
checkKeyPattern := `this\.checkKey_\s*=\s*\["([^"]+)","([^"]+)"\]\.reverse\(\)\.join\("-"\)\+"([^"]+)"`
checkKey, err := GuessCheckKey(mainJSPattern, checkKeyPattern, f.UserAgent)
if err != nil {
fmt.Printf("Warning: %v", err)
f.CheckKey = fallbackCheckKey
} else {
f.CheckKey = checkKey
}
return nil
}
func (f *FanslyHeaders) getFanslyClientCheck(reqURL string) string {
parsedURL, _ := url.Parse(reqURL)
urlPath := parsedURL.Path
uniqueIdentifier := fmt.Sprintf("%s_%s_%s", f.CheckKey, urlPath, f.DeviceID)
digest := cyrb53(uniqueIdentifier)
return fmt.Sprintf("%x", digest)
}
func cyrb53(str string) uint64 {
h1 := uint64(0xdeadbeef)
h2 := uint64(0x41c6ce57)
for i := 0; i < len(str); i++ {
ch := uint64(str[i])
h1 = (h1 ^ ch) * 2654435761
h2 = (h2 ^ ch) * 1597334677
}
h1 = ((h1 ^ (h1 >> 16)) * 2246822507) ^ ((h2 ^ (h2 >> 13)) * 3266489909)
h2 = ((h2 ^ (h2 >> 16)) * 2246822507) ^ ((h1 ^ (h1 >> 13)) * 3266489909)
return 4294967296*(h2&0xFFFFFFFF) + (h1 & 0xFFFFFFFF)
}
func GetDeviceID() (string, error) {
client := &http.Client{}
req, err := http.NewRequest("GET", "https://apiv3.fansly.com/api/v1/device/id", nil)
if err != nil {
return "", err
}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
var result struct {
Success bool `json:"success"`
Response string `json:"response"`
}
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
return "", err
}
if !result.Success {
return "", fmt.Errorf("failed to get device ID")
}
return result.Response, nil
}
func (f *FanslyHeaders) SetSessionID() error {
sessionID, err := GetSessionID(f.AuthToken)
if err != nil {
return err
}
f.SessionID = sessionID
return nil
}
func GetSessionID(authToken string) (string, error) {
c, _, err := websocket.DefaultDialer.Dial("wss://wsv3.fansly.com/", nil)
if err != nil {
return "", err
}
defer c.Close()
message := map[string]interface{}{
"t": 1,
"d": fmt.Sprintf("{\"token\":\"%s\"}", authToken),
}
err = c.WriteJSON(message)
if err != nil {
return "", err
}
_, msg, err := c.ReadMessage()
if err != nil {
return "", err
}
var response struct {
T int `json:"t"`
D string `json:"d"`
}
err = json.Unmarshal(msg, &response)
if err != nil {
return "", err
}
var sessionData struct {
Session struct {
ID string `json:"id"`
} `json:"session"`
}
err = json.Unmarshal([]byte(response.D), &sessionData)
if err != nil {
return "", err
}
return sessionData.Session.ID, nil
}
func GuessCheckKey(mainJSPattern, checkKeyPattern, userAgent string) (string, error) {
fanslyURL := "https://fansly.com/"
client := &http.Client{}
// Make request to fansly.com
req, err := http.NewRequest("GET", fanslyURL, nil)
if err != nil {
return "", err
}
req.Header.Set("User-Agent", userAgent)
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return "", fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
// Find main.*.js file
mainJSRegex := regexp.MustCompile(mainJSPattern)
mainJSMatch := mainJSRegex.FindStringSubmatch(string(body))
if len(mainJSMatch) < 2 {
return "", fmt.Errorf("main.js file not found")
}
mainJS := mainJSMatch[1]
mainJSURL := fmt.Sprintf("%s%s", fanslyURL, mainJS)
// Request main.js file
req, err = http.NewRequest("GET", mainJSURL, nil)
if err != nil {
return "", err
}
req.Header.Set("User-Agent", userAgent)
resp, err = client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return "", fmt.Errorf("unexpected status code for main.js: %d", resp.StatusCode)
}
jsBody, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
// Find check key
checkKeyRegex := regexp.MustCompile(checkKeyPattern)
checkKeyMatch := checkKeyRegex.FindStringSubmatch(string(jsBody))
if len(checkKeyMatch) < 4 {
return "", fmt.Errorf("check key not found")
}
checkKey := strings.Join([]string{checkKeyMatch[2], checkKeyMatch[1]}, "-") + checkKeyMatch[3]
//checkKey := reversedPart + "-bubayf"
return checkKey, nil
}
func (f *FanslyHeaders) RefreshSecurityHeaders() error {
var err error
f.DeviceID, err = GetDeviceID()
if err != nil {
return err
}
f.SessionID, err = GetSessionID(f.AuthToken)
if err != nil {
return err
}
err = f.SetCheckKey()
if err != nil {
return err
}
// Update the config
f.Config.SecurityHeaders.DeviceID = f.DeviceID
f.Config.SecurityHeaders.SessionID = f.SessionID
f.Config.SecurityHeaders.CheckKey = f.CheckKey
f.Config.SecurityHeaders.LastUpdated = time.Now()
// Save the updated config
err = config.SaveConfig(f.Config)
if err != nil {
return err
}
return nil
}