-
Notifications
You must be signed in to change notification settings - Fork 41
/
room.go
195 lines (176 loc) · 5.49 KB
/
room.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
package main
import (
"bytes"
"compress/gzip"
"io"
"log"
"net/http"
"regexp"
"strings"
"time"
dyproto "github.com/XiaoMiku01/douyin-live-go/protobuf"
"github.com/golang/protobuf/proto"
"github.com/gorilla/websocket"
)
type Room struct {
// 房间地址
Url string
Ttwid string
RoomStore string
RoomId string
RoomTitle string
wsConnect *websocket.Conn
}
func NewRoom(u string) (*Room, error) {
h := map[string]string{
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36",
"cookie": "__ac_nonce=0638733a400869171be51",
}
req, err := http.NewRequest("GET", u, nil)
if err != nil {
log.Println(err)
return nil, err
}
for k, v := range h {
req.Header.Set(k, v)
}
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
log.Println(err)
return nil, err
}
defer res.Body.Close()
data := res.Cookies()
var ttwid string
for _, c := range data {
if c.Name == "ttwid" {
ttwid = c.Value
break
}
}
body, err := io.ReadAll(res.Body)
if err != nil {
log.Println(err)
return nil, err
}
resText := string(body)
re := regexp.MustCompile(`roomId\\":\\"(\d+)\\"`)
match := re.FindStringSubmatch(resText)
if match == nil || len(match) < 2 {
log.Println("No match found")
return nil, err
}
liveRoomId := match[1]
return &Room{
Url: u,
Ttwid: ttwid,
RoomId: liveRoomId,
}, nil
}
func (r *Room) Connect() error {
wsUrl := "wss://webcast3-ws-web-lq.douyin.com/webcast/im/push/v2/?app_name=douyin_web&version_code=180800&webcast_sdk_version=1.3.0&update_version_code=1.3.0&compress=gzip&internal_ext=internal_src:dim|wss_push_room_id:%s|wss_push_did:%s|dim_log_id:202302171547011A160A7BAA76660E13ED|fetch_time:1676620021641|seq:1|wss_info:0-1676620021641-0-0|wrds_kvs:WebcastRoomStatsMessage-1676620020691146024_WebcastRoomRankMessage-1676619972726895075_AudienceGiftSyncData-1676619980834317696_HighlightContainerSyncData-2&cursor=t-1676620021641_r-1_d-1_u-1_h-1&host=https://live.douyin.com&aid=6383&live_id=1&did_rule=3&debug=false&endpoint=live_pc&support_wrds=1&im_path=/webcast/im/fetch/&user_unique_id=%s&device_platform=web&cookie_enabled=true&screen_width=1440&screen_height=900&browser_language=zh&browser_platform=MacIntel&browser_name=Mozilla&browser_version=5.0%20(Macintosh;%20Intel%20Mac%20OS%20X%2010_15_7)%20AppleWebKit/537.36%20(KHTML,%20like%20Gecko)%20Chrome/110.0.0.0%20Safari/537.36&browser_online=true&tz_name=Asia/Shanghai&identity=audience&room_id=%s&heartbeatDuration=0&signature=00000000"
wsUrl = strings.Replace(wsUrl, "%s", r.RoomId, -1)
h := http.Header{}
h.Set("cookie", "ttwid="+r.Ttwid)
h.Set("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36")
wsConn, wsResp, err := websocket.DefaultDialer.Dial(wsUrl, h)
if err != nil {
return err
}
log.Println(wsResp.StatusCode)
r.wsConnect = wsConn
go r.read()
go r.send()
return nil
}
func (r *Room) read() {
for {
_, data, err := r.wsConnect.ReadMessage()
if err != nil {
panic(err.Error())
}
var msgPack dyproto.PushFrame
_ = proto.Unmarshal(data, &msgPack)
// log.Println(msgPack.LogId)
decompressed, _ := degzip(msgPack.Payload)
var payloadPackage dyproto.Response
_ = proto.Unmarshal(decompressed, &payloadPackage)
if payloadPackage.NeedAck {
r.sendAck(msgPack.LogId, payloadPackage.InternalExt)
}
// log.Println(len(payloadPackage.MessagesList))
for _, msg := range payloadPackage.MessagesList {
switch msg.Method {
case "WebcastChatMessage":
parseChatMsg(msg.Payload)
case "WebcastGiftMessage":
parseGiftMsg(msg.Payload)
case "WebcastLikeMessage":
parseLikeMsg(msg.Payload)
case "WebcastMemberMessage":
parseEnterMsg(msg.Payload)
}
}
}
}
func (r *Room) send() {
for {
pingPack := &dyproto.PushFrame{
PayloadType: "bh",
}
data, _ := proto.Marshal(pingPack)
err := r.wsConnect.WriteMessage(websocket.BinaryMessage, data)
if err != nil {
panic(err.Error())
}
// log.Println("发送心跳")
time.Sleep(time.Second * 10)
}
}
func (r *Room) sendAck(logId uint64, iExt string) {
ackPack := &dyproto.PushFrame{
LogId: logId,
PayloadType: iExt,
}
data, _ := proto.Marshal(ackPack)
err := r.wsConnect.WriteMessage(websocket.BinaryMessage, data)
if err != nil {
panic(err.Error())
}
// log.Println("发送 ack 包")
}
func degzip(data []byte) ([]byte, error) {
b := bytes.NewReader(data)
var out bytes.Buffer
r, err := gzip.NewReader(b)
if err != nil {
return nil, err
}
_, err = io.Copy(&out, r)
if err != nil {
return nil, err
}
return out.Bytes(), nil
}
func parseChatMsg(msg []byte) {
var chatMsg dyproto.ChatMessage
_ = proto.Unmarshal(msg, &chatMsg)
log.Printf("[弹幕] %s : %s\n", chatMsg.User.NickName, chatMsg.Content)
}
func parseGiftMsg(msg []byte) {
var giftMsg dyproto.GiftMessage
_ = proto.Unmarshal(msg, &giftMsg)
log.Printf("[礼物] %s : %s * %d \n", giftMsg.User.NickName, giftMsg.Gift.Name, giftMsg.ComboCount)
}
func parseLikeMsg(msg []byte) {
var likeMsg dyproto.LikeMessage
_ = proto.Unmarshal(msg, &likeMsg)
log.Printf("[点赞] %s 点赞 * %d \n", likeMsg.User.NickName, likeMsg.Count)
}
func parseEnterMsg(msg []byte) {
var enterMsg dyproto.MemberMessage
_ = proto.Unmarshal(msg, &enterMsg)
log.Printf("[入场] %s 直播间\n", enterMsg.User.NickName)
}