Remove ToString0[T] usage to fix golangci-lint

This commit is contained in:
世界 2022-07-10 22:00:28 +08:00
parent 0ef2e330e3
commit 6048b1e270
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
24 changed files with 59 additions and 99 deletions

View file

@ -1,54 +1,17 @@
run:
timeout: 5m
linters:
enable-all: true
disable:
- errcheck
- wrapcheck
- varnamelen
- stylecheck
- nonamedreturns
- nlreturn
- ireturn
- gomnd
- exhaustivestruct
- ifshort
- goerr113
- gochecknoglobals
- forcetypeassert
- exhaustruct
- exhaustive
- cyclop
- containedctx
- wsl
- nestif
- lll
- funlen
- goconst
- godot
- gocognit
- golint
- goimports
- gochecknoinits
- maligned
- tagliatelle
- gocyclo
- maintidx
- gocritic
- nakedret
disable-all: true
enable:
- gofumpt
- govet
- gci
- staticcheck
- paralleltest
linters-settings:
revive:
rules:
- name: var-naming
disabled: true
govet:
enable-all: true
disable:
- composites
- fieldalignment
- shadow
gosec:
excludes:
- G404
gci:
sections:
- standard
- prefix(github.com/sagernet/)
- default
staticcheck:
go: '1.18'

View file

