summaryrefslogtreecommitdiff
path: root/danmaku/dmpkg/auth.go
diff options
context:
space:
mode:
authorKeuin <[email protected]>2022-09-07 02:48:46 +0800
committerKeuin <[email protected]>2022-09-07 02:48:46 +0800
commit8e15d802865ed57db0018c15ea5559c8bd44c01f (patch)
tree48f4632a1ad044bd7f7f8da3ebe2bb03ab4ca6fe /danmaku/dmpkg/auth.go
parent88234ca8fffc4e120adbe0d38071b625ad2f43c7 (diff)
First working version. Just a POC.
Diffstat (limited to 'danmaku/dmpkg/auth.go')
-rw-r--r--danmaku/dmpkg/auth.go52
1 files changed, 52 insertions, 0 deletions
diff --git a/danmaku/dmpkg/auth.go b/danmaku/dmpkg/auth.go
new file mode 100644
index 0000000..5a205ad
--- /dev/null
+++ b/danmaku/dmpkg/auth.go
@@ -0,0 +1,52 @@
+/*
+This file implements the auth exchange.
+When Bilibili live client established the WebSocket connection successfully,
+it sends this message at first. The server then responses a OpConnectOk exchange with body `{"code":0}` which indicates success.
+*/
+package dmpkg
+
+import (
+ "bilibili-livestream-archiver/common"
+ "encoding/json"
+ "fmt"
+)
+
+type authInfo struct {
+ UID uint64 `json:"uid"`
+ RoomId uint64 `json:"roomid"`
+ ProtoVer int `json:"protover"`
+ Platform string `json:"platform"`
+ Type int `json:"type"`
+ Key string `json:"key"`
+}
+
+// NewAuth creates a new authentication exchange.
+func NewAuth(protocol ProtocolVer, roomId common.RoomId, authKey string) (exc DanmakuExchange) {
+ exc, _ = NewPlainExchange(OpConnect, authInfo{
+ UID: kUidGuest,
+ RoomId: uint64(roomId),
+ ProtoVer: int(protocol),
+ Platform: kPlatformWeb,
+ Type: kAuthTypeDefault,
+ Key: authKey,
+ })
+ return
+}
+
+func IsAuthOk(serverResponse DanmakuExchange) (bool, error) {
+ if op := serverResponse.Operation; op != OpConnectOk {
+ return false, fmt.Errorf("server operation is not OpConnectOk: %w", op)
+ }
+ var body struct {
+ Code int `json:"code"`
+ }
+ body.Code = 1
+ err := json.Unmarshal(serverResponse.Body, &body)
+ if err != nil {
+ return false, fmt.Errorf("JSON decode error: %w", err)
+ }
+ if c := body.Code; c != 0 {
+ return false, fmt.Errorf("server response code is non-zero: %w", c)
+ }
+ return true, nil
+}