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
if strings.HasSuffix(domain, ".") {
domain = domain[:len(domain)-1]
}
domain = strings.TrimSuffix(domain, ".")
// Static host lookup
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/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
}

View file

@ -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())
}

View file

@ -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
}

View file

@ -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)
}

View file

@ -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
}

View file

@ -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,

View file

@ -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()

View file

@ -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)
}

View file

@ -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 != "" {