]> 127.0.0.1 Git - bili_danmu/.git/commitdiff
Add 直播流回放连接限制
authorqydysky <qydysky@foxmail.com>
Sun, 21 May 2023 00:51:19 +0000 (08:51 +0800)
committerqydysky <qydysky@foxmail.com>
Sun, 21 May 2023 00:51:22 +0000 (08:51 +0800)
Reply/F.go
demo/config/config_K_v.json
demo/config/config_disable_msg.json

index b1f0b4ce6fcfebee59d451b862bdd4c36a640ef6..2b7028401e7f91ea1b0ea731f79c360c037829e5 100644 (file)
@@ -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 + "/"
index a824844c495b2080f0daf23b3ec2b1056aebcfc0..533e81c3bb121e8a419005cdb9d0172b1e0f3511 100644 (file)
@@ -96,6 +96,8 @@
     "弹幕回放": true,
     "直播流回放速率-help": "速率为每秒速率 例最小值(1 MB)",
     "直播流回放速率": "2 MB",
+    "直播流回放连接限制-help": "限制回放连接数,0无限制",
+    "直播流回放连接限制": 0,
     "ass-help": "只有保存直播流时才考虑生成ass,ass编码默认GB18030(可选utf-8)",
     "生成Ass弹幕": true,
     "Ass编码": "GB18030",
index 3c33e8e2042722fd404d870cda467274c0fd384f..a810a3e9323e4e2e04de85b2dd247857f096b54c 100644 (file)
@@ -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,