From 9b5c3913989754370bd7d03ac8cf2e32a6172afb Mon Sep 17 00:00:00 2001 From: Keuin Date: Sat, 29 Jul 2023 19:43:27 +0800 Subject: Simulate real web app cookies & WebSocket fields --- danmaku/client.go | 42 ++++++++++++------------------------------ danmaku/dmpkg/auth.go | 4 +++- danmaku/dmpkg/raw.go | 2 +- 3 files changed, 16 insertions(+), 32 deletions(-) (limited to 'danmaku') diff --git a/danmaku/client.go b/danmaku/client.go index a94afbf..d7826d0 100644 --- a/danmaku/client.go +++ b/danmaku/client.go @@ -23,6 +23,16 @@ type DanmakuClient struct { wsio wsDatagramIO } +func NewClient(ctx context.Context, ws *websocket.Conn) DanmakuClient { + return DanmakuClient{ + ws: ws, + wsio: wsDatagramIO{ + ws: ws, + ctx: ctx, + }, + } +} + type DanmakuMessageType int // wsDatagramIO wraps websocket into a datagram I/O, @@ -48,34 +58,6 @@ func (w *wsDatagramIO) Get() (data []byte, err error) { return } -func NewDanmakuClient() DanmakuClient { - return DanmakuClient{ - ws: nil, - } -} - -func (d *DanmakuClient) Connect(ctx context.Context, url string) error { - // thread unsafe - - // dial - if d.ws != nil { - return fmt.Errorf("already connected") - } - ws, _, err := websocket.Dial(ctx, url, nil) - if err != nil { - return fmt.Errorf("failed to establish WebSocket connection: %w", err) - } - d.ws = ws - - // init wsio - d.wsio = wsDatagramIO{ - ws: ws, - ctx: ctx, - } - - return nil -} - func (d *DanmakuClient) Disconnect() error { // thread unsafe ws := d.ws @@ -87,8 +69,8 @@ func (d *DanmakuClient) Disconnect() error { return ws.Close(websocket.StatusInternalError, "disconnected") } -func (d *DanmakuClient) Authenticate(roomId types.RoomId, authKey string) error { - pkg := dmpkg.NewAuth(dmpkg.ProtoBrotli, roomId, authKey) +func (d *DanmakuClient) Authenticate(roomId types.RoomId, authKey, buvid3 string) error { + pkg := dmpkg.NewAuth(dmpkg.ProtoBrotli, roomId, authKey, buvid3) data, err := pkg.Marshal() if err != nil { return fmt.Errorf("exchange marshal failed: %w", err) diff --git a/danmaku/dmpkg/auth.go b/danmaku/dmpkg/auth.go index b6a90d3..81c365e 100644 --- a/danmaku/dmpkg/auth.go +++ b/danmaku/dmpkg/auth.go @@ -15,17 +15,19 @@ type authInfo struct { UID uint64 `json:"uid"` RoomId types.RoomId `json:"roomid"` ProtoVer int `json:"protover"` + BUVID3 string `json:"buvid"` Platform string `json:"platform"` Type int `json:"type"` Key string `json:"key"` } // NewAuth creates a new authentication exchange. -func NewAuth(protocol ProtocolVer, roomId types.RoomId, authKey string) (exc DanmakuExchange) { +func NewAuth(protocol ProtocolVer, roomId types.RoomId, authKey, buvid3 string) (exc DanmakuExchange) { exc, _ = NewPlainExchange(OpConnect, authInfo{ UID: UidGuest, RoomId: roomId, ProtoVer: int(protocol), + BUVID3: buvid3, Platform: PlatformWeb, Type: AuthTypeDefault, Key: authKey, diff --git a/danmaku/dmpkg/raw.go b/danmaku/dmpkg/raw.go index 34cd2e6..6de10d7 100644 --- a/danmaku/dmpkg/raw.go +++ b/danmaku/dmpkg/raw.go @@ -37,7 +37,7 @@ func NewPlainExchange(operation Operation, body interface{}) (exc DanmakuExchang DanmakuExchangeHeader: DanmakuExchangeHeader{ Length: uint32(length), HeaderLength: HeaderLength, - ProtocolVer: ProtoPlainJson, + ProtocolVer: ProtoMinimal, Operation: operation, SequenceId: SequenceId, }, -- cgit v1.2.3