diff --git a/pkg/configutils/match.go b/pkg/configutils/match.go index 961d7f8..cae57d8 100644 --- a/pkg/configutils/match.go +++ b/pkg/configutils/match.go @@ -7,7 +7,7 @@ import ( var whitespaceReg = regexp.MustCompile(`\s+`) -// 关键词匹配 +// MatchKeyword 关键词匹配 func MatchKeyword(source, keyword string) bool { if len(keyword) == 0 { return false @@ -16,7 +16,7 @@ func MatchKeyword(source, keyword string) bool { pieces := whitespaceReg.Split(keyword, -1) source = strings.ToLower(source) for _, piece := range pieces { - if strings.Index(source, strings.ToLower(piece)) > -1 { + if strings.Contains(source, strings.ToLower(piece)) { return true } } diff --git a/pkg/iplibrary/reader.go b/pkg/iplibrary/reader.go index 49cb4b1..d41cb72 100644 --- a/pkg/iplibrary/reader.go +++ b/pkg/iplibrary/reader.go @@ -46,7 +46,7 @@ func NewReader(reader io.Reader) (*Reader, error) { // 从Reader中加载数据 func (this *Reader) load(reader io.Reader) error { var buf = make([]byte, 1024) - var metaLine = []byte{} + var metaLine []byte var metaLineFound = false var dataBuf = []byte{} for { diff --git a/pkg/iplibrary/reader_file.go b/pkg/iplibrary/reader_file.go index 4e2bb9e..ee7491b 100644 --- a/pkg/iplibrary/reader_file.go +++ b/pkg/iplibrary/reader_file.go @@ -13,7 +13,7 @@ import ( type FileReader struct { rawReader *Reader - password string + //password string } func NewFileReader(path string, password string) (*FileReader, error) { diff --git a/pkg/serverconfigs/filterconfigs/filter_unicode_encode.go b/pkg/serverconfigs/filterconfigs/filter_unicode_encode.go index 1c5b56b..abfe184 100644 --- a/pkg/serverconfigs/filterconfigs/filter_unicode_encode.go +++ b/pkg/serverconfigs/filterconfigs/filter_unicode_encode.go @@ -9,15 +9,15 @@ import ( type UnicodeEncodeFilter struct { } -// 初始化 +// Init 初始化 func (this *UnicodeEncodeFilter) Init() error { return nil } -// 执行过滤 +// Do 执行过滤 func (this *UnicodeEncodeFilter) Do(input interface{}, options interface{}) (output interface{}, goNext bool, err error) { - s := []rune(types.String(input)) - result := strings.Builder{} + var s = types.String(input) + var result = strings.Builder{} for _, r := range s { if r < 128 { result.WriteRune(r) diff --git a/pkg/serverconfigs/firewallconfigs/http_firewall_checkpoints.go b/pkg/serverconfigs/firewallconfigs/http_firewall_checkpoints.go index 14769d9..9cbcd5a 100644 --- a/pkg/serverconfigs/firewallconfigs/http_firewall_checkpoints.go +++ b/pkg/serverconfigs/firewallconfigs/http_firewall_checkpoints.go @@ -304,7 +304,7 @@ var AllCheckpoints = []*HTTPFirewallCheckpointDefinition{ RightLabel: "秒", MaxLength: 8, Validate: func(value string) (ok bool, message string) { - if regexp.MustCompile("^\\d+$").MatchString(value) { + if regexp.MustCompile(`^\d+$`).MatchString(value) { ok = true return } diff --git a/pkg/serverconfigs/http_auth_method_base.go b/pkg/serverconfigs/http_auth_method_base.go index e73ac47..1d3dc0a 100644 --- a/pkg/serverconfigs/http_auth_method_base.go +++ b/pkg/serverconfigs/http_auth_method_base.go @@ -71,29 +71,3 @@ func (this *HTTPAuthBaseMethod) MatchRequest(req *http.Request) bool { return true } - -// cleanPath 清理Path中的多余的字符 -func (this *HTTPAuthBaseMethod) cleanPath(path string) string { - var l = len(path) - if l == 0 { - return "/" - } - var result = []byte{'/'} - var isSlash = true - for i := 0; i < l; i++ { - if path[i] == '?' { - result = append(result, path[i:]...) - break - } - if path[i] == '\\' || path[i] == '/' { - if !isSlash { - isSlash = true - result = append(result, '/') - } - } else { - isSlash = false - result = append(result, path[i]) - } - } - return string(result) -} diff --git a/pkg/serverconfigs/http_fastcgi_config.go b/pkg/serverconfigs/http_fastcgi_config.go index 546cad7..689ce04 100644 --- a/pkg/serverconfigs/http_fastcgi_config.go +++ b/pkg/serverconfigs/http_fastcgi_config.go @@ -2,8 +2,10 @@ package serverconfigs import ( "errors" + "github.com/TeaOSLab/EdgeCommon/pkg/configutils" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared" "github.com/iwind/TeaGo/maps" + "net" "path/filepath" "regexp" "time" @@ -60,11 +62,11 @@ func (this *HTTPFastcgiConfig) Init() error { } // 校验地址 - if regexp.MustCompile("^\\d+$").MatchString(this.Address) { + if regexp.MustCompile(`^\d+$`).MatchString(this.Address) { this.network = "tcp" this.address = "127.0.0.1:" + this.Address - } else if regexp.MustCompile("^(.*):(\\d+)$").MatchString(this.Address) { - matches := regexp.MustCompile("^(.*):(\\d+)$").FindStringSubmatch(this.Address) + } else if regexp.MustCompile(`^(.*):(\d+)$`).MatchString(this.Address) { + var matches = regexp.MustCompile(`^(.*):(\d+)$`).FindStringSubmatch(this.Address) ip := matches[1] port := matches[2] if len(ip) == 0 { @@ -72,9 +74,9 @@ func (this *HTTPFastcgiConfig) Init() error { } this.network = "tcp" this.address = ip + ":" + port - } else if regexp.MustCompile("^\\d+\\.\\d+.\\d+.\\d+$").MatchString(this.Address) { + } else if net.ParseIP(this.address) != nil { this.network = "tcp" - this.address = this.Address + ":9000" + this.address = configutils.QuoteIP(this.Address) + ":9000" } else if regexp.MustCompile("^unix:(.+)$").MatchString(this.Address) { matches := regexp.MustCompile("^unix:(.+)$").FindStringSubmatch(this.Address) path := matches[1] diff --git a/pkg/serverconfigs/http_location_config.go b/pkg/serverconfigs/http_location_config.go index 8d066bf..473a252 100644 --- a/pkg/serverconfigs/http_location_config.go +++ b/pkg/serverconfigs/http_location_config.go @@ -301,13 +301,13 @@ func (this *HTTPLocationConfig) Match(path string, formatter func(source string) if this.patternType == HTTPLocationPatternTypeExact { if this.reverse { if this.caseInsensitive { - return nil, strings.ToLower(path) != strings.ToLower(this.path) + return nil, !strings.EqualFold(path, this.path) } else { return nil, path != this.path } } else { if this.caseInsensitive { - return nil, strings.ToLower(path) == strings.ToLower(this.path) + return nil, strings.EqualFold(path, this.path) } else { return nil, path == this.path } diff --git a/pkg/serverconfigs/metric_item_config.go b/pkg/serverconfigs/metric_item_config.go index c5025ea..bf5ac39 100644 --- a/pkg/serverconfigs/metric_item_config.go +++ b/pkg/serverconfigs/metric_item_config.go @@ -20,7 +20,7 @@ type MetricItemConfig struct { Version int32 `yaml:"version" json:"version"` ExpiresPeriod int `yaml:"expiresPeriod" json:"expiresPeriod"` // 过期周期 - sumType string // 统计类型 + //sumType string // 统计类型 baseTime time.Time // 基准时间 hasHTTPConnectionValue bool // 是否有统计HTTP连接数的数值 } diff --git a/pkg/serverconfigs/origin_config.go b/pkg/serverconfigs/origin_config.go index 89a5891..337b207 100644 --- a/pkg/serverconfigs/origin_config.go +++ b/pkg/serverconfigs/origin_config.go @@ -66,8 +66,8 @@ type OriginConfig struct { requestPath string requestArgs string - hasRequestHeaders bool - hasResponseHeaders bool + //hasRequestHeaders bool + //hasResponseHeaders bool uniqueKey string diff --git a/pkg/serverconfigs/origin_config_test.go b/pkg/serverconfigs/origin_config_test.go index 3200190..1e079c0 100644 --- a/pkg/serverconfigs/origin_config_test.go +++ b/pkg/serverconfigs/origin_config_test.go @@ -1,13 +1,16 @@ package serverconfigs -import "testing" +import ( + "context" + "testing" +) func TestOriginConfig_UniqueKey(t *testing.T) { origin := &OriginConfig{ Id: 1, Version: 101, } - err := origin.Init(nil) + err := origin.Init(context.TODO()) if err != nil { t.Fatal(err) } diff --git a/pkg/serverconfigs/reverse_proxy_config.go b/pkg/serverconfigs/reverse_proxy_config.go index 22131ce..5f78a2a 100644 --- a/pkg/serverconfigs/reverse_proxy_config.go +++ b/pkg/serverconfigs/reverse_proxy_config.go @@ -53,9 +53,9 @@ type ReverseProxyConfig struct { schedulingGroupMap map[string]*SchedulingGroup // domain => *SchedulingGroup schedulingLocker sync.RWMutex - addXRealIPHeader bool - addXForwardedForHeader bool - addForwardedHeader bool + addXRealIPHeader bool + addXForwardedForHeader bool + //addForwardedHeader bool addXForwardedByHeader bool addXForwardedHostHeader bool addXForwardedProtoHeader bool @@ -131,12 +131,8 @@ func (this *ReverseProxyConfig) Init(ctx context.Context) error { if domain == "" { continue } - for _, origin := range defaultGroup.PrimaryOrigins { - group.PrimaryOrigins = append(group.PrimaryOrigins, origin) - } - for _, origin := range defaultGroup.BackupOrigins { - group.BackupOrigins = append(group.BackupOrigins, origin) - } + group.PrimaryOrigins = append(group.PrimaryOrigins, defaultGroup.PrimaryOrigins...) + group.BackupOrigins = append(group.BackupOrigins, defaultGroup.BackupOrigins...) } } } diff --git a/pkg/serverconfigs/reverse_proxy_config_test.go b/pkg/serverconfigs/reverse_proxy_config_test.go index 1cf727e..717e390 100644 --- a/pkg/serverconfigs/reverse_proxy_config_test.go +++ b/pkg/serverconfigs/reverse_proxy_config_test.go @@ -3,6 +3,7 @@ package serverconfigs import ( + "context" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared" "testing" ) @@ -30,7 +31,7 @@ func TestReverseProxyConfig_Init(t *testing.T) { Addr: &NetworkAddressConfig{Host: "127.0.0.4"}, IsOn: true, }) - err := config.Init(nil) + err := config.Init(context.TODO()) if err != nil { t.Fatal(err) } diff --git a/pkg/serverconfigs/schedulingconfigs/scheduling_round_robin.go b/pkg/serverconfigs/schedulingconfigs/scheduling_round_robin.go index 516e6e2..82acab0 100644 --- a/pkg/serverconfigs/schedulingconfigs/scheduling_round_robin.go +++ b/pkg/serverconfigs/schedulingconfigs/scheduling_round_robin.go @@ -52,7 +52,7 @@ func (this *RoundRobinScheduling) Next(call *shared.RequestCall) CandidateInterf if this.index > this.count-1 { this.index = 0 } - weight := this.currentWeights[this.index] + var weight = this.currentWeights[this.index] // 已经一轮了,则重置状态 if weight == 0 { @@ -60,11 +60,10 @@ func (this *RoundRobinScheduling) Next(call *shared.RequestCall) CandidateInterf this.currentWeights = append([]uint{}, this.rawWeights...) } this.index = 0 - weight = this.currentWeights[this.index] } c := this.Candidates[this.index] - this.currentWeights[this.index] -- + this.currentWeights[this.index]-- this.index++ return c } diff --git a/pkg/serverconfigs/server_config.go b/pkg/serverconfigs/server_config.go index b8f00c5..0b6cfcf 100644 --- a/pkg/serverconfigs/server_config.go +++ b/pkg/serverconfigs/server_config.go @@ -271,7 +271,7 @@ func (this *ServerConfig) Init(ctx context.Context) (results []error) { this.isOk = true - return nil + return } func (this *ServerConfig) IsInitialized() bool { diff --git a/pkg/serverconfigs/server_config_test.go b/pkg/serverconfigs/server_config_test.go index 61a871c..843bd22 100644 --- a/pkg/serverconfigs/server_config_test.go +++ b/pkg/serverconfigs/server_config_test.go @@ -1,6 +1,9 @@ package serverconfigs -import "testing" +import ( + "context" + "testing" +) func TestServerConfig_Protocols(t *testing.T) { { @@ -65,7 +68,7 @@ func TestServerConfig_Protocols(t *testing.T) { }, }, }} - err := server.Init(nil) + err := server.Init(context.TODO()) if err != nil { t.Fatal(err) } diff --git a/pkg/serverconfigs/shared/http_request_cond.go b/pkg/serverconfigs/shared/http_request_cond.go index d1e3cf3..4f2a5fc 100644 --- a/pkg/serverconfigs/shared/http_request_cond.go +++ b/pkg/serverconfigs/shared/http_request_cond.go @@ -214,12 +214,12 @@ func (this *HTTPRequestCond) match(formatter func(source string) string) bool { return types.Int64(paramValue)%100 == types.Int64(this.Value) case RequestCondOperatorEqString: if this.IsCaseInsensitive { - return strings.ToUpper(paramValue) == strings.ToUpper(this.Value) + return strings.EqualFold(paramValue, this.Value) } return paramValue == this.Value case RequestCondOperatorNeqString: if this.IsCaseInsensitive { - return strings.ToUpper(paramValue) != strings.ToUpper(this.Value) + return !strings.EqualFold(paramValue, this.Value) } return paramValue != this.Value case RequestCondOperatorHasPrefix: @@ -243,11 +243,11 @@ func (this *HTTPRequestCond) match(formatter func(source string) string) bool { } return !strings.Contains(paramValue, this.Value) case RequestCondOperatorEqIP: - ip := net.ParseIP(paramValue) + var ip = net.ParseIP(paramValue) if ip == nil { return false } - return this.isIP && bytes.Compare(this.ipValue, ip) == 0 + return this.isIP && ip.Equal(this.ipValue) case RequestCondOperatorGtIP: ip := net.ParseIP(paramValue) if ip == nil { diff --git a/pkg/serverconfigs/shared/regexp.go b/pkg/serverconfigs/shared/regexp.go index ab49949..ba42966 100644 --- a/pkg/serverconfigs/shared/regexp.go +++ b/pkg/serverconfigs/shared/regexp.go @@ -9,5 +9,5 @@ var ( RegexpAllDigitNumber = regexp.MustCompile(`^[+-]?\d+$`) // 整数,支持正负数 RegexpAllFloatNumber = regexp.MustCompile(`^[+-]?\d+(\.\d+)?$`) // 浮点数,支持正负数,不支持e RegexpExternalURL = regexp.MustCompile("(?i)^(http|https|ftp)://") // URL - RegexpNamedVariable = regexp.MustCompile("\\${[\\w.-]+}") // 命名变量 + RegexpNamedVariable = regexp.MustCompile(`\${[\w.-]+}`) // 命名变量 ) diff --git a/pkg/serverconfigs/shared/regexp_test.go b/pkg/serverconfigs/shared/regexp_test.go index 44f6228..bf2f9f6 100644 --- a/pkg/serverconfigs/shared/regexp_test.go +++ b/pkg/serverconfigs/shared/regexp_test.go @@ -1,17 +1,21 @@ -package shared +package shared_test import ( + "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared" "github.com/iwind/TeaGo/assert" "testing" ) func TestRegexp(t *testing.T) { - a := assert.NewAssertion(t) + var a = assert.NewAssertion(t) - a.IsTrue(RegexpFloatNumber.MatchString("123")) - a.IsTrue(RegexpFloatNumber.MatchString("123.456")) - a.IsFalse(RegexpFloatNumber.MatchString(".456")) - a.IsFalse(RegexpFloatNumber.MatchString("abc")) - a.IsFalse(RegexpFloatNumber.MatchString("123.")) - a.IsFalse(RegexpFloatNumber.MatchString("123.456e7")) + a.IsTrue(shared.RegexpFloatNumber.MatchString("123")) + a.IsTrue(shared.RegexpFloatNumber.MatchString("123.456")) + a.IsFalse(shared.RegexpFloatNumber.MatchString(".456")) + a.IsFalse(shared.RegexpFloatNumber.MatchString("abc")) + a.IsFalse(shared.RegexpFloatNumber.MatchString("123.")) + a.IsFalse(shared.RegexpFloatNumber.MatchString("123.456e7")) + a.IsTrue(shared.RegexpNamedVariable.MatchString("${abc.efg}")) + a.IsTrue(shared.RegexpNamedVariable.MatchString("${abc}")) + a.IsFalse(shared.RegexpNamedVariable.MatchString("{abc.efg}")) } diff --git a/pkg/serverconfigs/sslconfigs/ssl_policy.go b/pkg/serverconfigs/sslconfigs/ssl_policy.go index 5d77e51..fc0c894 100644 --- a/pkg/serverconfigs/sslconfigs/ssl_policy.go +++ b/pkg/serverconfigs/sslconfigs/ssl_policy.go @@ -252,7 +252,7 @@ func (this *SSLPolicy) certIsEqual(cert1 tls.Certificate, cert2 tls.Certificate) } for index, b := range b1 { - if bytes.Compare(b, b2[index]) != 0 { + if !bytes.Equal(b, b2[index]) { return false } } diff --git a/pkg/serverconfigs/sslconfigs/ssl_policy_test.go b/pkg/serverconfigs/sslconfigs/ssl_policy_test.go index eec082c..678a980 100644 --- a/pkg/serverconfigs/sslconfigs/ssl_policy_test.go +++ b/pkg/serverconfigs/sslconfigs/ssl_policy_test.go @@ -3,6 +3,7 @@ package sslconfigs_test import ( + "context" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/sslconfigs" "github.com/iwind/TeaGo/assert" "testing" @@ -120,7 +121,7 @@ Z3NIV2eNt6YBwkC69DzdazXT OCSPExpiresAt: nowTime + 2, }) - err := policy.Init(nil) + err := policy.Init(context.TODO()) if err != nil { t.Fatal(err) }