From 74ffac478c87b38286e542ac915b1cb3d1a2c5bb Mon Sep 17 00:00:00 2001 From: qydysky Date: Thu, 25 May 2023 07:16:12 +0800 Subject: [PATCH] =?utf8?q?Improve=20=E8=BF=9E=E6=8E=A5=E9=99=90=E5=88=B6?= =?utf8?q?=E7=A7=BB=E5=85=A5=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .github/workflows/go.yml | 2 ++ .github/workflows/test.yml | 2 ++ Reply/F.go | 74 ++++++-------------------------------- go.mod | 2 +- go.sum | 4 +-- 5 files changed, 17 insertions(+), 67 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 41bc95a..78f3e5a 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -61,6 +61,7 @@ jobs: run: | sudo apt-get update sudo apt-get install libgtk-3-dev libcairo2-dev libglib2.0-dev + git rev-parse --short HEAD > VERSION cd demo go get . CGO_ENABLED=0 go build -v -buildmode=exe -o demo.run main.go @@ -93,6 +94,7 @@ jobs: - name: Build run: | + git rev-parse --short HEAD > VERSION cd demo set CGO_ENABLED=0 go get . diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c7a6258..9226b8b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -62,6 +62,7 @@ jobs: - name: Build run: | + git rev-parse --short HEAD > VERSION cd demo sudo apt-get update sudo apt-get install libgtk-3-dev libcairo2-dev libglib2.0-dev @@ -94,6 +95,7 @@ jobs: - name: Build run: | + git rev-parse --short HEAD > VERSION cd demo go get . set CGO_ENABLED=0 diff --git a/Reply/F.go b/Reply/F.go index 1d18d94..e2e1607 100644 --- a/Reply/F.go +++ b/Reply/F.go @@ -9,7 +9,6 @@ import ( "io" "io/fs" "math" - "net" "net/http" "net/http/pprof" "sort" @@ -37,6 +36,7 @@ import ( limit "github.com/qydysky/part/limit" msgq "github.com/qydysky/part/msgq" psync "github.com/qydysky/part/sync" + pweb "github.com/qydysky/part/web" websocket "github.com/qydysky/part/websocket" encoder "golang.org/x/text/encoding" @@ -1156,14 +1156,7 @@ func init() { } // 直播流回放连接限制 - type limitItem struct { - cidr *net.IPNet - available int - } - var ( - limitCon []limitItem - limitL psync.RWMutex - ) + var climit pweb.CountLimits if limits, ok := c.C.K_v.LoadV(`直播流回放连接限制`).([]any); ok { for i := 0; i < len(limits); i++ { if vm, ok := limits[i].(map[string]any); ok { @@ -1172,11 +1165,7 @@ func init() { } else if max, ok := vm["max"].(float64); !ok { continue } else { - if _, cidrx, err := net.ParseCIDR(cidr); err != nil { - flog.L(`E: `, err) - } else { - limitCon = append(limitCon, limitItem{cidr: cidrx, available: int(max)}) - } + climit.SetMaxCount(cidr, int(max)) } } } @@ -1258,25 +1247,10 @@ func init() { // 直播流播放器 c.C.SerF.Store(path+"player/", func(w http.ResponseWriter, r *http.Request) { // 直播流回放连接限制 - if len(limitCon) != 0 { - ip := net.ParseIP(strings.Split(r.RemoteAddr, ":")[0]) - var isOverflow bool - ul := limitL.RLock() - for i := 0; i < len(limitCon); i++ { - if !limitCon[i].cidr.Contains(ip) { - continue - } - if limitCon[i].available == 0 { - isOverflow = true - break - } - } - ul() - if isOverflow { - w.WriteHeader(http.StatusTooManyRequests) - _, _ = w.Write([]byte("已达到设定最大连接数")) - return - } + if climit.ReachMax(r) { + w.WriteHeader(http.StatusTooManyRequests) + _, _ = w.Write([]byte("已达到设定最大连接数")) + return } p := strings.TrimPrefix(r.URL.Path, path+"player/") @@ -1302,37 +1276,9 @@ func init() { // 流地址 c.C.SerF.Store(path+"stream", func(w http.ResponseWriter, r *http.Request) { // 直播流回放连接限制 - if len(limitCon) != 0 { - ip := net.ParseIP(strings.Split(r.RemoteAddr, ":")[0]) - var add []int - ul := limitL.Lock() - for i := 0; i < len(limitCon); i++ { - if !limitCon[i].cidr.Contains(ip) { - continue - } - if limitCon[i].available != 0 { - add = append(add, i) - } - } - if len(add) == 0 { - ul() - w.WriteHeader(http.StatusTooManyRequests) - return - } else { - for i := 0; i < len(add); i++ { - limitCon[add[i]].available -= 1 - } - ul() - // 连接退出 - go func() { - <-r.Context().Done() - ul := limitL.Lock() - for i := 0; i < len(add); i++ { - limitCon[add[i]].available += 1 - } - ul() - }() - } + if climit.AddCount(r) { + w.WriteHeader(http.StatusTooManyRequests) + return } //header diff --git a/go.mod b/go.mod index 39ba9aa..e6bf7a3 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.9 + github.com/qydysky/part v0.27.12 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 3895d2a..f83e64a 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.9 h1:1VQHeayiAie8xSjVp4ILzfHtzK1QjgDSvcWH7ddUXvc= -github.com/qydysky/part v0.27.9/go.mod h1:IEMpGB0NBl6MklZmoenSpS5ChhaIL79JYFo6mF1UkAU= +github.com/qydysky/part v0.27.12 h1:IGVfRuiYYhfaIwlTYZL4QqFNJw7uF0l6uQcaI4Ajnso= +github.com/qydysky/part v0.27.12/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