-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathyandex.go
More file actions
482 lines (415 loc) · 12.6 KB
/
Copy pathyandex.go
File metadata and controls
482 lines (415 loc) · 12.6 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
package yandex
import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"math/rand"
"net"
"net/http"
"regexp"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/gorilla/websocket"
"universal-bypass-tool/transport"
"universal-bypass-tool/utils"
)
type YandexDocsInfo struct {
CookieStr string
Token string
DocID string
CallbackURL string
UserID string
Origin string
Host string
WsURL string
Permissions map[string]interface{}
OpenCmd map[string]interface{}
}
type DocSession struct {
Info YandexDocsInfo
Conn *websocket.Conn
WriteQueue chan []byte
UserID string
writeMu sync.Mutex
}
func (s *DocSession) safeWrite(messageType int, data []byte) error {
s.writeMu.Lock()
defer s.writeMu.Unlock()
return s.Conn.WriteMessage(messageType, data)
}
type YandexDocsTransport struct {
*transport.BaseTransport
url string
session *DocSession
userCounter atomic.Int32
baseUserID string
}
func NewYandexDocsTransport(url string, config transport.TransportConfig) *YandexDocsTransport {
t := &YandexDocsTransport{
BaseTransport: transport.NewBaseTransport(config),
url: url,
}
t.baseUserID = randUserID()
return t
}
func (t *YandexDocsTransport) Start() error {
if err := t.BaseTransport.Start(); err != nil {
return err
}
t.baseUserID = randUserID()
utils.SafeGo("yandex.keepAlive", t.keepAliveLoop)
t.connectToDoc(0)
return nil
}
func (t *YandexDocsTransport) Send(data []byte) error {
if !t.IsConnected() {
return fmt.Errorf("transport not connected")
}
t.Mu.RLock()
session := t.session
t.Mu.RUnlock()
if session == nil {
return fmt.Errorf("no active session")
}
select {
case session.WriteQueue <- data:
t.RecordSend(len(data))
return nil
default:
return fmt.Errorf("write queue full")
}
}
func (t *YandexDocsTransport) connectToDoc(attempt int) {
if !t.IsRunning() {
return
}
utils.Debugf("[YDOCS] connectToDoc attempt ...")
go func() {
defer func() {
if r := recover(); r != nil {
utils.Debugf("[PANIC] recovered in yandex.connect: %v", r)
}
}()
t.Mu.Lock()
existingSession := t.session
t.Mu.Unlock()
var userID string
if existingSession != nil {
userID = existingSession.UserID
} else {
suffix := fmt.Sprintf("%03d", t.userCounter.Add(1)%1000)
userID = t.baseUserID + suffix
}
info, err := t.fetchDocInfo(t.url, userID)
if err != nil {
utils.Debugf("[YDOCS] fetchDocInfo failed: %v", err)
t.scheduleReconnect(attempt)
return
}
// Hard TCP dial timeout so a stuck connect/DNS to the balancer host
// can't hang the whole transport (HandshakeTimeout alone proved
// insufficient on iOS).
dialer := websocket.Dialer{
HandshakeTimeout: 15 * time.Second,
NetDialContext: (&net.Dialer{
Timeout: 10 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
}
headers := http.Header{}
headers.Set("User-Agent", "Mozilla/5.0")
headers.Set("Origin", info.Origin)
headers.Set("Cookie", info.CookieStr)
headers.Set("Host", info.Host)
utils.Debugf("[YDOCS] WebSocket dial %s", info.WsURL)
conn, resp, err := dialer.Dial(info.WsURL, headers)
if err != nil {
status := 0
if resp != nil {
status = resp.StatusCode
}
utils.Debugf("[YDOCS] WebSocket dial failed (http %d): %v", status, err)
t.scheduleReconnect(attempt)
return
}
utils.Debugf("[YDOCS] WebSocket connected to %s", info.Host)
writeQueue := make(chan []byte, t.GetConfig().MaxQueueSize)
if existingSession != nil {
writeQueue = existingSession.WriteQueue
}
session := &DocSession{
Info: info,
Conn: conn,
WriteQueue: writeQueue,
UserID: userID,
}
connectedAt := time.Now()
// Wait for the server's engine.io OPEN packet ("0{...sid...}") before
// sending anything. Sending our socket.io "40"/"42" packets right
// after the WS upgrade (the old behavior) races the server's own
// handshake packet - observed empirically as the server closing with
// 1005 within ~50-100ms of accepting the connection, right after it
// emits its "0{...}" packet, because the client wrote to the
// namespace before the handshake it announces was actually open.
_, first, err := conn.ReadMessage()
if err != nil {
utils.Debugf("[YDOCS] Read error waiting for engine.io open: %v", err)
conn.Close()
t.scheduleReconnect(attempt)
return
}
if len(first) == 0 || first[0] != '0' {
utils.Debugf("[YDOCS] unexpected first message (wanted engine.io open \"0...\"): %s", string(first))
}
t.Mu.Lock()
t.session = session
t.SetConnected(true)
t.Mu.Unlock()
if existingSession == nil {
utils.SafeGo("yandex.writer", t.writerLoop)
}
// Auth - use safeWrite
auth1 := fmt.Sprintf(`40{"token":"%s"}`, info.Token)
session.safeWrite(websocket.TextMessage, []byte(auth1))
authData := map[string]interface{}{
"type": "auth", "docid": info.DocID, "token": "fghhfgsjdgfjs",
"user": map[string]interface{}{"id": userID}, "editorType": 0,
"lastOtherSaveTime": -1, "permissions": info.Permissions,
"openCmd": info.OpenCmd, "coEditingMode": "fast", "jwtOpen": info.Token,
}
messagePart, _ := json.Marshal([]interface{}{"message", authData})
session.safeWrite(websocket.TextMessage, []byte(fmt.Sprintf("42%s", string(messagePart))))
for t.IsRunning() {
_, message, err := conn.ReadMessage()
if err != nil {
utils.Debugf("[YDOCS] Read error: %v", err)
t.SetConnected(false)
// If the session was healthy for a while, treat the next
// connect as fresh (attempt -1 -> next attempt 0) so backoff
// doesn't keep growing across normal long-lived reconnects.
next := attempt
if time.Since(connectedAt) > 15*time.Second {
next = -1
}
t.scheduleReconnect(next)
return
}
t.handleMessage(session, message)
}
}()
}
func (t *YandexDocsTransport) writerLoop() {
for t.IsRunning() {
t.Mu.Lock()
session := t.session
t.Mu.Unlock()
if session == nil || session.Conn == nil {
time.Sleep(10 * time.Millisecond)
continue
}
select {
case packet := <-session.WriteQueue:
payload := base64.StdEncoding.EncodeToString(packet)
msg := fmt.Sprintf(`42["message",{"type":"cursor","cursor":"18;%s"}]`, payload)
if err := session.safeWrite(websocket.TextMessage, []byte(msg)); err != nil {
utils.Debugf("[YDOCS] Write error: %v", err)
}
default:
time.Sleep(10 * time.Millisecond)
}
}
}
func (t *YandexDocsTransport) keepAliveLoop() {
ticker := time.NewTicker(t.GetConfig().KeepAliveInterval)
defer ticker.Stop()
keepAliveMsg := `42["message",{"type":"cursor","cursor":"18;---KA---"}]`
for t.IsRunning() {
<-ticker.C
t.Mu.Lock()
session := t.session
t.Mu.Unlock()
if session != nil && session.Conn != nil {
if err := session.safeWrite(websocket.TextMessage, []byte(keepAliveMsg)); err != nil {
utils.Debugf("[YDOCS] Keep-alive failed: %v", err)
t.SetConnected(false)
}
}
}
}
func (t *YandexDocsTransport) handleMessage(session *DocSession, data []byte) {
text := string(data)
if strings.Contains(text, "---KA---") {
return
}
// Socket.IO ping - respond with pong (use safeWrite)
if text == "2" {
if session != nil && session.Conn != nil {
session.safeWrite(websocket.TextMessage, []byte("3"))
}
return
}
if text == "3" {
return
}
if strings.Contains(text, "saveChanges") || strings.Contains(text, "cursor") {
base64Str := t.extractBase64String(text)
if base64Str == "" {
return
}
decoded, err := base64.StdEncoding.DecodeString(base64Str)
if err != nil {
utils.Debugf("[YDOCS] Base64 decode error: %v", err)
return
}
t.RecordReceive(len(decoded))
t.CallReceive(decoded)
}
}
func (t *YandexDocsTransport) extractBase64String(response string) string {
if strings.Contains(response, "saveChanges") {
marker := `"excelAdditionalInfo":"`
left := strings.Index(response, marker) + len(marker)
if left < len(marker) {
return ""
}
right := strings.Index(response[left:], `"`)
if right == -1 {
return ""
}
return response[left : left+right]
}
re := regexp.MustCompile(`"cursor":"[^;]+;([^"]+)"`)
matches := re.FindStringSubmatch(response)
if len(matches) > 1 {
return matches[1]
}
return ""
}
func (t *YandexDocsTransport) scheduleReconnect(attempt int) {
next := attempt + 1
if !t.IsRunning() || next >= t.GetConfig().MaxReconnectAttempts {
return
}
// Back off before retrying so a server that closes us immediately doesn't
// turn into a tight connect/close loop (previously reconnect was instant).
d := reconnectBackoff(next)
utils.Debugf("[YDOCS] reconnecting in %v (attempt %d)", d, next)
time.Sleep(d)
if !t.IsRunning() {
return
}
t.RecordReconnect()
t.connectToDoc(next)
}
// reconnectBackoff returns an exponential backoff with jitter, capped at 15s.
func reconnectBackoff(n int) time.Duration {
if n < 1 {
n = 1
}
shift := n - 1
if shift > 5 {
shift = 5
}
d := 500 * time.Millisecond * time.Duration(1<<uint(shift))
if d > 15*time.Second {
d = 15 * time.Second
}
// add up to +50% jitter
d += time.Duration(rand.Int63n(int64(d/2) + 1))
return d
}
func (t *YandexDocsTransport) fetchDocInfo(url, userID string) (YandexDocsInfo, error) {
client := &http.Client{
// Cap redirects so an auth/login redirect loop fails fast instead of
// hanging until the timeout (a private doc redirects to passport).
CheckRedirect: func(req *http.Request, via []*http.Request) error {
if len(via) >= 10 {
return fmt.Errorf("stopped after 10 redirects (login required? doc not public?)")
}
return nil
},
Timeout: 15 * time.Second,
}
utils.Debugf("[YDOCS] fetchDocInfo GET %s", url)
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("User-Agent", "Mozilla/5.0")
resp, err := client.Do(req)
if err != nil {
return YandexDocsInfo{}, err
}
defer resp.Body.Close()
htmlBytes, _ := io.ReadAll(resp.Body)
html := string(htmlBytes)
utils.Debugf("[YDOCS] response status=%d finalURL=%s body=%dB", resp.StatusCode, resp.Request.URL.String(), len(html))
var cookies []string
for _, c := range resp.Cookies() {
cookies = append(cookies, fmt.Sprintf("%s=%s", c.Name, c.Value))
}
re := regexp.MustCompile(`<script[^>]*id="client-config"[^>]*>(.*?)</script>`)
matches := re.FindStringSubmatch(html)
if len(matches) < 2 {
// Help diagnose: is this a login page, a new-editor page, etc.?
hint := "no client-config script"
if strings.Contains(html, "passport") || strings.Contains(strings.ToLower(html), "login") {
hint = "looks like a login page (doc not public?)"
}
return YandexDocsInfo{}, fmt.Errorf("config not found: %s (status %d, final %s)", hint, resp.StatusCode, resp.Request.URL.String())
}
var config map[string]interface{}
if err := json.Unmarshal([]byte(matches[1]), &config); err != nil {
return YandexDocsInfo{}, fmt.Errorf("client-config is not valid JSON: %w", err)
}
officeAction, ok := config["officeActionData"].(map[string]interface{})
if !ok || officeAction == nil {
return YandexDocsInfo{}, fmt.Errorf("officeActionData missing - will reconnect")
}
editorConfigRaw, ok := officeAction["editor_config"].(map[string]interface{})
if !ok || editorConfigRaw == nil {
return YandexDocsInfo{}, fmt.Errorf("editor_config nil - will reconnect")
}
balancerURL, ok := officeAction["balancer_url"].(string)
if !ok || balancerURL == "" {
return YandexDocsInfo{}, fmt.Errorf("officeActionData.balancer_url missing - will reconnect")
}
host := strings.TrimPrefix(balancerURL, "https://")
document, ok := editorConfigRaw["document"].(map[string]interface{})
if !ok || document == nil {
return YandexDocsInfo{}, fmt.Errorf("editor_config.document missing - will reconnect")
}
token, ok := editorConfigRaw["token"].(string)
if !ok || token == "" {
return YandexDocsInfo{}, fmt.Errorf("editor_config.token missing - will reconnect")
}
docKey, ok := document["key"].(string)
if !ok || docKey == "" {
return YandexDocsInfo{}, fmt.Errorf("editor_config.document.key missing - will reconnect")
}
perms, _ := document["permissions"].(map[string]interface{})
if perms == nil {
perms = make(map[string]interface{})
}
return YandexDocsInfo{
CookieStr: strings.Join(cookies, "; "),
Token: token,
DocID: docKey,
Origin: balancerURL,
Host: host,
WsURL: fmt.Sprintf("wss://%s/2024.1.1-375/doc/%s/c/?EIO=4&transport=websocket", host, docKey),
Permissions: perms,
OpenCmd: map[string]interface{}{
"c": "open",
"id": docKey,
"userid": userID,
"format": document["fileType"],
"url": document["url"],
"title": document["title"],
"lcid": 25,
},
}, nil
}
func randUserID() string {
return fmt.Sprintf("%010d", rand.New(rand.NewSource(time.Now().UnixNano())).Intn(1000000000))
}