forked from erroot/plugin-erwscascade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
357 lines (311 loc) · 9.75 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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package erwscascade
import (
"embed"
"io/ioutil"
"mime"
"net/http"
"net/url"
"path/filepath"
"strings"
"go.uber.org/zap"
. "m7s.live/engine/v4"
"m7s.live/engine/v4/config"
"m7s.live/engine/v4/util"
)
//go:embed ui
var f embed.FS
var defaultYaml DefaultYaml
type ServerConfig struct {
Protocol string `default:"https" desc:"上级平台端口" yaml:"protocol"`
Host string `default:"127.0.0.1" desc:"上级平台IP" yaml:"host"`
Port int `default:"8440" desc:"上级平台端口" yaml:"port"`
ConextPath string `default:"" desc:"上级平台根目录" yaml:"conextpath"`
}
type ClientInfo struct {
Cid string `default:"" desc:"上级平台端口" yaml:"cid" json:"cid"`
Name string `default:"" desc:"上级平台端口" yaml:"name" json:"name"`
Serial string `default:"" desc:"上级平台端口" yaml:"serial" json:"serial"`
}
type ErWsCascadeConfig struct {
DefaultYaml
CInfo ClientInfo `desc:"客户端信息" yaml:"cinfo"`
//erwscascade/wsocket/register
ServerConfig []ServerConfig `yaml:"server"`
config.Publish
config.Subscribe
config.Push
}
func (p *ErWsCascadeConfig) push(streamPath string, url string) {
if err := ErWsCascadePlugin.Push(streamPath, url, NewWscPusher(p), false); err != nil {
ErWsCascadePlugin.Error("push", zap.String("streamPath", streamPath), zap.String("url", url), zap.Error(err))
}
}
func (p *ErWsCascadeConfig) OnEvent(event any) {
switch event.(type) {
case FirstConfig:
p.onClientSetup()
for streamPath, url := range p.PushList {
p.push(streamPath, url)
}
break
case config.Config:
break
case SEclose:
break
}
}
var ErWsCascadePlugin = InstallPlugin(&ErWsCascadeConfig{})
// http 接口向上级平台推流
func (p *ErWsCascadeConfig) API_Push(rw http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
err := ErWsCascadePlugin.Push(query.Get("streamPath"), query.Get("target"), NewWscPusher(p), query.Has("save"))
if err != nil {
util.ReturnError(util.APIErrorQueryParse, err.Error(), rw, r)
} else {
util.ReturnOK(rw, r)
}
}
/*
http ws 代理转发接口,实现 m7s 上级代理转发下级api
示例1:请求下级平台test-c001,通过erwscascade ws 推流接口推流到上级 推送本地的流njtv/glgc 到上级平台 ws://127.0.0.1:8450/erwscascade/wspush/on 这个地址
http://127.0.0.1:8450/erwscascade/httpproxy/?cid=test-c001&httpPath=/erwscascade/api/push?streamPath=njtv/glgc&target=ws://127.0.0.1:8450/erwscascade/wspush/on
示例2: 请求下级平台 test-c001, 通过rtmp 推流接口推送流到上级 推送本地的流njtv/glgc 到上级平台 rtmp://127.0.0.1:1945/njtv/glgc-rtmp-push 这个地址
http://127.0.0.1:8450/erwscascade/httpproxy/?cid=test-c001&httpPath=/rtmp/api/push?streamPath=njtv/glgc&target=rtmp://127.0.0.1:1945/njtv/glgc-rtmp-push
*/
func (p *ErWsCascadeConfig) HttpProxy_(w http.ResponseWriter, r *http.Request) {
///erwscascade/httpProxy/webrtc/play/live/webrtc
// 修改请求 URL
//fmt.Printf("config: %v\n", config.Gloub)
// 去除原始请求中的 /proxy 部分
//targetPath := strings.TrimPrefix(r.URL.Path, "/httpproxy")
// if r.URL.RawQuery != "" {
// targetURL += "?" + r.URL.RawQuery
// }
// 构造新的查询参数
queryParams := r.URL.Query()
cid := queryParams.Get("cid")
if cid != "" {
queryParams.Del("cid")
} else {
util.ReturnError(util.APIErrorQueryParse, "invalid cid", w, r)
return
}
httpPath := queryParams.Get("httpPath")
if httpPath != "" {
queryParams.Del("httpPath")
} else {
util.ReturnError(util.APIErrorQueryParse, "invalid httpPath", w, r)
return
}
// 重新构造查询参数
newQuery := queryParams.Encode() //特殊字符被编码了
//代理到下级平台
if newQuery != "" {
if strings.Contains(httpPath, "?") {
httpPath += "&"
} else {
httpPath += "?"
}
httpPath += newQuery
}
decodedStr, err := url.QueryUnescape(httpPath) //url 解码
if err != nil {
util.ReturnError(util.APIErrorQueryParse, "QueryUnescape faild", w, r)
return
}
req := ProxyMessage{
Url: decodedStr, //url 解码
Method: r.Method,
}
// 创建一个新的 http.Header 对象
newHeader := make(http.Header)
// 获取 Content-Type
contentType := r.Header.Get("Content-Type")
if contentType != "" {
newHeader.Set("Content-Type", contentType)
}
// 获取 Set-Cookie
setCookie := r.Header.Get("Set-Cookie")
if setCookie != "" {
newHeader.Set("Set-Cookie", setCookie)
}
req.Header = newHeader
if r.ContentLength > 0 {
// 读取 Body 的内容
body, err := ioutil.ReadAll(r.Body)
if err != nil {
ErWsCascadePlugin.Error("Error reading request body", zap.Error(err))
return
}
req.Body = body
}
//ws send msg
rsp, err := p.transWsProxyMessage(cid, req)
if err != nil {
ErWsCascadePlugin.Error("WsProxy faild", zap.Error(err))
return
}
// 将读取的 Body 内容作为响应返回给客户端
w.Write(rsp)
return
// //本机代理
// targetURL := "http://127.0.0.1:8440" + targetPath
// if newQuery != "" {
// targetURL += "?" + newQuery
// }
// // 创建新的请求
// req, err := http.NewRequest(r.Method, targetURL, r.Body)
// if err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// return
// }
// // 复制请求头
// req.Header = make(http.Header)
// for key, values := range r.Header {
// for _, value := range values {
// req.Header.Add(key, value)
// }
// }
// // 发送请求
// resp, err := http.DefaultClient.Do(req)
// if err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// return
// }
// defer resp.Body.Close()
// // 将响应写回给客户端
// for key, values := range resp.Header {
// for _, value := range values {
// w.Header().Add(key, value)
// }
// w.WriteHeader(resp.StatusCode)
// io.Copy(w, resp.Body)
// }
}
/*
func (p *ErWsCascadeConfig) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/erwscascade/" {
var s string
Streams.Range(func(streamPath string, _ *Stream) {
s += fmt.Sprintf("<a href='%s'>%s</a><br>", streamPath, streamPath)
})
if s != "" {
s = "<b>Live Streams</b><br>" + s
}
for name, p := range Plugins {
if pullcfg, ok := p.Config.(config.PullConfig); ok {
if pullonsub := pullcfg.GetPullConfig().PullOnSub; pullonsub != nil {
s += fmt.Sprintf("<b>%s pull stream on subscribe</b><br>", name)
for streamPath, url := range pullonsub {
s += fmt.Sprintf("<a href='%s'>%s</a> <-- %s<br>", streamPath, streamPath, url)
}
}
}
}
w.Write([]byte(s))
return
}
ss := strings.Split(r.URL.Path, "/")
if b, err := f.ReadFile("ui/" + ss[len(ss)-1]); err == nil {
w.Header().Set("Content-Type", mime.TypeByExtension(filepath.Ext(ss[len(ss)-1])))
w.Write(b)
} else {
//w.Header().Set("Cross-Origin-Opener-Policy", "same-origin")
//w.Header().Set("Cross-Origin-Embedder-Policy", "require-corp")
b, err = f.ReadFile("ui/index.html")
w.Write(b)
}
}
*/
func (p *ErWsCascadeConfig) Ui_(w http.ResponseWriter, r *http.Request) {
ss := strings.Split(r.URL.Path, "/")
if b, err := f.ReadFile("ui/" + ss[len(ss)-1]); err == nil {
w.Header().Set("Content-Type", mime.TypeByExtension(filepath.Ext(ss[len(ss)-1])))
w.Write(b)
} else {
//w.Header().Set("Cross-Origin-Opener-Policy", "same-origin")
//w.Header().Set("Cross-Origin-Embedder-Policy", "require-corp")
b, err = f.ReadFile("ui/index.html")
w.Write(b)
}
}
type CascadingStream struct {
Source string
StreamPath string
}
func filterStreams() (ss []*CascadingStream) {
//优先获取配置文件中视频流
for name, p := range Plugins {
if pullcfg, ok := p.Config.(config.PullConfig); ok {
if pullonstart := pullcfg.GetPullConfig().PullOnStart; pullonstart != nil {
//s += fmt.Sprintf("<b>%s pull stream on subscribe</b><br>", name)
//for streamPath, url := range pullonsub {
var sourcename string = name
sourcename += "Pull"
for streamPath := range pullonstart {
ss = append(ss, &CascadingStream{sourcename, streamPath})
//s += fmt.Sprintf("<a href='%s'>%s</a> <-- %s<br>", streamPath, streamPath, url)
}
}
if pullonsub := pullcfg.GetPullConfig().PullOnSub; pullonsub != nil {
//s += fmt.Sprintf("<b>%s pull stream on subscribe</b><br>", name)
//for streamPath, url := range pullonsub {
var sourcename string = name
sourcename += "Pull"
for streamPath := range pullonsub {
ss = append(ss, &CascadingStream{sourcename, streamPath})
//s += fmt.Sprintf("<a href='%s'>%s</a> <-- %s<br>", streamPath, streamPath, url)
}
}
}
}
//过滤出动态添加的视频流
//Streams.RLock()
//defer Streams.RUnlock()
Streams.Range(func(streamPath string, s *Stream) {
//s += fmt.Sprintf("<a href='%s'>%s</a><br>", streamPath, streamPath)
var isrepeat bool = false
for _, s := range ss {
if streamPath == s.StreamPath {
isrepeat = true
}
}
if !isrepeat {
ss = append(ss, &CascadingStream{"api", streamPath})
}
})
return
}
// 客户端列表
func (*ErWsCascadeConfig) API_clientlist(w http.ResponseWriter, r *http.Request) {
// 输出map对象内容
list := make([]ClientInfo, 0)
for _, value := range clientConnections {
//fmt.Println("Key:", key, "Value:", value)
list = append(list, value.CInfo)
}
util.ReturnValue(list, w, r)
}
func (p *ErWsCascadeConfig) API_streamlist(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
cid := query.Get("cid")
// 本级流资源列表
if cid == "" {
util.ReturnFetchValue(filterStreams, w, r)
return
}
// 下级平台流列表
req := ProxyMessage{
Url: "/erwscascade/api/streamlist", //url 解码
Method: "GET",
}
//ws send msg
rsp, err := p.transWsProxyMessage(cid, req)
if err != nil {
ErWsCascadePlugin.Error("WsProxy faild", zap.Error(err))
return
}
// 将读取的 Body 内容作为响应返回给客户端
w.Write(rsp)
return
}