]> 127.0.0.1 Git - bili_danmu/.git/commitdiff
Add 添加web超时配置
authorqydysky <qydysky@foxmail.com>
Mon, 29 May 2023 17:33:42 +0000 (01:33 +0800)
committerqydysky <qydysky@foxmail.com>
Mon, 29 May 2023 17:33:42 +0000 (01:33 +0800)
CV/Var.go
F/api.go
Reply/F.go
demo/config/config_K_v.json
go.mod
go.sum

index ab934f9e8314dd72f5562fc297a93ddc39e2b070..f3aeae9d2171d947ecdb1854a19c477bdf0b2af5 100644 (file)
--- a/CV/Var.go
+++ b/CV/Var.go
@@ -302,8 +302,29 @@ func (t *Common) Init() *Common {
                        panic(e)
                }
 
+               var (
+                       readTimeout       = 3
+                       readHeaderTimeout = 3
+                       idleTimeout       = 3
+               )
+
+               if v, ok := t.K_v.LoadV("Web服务超时配置").(map[string]any); ok {
+                       if v1, ok := v["ReadTimeout"].(float64); ok && v1 > 3 {
+                               readTimeout = int(v1)
+                       }
+                       if v1, ok := v["ReadHeaderTimeout"].(float64); ok && v1 > 3 {
+                               readHeaderTimeout = int(v1)
+                       }
+                       if v1, ok := v["IdleTimeout"].(float64); ok && v1 > 3 {
+                               idleTimeout = int(v1)
+                       }
+               }
+
                web.NewSyncMap(&http.Server{
-                       Addr: serUrl.Host,
+                       Addr:              serUrl.Host,
+                       ReadTimeout:       time.Duration(int(time.Second) * readTimeout),
+                       ReadHeaderTimeout: time.Duration(int(time.Second) * readHeaderTimeout),
+                       IdleTimeout:       time.Duration(int(time.Second) * idleTimeout),
                }, t.SerF)
 
                if limits, ok := t.K_v.LoadV(`Web服务连接限制`).([]any); ok {
@@ -323,9 +344,7 @@ func (t *Common) Init() *Common {
                if val, ok := t.K_v.LoadV("性能路径").(string); ok && val != "" {
                        var cache web.Cache
                        t.SerF.Store(val, func(w http.ResponseWriter, r *http.Request) {
-                               //limit
-                               if t.SerLimit.AddCount(r) {
-                                       web.WithStatusCode(w, http.StatusTooManyRequests)
+                               if DefaultHttpCheck(t, w, r, http.MethodGet) {
                                        return
                                }
 
@@ -510,3 +529,17 @@ func (t ResStruct) Write(w http.ResponseWriter) []byte {
        _, _ = w.Write(data)
        return data
 }
+
+func DefaultHttpCheck(c *Common, w http.ResponseWriter, r *http.Request, method ...string) bool {
+       //method
+       if !web.IsMethod(r, method...) {
+               web.WithStatusCode(w, http.StatusMethodNotAllowed)
+               return true
+       }
+       //limit
+       if c.SerLimit.AddCount(r) {
+               web.WithStatusCode(w, http.StatusTooManyRequests)
+               return true
+       }
+       return false
+}
index 386ec1831268e88d603bc22783a80480088a4d2a..8a76ebc562974c9f1ac35ed14f5d6b505f08794c 100644 (file)
--- a/F/api.go
+++ b/F/api.go
@@ -26,7 +26,6 @@ import (
        limit "github.com/qydysky/part/limit"
        reqf "github.com/qydysky/part/reqf"
        psync "github.com/qydysky/part/sync"
-       web "github.com/qydysky/part/web"
 
        "github.com/mdp/qrterminal/v3"
        qr "github.com/skip2/go-qrcode"
@@ -1246,21 +1245,21 @@ func (c *GetFunc) GetNav() (J.Nav, error) {
 }
 
 // 扫码登录
-func (c *GetFunc) Get_cookie() (missKey []string) {
+func (t *GetFunc) Get_cookie() (missKey []string) {
        apilog := apilog.Base_add(`获取Cookie`)
        //获取其他Cookie
-       defer c.Get_other_cookie()
+       defer t.Get_other_cookie()
 
        if p.Checkfile().IsExist("cookie.txt") { //读取cookie文件
                if cookieString := string(CookieGet()); cookieString != `` {
                        for k, v := range reqf.Cookies_String_2_Map(cookieString) { //cookie存入全局变量syncmap
-                               c.Cookie.Store(k, v)
+                               t.Cookie.Store(k, v)
                        }
                        if miss := CookieCheck([]string{
                                `bili_jct`,
                                `DedeUserID`,
                        }); len(miss) == 0 {
-                               if v, e := c.GetNav(); e != nil {
+                               if v, e := t.GetNav(); e != nil {
                                        apilog.L(`E: `, e)
                                } else if v.Data.IsLogin {
                                        apilog.L(`I: `, `已登录`)
@@ -1270,7 +1269,7 @@ func (c *GetFunc) Get_cookie() (missKey []string) {
                }
        }
 
-       if v, ok := c.K_v.LoadV(`扫码登录`).(bool); !ok || !v {
+       if v, ok := t.K_v.LoadV(`扫码登录`).(bool); !ok || !v {
                apilog.L(`W: `, `配置文件已禁止扫码登录,如需登录,修改配置文件"扫码登录"为true`)
                return
        }
@@ -1282,11 +1281,11 @@ func (c *GetFunc) Get_cookie() (missKey []string) {
        var img_url string
        var oauth string
        { //获取二维码
-               r := c.ReqPool.Get()
-               defer c.ReqPool.Put(r)
+               r := t.ReqPool.Get()
+               defer t.ReqPool.Put(r)
                if e := r.Reqf(reqf.Rval{
                        Url:     `https://passport.bilibili.com/qrcode/getLoginUrl`,
-                       Proxy:   c.Proxy,
+                       Proxy:   t.Proxy,
                        Timeout: 10 * 1000,
                        Retry:   2,
                }); e != nil {
@@ -1340,19 +1339,17 @@ func (c *GetFunc) Get_cookie() (missKey []string) {
                }
                defer os.RemoveAll(`qr.png`)
                //启动web
-               if scanPath, ok := c.K_v.LoadV("扫码登录路径").(string); ok && scanPath != "" {
-                       c.SerF.Store(scanPath, func(w http.ResponseWriter, r *http.Request) {
-                               //limit
-                               if c.SerLimit.AddCount(r) {
-                                       web.WithStatusCode(w, http.StatusTooManyRequests)
+               if scanPath, ok := t.K_v.LoadV("扫码登录路径").(string); ok && scanPath != "" {
+                       t.SerF.Store(scanPath, func(w http.ResponseWriter, r *http.Request) {
+                               if c.DefaultHttpCheck(t.Common, w, r, http.MethodGet) {
                                        return
                                }
                                _ = file.New("qr.png", 0, true).CopyToIoWriter(w, humanize.MByte, true)
                        })
-                       if c.K_v.LoadV(`扫码登录自动打开标签页`).(bool) {
-                               _ = open.Run(`http://127.0.0.1:` + c.Stream_url.Port() + scanPath)
+                       if t.K_v.LoadV(`扫码登录自动打开标签页`).(bool) {
+                               _ = open.Run(`http://127.0.0.1:` + t.Stream_url.Port() + scanPath)
                        }
-                       apilog.L(`W: `, `或打开链接扫码登录:`+c.Stream_url.String()+scanPath)
+                       apilog.L(`W: `, `或打开链接扫码登录:`+t.Stream_url.String()+scanPath)
                }
 
                apilog.Block(1000)
@@ -1374,7 +1371,7 @@ func (c *GetFunc) Get_cookie() (missKey []string) {
 
        { //循环查看是否通过
                Cookie := make(map[string]string)
-               c.Cookie.Range(func(k, v interface{}) bool {
+               t.Cookie.Range(func(k, v interface{}) bool {
                        Cookie[k.(string)] = v.(string)
                        return true
                })
@@ -1388,8 +1385,8 @@ func (c *GetFunc) Get_cookie() (missKey []string) {
                                return
                        }
 
-                       r := c.ReqPool.Get()
-                       defer c.ReqPool.Put(r)
+                       r := t.ReqPool.Get()
+                       defer t.ReqPool.Put(r)
                        if e := r.Reqf(reqf.Rval{
                                Url:     `https://passport.bilibili.com/qrcode/getLoginInfo`,
                                PostStr: `oauthKey=` + oauth,
@@ -1398,7 +1395,7 @@ func (c *GetFunc) Get_cookie() (missKey []string) {
                                        `Referer`:      `https://passport.bilibili.com/login`,
                                        `Cookie`:       reqf.Map_2_Cookies_String(Cookie),
                                },
-                               Proxy:   c.Proxy,
+                               Proxy:   t.Proxy,
                                Timeout: 10 * 1000,
                                Retry:   2,
                        }); e != nil {
index 3d31e101f80cb0038c6b2fc2d3ee3bb9e5f889be..7d02b996209b929d6607b7ed5f20093f4dabe312 100644 (file)
@@ -1149,41 +1149,31 @@ func init() {
                // debug模式
                if de, ok := c.C.K_v.LoadV(`debug模式`).(bool); ok && de {
                        c.C.SerF.Store("/debug/pprof/", func(w http.ResponseWriter, r *http.Request) {
-                               //limit
-                               if c.C.SerLimit.AddCount(r) {
-                                       pweb.WithStatusCode(w, http.StatusTooManyRequests)
+                               if c.DefaultHttpCheck(c.C, w, r, http.MethodGet) {
                                        return
                                }
                                pprof.Index(w, r)
                        })
                        c.C.SerF.Store("/debug/pprof/cmdline", func(w http.ResponseWriter, r *http.Request) {
-                               //limit
-                               if c.C.SerLimit.AddCount(r) {
-                                       pweb.WithStatusCode(w, http.StatusTooManyRequests)
+                               if c.DefaultHttpCheck(c.C, w, r, http.MethodGet) {
                                        return
                                }
                                pprof.Cmdline(w, r)
                        })
                        c.C.SerF.Store("/debug/pprof/profile", func(w http.ResponseWriter, r *http.Request) {
-                               //limit
-                               if c.C.SerLimit.AddCount(r) {
-                                       pweb.WithStatusCode(w, http.StatusTooManyRequests)
+                               if c.DefaultHttpCheck(c.C, w, r, http.MethodGet) {
                                        return
                                }
                                pprof.Profile(w, r)
                        })
                        c.C.SerF.Store("/debug/pprof/symbol", func(w http.ResponseWriter, r *http.Request) {
-                               //limit
-                               if c.C.SerLimit.AddCount(r) {
-                                       pweb.WithStatusCode(w, http.StatusTooManyRequests)
+                               if c.DefaultHttpCheck(c.C, w, r, http.MethodGet) {
                                        return
                                }
                                pprof.Symbol(w, r)
                        })
                        c.C.SerF.Store("/debug/pprof/trace", func(w http.ResponseWriter, r *http.Request) {
-                               //limit
-                               if c.C.SerLimit.AddCount(r) {
-                                       pweb.WithStatusCode(w, http.StatusTooManyRequests)
+                               if c.DefaultHttpCheck(c.C, w, r, http.MethodGet) {
                                        return
                                }
                                pprof.Trace(w, r)
@@ -1211,9 +1201,7 @@ func init() {
 
                // 直播流主页
                c.C.SerF.Store(path, func(w http.ResponseWriter, r *http.Request) {
-                       //limit
-                       if c.C.SerLimit.AddCount(r) {
-                               pweb.WithStatusCode(w, http.StatusTooManyRequests)
+                       if c.DefaultHttpCheck(c.C, w, r, http.MethodGet) {
                                return
                        }
 
@@ -1250,9 +1238,7 @@ func init() {
 
                // 直播流文件列表api
                c.C.SerF.Store(path+"filePath", func(w http.ResponseWriter, r *http.Request) {
-                       //limit
-                       if c.C.SerLimit.AddCount(r) {
-                               pweb.WithStatusCode(w, http.StatusTooManyRequests)
+                       if c.DefaultHttpCheck(c.C, w, r, http.MethodGet) {
                                return
                        }
 
@@ -1316,9 +1302,7 @@ func init() {
 
                // 直播流播放器
                c.C.SerF.Store(path+"player/", func(w http.ResponseWriter, r *http.Request) {
-                       //limit
-                       if c.C.SerLimit.AddCount(r) {
-                               pweb.WithStatusCode(w, http.StatusTooManyRequests)
+                       if c.DefaultHttpCheck(c.C, w, r, http.MethodGet) {
                                return
                        }
 
@@ -1362,9 +1346,7 @@ func init() {
 
                // 流地址
                c.C.SerF.Store(path+"stream", func(w http.ResponseWriter, r *http.Request) {
-                       //limit
-                       if c.C.SerLimit.AddCount(r) {
-                               pweb.WithStatusCode(w, http.StatusTooManyRequests)
+                       if c.DefaultHttpCheck(c.C, w, r, http.MethodGet) {
                                return
                        }
 
@@ -1492,9 +1474,7 @@ func init() {
 
                // 弹幕回放
                c.C.SerF.Store(path+"player/ws", func(w http.ResponseWriter, r *http.Request) {
-                       //limit
-                       if c.C.SerLimit.AddCount(r) {
-                               pweb.WithStatusCode(w, http.StatusTooManyRequests)
+                       if c.DefaultHttpCheck(c.C, w, r, http.MethodGet) {
                                return
                        }
 
index f4e86dc59c88778701c09d251aceadceb4028347..8fdabe86a79b51adb1b5d09c3d764cab9aedb994 100644 (file)
             "max":-1
         }
     ],
+    "Web服务超时配置-help":"单位秒,最小值3",
+    "Web服务超时配置": {
+        "ReadTimeout":3,
+        "ReadHeaderTimeout":3,
+        "IdleTimeout":3
+    },
     "直播Web服务路径":"/web/",
     "直播Web可以发送弹幕":true,
     "弹幕回放-help": "仅保存当前直播间流为true时才有效",
diff --git a/go.mod b/go.mod
index bfa823f3cee09cad98809a728c107e2f9ad8ff09..c1f203d9575733024eeb21d0d1b84eab28d23655 100644 (file)
--- a/go.mod
+++ b/go.mod
@@ -5,7 +5,7 @@ go 1.20
 require (
        github.com/gotk3/gotk3 v0.6.2
        github.com/mdp/qrterminal/v3 v3.0.0
-       github.com/qydysky/part v0.27.17
+       github.com/qydysky/part v0.28.1-0.20230529171227-d4a6f98e0263
        github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
        github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966
        golang.org/x/text v0.9.0
diff --git a/go.sum b/go.sum
index 39e07f36e3160e752676b471cf737b6e313b25fd..9eac1b631bb348a6f639fe41b0322abe42832abd 100644 (file)
--- a/go.sum
+++ b/go.sum
@@ -31,8 +31,8 @@ github.com/mdp/qrterminal/v3 v3.0.0/go.mod h1:NJpfAs7OAm77Dy8EkWrtE4aq+cE6McoLXl
 github.com/miekg/dns v1.1.54 h1:5jon9mWcb0sFJGpnI99tOMhCPyJ+RPVz5b63MQG0VWI=
 github.com/miekg/dns v1.1.54/go.mod h1:uInx36IzPl7FYnDcMeVWxj9byh7DutNykX4G9Sj60FY=
 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
-github.com/qydysky/part v0.27.17 h1:hYSOjnkV0jIf/tSLBmqEAXfkprEhwySQrVWRtrZxuXE=
-github.com/qydysky/part v0.27.17/go.mod h1:IEMpGB0NBl6MklZmoenSpS5ChhaIL79JYFo6mF1UkAU=
+github.com/qydysky/part v0.28.1-0.20230529171227-d4a6f98e0263 h1:kkumZFaAahNhSFjeY1VCihzG/DOATDtXLAyhsh9R2AE=
+github.com/qydysky/part v0.28.1-0.20230529171227-d4a6f98e0263/go.mod h1:IEMpGB0NBl6MklZmoenSpS5ChhaIL79JYFo6mF1UkAU=
 github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
 github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
 github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=