From 6493b85a7c7f297335e589a10b3617f0b3d5bd33 Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Tue, 8 Aug 2023 18:14:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0golangci-lint=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .golangci.yaml | 74 +++++++++++++++++++ internal/caches/storage_file_test.go | 2 +- internal/encrypt/method_aes_128_cfb_test.go | 1 - internal/events/utils_test.go | 4 +- internal/nodes/http_request_reverse_proxy.go | 30 +++----- internal/nodes/http_writer.go | 1 - internal/remotelogs/utils.go | 2 - internal/utils/writers/writer_rate_limit.go | 1 - .../response_general_header_length.go | 1 - 9 files changed, 89 insertions(+), 27 deletions(-) create mode 100644 .golangci.yaml diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 0000000..01a74b1 --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,74 @@ +# https://golangci-lint.run/usage/configuration/ + +linters: + enable-all: true + disable: + - ifshort + - exhaustivestruct + - golint + - nosnakecase + - scopelint + - varcheck + - structcheck + - interfacer + - exhaustivestruct + - maligned + - deadcode + - dogsled + - wrapcheck + - wastedassign + - varnamelen + - testpackage + - thelper + - nilerr + - sqlclosecheck + - paralleltest + - nonamedreturns + - nlreturn + - nakedret + - ireturn + - interfacebloat + - gosmopolitan + - gomnd + - goerr113 + - gochecknoglobals + - exhaustruct + - errorlint + - depguard + - exhaustive + - containedctx + - wsl + - cyclop + - dupword + - errchkjson + - contextcheck + - tagalign + - dupl + - forbidigo + - funlen + - goconst + - godox + - gosec + - lll + - nestif + - revive + - unparam + - stylecheck + - gocritic + - gofumpt + - gomoddirectives + - godot + - gofmt + - gocognit + - mirror + - gocyclo + - gochecknoinits + - gci + - maintidx + - prealloc + - goimports + - errname + - musttag + - forcetypeassert + - whitespace + - noctx \ No newline at end of file diff --git a/internal/caches/storage_file_test.go b/internal/caches/storage_file_test.go index 4a51c4c..a66c977 100644 --- a/internal/caches/storage_file_test.go +++ b/internal/caches/storage_file_test.go @@ -159,7 +159,7 @@ func TestFileStorage_OpenWriter_HTTP(t *testing.T) { t.Log(writer) resp := &http.Response{ - StatusCode: 200, + StatusCode: http.StatusOK, Header: http.Header{ "Content-Type": []string{"text/html; charset=utf-8"}, "Last-Modified": []string{"Wed, 06 Jan 2021 10:03:29 GMT"}, diff --git a/internal/encrypt/method_aes_128_cfb_test.go b/internal/encrypt/method_aes_128_cfb_test.go index d0e9d33..3796079 100644 --- a/internal/encrypt/method_aes_128_cfb_test.go +++ b/internal/encrypt/method_aes_128_cfb_test.go @@ -53,7 +53,6 @@ func TestAES128CFBMethod_Encrypt2(t *testing.T) { } { - a := []byte{1} _, err = method.Decrypt(a) if err != nil { diff --git a/internal/events/utils_test.go b/internal/events/utils_test.go index da01f79..f1dd9fb 100644 --- a/internal/events/utils_test.go +++ b/internal/events/utils_test.go @@ -9,8 +9,8 @@ func TestOn(t *testing.T) { type User struct { name string } - var u = &User{} - var u2 = &User{} + var u = &User{name: "lily"} + var u2 = &User{name: "lucy"} events.On("hello", func() { t.Log("world") diff --git a/internal/nodes/http_request_reverse_proxy.go b/internal/nodes/http_request_reverse_proxy.go index 0256631..2d8008e 100644 --- a/internal/nodes/http_request_reverse_proxy.go +++ b/internal/nodes/http_request_reverse_proxy.go @@ -258,6 +258,7 @@ func (this *HTTPRequest) doOriginRequest(failedOriginIds []int64, failedLnNodeId } var resp *http.Response + var respBodyIsClosed bool var requestErr error var requestErrCode string if isHTTPOrigin { // 普通HTTP(S)源站 @@ -296,6 +297,15 @@ func (this *HTTPRequest) doOriginRequest(failedOriginIds []int64, failedLnNodeId this.writeCode(http.StatusBadGateway, "The type of origin site has not been supported", "设置的源站类型尚未支持") return } + + if resp != nil && resp.Body != nil { + defer func() { + if !respBodyIsClosed { + _ = resp.Body.Close() + } + }() + } + if requestErr != nil { // 客户端取消请求,则不提示 httpErr, ok := requestErr.(*url.Error) @@ -324,10 +334,6 @@ func (this *HTTPRequest) doOriginRequest(failedOriginIds []int64, failedLnNodeId shouldRetry = true this.uri = oldURI // 恢复备份 - if resp != nil && resp.Body != nil { - _ = resp.Body.Close() - } - if httpErr.Err != io.EOF { remotelogs.WarnServer("HTTP_REQUEST_REVERSE_PROXY", this.URL()+": Request origin server failed: "+requestErr.Error()) } @@ -366,9 +372,6 @@ func (this *HTTPRequest) doOriginRequest(failedOriginIds []int64, failedLnNodeId this.write50x(requestErr, http.StatusBadGateway, "Failed to read origin site", "源站读取失败", true) } } - if resp != nil && resp.Body != nil { - _ = resp.Body.Close() - } return } @@ -376,9 +379,6 @@ func (this *HTTPRequest) doOriginRequest(failedOriginIds []int64, failedLnNodeId if isLowVersionHTTP && resp.ContentLength < 0 { this.writer.WriteHeader(http.StatusBadRequest) _, _ = this.writer.WriteString("The content does not support " + this.RawReq.Proto + " request.") - if resp.Body != nil { - _ = resp.Body.Close() - } return } @@ -395,20 +395,12 @@ func (this *HTTPRequest) doOriginRequest(failedOriginIds []int64, failedLnNodeId // WAF对出站进行检查 if this.web.FirewallRef != nil && this.web.FirewallRef.IsOn { if this.doWAFResponse(resp) { - err := resp.Body.Close() - if err != nil { - remotelogs.WarnServer("HTTP_REQUEST_REVERSE_PROXY", this.URL()+": Closing Error (WAF): "+err.Error()) - } return } } // 特殊页面 if this.doPage(resp.StatusCode) { - err := resp.Body.Close() - if err != nil { - remotelogs.WarnServer("HTTP_REQUEST_REVERSE_PROXY", this.URL()+": Closing error (Page): "+err.Error()) - } return } @@ -499,6 +491,7 @@ func (this *HTTPRequest) doOriginRequest(failedOriginIds []int64, failedLnNodeId _, _ = io.CopyBuffer(this.writer, resp.Body, buf) utils.BytePool4k.Put(buf) _ = resp.Body.Close() + respBodyIsClosed = true this.writer.SetOk() return @@ -550,6 +543,7 @@ func (this *HTTPRequest) doOriginRequest(failedOriginIds []int64, failedLnNodeId pool.Put(buf) var closeErr = resp.Body.Close() + respBodyIsClosed = true if closeErr != nil { if !this.canIgnore(closeErr) { remotelogs.WarnServer("HTTP_REQUEST_REVERSE_PROXY", this.URL()+": Closing error: "+closeErr.Error()) diff --git a/internal/nodes/http_writer.go b/internal/nodes/http_writer.go index b39c6d1..44d4083 100644 --- a/internal/nodes/http_writer.go +++ b/internal/nodes/http_writer.go @@ -24,7 +24,6 @@ import ( _ "golang.org/x/image/webp" "image" "image/gif" - _ "image/gif" _ "image/jpeg" _ "image/png" "io" diff --git a/internal/remotelogs/utils.go b/internal/remotelogs/utils.go index abd43d3..3d9589c 100644 --- a/internal/remotelogs/utils.go +++ b/internal/remotelogs/utils.go @@ -60,7 +60,6 @@ func Println(tag string, description string) { CreatedAt: time.Now().Unix(), }: default: - } } @@ -78,7 +77,6 @@ func Warn(tag string, description string) { CreatedAt: time.Now().Unix(), }: default: - } } diff --git a/internal/utils/writers/writer_rate_limit.go b/internal/utils/writers/writer_rate_limit.go index 101a196..89bb713 100644 --- a/internal/utils/writers/writer_rate_limit.go +++ b/internal/utils/writers/writer_rate_limit.go @@ -79,7 +79,6 @@ func (this *RateLimitWriter) write(p []byte) (n int, err error) { err = io.EOF return default: - } this.written += n diff --git a/internal/waf/checkpoints/response_general_header_length.go b/internal/waf/checkpoints/response_general_header_length.go index 11eb299..cea2fbf 100644 --- a/internal/waf/checkpoints/response_general_header_length.go +++ b/internal/waf/checkpoints/response_general_header_length.go @@ -19,7 +19,6 @@ func (this *ResponseGeneralHeaderLengthCheckpoint) IsComposed() bool { } func (this *ResponseGeneralHeaderLengthCheckpoint) RequestValue(req requests.Request, param string, options maps.Map, ruleId int64) (value interface{}, hasRequestBody bool, sysErr error, userErr error) { - return }