From 9a42b10e3885dffe39f9f8c60531f6d70235da99 Mon Sep 17 00:00:00 2001 From: qydysky Date: Tue, 30 May 2023 01:33:42 +0800 Subject: [PATCH] =?utf8?q?Add=20=E6=B7=BB=E5=8A=A0web=E8=B6=85=E6=97=B6?= =?utf8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- CV/Var.go | 41 +++++++++++++++++++++++++++++++++---- F/api.go | 39 ++++++++++++++++------------------- Reply/F.go | 40 +++++++++--------------------------- demo/config/config_K_v.json | 6 ++++++ go.mod | 2 +- go.sum | 4 ++-- 6 files changed, 74 insertions(+), 58 deletions(-) diff --git a/CV/Var.go b/CV/Var.go index ab934f9..f3aeae9 100644 --- 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 +} diff --git a/F/api.go b/F/api.go index 386ec18..8a76ebc 100644 --- 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 { diff --git a/Reply/F.go b/Reply/F.go index 3d31e10..7d02b99 100644 --- a/Reply/F.go +++ b/Reply/F.go @@ -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 } diff --git a/demo/config/config_K_v.json b/demo/config/config_K_v.json index f4e86dc..8fdabe8 100644 --- a/demo/config/config_K_v.json +++ b/demo/config/config_K_v.json @@ -97,6 +97,12 @@ "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 bfa823f..c1f203d 100644 --- 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 39e07f3..9eac1b6 100644 --- 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= -- 2.39.2