From a994bf8b04c842cb9e2352cd969061ada7501854 Mon Sep 17 00:00:00 2001 From: A1lo Date: Sun, 3 Mar 2024 23:52:22 +0800 Subject: [PATCH] chore: fix some errors detected by staticcheck (#3089) * chore: fix some errors detected by staticcheck * feat: remove `rand.Seed()` usage for possibly using "fastrand64" runtime to avoid locking ref: https://pkg.go.dev/math/rand#Seed --- app/dns/dns.go | 4 +--- app/router/command/command_test.go | 7 ++++--- common/dice/dice.go | 5 ----- common/drain/drainer.go | 3 +-- main/commands/all/api/shared.go | 6 ++++-- main/commands/all/tls/certchainhash.go | 5 ++--- proxy/socks/server.go | 4 ++-- testing/scenarios/command_test.go | 7 ++++--- testing/scenarios/metrics_test.go | 6 +++--- transport/internet/grpc/dial.go | 3 ++- 10 files changed, 23 insertions(+), 27 deletions(-) diff --git a/app/dns/dns.go b/app/dns/dns.go index 3b173677..fbdf74ee 100644 --- a/app/dns/dns.go +++ b/app/dns/dns.go @@ -181,9 +181,7 @@ func (s *DNS) LookupIP(domain string, option dns.IPOption) ([]net.IP, error) { } // Normalize the FQDN form query - if strings.HasSuffix(domain, ".") { - domain = domain[:len(domain)-1] - } + domain = strings.TrimSuffix(domain, ".") // Static host lookup switch addrs := s.hosts.Lookup(domain, option); { diff --git a/app/router/command/command_test.go b/app/router/command/command_test.go index fa8d48b3..096e90ee 100644 --- a/app/router/command/command_test.go +++ b/app/router/command/command_test.go @@ -16,6 +16,7 @@ import ( "github.com/xtls/xray-core/features/routing" "github.com/xtls/xray-core/testing/mocks" "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" "google.golang.org/grpc/test/bufconn" ) @@ -80,7 +81,7 @@ func TestServiceSubscribeRoutingStats(t *testing.T) { // Client goroutine go func() { defer lis.Close() - conn, err := grpc.DialContext(context.Background(), "bufnet", grpc.WithContextDialer(bufDialer), grpc.WithInsecure()) + conn, err := grpc.DialContext(context.Background(), "bufnet", grpc.WithContextDialer(bufDialer), grpc.WithTransportCredentials(insecure.NewCredentials())) if err != nil { errCh <- err return @@ -214,7 +215,7 @@ func TestServiceSubscribeSubsetOfFields(t *testing.T) { // Client goroutine go func() { defer lis.Close() - conn, err := grpc.DialContext(context.Background(), "bufnet", grpc.WithContextDialer(bufDialer), grpc.WithInsecure()) + conn, err := grpc.DialContext(context.Background(), "bufnet", grpc.WithContextDialer(bufDialer), grpc.WithTransportCredentials(insecure.NewCredentials())) if err != nil { errCh <- err return @@ -337,7 +338,7 @@ func TestSerivceTestRoute(t *testing.T) { // Client goroutine go func() { defer lis.Close() - conn, err := grpc.DialContext(context.Background(), "bufnet", grpc.WithContextDialer(bufDialer), grpc.WithInsecure()) + conn, err := grpc.DialContext(context.Background(), "bufnet", grpc.WithContextDialer(bufDialer), grpc.WithTransportCredentials(insecure.NewCredentials())) if err != nil { errCh <- err } diff --git a/common/dice/dice.go b/common/dice/dice.go index 896c1e8e..2ff925b8 100644 --- a/common/dice/dice.go +++ b/common/dice/dice.go @@ -4,7 +4,6 @@ package dice // import "github.com/xtls/xray-core/common/dice" import ( "math/rand" - "time" ) // Roll returns a non-negative number between 0 (inclusive) and n (exclusive). @@ -46,7 +45,3 @@ func (dd *DeterministicDice) Roll(n int) int { } return dd.Intn(n) } - -func init() { - rand.Seed(time.Now().Unix()) -} diff --git a/common/drain/drainer.go b/common/drain/drainer.go index bdb12d4f..bb1ae68a 100644 --- a/common/drain/drainer.go +++ b/common/drain/drainer.go @@ -2,7 +2,6 @@ package drain import ( "io" - "io/ioutil" "github.com/xtls/xray-core/common/dice" ) @@ -36,7 +35,7 @@ func (d *BehaviorSeedLimitedDrainer) Drain(reader io.Reader) error { } func drainReadN(reader io.Reader, n int) error { - _, err := io.CopyN(ioutil.Discard, reader, int64(n)) + _, err := io.CopyN(io.Discard, reader, int64(n)) return err } diff --git a/main/commands/all/api/shared.go b/main/commands/all/api/shared.go index 4d98359f..751b8615 100644 --- a/main/commands/all/api/shared.go +++ b/main/commands/all/api/shared.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "fmt" - "google.golang.org/protobuf/encoding/protojson" "io" "net/http" "net/url" @@ -13,6 +12,9 @@ import ( "strings" "time" + "google.golang.org/grpc/credentials/insecure" + "google.golang.org/protobuf/encoding/protojson" + "github.com/xtls/xray-core/common/buf" "github.com/xtls/xray-core/main/commands/base" "google.golang.org/grpc" @@ -37,7 +39,7 @@ func setSharedFlags(cmd *base.Command) { func dialAPIServer() (conn *grpc.ClientConn, ctx context.Context, close func()) { ctx, cancel := context.WithTimeout(context.Background(), time.Duration(apiTimeout)*time.Second) - conn, err := grpc.DialContext(ctx, apiServerAddrPtr, grpc.WithInsecure(), grpc.WithBlock()) + conn, err := grpc.DialContext(ctx, apiServerAddrPtr, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock()) if err != nil { base.Fatalf("failed to dial %s", apiServerAddrPtr) } diff --git a/main/commands/all/tls/certchainhash.go b/main/commands/all/tls/certchainhash.go index 3fbdb4c4..304f668d 100644 --- a/main/commands/all/tls/certchainhash.go +++ b/main/commands/all/tls/certchainhash.go @@ -3,7 +3,7 @@ package tls import ( "flag" "fmt" - "io/ioutil" + "os" "github.com/xtls/xray-core/main/commands/base" "github.com/xtls/xray-core/transport/internet/tls" @@ -30,12 +30,11 @@ func executeCertChainHash(cmd *base.Command, args []string) { fmt.Println(err) return } - certContent, err := ioutil.ReadFile(*input) + certContent, err := os.ReadFile(*input) if err != nil { fmt.Println(err) return } certChainHashB64 := tls.CalculatePEMCertChainSHA256Hash(certContent) fmt.Println(certChainHashB64) - return } diff --git a/proxy/socks/server.go b/proxy/socks/server.go index 6964fdf2..2f789757 100644 --- a/proxy/socks/server.go +++ b/proxy/socks/server.go @@ -101,7 +101,7 @@ func (s *Server) processTCP(ctx context.Context, conn stat.Connection, dispatche reader := &buf.BufferedReader{Reader: buf.NewReader(conn)} request, err := svrSession.Handshake(reader, conn) if err != nil { - if inbound != nil && inbound.Source.IsValid() { + if inbound.Source.IsValid() { log.Record(&log.AccessMessage{ From: inbound.Source, To: "", @@ -122,7 +122,7 @@ func (s *Server) processTCP(ctx context.Context, conn stat.Connection, dispatche if request.Command == protocol.RequestCommandTCP { dest := request.Destination() newError("TCP Connect request to ", dest).WriteToLog(session.ExportIDToError(ctx)) - if inbound != nil && inbound.Source.IsValid() { + if inbound.Source.IsValid() { ctx = log.ContextWithAccessMessage(ctx, &log.AccessMessage{ From: inbound.Source, To: dest, diff --git a/testing/scenarios/command_test.go b/testing/scenarios/command_test.go index d82e6a37..44513b77 100644 --- a/testing/scenarios/command_test.go +++ b/testing/scenarios/command_test.go @@ -30,6 +30,7 @@ import ( "github.com/xtls/xray-core/proxy/vmess/outbound" "github.com/xtls/xray-core/testing/servers/tcp" "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" ) func TestCommanderRemoveHandler(t *testing.T) { @@ -103,7 +104,7 @@ func TestCommanderRemoveHandler(t *testing.T) { t.Fatal(err) } - cmdConn, err := grpc.Dial(fmt.Sprintf("127.0.0.1:%d", cmdPort), grpc.WithInsecure(), grpc.WithBlock()) + cmdConn, err := grpc.Dial(fmt.Sprintf("127.0.0.1:%d", cmdPort), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock()) common.Must(err) defer cmdConn.Close() @@ -270,7 +271,7 @@ func TestCommanderAddRemoveUser(t *testing.T) { t.Fatal("expected error: ", err) } - cmdConn, err := grpc.Dial(fmt.Sprintf("127.0.0.1:%d", cmdPort), grpc.WithInsecure(), grpc.WithBlock()) + cmdConn, err := grpc.Dial(fmt.Sprintf("127.0.0.1:%d", cmdPort), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock()) common.Must(err) defer cmdConn.Close() @@ -451,7 +452,7 @@ func TestCommanderStats(t *testing.T) { t.Fatal(err) } - cmdConn, err := grpc.Dial(fmt.Sprintf("127.0.0.1:%d", cmdPort), grpc.WithInsecure(), grpc.WithBlock()) + cmdConn, err := grpc.Dial(fmt.Sprintf("127.0.0.1:%d", cmdPort), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock()) common.Must(err) defer cmdConn.Close() diff --git a/testing/scenarios/metrics_test.go b/testing/scenarios/metrics_test.go index 914e46c4..eee2d942 100644 --- a/testing/scenarios/metrics_test.go +++ b/testing/scenarios/metrics_test.go @@ -3,7 +3,7 @@ package scenarios import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "testing" @@ -80,7 +80,7 @@ func TestMetrics(t *testing.T) { if resp.StatusCode != http.StatusOK { t.Error("unexpected pprof status code") } - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { t.Fatal(err) } @@ -96,7 +96,7 @@ func TestMetrics(t *testing.T) { if resp2.StatusCode != http.StatusOK { t.Error("unexpected expvars status code") } - body2, err2 := ioutil.ReadAll(resp2.Body) + body2, err2 := io.ReadAll(resp2.Body) if err2 != nil { t.Fatal(err2) } diff --git a/transport/internet/grpc/dial.go b/transport/internet/grpc/dial.go index 2355b231..5d5789b4 100644 --- a/transport/internet/grpc/dial.go +++ b/transport/internet/grpc/dial.go @@ -17,6 +17,7 @@ import ( "google.golang.org/grpc" "google.golang.org/grpc/backoff" "google.golang.org/grpc/connectivity" + "google.golang.org/grpc/credentials/insecure" "google.golang.org/grpc/keepalive" ) @@ -141,7 +142,7 @@ func getGrpcClient(ctx context.Context, dest net.Destination, streamSettings *in }), } - dialOptions = append(dialOptions, grpc.WithInsecure()) + dialOptions = append(dialOptions, grpc.WithTransportCredentials(insecure.NewCredentials())) authority := "" if grpcSettings.Authority != "" {