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
This commit is contained in:
A1lo 2024-03-03 23:52:22 +08:00 committed by GitHub
parent 173b03448f
commit a994bf8b04
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 23 additions and 27 deletions

View File

@ -181,9 +181,7 @@ func (s *DNS) LookupIP(domain string, option dns.IPOption) ([]net.IP, error) {
} }
// Normalize the FQDN form query // Normalize the FQDN form query
if strings.HasSuffix(domain, ".") { domain = strings.TrimSuffix(domain, ".")
domain = domain[:len(domain)-1]
}
// Static host lookup // Static host lookup
switch addrs := s.hosts.Lookup(domain, option); { switch addrs := s.hosts.Lookup(domain, option); {

View File

@ -16,6 +16,7 @@ import (
"github.com/xtls/xray-core/features/routing" "github.com/xtls/xray-core/features/routing"
"github.com/xtls/xray-core/testing/mocks" "github.com/xtls/xray-core/testing/mocks"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/test/bufconn" "google.golang.org/grpc/test/bufconn"
) )
@ -80,7 +81,7 @@ func TestServiceSubscribeRoutingStats(t *testing.T) {
// Client goroutine // Client goroutine
go func() { go func() {
defer lis.Close() 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 { if err != nil {
errCh <- err errCh <- err
return return
@ -214,7 +215,7 @@ func TestServiceSubscribeSubsetOfFields(t *testing.T) {
// Client goroutine // Client goroutine
go func() { go func() {
defer lis.Close() 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 { if err != nil {
errCh <- err errCh <- err
return return
@ -337,7 +338,7 @@ func TestSerivceTestRoute(t *testing.T) {
// Client goroutine // Client goroutine
go func() { go func() {
defer lis.Close() 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 { if err != nil {
errCh <- err errCh <- err
} }

View File

@ -4,7 +4,6 @@ package dice // import "github.com/xtls/xray-core/common/dice"
import ( import (
"math/rand" "math/rand"
"time"
) )
// Roll returns a non-negative number between 0 (inclusive) and n (exclusive). // 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) return dd.Intn(n)
} }
func init() {
rand.Seed(time.Now().Unix())
}

View File

@ -2,7 +2,6 @@ package drain
import ( import (
"io" "io"
"io/ioutil"
"github.com/xtls/xray-core/common/dice" "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 { 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 return err
} }

View File

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"google.golang.org/protobuf/encoding/protojson"
"io" "io"
"net/http" "net/http"
"net/url" "net/url"
@ -13,6 +12,9 @@ import (
"strings" "strings"
"time" "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/common/buf"
"github.com/xtls/xray-core/main/commands/base" "github.com/xtls/xray-core/main/commands/base"
"google.golang.org/grpc" "google.golang.org/grpc"
@ -37,7 +39,7 @@ func setSharedFlags(cmd *base.Command) {
func dialAPIServer() (conn *grpc.ClientConn, ctx context.Context, close func()) { func dialAPIServer() (conn *grpc.ClientConn, ctx context.Context, close func()) {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(apiTimeout)*time.Second) 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 { if err != nil {
base.Fatalf("failed to dial %s", apiServerAddrPtr) base.Fatalf("failed to dial %s", apiServerAddrPtr)
} }

View File

@ -3,7 +3,7 @@ package tls
import ( import (
"flag" "flag"
"fmt" "fmt"
"io/ioutil" "os"
"github.com/xtls/xray-core/main/commands/base" "github.com/xtls/xray-core/main/commands/base"
"github.com/xtls/xray-core/transport/internet/tls" "github.com/xtls/xray-core/transport/internet/tls"
@ -30,12 +30,11 @@ func executeCertChainHash(cmd *base.Command, args []string) {
fmt.Println(err) fmt.Println(err)
return return
} }
certContent, err := ioutil.ReadFile(*input) certContent, err := os.ReadFile(*input)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return
} }
certChainHashB64 := tls.CalculatePEMCertChainSHA256Hash(certContent) certChainHashB64 := tls.CalculatePEMCertChainSHA256Hash(certContent)
fmt.Println(certChainHashB64) fmt.Println(certChainHashB64)
return
} }

View File

@ -101,7 +101,7 @@ func (s *Server) processTCP(ctx context.Context, conn stat.Connection, dispatche
reader := &buf.BufferedReader{Reader: buf.NewReader(conn)} reader := &buf.BufferedReader{Reader: buf.NewReader(conn)}
request, err := svrSession.Handshake(reader, conn) request, err := svrSession.Handshake(reader, conn)
if err != nil { if err != nil {
if inbound != nil && inbound.Source.IsValid() { if inbound.Source.IsValid() {
log.Record(&log.AccessMessage{ log.Record(&log.AccessMessage{
From: inbound.Source, From: inbound.Source,
To: "", To: "",
@ -122,7 +122,7 @@ func (s *Server) processTCP(ctx context.Context, conn stat.Connection, dispatche
if request.Command == protocol.RequestCommandTCP { if request.Command == protocol.RequestCommandTCP {
dest := request.Destination() dest := request.Destination()
newError("TCP Connect request to ", dest).WriteToLog(session.ExportIDToError(ctx)) 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{ ctx = log.ContextWithAccessMessage(ctx, &log.AccessMessage{
From: inbound.Source, From: inbound.Source,
To: dest, To: dest,

View File

@ -30,6 +30,7 @@ import (
"github.com/xtls/xray-core/proxy/vmess/outbound" "github.com/xtls/xray-core/proxy/vmess/outbound"
"github.com/xtls/xray-core/testing/servers/tcp" "github.com/xtls/xray-core/testing/servers/tcp"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
) )
func TestCommanderRemoveHandler(t *testing.T) { func TestCommanderRemoveHandler(t *testing.T) {
@ -103,7 +104,7 @@ func TestCommanderRemoveHandler(t *testing.T) {
t.Fatal(err) 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) common.Must(err)
defer cmdConn.Close() defer cmdConn.Close()
@ -270,7 +271,7 @@ func TestCommanderAddRemoveUser(t *testing.T) {
t.Fatal("expected error: ", err) 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) common.Must(err)
defer cmdConn.Close() defer cmdConn.Close()
@ -451,7 +452,7 @@ func TestCommanderStats(t *testing.T) {
t.Fatal(err) 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) common.Must(err)
defer cmdConn.Close() defer cmdConn.Close()

View File

@ -3,7 +3,7 @@ package scenarios
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"testing" "testing"
@ -80,7 +80,7 @@ func TestMetrics(t *testing.T) {
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
t.Error("unexpected pprof status code") t.Error("unexpected pprof status code")
} }
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -96,7 +96,7 @@ func TestMetrics(t *testing.T) {
if resp2.StatusCode != http.StatusOK { if resp2.StatusCode != http.StatusOK {
t.Error("unexpected expvars status code") t.Error("unexpected expvars status code")
} }
body2, err2 := ioutil.ReadAll(resp2.Body) body2, err2 := io.ReadAll(resp2.Body)
if err2 != nil { if err2 != nil {
t.Fatal(err2) t.Fatal(err2)
} }

View File

@ -17,6 +17,7 @@ import (
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/backoff" "google.golang.org/grpc/backoff"
"google.golang.org/grpc/connectivity" "google.golang.org/grpc/connectivity"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/keepalive" "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 := "" authority := ""
if grpcSettings.Authority != "" { if grpcSettings.Authority != "" {