forked from wwengg/douyin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
136 lines (121 loc) · 3.35 KB
/
main.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
package main
import (
"bytes"
"encoding/json"
"errors"
"github.com/wwengg/douyin/fay/impl"
"github.com/wwengg/douyin/model"
"github.com/wwengg/douyin/proto"
"github.com/wwengg/douyin/utils"
"io"
"log"
"net/http"
"strings"
"github.com/elazarl/goproxy"
)
func main() {
utils.ConfigureCA()
proxy := goproxy.NewProxyHttpServer()
proxy.Verbose = false
fayProxyServer := impl.NewFayProxyServer()
go fayProxyServer.StartWebsocket()
//ws数据处理
proxy.AddWebsocketHandler(func(data []byte, direction goproxy.WebsocketDirection, ctx *goproxy.ProxyCtx) (reply []byte) {
reply = data
if len(data) == 0 {
return
}
if data[0] != 0x08 {
return
}
wssResponse := proto.WssResponse{}
if err := wssResponse.XXX_Unmarshal(data); err == nil {
//检测包格式
if v, ok := wssResponse.Headers["compress_type"]; !ok && v != "gzip" {
return
}
//解压gzip
deData, err := utils.GzipDecode(wssResponse.Payload)
if err != nil {
ctx.Logf("gzip解压失败")
return
}
res := proto.Response{}
if err = res.XXX_Unmarshal(deData); err != nil {
return
}
for _, message := range res.Messages {
fayProxyServer.DoMessage(message)
}
}
return
})
proxy.WebSocketHandler = func(dst io.Writer, src io.Reader, direction goproxy.WebsocketDirection, ctx *goproxy.ProxyCtx) error {
fullPacket := make([]byte, 0)
buf := make([]byte, 32*1024)
var err error = nil
for {
nr, er := src.Read(buf)
if er != nil {
if er != io.EOF {
err = er
}
break
}
if nr > 0 {
fullPacket = append(fullPacket, buf[:nr]...)
websocketPacket := model.NewWebsocketPacket(fullPacket)
if !websocketPacket.Valid {
continue
}
websocketPacket.Payload = proxy.FilterWebsocketPacket(websocketPacket.Payload, direction, ctx)
encodedPacket := websocketPacket.Encode()
nw, ew := dst.Write(encodedPacket)
fullPacket = fullPacket[websocketPacket.PacketSize:]
if nw < 0 || len(encodedPacket) < nw {
nw = 0
if ew == nil {
ew = errors.New("invalid write result")
}
}
if ew != nil {
err = ew
break
}
if len(encodedPacket) != nw {
err = io.ErrShortWrite
break
}
}
}
return err
}
proxy.OnRequest(goproxy.ReqHostIs("webcast.amemv.com:443", "frontier-im.douyin.com:443", "webcast100-ws-web-lq.amemv.com:443", "webcast3-ws-web-lf.douyin.com:443", "webcast3-ws-web-hl.douyin.com:443")).
HandleConnect(goproxy.AlwaysMitm)
proxy.OnResponse(goproxy.UrlHasPrefix("httpswebcast.amemv.com:443/webcast/room/create/")).DoFunc(
func(resp *http.Response, ctx *goproxy.ProxyCtx) *http.Response {
buf, _ := io.ReadAll(resp.Body)
responseStream := io.NopCloser(bytes.NewBuffer(buf))
rtmpLive := RtmpLive{}
json.Unmarshal(buf, &rtmpLive)
log.Println(rtmpLive)
url := rtmpLive.Data.StreamUrl.RtmpPushUrl
array := strings.Split(url, "/")
secret := array[len(array)-1]
serverName := strings.Split(url, secret)[0]
log.Printf(`服务器:%s`, serverName)
log.Printf(`推流码:%s`, secret)
resp.Body = responseStream
return resp
},
)
log.Println("软件准备就绪,请启动【直播伴侣】并且点击【开始直播】")
log.Fatal(http.ListenAndServe(":8001", proxy))
}
type RtmpLive struct {
Data struct {
StreamUrl struct {
RtmpPushUrl string `json:"rtmp_push_url"`
} `json:"stream_url"`
} `json:"data"`
}