-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwss.go
318 lines (281 loc) · 7.57 KB
/
wss.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
package gotiktoklive
import (
"context"
"encoding/hex"
"fmt"
"io"
"net"
"net/http"
"net/url"
"os"
"time"
pb "github.com/steampoweredtaco/gotiktoklive/proto"
"github.com/gobwas/ws"
"github.com/gobwas/ws/wsutil"
"golang.org/x/net/proxy"
"google.golang.org/protobuf/proto"
)
func (l *Live) connect(addr string, params map[string]string) error {
u, err := url.Parse("https://tiktok.com/")
if err != nil {
return nil
}
// Take the cookies from the HTTP client cookie jar and pass them as a GET parameter.
cookies := l.t.c.Jar.Cookies(u)
headers := http.Header{}
var s string
for _, cookie := range cookies {
if s == "" {
s = cookie.String()
} else {
s += "; " + cookie.String()
}
}
if len(cookies) > 0 {
headers.Set("Cookie", s)
}
vs := url.Values{}
for key, value := range defaultGETParams {
vs.Add(key, value)
}
for key, value := range params {
vs.Add(key, value)
}
vs.Add("room_id", l.ID)
wsURL := fmt.Sprintf("%s?%s", addr, vs.Encode())
// Read proxies from HTTP env variables, HTTPS takes precedent
var proxyURI *url.URL
envVars := []string{"HTTP_PROXY", "HTTPS_PROXY"}
for _, envVar := range envVars {
if httpProxy := os.Getenv(envVar); httpProxy != "" {
uri, err := url.Parse(httpProxy)
if err != nil {
l.t.warnHandler(fmt.Errorf("Failed to parse %s url: %w", envVar, err))
} else {
proxyURI = uri
}
}
}
// If proxy manually defined, use that
if l.t.proxy != nil {
proxyURI = l.t.proxy
}
// Default proxy net dial
proxyNetDial := proxy.FromEnvironment()
// If custom URI is defined, genereate proxy net dial from that
if proxyURI != nil {
dial, err := proxy.FromURL(proxyURI, Direct)
if err != nil {
l.t.warnHandler(fmt.Errorf("Failed to configure proxy dialer: %w", err))
} else {
proxyNetDial = dial
}
}
dialer := ws.Dialer{
Header: ws.HandshakeHeaderHTTP(headers),
NetDial: func(ctx context.Context, a, b string) (net.Conn, error) {
return proxyNetDial.Dial(a, b)
},
// NetDial: proxy.Dial,
Protocols: []string{"echo-protocol"},
}
conn, _, _, err := dialer.Dial(context.Background(), wsURL)
if err != nil {
return fmt.Errorf("Failed to connect: %w", err)
}
l.wss = conn
return nil
}
func (l *Live) readSocket() {
defer l.wss.Close()
defer func() {
select {
case <-time.After(5 * time.Second):
case l.Events <- &DisconnectEvent{created: time.Now()}:
}
}()
defer l.cancel()
want := ws.OpBinary
s := ws.StateClientSide
controlHandler := wsutil.ControlFrameHandler(l.wss, s)
rd := wsutil.Reader{
Source: l.wss,
State: s,
CheckUTF8: true,
SkipHeaderCheck: false,
OnIntermediate: controlHandler,
}
for {
hdr, err := rd.NextFrame()
if err != nil {
l.t.errHandler(fmt.Errorf("failed to read websocket from server: %w", err))
l.wss.Close()
return
}
// If msg is ping or close
if hdr.OpCode.IsControl() {
if err := controlHandler(hdr, &rd); err != nil {
l.t.errHandler(fmt.Errorf("websocket control handler failed: %w", err))
}
continue
}
if hdr.OpCode == ws.OpClose {
l.t.warnHandler("Websocket connection was closed by server.")
if l.t.enableWSTrace {
l.t.wsTraceChan <- struct{ direction, hex string }{direction: "<=", hex: "websocket closed"}
}
return
}
// Wrong OpCode
if hdr.OpCode&want == 0 {
msgBytes, err := io.ReadAll(&rd)
if err != nil {
l.t.errHandler(fmt.Errorf("Failed to read websocket message: %w", err))
}
if l.t.enableWSTrace {
l.t.wsTraceChan <- struct{ direction, hex string }{direction: fmt.Sprintf("<= (unexpected opcode %02x)", hdr.OpCode), hex: hex.EncodeToString(msgBytes)}
}
continue
}
// Read message
msgBytes, err := io.ReadAll(&rd)
if l.t.enableWSTrace {
l.t.wsTraceChan <- struct{ direction, hex string }{direction: "<=", hex: hex.EncodeToString(msgBytes)}
}
if err != nil {
l.t.errHandler(fmt.Errorf("Failed to read websocket message: %w", err))
}
if err := l.parseWssMsg(msgBytes); err != nil {
l.t.errHandler(fmt.Errorf("Failed to parse websocket message: %w", err))
}
// Gracefully shutdown
select {
case <-l.done():
return
case <-l.t.done():
l.t.infoHandler("Close websocket, global context done")
return
default:
}
}
}
func (l *Live) parseWssMsg(wssMsg []byte) error {
var rsp pb.WebcastPushFrame
if err := proto.Unmarshal(wssMsg, &rsp); err != nil {
return fmt.Errorf("Failed to unmarshal proto WebcastWebsocketMessage: %w", err)
}
if rsp.PayloadType == "msg" {
var response pb.WebcastResponse
if err := proto.Unmarshal(rsp.Payload, &response); err != nil {
return fmt.Errorf("Failed to unmarshal proto WebcastResponse: %w", err)
}
if response.NeedsAck {
if err := l.sendAck(rsp.LogId, response.InternalExt); err != nil {
// Might as well finishing processing all messages, the connection reset will be
// caught later
l.t.warnHandler("Failed to send websocket ack msg, likely connection reset: %w", err)
}
}
l.cursor = response.Cursor
if l.t.Debug {
l.t.debugHandler(fmt.Sprintf("Got %d messages, %s", len(response.Messages), response.Cursor))
}
for _, rawMsg := range response.Messages {
msg, err := parseMsg(rawMsg, l.t.warnHandler, l.t.debugHandler, l.t.enableExperimentalEvents)
if err != nil {
return fmt.Errorf("Failed to parse response message: %w", err)
}
if msg != nil {
// TODO, let's fix this
// If channel is full, discard the first message
if len(l.Events) == l.chanSize {
<-l.Events
}
l.Events <- msg
}
// If livestream has ended
if m, ok := msg.(ControlEvent); ok &&
(pb.ControlAction(m.Action) == pb.ControlAction_STREAM_ENDED ||
pb.ControlAction(m.Action) == pb.ControlAction_STREAM_ENDED_BAN) {
l.t.warnHandler(fmt.Sprintf("live has ended due to %s, closing event stream", pb.ControlAction(m.Action).String()))
l.cancel()
}
}
return nil
}
if l.t.Debug {
l.t.debugHandler(fmt.Sprintf("Message type unknown, %s : '%s\n%s", rsp.PayloadType, string(rsp.Payload), hex.EncodeToString(wssMsg)))
}
return nil
}
func (l *Live) sendPing() {
const helloHex = "3a026862"
b, err := hex.DecodeString(helloHex)
if err != nil {
l.t.errHandler(err)
}
t := time.NewTicker(10000 * time.Millisecond)
defer t.Stop()
for {
select {
case <-l.done():
return
case <-l.t.done():
return
case <-t.C:
if err := wsutil.WriteClientBinary(l.wss, b); err != nil {
l.t.errHandler(fmt.Errorf("Failed to send ping: %w", err))
} else {
if l.t.enableWSTrace {
l.t.wsTraceChan <- struct{ direction, hex string }{direction: "=>", hex: helloHex}
}
}
}
}
}
func (l *Live) sendAck(id uint64, extra []byte) error {
msg := pb.WebcastPushFrame{
LogId: id,
PayloadType: "ack",
Payload: extra,
}
b, err := proto.Marshal(&msg)
if err != nil {
return err
}
if err := wsutil.WriteClientBinary(l.wss, b); err != nil {
return err
}
if l.t.enableWSTrace {
l.t.wsTraceChan <- struct{ direction, hex string }{direction: "=>", hex: hex.EncodeToString(b)}
}
return nil
}
func (l *Live) tryConnectionUpgrade() error {
if l.wsURL == "" {
return fmt.Errorf("cannot upgrade connection without a wsURL")
}
if l.wsParams == nil {
return fmt.Errorf("cannot upgrade connection without a wsURL")
}
err := l.connect(l.wsURL, l.wsParams)
if err != nil {
close(l.Events)
return fmt.Errorf("Connection upgrade failed: %w", err)
}
if l.t.Debug {
l.t.debugHandler("Connected to websocket")
}
l.wg.Add(2)
go func() {
defer l.wg.Done()
defer close(l.Events)
l.readSocket()
}()
go func() {
defer l.wg.Done()
l.sendPing()
}()
l.t.infoHandler("Connected to websocket")
return nil
}