From fc6ae4d4e7f5158d7222a42c749d2bf7ca913632 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Tue, 28 Jun 2022 20:26:01 +0800 Subject: [PATCH] Fix lint --- .github/linters/.golangci.yml | 2 + app/log/command/command.go | 4 +- app/router/strategy_leastload_test.go | 110 +++++++++--------- common/environment/envctx/env.go | 2 +- .../strmatcher/matchergroup_ac_automation.go | 6 +- common/strmatcher/strmatcher.go | 12 +- context.go | 7 +- infra/conf/merge/merge.go | 1 - infra/conf/v5cfg/common.go | 2 +- proxy/vmess/outbound/outbound.go | 3 +- proxy/vmess/validator.go | 4 +- transport/internet/kcp/kcp.go | 5 +- transport/internet/system_dns_android.go | 12 +- transport/internet/websocket/ws.go | 3 +- 14 files changed, 93 insertions(+), 80 deletions(-) diff --git a/.github/linters/.golangci.yml b/.github/linters/.golangci.yml index 056d47a69..fee978d49 100644 --- a/.github/linters/.golangci.yml +++ b/.github/linters/.golangci.yml @@ -2,6 +2,8 @@ run: timeout: 5m skip-files: - generated.* + - .*_test.go + - proxy/vlite/* issues: new: true diff --git a/app/log/command/command.go b/app/log/command/command.go index 0c63d2bc3..2fe457c22 100644 --- a/app/log/command/command.go +++ b/app/log/command/command.go @@ -4,11 +4,13 @@ package command import ( "context" + + grpc "google.golang.org/grpc" + core "github.com/v2fly/v2ray-core/v5" "github.com/v2fly/v2ray-core/v5/app/log" "github.com/v2fly/v2ray-core/v5/common" cmlog "github.com/v2fly/v2ray-core/v5/common/log" - grpc "google.golang.org/grpc" ) // LoggerServer is the implemention of LoggerService diff --git a/app/router/strategy_leastload_test.go b/app/router/strategy_leastload_test.go index a3b05c06a..2d11301d5 100644 --- a/app/router/strategy_leastload_test.go +++ b/app/router/strategy_leastload_test.go @@ -7,64 +7,64 @@ import ( /* Split into multiple package, need to be tested separately -func TestSelectLeastLoad(t *testing.T) { - settings := &StrategyLeastLoadConfig{ - HealthCheck: &HealthPingConfig{ - SamplingCount: 10, - }, - Expected: 1, - MaxRTT: int64(time.Millisecond * time.Duration(800)), + func TestSelectLeastLoad(t *testing.T) { + settings := &StrategyLeastLoadConfig{ + HealthCheck: &HealthPingConfig{ + SamplingCount: 10, + }, + Expected: 1, + MaxRTT: int64(time.Millisecond * time.Duration(800)), + } + strategy := NewLeastLoadStrategy(settings) + // std 40 + strategy.PutResult("a", time.Millisecond*time.Duration(60)) + strategy.PutResult("a", time.Millisecond*time.Duration(140)) + strategy.PutResult("a", time.Millisecond*time.Duration(60)) + strategy.PutResult("a", time.Millisecond*time.Duration(140)) + // std 60 + strategy.PutResult("b", time.Millisecond*time.Duration(40)) + strategy.PutResult("b", time.Millisecond*time.Duration(160)) + strategy.PutResult("b", time.Millisecond*time.Duration(40)) + strategy.PutResult("b", time.Millisecond*time.Duration(160)) + // std 0, but >MaxRTT + strategy.PutResult("c", time.Millisecond*time.Duration(1000)) + strategy.PutResult("c", time.Millisecond*time.Duration(1000)) + strategy.PutResult("c", time.Millisecond*time.Duration(1000)) + strategy.PutResult("c", time.Millisecond*time.Duration(1000)) + expected := "a" + actual := strategy.SelectAndPick([]string{"a", "b", "c", "untested"}) + if actual != expected { + t.Errorf("expected: %v, actual: %v", expected, actual) + } } - strategy := NewLeastLoadStrategy(settings) - // std 40 - strategy.PutResult("a", time.Millisecond*time.Duration(60)) - strategy.PutResult("a", time.Millisecond*time.Duration(140)) - strategy.PutResult("a", time.Millisecond*time.Duration(60)) - strategy.PutResult("a", time.Millisecond*time.Duration(140)) - // std 60 - strategy.PutResult("b", time.Millisecond*time.Duration(40)) - strategy.PutResult("b", time.Millisecond*time.Duration(160)) - strategy.PutResult("b", time.Millisecond*time.Duration(40)) - strategy.PutResult("b", time.Millisecond*time.Duration(160)) - // std 0, but >MaxRTT - strategy.PutResult("c", time.Millisecond*time.Duration(1000)) - strategy.PutResult("c", time.Millisecond*time.Duration(1000)) - strategy.PutResult("c", time.Millisecond*time.Duration(1000)) - strategy.PutResult("c", time.Millisecond*time.Duration(1000)) - expected := "a" - actual := strategy.SelectAndPick([]string{"a", "b", "c", "untested"}) - if actual != expected { - t.Errorf("expected: %v, actual: %v", expected, actual) - } -} -func TestSelectLeastLoadWithCost(t *testing.T) { - settings := &StrategyLeastLoadConfig{ - HealthCheck: &HealthPingConfig{ - SamplingCount: 10, - }, - Costs: []*StrategyWeight{ - {Match: "a", Value: 9}, - }, - Expected: 1, + func TestSelectLeastLoadWithCost(t *testing.T) { + settings := &StrategyLeastLoadConfig{ + HealthCheck: &HealthPingConfig{ + SamplingCount: 10, + }, + Costs: []*StrategyWeight{ + {Match: "a", Value: 9}, + }, + Expected: 1, + } + strategy := NewLeastLoadStrategy(settings, nil) + // std 40, std+c 120 + strategy.PutResult("a", time.Millisecond*time.Duration(60)) + strategy.PutResult("a", time.Millisecond*time.Duration(140)) + strategy.PutResult("a", time.Millisecond*time.Duration(60)) + strategy.PutResult("a", time.Millisecond*time.Duration(140)) + // std 60 + strategy.PutResult("b", time.Millisecond*time.Duration(40)) + strategy.PutResult("b", time.Millisecond*time.Duration(160)) + strategy.PutResult("b", time.Millisecond*time.Duration(40)) + strategy.PutResult("b", time.Millisecond*time.Duration(160)) + expected := "b" + actual := strategy.SelectAndPick([]string{"a", "b", "untested"}) + if actual != expected { + t.Errorf("expected: %v, actual: %v", expected, actual) + } } - strategy := NewLeastLoadStrategy(settings, nil) - // std 40, std+c 120 - strategy.PutResult("a", time.Millisecond*time.Duration(60)) - strategy.PutResult("a", time.Millisecond*time.Duration(140)) - strategy.PutResult("a", time.Millisecond*time.Duration(60)) - strategy.PutResult("a", time.Millisecond*time.Duration(140)) - // std 60 - strategy.PutResult("b", time.Millisecond*time.Duration(40)) - strategy.PutResult("b", time.Millisecond*time.Duration(160)) - strategy.PutResult("b", time.Millisecond*time.Duration(40)) - strategy.PutResult("b", time.Millisecond*time.Duration(160)) - expected := "b" - actual := strategy.SelectAndPick([]string{"a", "b", "untested"}) - if actual != expected { - t.Errorf("expected: %v, actual: %v", expected, actual) - } -} */ func TestSelectLeastExpected(t *testing.T) { strategy := &LeastLoadStrategy{ diff --git a/common/environment/envctx/env.go b/common/environment/envctx/env.go index 2957735d4..bea27bb60 100644 --- a/common/environment/envctx/env.go +++ b/common/environment/envctx/env.go @@ -7,7 +7,7 @@ const ( ) func ContextWithEnvironment(ctx context.Context, environment interface{}) context.Context { - return context.WithValue(ctx, environmentKey, environment) //nolint: revive + return context.WithValue(ctx, environmentKey, environment) //nolint: revive,staticcheck } func EnvironmentFromContext(ctx context.Context) interface{} { diff --git a/common/strmatcher/matchergroup_ac_automation.go b/common/strmatcher/matchergroup_ac_automation.go index e1d5e52ea..1f8e2de1e 100644 --- a/common/strmatcher/matchergroup_ac_automation.go +++ b/common/strmatcher/matchergroup_ac_automation.go @@ -216,9 +216,9 @@ func (ac *ACAutomatonMatcherGroup) MatchAny(input string) bool { } // Letter-Digit-Hyphen (LDH) subset (https://tools.ietf.org/html/rfc952): -// * Letters A to Z (no distinction is made between uppercase and lowercase) -// * Digits 0 to 9 -// * Hyphens(-) and Periods(.) +// - Letters A to Z (no distinction is made between uppercase and lowercase) +// - Digits 0 to 9 +// - Hyphens(-) and Periods(.) // // If for future the strmatcher are used for other scenarios than domain, // we could add a new Charset interface to represent variable charsets. diff --git a/common/strmatcher/strmatcher.go b/common/strmatcher/strmatcher.go index a399e74eb..8d9b74aad 100644 --- a/common/strmatcher/strmatcher.go +++ b/common/strmatcher/strmatcher.go @@ -15,7 +15,7 @@ const ( ) // Matcher is the interface to determine a string matches a pattern. -// * This is a basic matcher to represent a certain kind of match semantic(full, substr, domain or regex). +// - This is a basic matcher to represent a certain kind of match semantic(full, substr, domain or regex). type Matcher interface { // Type returns the matcher's type. Type() Type @@ -34,8 +34,8 @@ type Matcher interface { // MatcherGroup is an advanced type of matcher to accept a bunch of basic Matchers (of certain type, not all matcher types). // For example: -// * FullMatcherGroup accepts FullMatcher and uses a hash table to facilitate lookup. -// * DomainMatcherGroup accepts DomainMatcher and uses a trie to optimize both memory consumption and lookup speed. +// - FullMatcherGroup accepts FullMatcher and uses a hash table to facilitate lookup. +// - DomainMatcherGroup accepts DomainMatcher and uses a trie to optimize both memory consumption and lookup speed. type MatcherGroup interface { // Match returns all matched matchers with their corresponding values. Match(input string) []uint32 @@ -46,9 +46,9 @@ type MatcherGroup interface { // IndexMatcher is a general type of matcher thats accepts all kinds of basic matchers. // It should: -// * Accept all Matcher types with no exception. -// * Optimize string matching with a combination of MatcherGroups. -// * Obey certain priority order specification when returning matched Matchers. +// - Accept all Matcher types with no exception. +// - Optimize string matching with a combination of MatcherGroups. +// - Obey certain priority order specification when returning matched Matchers. type IndexMatcher interface { // Size returns number of matchers added to IndexMatcher. Size() uint32 diff --git a/context.go b/context.go index f5b00a8c6..668f5c1df 100644 --- a/context.go +++ b/context.go @@ -26,7 +26,8 @@ func MustFromContext(ctx context.Context) *Instance { return v } -/* toContext returns ctx from the given context, or creates an Instance if the context doesn't find that. +/* + toContext returns ctx from the given context, or creates an Instance if the context doesn't find that. It is unsupported to use this function to create a context that is suitable to invoke V2Ray's internal component in third party code, you shouldn't use //go:linkname to alias of this function into your own package and @@ -34,7 +35,6 @@ use this function in your third party code. For third party code, usage enabled by creating a context to interact with V2Ray's internal component is unsupported, and may break at any time. - */ func toContext(ctx context.Context, v *Instance) context.Context { if FromContext(ctx) != v { @@ -43,7 +43,8 @@ func toContext(ctx context.Context, v *Instance) context.Context { return ctx } -/*ToBackgroundDetachedContext create a detached context from another context +/* +ToBackgroundDetachedContext create a detached context from another context Internal API */ func ToBackgroundDetachedContext(ctx context.Context) context.Context { diff --git a/infra/conf/merge/merge.go b/infra/conf/merge/merge.go index db0ffe564..db6e37df8 100644 --- a/infra/conf/merge/merge.go +++ b/infra/conf/merge/merge.go @@ -11,7 +11,6 @@ Merge Rules: - Simple values (string, number, boolean) are overwritten, others are merged - Elements with same "tag" (or "_tag") in an array will be merged - Add "_priority" property to array elements will help sort the - */ package merge diff --git a/infra/conf/v5cfg/common.go b/infra/conf/v5cfg/common.go index 050b568ba..a7296d5b6 100644 --- a/infra/conf/v5cfg/common.go +++ b/infra/conf/v5cfg/common.go @@ -14,7 +14,7 @@ import ( func loadHeterogeneousConfigFromRawJSON(interfaceType, name string, rawJSON json.RawMessage) (proto.Message, error) { fsdef := envimpl.NewDefaultFileSystemDefaultImpl() ctx := envctx.ContextWithEnvironment(context.TODO(), fsdef) - if rawJSON == nil || len(rawJSON) == 0 { + if len(rawJSON) == 0 { rawJSON = []byte("{}") } return registry.LoadImplementationByAlias(ctx, interfaceType, name, []byte(rawJSON)) diff --git a/proxy/vmess/outbound/outbound.go b/proxy/vmess/outbound/outbound.go index 5ede5da34..a0463c83e 100644 --- a/proxy/vmess/outbound/outbound.go +++ b/proxy/vmess/outbound/outbound.go @@ -6,6 +6,8 @@ import ( "context" "crypto/hmac" "crypto/sha256" + "hash/crc64" + core "github.com/v2fly/v2ray-core/v5" "github.com/v2fly/v2ray-core/v5/common" "github.com/v2fly/v2ray-core/v5/common/buf" @@ -23,7 +25,6 @@ import ( "github.com/v2fly/v2ray-core/v5/proxy/vmess/encoding" "github.com/v2fly/v2ray-core/v5/transport" "github.com/v2fly/v2ray-core/v5/transport/internet" - "hash/crc64" ) // Handler is an outbound connection handler for VMess protocol. diff --git a/proxy/vmess/validator.go b/proxy/vmess/validator.go index 76bae8d36..226bf2679 100644 --- a/proxy/vmess/validator.go +++ b/proxy/vmess/validator.go @@ -252,7 +252,9 @@ func (v *TimedUserValidator) BurnTaintFuse(userHash []byte) error { return ErrNotFound } -/* ShouldShowLegacyWarn will return whether a Legacy Warning should be shown +/* + ShouldShowLegacyWarn will return whether a Legacy Warning should be shown + Not guaranteed to only return true once for every inbound, but it is okay. */ func (v *TimedUserValidator) ShouldShowLegacyWarn() bool { diff --git a/transport/internet/kcp/kcp.go b/transport/internet/kcp/kcp.go index 3c02a33bc..5d2721aca 100644 --- a/transport/internet/kcp/kcp.go +++ b/transport/internet/kcp/kcp.go @@ -1,8 +1,9 @@ // Package kcp - A Fast and Reliable ARQ Protocol // // Acknowledgement: -// skywind3000@github for inventing the KCP protocol -// xtaci@github for translating to Golang +// +// skywind3000@github for inventing the KCP protocol +// xtaci@github for translating to Golang package kcp //go:generate go run github.com/v2fly/v2ray-core/v5/common/errors/errorgen diff --git a/transport/internet/system_dns_android.go b/transport/internet/system_dns_android.go index a53e0e5bd..9cdd42a89 100644 --- a/transport/internet/system_dns_android.go +++ b/transport/internet/system_dns_android.go @@ -10,13 +10,17 @@ import ( const SystemDNS = "8.8.8.8:53" -/* DNSResolverFunc - This is a temporary API and is subject to removal at any time. +/* +DNSResolverFunc + + This is a temporary API and is subject to removal at any time. */ type DNSResolverFunc func() *net.Resolver -/* NewDNSResolver - This is a temporary API and is subject to removal at any time. +/* +NewDNSResolver + + This is a temporary API and is subject to removal at any time. */ var NewDNSResolver DNSResolverFunc = func() *net.Resolver { return &net.Resolver{ diff --git a/transport/internet/websocket/ws.go b/transport/internet/websocket/ws.go index 19d994403..f64649ea0 100644 --- a/transport/internet/websocket/ws.go +++ b/transport/internet/websocket/ws.go @@ -1,4 +1,5 @@ -/*Package websocket implements Websocket transport +/* +Package websocket implements Websocket transport Websocket transport implements an HTTP(S) compliable, surveillance proof transport method with plausible deniability. */