mirror of
https://github.com/AynaLivePlayer/blivedm-go.git
synced 2025-12-06 11:22:50 +08:00
feat: dm_v2
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
@@ -68,12 +69,14 @@ func (c *Client) init() error {
|
||||
}
|
||||
|
||||
func (c *Client) connect() error {
|
||||
reqHeader := &http.Header{}
|
||||
reqHeader.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36")
|
||||
retryCount := 0
|
||||
retry:
|
||||
// 随着重连会自动切换弹幕服务器
|
||||
c.host = c.hostList[retryCount%len(c.hostList)]
|
||||
retryCount++
|
||||
conn, res, err := websocket.DefaultDialer.Dial(fmt.Sprintf("wss://%s/sub", c.host), nil)
|
||||
conn, res, err := websocket.DefaultDialer.Dial(fmt.Sprintf("wss://%s/sub", c.host), *reqHeader)
|
||||
if err != nil {
|
||||
log.Errorf("connect dial failed, retry %d times", retryCount)
|
||||
time.Sleep(2 * time.Second)
|
||||
|
||||
@@ -13,7 +13,8 @@ import (
|
||||
|
||||
func main() {
|
||||
log.SetLevel(log.DebugLevel)
|
||||
c := client.NewClient("732", "194484313")
|
||||
c := client.NewClient("23555200", "194484313")
|
||||
c.UseDefaultHost()
|
||||
//弹幕事件
|
||||
c.OnDanmaku(func(danmaku *message.Danmaku) {
|
||||
if danmaku.Type == message.EmoticonDanmaku {
|
||||
|
||||
1
go.mod
1
go.mod
@@ -8,4 +8,5 @@ require (
|
||||
github.com/sirupsen/logrus v1.8.1
|
||||
github.com/tidwall/gjson v1.13.0
|
||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da // indirect
|
||||
google.golang.org/protobuf v1.31.0 // indirect
|
||||
)
|
||||
|
||||
12
go.sum
12
go.sum
@@ -2,6 +2,11 @@ github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY
|
||||
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
||||
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
|
||||
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
|
||||
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
|
||||
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
@@ -19,3 +24,10 @@ github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhso
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da h1:b3NXsE2LusjYGGjL5bxEVZZORm/YEFFrWFjR8eFrw/c=
|
||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||
google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
|
||||
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
|
||||
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
|
||||
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package message
|
||||
|
||||
import (
|
||||
"github.com/Akegarasu/blivedm-go/pb"
|
||||
"github.com/Akegarasu/blivedm-go/utils"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/tidwall/gjson"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -60,7 +62,9 @@ type (
|
||||
|
||||
func (d *Danmaku) Parse(data []byte) {
|
||||
sb := utils.BytesToString(data)
|
||||
info := gjson.Parse(sb).Get("info")
|
||||
parsed := gjson.Parse(sb)
|
||||
info := parsed.Get("info")
|
||||
|
||||
ext := new(Extra)
|
||||
emo := new(Emoticon)
|
||||
err := utils.UnmarshalStr(info.Get("0.15.extra").String(), ext)
|
||||
@@ -95,4 +99,17 @@ func (d *Danmaku) Parse(data []byte) {
|
||||
d.Type = int(info.Get("0.12").Int())
|
||||
d.Timestamp = info.Get("0.4").Int()
|
||||
d.Raw = sb
|
||||
|
||||
dmv2Content := parsed.Get("dm_v2").String()
|
||||
if dmv2Content != "" {
|
||||
decoded, _ := utils.B64Decode(dmv2Content)
|
||||
dmv2 := new(pb.Dm)
|
||||
|
||||
err := proto.Unmarshal(decoded, dmv2)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
d.Content = dmv2.Content
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -6,26 +6,25 @@ import (
|
||||
)
|
||||
|
||||
type Enter struct {
|
||||
UID int `json:"uid"`
|
||||
RoomID int `json:"roomid"`
|
||||
ProtoVer int `json:"protover"`
|
||||
Platform string `json:"platform"`
|
||||
ClientVer string `json:"clientver"`
|
||||
Type int `json:"type"`
|
||||
Key string `json:"key"`
|
||||
UID int `json:"uid"`
|
||||
RoomID int `json:"roomid"`
|
||||
ProtoVer int `json:"protover"`
|
||||
Buvid string `json:"buvid"`
|
||||
Platform string `json:"platform"`
|
||||
Type int `json:"type"`
|
||||
Key string `json:"key"`
|
||||
}
|
||||
|
||||
// NewEnterPacket 构造进入房间的包
|
||||
// uid 可以为 0, key 在使用 broadcastlv 服务器的时候不需要
|
||||
func NewEnterPacket(uid int, roomID int, key string) []byte {
|
||||
ent := &Enter{
|
||||
UID: uid,
|
||||
RoomID: roomID,
|
||||
ProtoVer: 2,
|
||||
Platform: "web",
|
||||
ClientVer: "1.14.3",
|
||||
Type: 2,
|
||||
Key: key,
|
||||
UID: uid,
|
||||
RoomID: roomID,
|
||||
ProtoVer: 3,
|
||||
Platform: "web",
|
||||
Type: 2,
|
||||
Key: key,
|
||||
}
|
||||
m, err := json.Marshal(ent)
|
||||
if err != nil {
|
||||
|
||||
1767
pb/dmv2.pb.go
Normal file
1767
pb/dmv2.pb.go
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,7 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
@@ -16,3 +17,13 @@ func StringToBytes(s string) []byte {
|
||||
func BytesToString(b []byte) string {
|
||||
return *(*string)(unsafe.Pointer(&b))
|
||||
}
|
||||
|
||||
func B64Decode(s string) ([]byte, error) {
|
||||
dst := make([]byte, base64.StdEncoding.DecodedLen(len(s)))
|
||||
n, err := base64.StdEncoding.Decode(dst, []byte(s))
|
||||
if err != nil {
|
||||
return dst, err
|
||||
}
|
||||
dst = dst[:n]
|
||||
return dst, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user