@ -45,7 +45,7 @@ func formatConfiguration(cmd *cobra.Command, args []string) {
os.Stdout.WriteString(buffer.String() + "\n")
return
}
if bytes.Compare(configContent, buffer.Bytes()) == 0 {
if !bytes.Equal(configContent, buffer.Bytes()) {
return
}
output, err := os.Create(configPath)

View file

@ -33,6 +33,7 @@ func ListenSerial(ctx context.Context, dialer N.Dialer, destination M.Socksaddr,
conn, err = dialer.ListenPacket(ctx, M.SocksaddrFromAddrPort(address, destination.Port))
if err != nil {
connErrors = append(connErrors, err)
continue
}
return conn, nil
}

View file

@ -9,6 +9,7 @@ import (
)
func TestMatch(t *testing.T) {
t.Parallel()
r := require.New(t)
matcher := domain.NewMatcher([]string{"domain.com"}, []string{"suffix.com", ".suffix.org"})
r.True(matcher.Match("domain.com"))

View file

@ -20,7 +20,7 @@ func Write(writer io.Writer, domains map[string][]Item) error {
for _, code := range keys {
index[code] = content.Len()
for _, domain := range domains[code] {
err := rw.WriteByte(content, byte(domain.Type))
err := rw.WriteByte(content, domain.Type)
if err != nil {
return err
}

View file

@ -36,6 +36,7 @@ func (m *monitor) Start() error {
if err != nil {
return err
}
go m.loopUpdate()
return nil
}
@ -72,9 +73,16 @@ func (m *monitor) checkUpdate() error {
continue
}
oldInterface := m.defaultInterfaceName
oldIndex := m.defaultInterfaceIndex
m.defaultInterfaceName = link.Attrs().Name
m.defaultInterfaceIndex = link.Attrs().Index
if oldInterface == m.defaultInterfaceName && oldIndex == m.defaultInterfaceIndex {
return nil
}
m.logger.Info("updated default interface ", m.defaultInterfaceName, ", index ", m.defaultInterfaceIndex)
return nil
}

View file

@ -11,6 +11,7 @@ import (
)
func TestSniffQUICv1(t *testing.T) {
t.Parallel()
pkt, err := hex.DecodeString("cc0000000108d2dc7bad02241f5003796e71004215a71bfcb05159416c724be418537389acdd9a4047306283dcb4d7a9cad5cc06322042d204da67a8dbaa328ab476bb428b48fd001501863afd203f8d4ef085629d664f1a734a65969a47e4a63d4e01a21f18c1d90db0c027180906dc135f9ae421bb8617314c8d54c175fef3d3383d310d0916ebcbd6eed9329befbbb109d8fd4af1d2cf9d6adce8e6c1260a7f8256e273e326da0aa7cc148d76e7a08489dc9d52ade89c027cbc3491ada46417c2c04e2ca768e9a7dd6aa00c594e48b678927325da796817693499bb727050cb3baf3d3291a397c3a8d868e8ec7b8f7295e347455c9dadbe2252ae917ac793d958c7fb8a3d2cdb34e3891eb4286f18617556ff7216dd60256aa5b1d11ff4753459fc5f9dedf11d483a26a0835dc6cd50e1c1f54f86e8f1e502821183cd874f6447a74e818bf3445c7795acf4559d1c1fac474911d2ead5c8d23e4aa4f67afb66efe305a30a0b5d825679b31ddc186cbea936535795c7e8c378c87b8c5adc065154d15bae8f85ac8fec2da40c3aa623b682a065440831555011d7647cde44446a0fb4cf5892f2c088ae1920643094be72e3c499fe8d265caf939e8ab607a5b9317917d2a32a812e8a0e6a2f84721bbb5984ffd242838f705d13f4cfb249bc6a5c80d58ac2595edf56648ec3fe21d787573c253a79805252d6d81e26d367d4ff29ef66b5fe8992086af7bada8cad10b82a7c0dc406c5b6d0c5ec3c583e767f759ce08cad6c3c8f91e5a8")
require.NoError(t, err)
metadata, err := sniff.QUICClientHello(context.Background(), pkt)

View file

@ -17,7 +17,7 @@ func PeekStream(ctx context.Context, reader io.Reader, sniffers ...StreamSniffer
for _, sniffer := range sniffers {
sniffMetadata, err := sniffer(ctx, reader)
if err != nil {
return nil, err
continue
}
return sniffMetadata, nil
}
@ -28,7 +28,7 @@ func PeekPacket(ctx context.Context, packet []byte, sniffers ...PacketSniffer) (
for _, sniffer := range sniffers {
sniffMetadata, err := sniffer(ctx, packet)
if err != nil {
return nil, err
continue
}
return sniffMetadata, nil
}

View file

@ -13,10 +13,10 @@ import (
N "github.com/sagernet/sing/common/network"
"github.com/stretchr/testify/require"
"golang.org/x/net/dns/dnsmessage"
)
func TestClient(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
client := dns.NewClient(option.DNSClientOptions{})
dnsTransport := dns.NewTCPTransport(context.Background(), N.SystemDialer, log.NewNopLogger(), M.ParseSocksaddr("1.0.0.1:53"))
@ -31,15 +31,3 @@ func TestClient(t *testing.T) {
require.NotEmpty(t, addresses, "no answers")
cancel()
}
func makeQuery() *dnsmessage.Message {
message := &dnsmessage.Message{}
message.Header.ID = 1
message.Header.RecursionDesired = true
message.Questions = append(message.Questions, dnsmessage.Question{
Name: dnsmessage.MustNewName("google.com."),
Type: dnsmessage.TypeA,
Class: dnsmessage.ClassINET,
})
return message
}

View file

@ -63,11 +63,10 @@ func (t *HTTPSTransport) Exchange(ctx context.Context, message *dnsmessage.Messa
return nil, err
}
buffer.Truncate(len(rawMessage))
request, err := http.NewRequest(http.MethodPost, t.destination, bytes.NewReader(buffer.Bytes()))
request, err := http.NewRequestWithContext(ctx, http.MethodPost, t.destination, bytes.NewReader(buffer.Bytes()))
if err != nil {
return nil, err
}
request.WithContext(ctx)
request.Header.Set("content-type", dnsMimeType)
request.Header.Set("accept", dnsMimeType)

View file

@ -1,4 +1,4 @@
package dns
package dns_test
import (
"context"
@ -6,6 +6,7 @@ import (
"time"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/dns"
"github.com/sagernet/sing-box/log"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
@ -15,8 +16,9 @@ import (
)
func TestTCPDNS(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
transport := NewTCPTransport(ctx, N.SystemDialer, log.NewNopLogger(), M.ParseSocksaddr("1.0.0.1:53"))
transport := dns.NewTCPTransport(ctx, N.SystemDialer, log.NewNopLogger(), M.ParseSocksaddr("1.0.0.1:53"))
response, err := transport.Exchange(ctx, makeQuery())
cancel()
require.NoError(t, err)
@ -27,8 +29,9 @@ func TestTCPDNS(t *testing.T) {
}
func TestTLSDNS(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
transport := NewTLSTransport(ctx, N.SystemDialer, log.NewNopLogger(), M.ParseSocksaddr("1.0.0.1:853"))
transport := dns.NewTLSTransport(ctx, N.SystemDialer, log.NewNopLogger(), M.ParseSocksaddr("1.0.0.1:853"))
response, err := transport.Exchange(ctx, makeQuery())
cancel()
require.NoError(t, err)
@ -39,8 +42,9 @@ func TestTLSDNS(t *testing.T) {
}
func TestHTTPSDNS(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
transport := NewHTTPSTransport(N.SystemDialer, "https://1.0.0.1:443/dns-query")
transport := dns.NewHTTPSTransport(N.SystemDialer, "https://1.0.0.1:443/dns-query")
response, err := transport.Exchange(ctx, makeQuery())
cancel()
require.NoError(t, err)
@ -51,8 +55,9 @@ func TestHTTPSDNS(t *testing.T) {
}
func TestUDPDNS(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
transport := NewUDPTransport(ctx, N.SystemDialer, log.NewNopLogger(), M.ParseSocksaddr("1.0.0.1:53"))
transport := dns.NewUDPTransport(ctx, N.SystemDialer, log.NewNopLogger(), M.ParseSocksaddr("1.0.0.1:53"))
response, err := transport.Exchange(ctx, makeQuery())
cancel()
require.NoError(t, err)
@ -63,8 +68,9 @@ func TestUDPDNS(t *testing.T) {
}
func TestLocalDNS(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
transport := NewLocalTransport()
transport := dns.NewLocalTransport()
response, err := transport.Lookup(ctx, "google.com", C.DomainStrategyAsIS)
cancel()
require.NoError(t, err)

2
go.mod
View file

@ -7,7 +7,7 @@ require (
github.com/goccy/go-json v0.9.8
github.com/logrusorgru/aurora v2.0.3+incompatible
github.com/oschwald/maxminddb-golang v1.9.0
github.com/sagernet/sing v0.0.0-20220709090827-3e39af603559
github.com/sagernet/sing v0.0.0-20220710135805-84be1c5eb67a
github.com/sagernet/sing-shadowsocks v0.0.0-20220701084835-2208da1d8649
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.5.0

4
go.sum
View file

@ -25,8 +25,8 @@ github.com/oschwald/maxminddb-golang v1.9.0/go.mod h1:TK+s/Z2oZq0rSl4PSeAEoP0bgm
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sagernet/sing v0.0.0-20220709090827-3e39af603559 h1:o5/fAARzVV2PkEDzjOct1FRUdWVw7hPDFYY8drulH50=
github.com/sagernet/sing v0.0.0-20220709090827-3e39af603559/go.mod h1:3ZmoGNg/nNJTyHAZFNRSPaXpNIwpDvyIiAUd0KIWV5c=
github.com/sagernet/sing v0.0.0-20220710135805-84be1c5eb67a h1:1SquyxA41EGvKGBrhj/HQkj4zhteThBPRFvJby0k2HE=
github.com/sagernet/sing v0.0.0-20220710135805-84be1c5eb67a/go.mod h1:3ZmoGNg/nNJTyHAZFNRSPaXpNIwpDvyIiAUd0KIWV5c=
github.com/sagernet/sing-shadowsocks v0.0.0-20220701084835-2208da1d8649 h1:whNDUGOAX5GPZkSy4G3Gv9QyIgk5SXRyjkRuP7ohF8k=
github.com/sagernet/sing-shadowsocks v0.0.0-20220701084835-2208da1d8649/go.mod h1:MuyT+9fEPjvauAv0fSE0a6Q+l0Tv2ZrAafTkYfnxBFw=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=

View file

@ -101,7 +101,5 @@ func NewDNSPacketConnection(ctx context.Context, router adapter.Router, logger l
}
func formatDNSQuestion(question dnsmessage.Question) string {
domain := question.Name.String()
domain = domain[:len(domain)-1]
return string(question.Name.Data[:question.Name.Length-1]) + " " + question.Type.String()[4:] + " " + question.Class.String()[5:]
}

View file

@ -145,7 +145,7 @@ type ShadowsocksDestination struct {
type TunInboundOptions struct {
InterfaceName string `json:"interface_name,omitempty"`
MTU uint32 `json:"mtu,omitempty,omitempty"`
MTU uint32 `json:"mtu,omitempty"`
Inet4Address *ListenPrefix `json:"inet4_address,omitempty"`
Inet6Address *ListenPrefix `json:"inet6_address,omitempty"`
AutoRoute bool `json:"auto_route,omitempty"`

View file

@ -43,9 +43,6 @@ func (h *HTTP) DialContext(ctx context.Context, network string, destination M.So
}
func (h *HTTP) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
ctx, metadata := adapter.AppendContext(ctx)
metadata.Outbound = h.tag
metadata.Destination = destination
return nil, os.ErrInvalid
}

View file

@ -414,7 +414,7 @@ func (r *Router) RouteConnection(ctx context.Context, conn net.Conn, metadata ad
return err
}
metadata.DestinationAddresses = addresses
r.dnsLogger.WithContext(ctx).Info("resolved [", strings.Join(common.Map(metadata.DestinationAddresses, F.ToString0[netip.Addr]), " "), "]")
r.dnsLogger.WithContext(ctx).Info("resolved [", strings.Join(F.MapToString(metadata.DestinationAddresses), " "), "]")
}
detour := r.match(ctx, metadata, r.defaultOutboundForConnection)
if !common.Contains(detour.Network(), C.NetworkTCP) {
@ -456,7 +456,7 @@ func (r *Router) RoutePacketConnection(ctx context.Context, conn N.PacketConn, m
return err
}
metadata.DestinationAddresses = addresses
r.dnsLogger.WithContext(ctx).Info("resolved [", strings.Join(common.Map(metadata.DestinationAddresses, F.ToString0[netip.Addr]), " "), "]")
r.dnsLogger.WithContext(ctx).Info("resolved [", strings.Join(F.MapToString(metadata.DestinationAddresses), " "), "]")
}
detour := r.match(ctx, metadata, r.defaultOutboundForPacketConnection)
if !common.Contains(detour.Network(), C.NetworkUDP) {

View file

@ -222,7 +222,7 @@ func (r *DefaultRule) Outbound() string {
}
func (r *DefaultRule) String() string {
return strings.Join(common.Map(r.allItems, F.ToString0[RuleItem]), " ")
return strings.Join(F.MapToString(r.allItems), " ")
}
var _ adapter.Rule = (*LogicalRule)(nil)
@ -310,5 +310,5 @@ func (r *LogicalRule) String() string {
case C.LogicalTypeOr:
op = "||"
}
return "logical(" + strings.Join(common.Map(r.rules, F.ToString0[*DefaultRule]), " "+op+" ") + ")"
return "logical(" + strings.Join(F.MapToString(r.rules), " "+op+" ") + ")"
}

View file

@ -5,7 +5,6 @@ import (
"strings"
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions"
F "github.com/sagernet/sing/common/format"
)
@ -70,7 +69,7 @@ func (r *IPCIDRItem) String() string {
if pLen == 1 {
description += r.prefixes[0].String()
} else {
description += "[" + strings.Join(common.Map(r.prefixes, F.ToString0[netip.Prefix]), " ") + "]"
description += "[" + strings.Join(F.MapToString(r.prefixes), " ") + "]"
}
return description
}

View file

@ -183,7 +183,7 @@ func (r *DefaultDNSRule) Outbound() string {
}
func (r *DefaultDNSRule) String() string {
return strings.Join(common.Map(r.allItems, F.ToString0[RuleItem]), " ")
return strings.Join(F.MapToString(r.allItems), " ")
}
var _ adapter.Rule = (*LogicalRule)(nil)
@ -271,5 +271,5 @@ func (r *LogicalDNSRule) String() string {
case C.LogicalTypeOr:
op = "||"
}
return "logical(" + strings.Join(common.Map(r.rules, F.ToString0[*DefaultDNSRule]), " "+op+" ") + ")"
return "logical(" + strings.Join(F.MapToString(r.rules), " "+op+" ") + ")"
}

View file

@ -26,7 +26,7 @@ func NewGeositeItem(router adapter.Router, logger log.Logger, codes []string) *G
}
func (r *GeositeItem) Update() error {
var matchers []adapter.Rule
matchers := make([]adapter.Rule, 0, len(r.codes))
for _, code := range r.codes {
matcher, err := r.router.LoadGeosite(code)
if err != nil {

View file

@ -4,7 +4,6 @@ import (
"strings"
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing/common"
F "github.com/sagernet/sing/common/format"
)
@ -47,7 +46,7 @@ func (r *PortItem) String() string {
if pLen == 1 {
description += F.ToString(r.ports[0])
} else {
description += "[" + strings.Join(common.Map(r.ports, F.ToString0[uint16]), " ") + "]"
description += "[" + strings.Join(F.MapToString(r.ports), " ") + "]"
}
return description
}

View file

@ -5,7 +5,7 @@ go 1.18
require (
github.com/docker/docker v20.10.17+incompatible
github.com/docker/go-connections v0.4.0
github.com/sagernet/sing v0.0.0-20220709090827-3e39af603559
github.com/sagernet/sing v0.0.0-20220710135805-84be1c5eb67a
github.com/sagernet/sing-box v0.0.0
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.8.0

View file

@ -52,8 +52,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sagernet/sing v0.0.0-20220709090827-3e39af603559 h1:o5/fAARzVV2PkEDzjOct1FRUdWVw7hPDFYY8drulH50=
github.com/sagernet/sing v0.0.0-20220709090827-3e39af603559/go.mod h1:3ZmoGNg/nNJTyHAZFNRSPaXpNIwpDvyIiAUd0KIWV5c=
github.com/sagernet/sing v0.0.0-20220710135805-84be1c5eb67a h1:1SquyxA41EGvKGBrhj/HQkj4zhteThBPRFvJby0k2HE=
github.com/sagernet/sing v0.0.0-20220710135805-84be1c5eb67a/go.mod h1:3ZmoGNg/nNJTyHAZFNRSPaXpNIwpDvyIiAUd0KIWV5c=
github.com/sagernet/sing-shadowsocks v0.0.0-20220701084835-2208da1d8649 h1:whNDUGOAX5GPZkSy4G3Gv9QyIgk5SXRyjkRuP7ohF8k=
github.com/sagernet/sing-shadowsocks v0.0.0-20220701084835-2208da1d8649/go.mod h1:MuyT+9fEPjvauAv0fSE0a6Q+l0Tv2ZrAafTkYfnxBFw=
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=