diff --git a/.golangci.yml b/.golangci.yml index 948fcc4d..049c3704 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -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 \ No newline at end of file + gci: + sections: + - standard + - prefix(github.com/sagernet/) + - default + staticcheck: + go: '1.18' diff --git a/cmd/sing-box/cmd_format.go b/cmd/sing-box/cmd_format.go index d5cbbcf9..c0408656 100644 --- a/cmd/sing-box/cmd_format.go +++ b/cmd/sing-box/cmd_format.go @@ -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) diff --git a/common/dialer/serial.go b/common/dialer/serial.go index 023443fe..6fdaa389 100644 --- a/common/dialer/serial.go +++ b/common/dialer/serial.go @@ -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 } diff --git a/common/domain/matcher_test.go b/common/domain/matcher_test.go index 4ba31e4b..93e4bde3 100644 --- a/common/domain/matcher_test.go +++ b/common/domain/matcher_test.go @@ -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")) diff --git a/common/geosite/writer.go b/common/geosite/writer.go index 4d41681b..e055055c 100644 --- a/common/geosite/writer.go +++ b/common/geosite/writer.go @@ -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 } diff --git a/common/iffmonitor/monitor_linux.go b/common/iffmonitor/monitor_linux.go index 41a0ad5e..19b196a2 100644 --- a/common/iffmonitor/monitor_linux.go +++ b/common/iffmonitor/monitor_linux.go @@ -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 } diff --git a/common/sniff/quic_test.go b/common/sniff/quic_test.go index d47e0416..028315fa 100644 --- a/common/sniff/quic_test.go +++ b/common/sniff/quic_test.go @@ -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) diff --git a/common/sniff/sniff.go b/common/sniff/sniff.go index 12055bed..b3a91aac 100644 --- a/common/sniff/sniff.go +++ b/common/sniff/sniff.go @@ -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 } diff --git a/dns/client_test.go b/dns/client_test.go index 4cd37f6f..074971bf 100644 --- a/dns/client_test.go +++ b/dns/client_test.go @@ -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 -} diff --git a/dns/transport_https.go b/dns/transport_https.go index 07a4fd20..45c3b874 100644 --- a/dns/transport_https.go +++ b/dns/transport_https.go @@ -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) diff --git a/dns/transport_test.go b/dns/transport_test.go index cce193de..3c4eafc1 100644 --- a/dns/transport_test.go +++ b/dns/transport_test.go @@ -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) diff --git a/go.mod b/go.mod index 26db5b86..cebd30ba 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 4057396e..6c7ce94f 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/inbound/dns.go b/inbound/dns.go index ea3078f1..899bfcd7 100644 --- a/inbound/dns.go +++ b/inbound/dns.go @@ -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:] } diff --git a/option/inbound.go b/option/inbound.go index 2e8c190b..0f685940 100644 --- a/option/inbound.go +++ b/option/inbound.go @@ -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"` diff --git a/outbound/http.go b/outbound/http.go index 74313581..5e59eab0 100644 --- a/outbound/http.go +++ b/outbound/http.go @@ -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 } diff --git a/route/router.go b/route/router.go index 130a4f57..22cab96d 100644 --- a/route/router.go +++ b/route/router.go @@ -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) { diff --git a/route/rule.go b/route/rule.go index d2656017..7fc88ead 100644 --- a/route/rule.go +++ b/route/rule.go @@ -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+" ") + ")" } diff --git a/route/rule_cidr.go b/route/rule_cidr.go index 571a4124..95bf0f64 100644 --- a/route/rule_cidr.go +++ b/route/rule_cidr.go @@ -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 } diff --git a/route/rule_dns.go b/route/rule_dns.go index c7f30ea7..6d20fe2a 100644 --- a/route/rule_dns.go +++ b/route/rule_dns.go @@ -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+" ") + ")" } diff --git a/route/rule_geosite.go b/route/rule_geosite.go index 7fc5d7d6..547ec1c9 100644 --- a/route/rule_geosite.go +++ b/route/rule_geosite.go @@ -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 { diff --git a/route/rule_port.go b/route/rule_port.go index 839325bb..62478933 100644 --- a/route/rule_port.go +++ b/route/rule_port.go @@ -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 } diff --git a/test/go.mod b/test/go.mod index ee9ece63..25dd809b 100644 --- a/test/go.mod +++ b/test/go.mod @@ -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 diff --git a/test/go.sum b/test/go.sum index a1c73fcb..5d2e8e88 100644 --- a/test/go.sum +++ b/test/go.sum @@ -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=