From: qydysky Date: Sun, 21 May 2023 00:51:19 +0000 (+0800) Subject: Add 直播流回放连接限制 X-Git-Tag: v0.9.6~9 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=0b24c2e7fcfbfbb0c7fb5f91cddc659b785532d9;p=bili_danmu%2F.git Add 直播流回放连接限制 --- diff --git a/Reply/F.go b/Reply/F.go index b1f0b4c..2b70284 100644 --- a/Reply/F.go +++ b/Reply/F.go @@ -1154,6 +1154,15 @@ func init() { c.C.SerF.Store("/debug/pprof/trace", pprof.Trace) } + // 直播流回放连接限制 + var ( + limitCon atomic.Int32 + maxCon int32 + ) + if count, ok := c.C.K_v.LoadV(`直播流回放连接限制`).(float64); ok { + maxCon = int32(count) + } + // 直播流主页 c.C.SerF.Store(path, func(w http.ResponseWriter, r *http.Request) { p := strings.TrimPrefix(r.URL.Path, path) @@ -1229,6 +1238,18 @@ func init() { // 直播流播放器 c.C.SerF.Store(path+"player/", func(w http.ResponseWriter, r *http.Request) { + // 直播流回放连接限制 + if maxCon > 0 { + if limitCon.Add(1) > maxCon { + limitCon.Add(-1) + w.WriteHeader(http.StatusTooManyRequests) + _, _ = w.Write([]byte("已达到设定最大连接数")) + return + } else { + defer limitCon.Add(-1) + } + } + p := strings.TrimPrefix(r.URL.Path, path+"player/") if len(p) == 0 || p[len(p)-1] == '/' { p += "index.html" @@ -1251,6 +1272,18 @@ func init() { // 流地址 c.C.SerF.Store(path+"stream", func(w http.ResponseWriter, r *http.Request) { + // 直播流回放连接限制 + if maxCon > 0 { + if limitCon.Add(1) > maxCon { + limitCon.Add(-1) + w.WriteHeader(http.StatusTooManyRequests) + _, _ = w.Write([]byte("已达到设定最大连接数")) + return + } else { + defer limitCon.Add(-1) + } + } + //header w.Header().Set("Access-Control-Allow-Credentials", "true") w.Header().Set("Access-Control-Allow-Headers", "*") @@ -1260,14 +1293,6 @@ func init() { w.Header().Set("Content-Transfer-Encoding", "binary") var rpath string - // if referer, e := url.Parse(r.Header.Get(`Referer`)); e != nil { - // w.Header().Set("Retry-After", "1") - // w.WriteHeader(http.StatusServiceUnavailable) - // flog.L(`E: `, e) - // return - // } else { - // rpath = referer.Path - // } if qref := r.URL.Query().Get("ref"); rpath == "" && qref != "" { rpath = "/" + qref + "/" diff --git a/demo/config/config_K_v.json b/demo/config/config_K_v.json index a824844..533e81c 100644 --- a/demo/config/config_K_v.json +++ b/demo/config/config_K_v.json @@ -96,6 +96,8 @@ "弹幕回放": true, "直播流回放速率-help": "速率为每秒速率 例最小值(1 MB)", "直播流回放速率": "2 MB", + "直播流回放连接限制-help": "限制回放连接数,0无限制", + "直播流回放连接限制": 0, "ass-help": "只有保存直播流时才考虑生成ass,ass编码默认GB18030(可选utf-8)", "生成Ass弹幕": true, "Ass编码": "GB18030", diff --git a/demo/config/config_disable_msg.json b/demo/config/config_disable_msg.json index 3c33e8e..a810a3e 100644 --- a/demo/config/config_disable_msg.json +++ b/demo/config/config_disable_msg.json @@ -1,5 +1,7 @@ { "help": "禁用指定消息类型,false将忽略此类消息,true将显示此类消息的json纯文本", + "PANEL_INTERACTIVE_NOTIFY_CHANGE": false, + "INTERACTIVE_USER": false, "VOICE_CHAT_UPDATE": false, "LIVE_PANEL_CHANGE": false, "PK_BATTLE_PRE": false,