From 2a7bcdf6a94e2ca0dba0e9b2dd24c4e777ca4265 Mon Sep 17 00:00:00 2001 From: Hubert Grochowski Date: Wed, 7 Feb 2024 15:44:37 +0100 Subject: [PATCH 1/2] chore: bump go version --- .version | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.version b/.version index 7e7dbdc2..cc44278d 100644 --- a/.version +++ b/.version @@ -1,4 +1,4 @@ -GO_VERSION=1.21.6 +GO_VERSION=1.22.0 X_TOOLS_VERSION=v0.15.0 GOLANGCI_LINT_VERSION=v1.55.2 From 69fb8d01f00cb7adb8c43059ad6eae6b6d523d1e Mon Sep 17 00:00:00 2001 From: Hubert Grochowski Date: Wed, 7 Feb 2024 15:58:38 +0100 Subject: [PATCH 2/2] chore: bump golangci version Fix linting issues. --- .version | 2 +- bind/flag.go | 2 +- bind/redact.go | 2 +- config.go | 21 ++++++++++---------- credentials.go | 11 +++++----- e2e/setup/callback.go | 3 ++- http_proxy.go | 2 +- http_proxy_test.go | 3 +-- httplog/httplog.go | 2 +- internal/martian/context.go | 7 ++++--- internal/martian/h2/h2_test.go | 2 +- internal/martian/h2/testing/fixture.go | 2 +- internal/martian/header/framing_modifier.go | 3 ++- internal/martian/proxy_test.go | 4 ++-- internal/martian/proxyutil/proxyutil_test.go | 4 ++-- middleware/prometheus.go | 4 ++-- mitm.go | 4 ++-- pac/pac.go | 7 ++++--- pac/pac_ipv6.go | 4 ++-- pac/pac_test.go | 3 ++- pac/proxy.go | 3 ++- readurl.go | 3 ++- resolver.go | 4 ++-- utils/cobrautil/bind.go | 7 ++++--- utils/compose/network.go | 5 +++-- utils/compose/service.go | 8 ++++---- utils/osdns/configure.go | 3 ++- 27 files changed, 68 insertions(+), 57 deletions(-) diff --git a/.version b/.version index cc44278d..f63510eb 100644 --- a/.version +++ b/.version @@ -1,7 +1,7 @@ GO_VERSION=1.22.0 X_TOOLS_VERSION=v0.15.0 -GOLANGCI_LINT_VERSION=v1.55.2 +GOLANGCI_LINT_VERSION=v1.56.0 GORELEASER_VERSION=v1.22.1 GO_LICENSES_VERSION=v1.6.0 diff --git a/bind/flag.go b/bind/flag.go index 239d3550..4c6c40e5 100644 --- a/bind/flag.go +++ b/bind/flag.go @@ -289,7 +289,7 @@ func HTTPServerConfig(fs *pflag.FlagSet, cfg *forwarder.HTTPServerConfig, prefix func HTTPLogConfig(fs *pflag.FlagSet, cfg []NamedParam[httplog.Mode]) { for _, p := range cfg { if p.Param == nil { - panic(fmt.Sprintf("httplog mode is nil for %s", p.Name)) + panic("httplog mode is nil for " + p.Name) } } diff --git a/bind/redact.go b/bind/redact.go index a8d20cb1..b947077c 100644 --- a/bind/redact.go +++ b/bind/redact.go @@ -23,7 +23,7 @@ func RedactUserinfo(ui *url.Userinfo) string { return "" } if _, has := ui.Password(); has { - return fmt.Sprintf("%s:xxxxx", ui.Username()) + return ui.Username() + ":xxxxx" } return ui.Username() } diff --git a/config.go b/config.go index 56378cd8..351ad58d 100644 --- a/config.go +++ b/config.go @@ -7,6 +7,7 @@ package forwarder import ( + "errors" "fmt" "net" "net/netip" @@ -24,7 +25,7 @@ import ( // ParseUserinfo parses a user:password string into *url.Userinfo. func ParseUserinfo(val string) (*url.Userinfo, error) { if val == "" { - return nil, fmt.Errorf("expected username[:password]") + return nil, errors.New("expected username[:password]") } var ui *url.Userinfo @@ -46,7 +47,7 @@ func validatedUserInfo(ui *url.Userinfo) error { return nil } if ui.Username() == "" { - return fmt.Errorf("username cannot be empty") + return errors.New("username cannot be empty") } return nil @@ -63,15 +64,15 @@ func wildcardPortTo0(val string) string { // ParseHostPortUser parses a user:password@host:port string into HostUser. func ParseHostPortUser(val string) (*HostPortUser, error) { if val == "" { - return nil, fmt.Errorf("expected user[:password]@host:port") + return nil, errors.New("expected user[:password]@host:port") } if strings.Index(val, "@") != strings.LastIndex(val, "@") { - return nil, fmt.Errorf("only one '@' is allowed") + return nil, errors.New("only one '@' is allowed") } up, hp, ok := strings.Cut(val, "@") if !ok { - return nil, fmt.Errorf("expected user[:password]@host:port") + return nil, errors.New("expected user[:password]@host:port") } ui, err := ParseUserinfo(up) @@ -104,7 +105,7 @@ func ParseProxyURL(val string) (*url.URL, error) { } if strings.Index(hpu, "@") != strings.LastIndex(hpu, "@") { - return nil, fmt.Errorf("only one '@' is allowed") + return nil, errors.New("only one '@' is allowed") } var ( @@ -162,7 +163,7 @@ func validateProxyURL(u *url.URL) error { uu := *u uu.User = nil if uu.String() != c.String() { - return fmt.Errorf("unsupported URL elements, format: [://]:") + return errors.New("unsupported URL elements, format: [://]:") } } @@ -182,14 +183,14 @@ func validateProxyURL(u *url.URL) error { { if u.Port() == "" { - return fmt.Errorf("port is required") + return errors.New("port is required") } p, err := strconv.ParseUint(u.Port(), 10, 16) if err != nil { return fmt.Errorf("port: %w", err) } if p == 0 { - return fmt.Errorf("port cannot be 0") + return errors.New("port cannot be 0") } } @@ -233,7 +234,7 @@ func validateDNSAddress(p netip.AddrPort) error { return fmt.Errorf("IP: %s", p.Addr()) } if p.Port() == 0 { - return fmt.Errorf("port cannot be 0") + return errors.New("port cannot be 0") } return nil } diff --git a/credentials.go b/credentials.go index bf860b97..6b0a696e 100644 --- a/credentials.go +++ b/credentials.go @@ -7,6 +7,7 @@ package forwarder import ( + "errors" "fmt" "net" "net/url" @@ -22,13 +23,13 @@ type HostPortUser struct { func (hpu *HostPortUser) Validate() error { if hpu.Host == "" { - return fmt.Errorf("missing host") + return errors.New("missing host") } if hpu.Port == "" { - return fmt.Errorf("missing port") + return errors.New("missing port") } if hpu.Userinfo == nil { - return fmt.Errorf("missing user") + return errors.New("missing user") } return validatedUserInfo(hpu.Userinfo) } @@ -100,7 +101,7 @@ func NewCredentialsMatcher(credentials []*HostPortUser, log log.Logger) (*Creden switch { case hpu.Host == "*" && hpu.Port == "0": if m.global != nil { - return nil, withRowInfo(fmt.Errorf("duplicate global input")) + return nil, withRowInfo(errors.New("duplicate global input")) } m.global = hpu.Userinfo case hpu.Host == "*": @@ -116,7 +117,7 @@ func NewCredentialsMatcher(credentials []*HostPortUser, log log.Logger) (*Creden default: hostport := net.JoinHostPort(hpu.Host, hpu.Port) if _, ok := m.hostport[hostport]; ok { - return nil, fmt.Errorf("duplicate input") + return nil, errors.New("duplicate input") } m.hostport[hostport] = hpu.Userinfo } diff --git a/e2e/setup/callback.go b/e2e/setup/callback.go index 2b746725..3a52550a 100644 --- a/e2e/setup/callback.go +++ b/e2e/setup/callback.go @@ -8,6 +8,7 @@ package setup import ( "bytes" + "errors" "fmt" "os" "os/exec" @@ -40,7 +41,7 @@ func makeTestCallback(run string, debug bool) func() error { fmt.Fprintln(os.Stderr, "stderr:") stderr.WriteTo(os.Stderr) fmt.Fprintln(os.Stderr) - return fmt.Errorf("unexpected stderr") + return errors.New("unexpected stderr") } s := strings.Split(stdout.String(), "\n") diff --git a/http_proxy.go b/http_proxy.go index df589b14..b438ec8f 100644 --- a/http_proxy.go +++ b/http_proxy.go @@ -190,7 +190,7 @@ func newHTTPProxy(cfg *HTTPProxyConfig, pr PACResolver, cm *CredentialsMatcher, return nil, err } if cfg.UpstreamProxy != nil && pr != nil { - return nil, fmt.Errorf("cannot use both upstream proxy and PAC") + return nil, errors.New("cannot use both upstream proxy and PAC") } // If not set, use http.DefaultTransport. diff --git a/http_proxy_test.go b/http_proxy_test.go index e00483f5..d1805fa7 100644 --- a/http_proxy_test.go +++ b/http_proxy_test.go @@ -9,7 +9,6 @@ package forwarder import ( "context" "errors" - "fmt" "net" "net/http" "net/http/httptest" @@ -82,7 +81,7 @@ func TestAbortIf(t *testing.T) { } func TestNopDialer(t *testing.T) { - nopDialerErr := fmt.Errorf("nop dialer") + nopDialerErr := errors.New("nop dialer") tr := &http.Transport{ DialContext: func(_ context.Context, _, _ string) (net.Conn, error) { diff --git a/httplog/httplog.go b/httplog/httplog.go index e5237e6b..bf068e31 100644 --- a/httplog/httplog.go +++ b/httplog/httplog.go @@ -154,7 +154,7 @@ func (w *logWriter) ShortURLLine(e middleware.LogEntry) { if scheme != "" { scheme += "://" } - if len(path) > 0 && path[0] != '/' { + if path != "" && path[0] != '/' { path = "/" + path } diff --git a/internal/martian/context.go b/internal/martian/context.go index af9d687c..abcefcd7 100644 --- a/internal/martian/context.go +++ b/internal/martian/context.go @@ -17,6 +17,7 @@ package martian import ( "bufio" "context" + "errors" "fmt" "net" "net/http" @@ -116,7 +117,7 @@ func (s *Session) Hijack() (conn net.Conn, brw *bufio.ReadWriter, err error) { defer s.mu.Unlock() if s.hijacked { - return nil, nil, fmt.Errorf("session has already been hijacked") + return nil, nil, errors.New("session has already been hijacked") } defer func() { s.hijacked = err == nil @@ -138,7 +139,7 @@ func (s *Session) HijackResponseWriter() (http.ResponseWriter, error) { defer s.mu.Unlock() if s.hijacked { - return nil, fmt.Errorf("session has already been hijacked") + return nil, errors.New("session has already been hijacked") } if s.rw != nil { @@ -146,7 +147,7 @@ func (s *Session) HijackResponseWriter() (http.ResponseWriter, error) { return s.rw, nil } - return nil, fmt.Errorf("session has no response writer") + return nil, errors.New("session has no response writer") } // Hijacked returns whether the connection has been hijacked. diff --git a/internal/martian/h2/h2_test.go b/internal/martian/h2/h2_test.go index 97224fe1..ad4f4f55 100644 --- a/internal/martian/h2/h2_test.go +++ b/internal/martian/h2/h2_test.go @@ -197,7 +197,7 @@ func (p *plusOne) Message(data []byte, streamEnded bool) error { if err := proto.Unmarshal(data, msg); err != nil { return fmt.Errorf("unmarshalling request: %w", err) } - msg.Values = append(msg.Values, 1) //nolint:protogetter // synthetic data + msg.Values = append(msg.Values, 1) data, err := proto.Marshal(msg) if err != nil { diff --git a/internal/martian/h2/testing/fixture.go b/internal/martian/h2/testing/fixture.go index 24290d0c..ff1a54b5 100644 --- a/internal/martian/h2/testing/fixture.go +++ b/internal/martian/h2/testing/fixture.go @@ -98,7 +98,7 @@ func New(spf []h2.StreamProcessorFactory) (*Fixture, error) { } proxyTarget := hostname + ":" + strconv.Itoa(proxyPort) // Sets the HTTPS_PROXY environment variable so that http requests will go through the proxy. - os.Setenv("HTTPS_PROXY", fmt.Sprintf("http://%s", proxyTarget)) + os.Setenv("HTTPS_PROXY", "http://"+proxyTarget) fmt.Printf("proxy at %s\n", proxyTarget) } if f.proxyListener == nil { diff --git a/internal/martian/header/framing_modifier.go b/internal/martian/header/framing_modifier.go index e5e030b5..25ed5179 100644 --- a/internal/martian/header/framing_modifier.go +++ b/internal/martian/header/framing_modifier.go @@ -15,6 +15,7 @@ package header import ( + "errors" "fmt" "net/http" "strings" @@ -66,7 +67,7 @@ func NewBadFramingModifier() martian.RequestModifier { // "chunked", else we have no way to determine when the request is // finished. if strings.TrimSpace(last[len(last)-1]) != "chunked" { - return fmt.Errorf(`bad request framing: "Transfer-Encoding" header is present, but does not end in "chunked"`) + return errors.New(`bad request framing: "Transfer-Encoding" header is present, but does not end in "chunked"`) } // Transfer-Encoding "chunked" takes precedence over diff --git a/internal/martian/proxy_test.go b/internal/martian/proxy_test.go index 6a2fbfdf..8d66d078 100644 --- a/internal/martian/proxy_test.go +++ b/internal/martian/proxy_test.go @@ -708,7 +708,7 @@ func TestIntegrationTLSHandshakeErrorCallback(t *testing.T) { } var herr error - mc.SetHandshakeErrorCallback(func(_ *http.Request, err error) { herr = fmt.Errorf("handshake error") }) + mc.SetHandshakeErrorCallback(func(_ *http.Request, err error) { herr = errors.New("handshake error") }) p.SetMITM(mc) tl, err := net.Listen("tcp", "[::]:0") @@ -1808,7 +1808,7 @@ func TestServerClosesConnection(t *testing.T) { } defer conn.Close() - req, err := http.NewRequest(http.MethodConnect, fmt.Sprintf("//%s", dstl.Addr().String()), http.NoBody) + req, err := http.NewRequest(http.MethodConnect, "//"+dstl.Addr().String(), http.NoBody) if err != nil { t.Fatalf("http.NewRequest(): got %v, want no error", err) } diff --git a/internal/martian/proxyutil/proxyutil_test.go b/internal/martian/proxyutil/proxyutil_test.go index a02f2caa..03315616 100644 --- a/internal/martian/proxyutil/proxyutil_test.go +++ b/internal/martian/proxyutil/proxyutil_test.go @@ -15,7 +15,7 @@ package proxyutil import ( - "fmt" + "errors" "net/http" "strings" "testing" @@ -57,7 +57,7 @@ func TestNewResponse(t *testing.T) { func TestWarning(t *testing.T) { hdr := http.Header{} - err := fmt.Errorf("modifier error") + err := errors.New("modifier error") Warning(hdr, err) diff --git a/middleware/prometheus.go b/middleware/prometheus.go index 35ca3c1a..10875b2e 100644 --- a/middleware/prometheus.go +++ b/middleware/prometheus.go @@ -7,7 +7,7 @@ package middleware import ( - "fmt" + "errors" "net/http" "strconv" "time" @@ -138,7 +138,7 @@ func (p *Prometheus) ModifyResponse(res *http.Response) error { start := t0.(time.Time) //nolint:forcetypeassert // we know it's time elapsed = float64(time.Since(start)) / float64(time.Second) } else { - return fmt.Errorf("prometheus duration key not found") + return errors.New("prometheus duration key not found") } reqSize := computeApproximateRequestSize(r) diff --git a/mitm.go b/mitm.go index 798a9532..694de115 100644 --- a/mitm.go +++ b/mitm.go @@ -9,7 +9,7 @@ package forwarder import ( "crypto/tls" "crypto/x509" - "fmt" + "errors" "time" "github.com/saucelabs/forwarder/internal/martian/mitm" @@ -53,7 +53,7 @@ func newMartianMITMConfig(c *MITMConfig) (*mitm.Config, error) { } if !ca.IsCA { - return nil, fmt.Errorf("certificate is not a CA") + return nil, errors.New("certificate is not a CA") } cfg, err := mitm.NewConfig(ca, cert.PrivateKey) diff --git a/pac/pac.go b/pac/pac.go index 08c748ef..8db53800 100644 --- a/pac/pac.go +++ b/pac/pac.go @@ -8,6 +8,7 @@ package pac import ( "context" + "errors" "fmt" "io" "net" @@ -28,7 +29,7 @@ type ProxyResolverConfig struct { func (c *ProxyResolverConfig) Validate() error { if c.Script == "" { - return fmt.Errorf("PAC script is empty") + return errors.New("PAC script is empty") } return nil } @@ -82,10 +83,10 @@ func NewProxyResolver(cfg *ProxyResolverConfig, r *net.Resolver, opts ...Option) // Find the FindProxyForURL function. fnx, fn := pr.entryPoint() if fnx == nil && fn == nil { - return nil, fmt.Errorf("PAC script: missing required function FindProxyForURL or FindProxyForURLEx") + return nil, errors.New("PAC script: missing required function FindProxyForURL or FindProxyForURLEx") } if fnx != nil && fn != nil { - return nil, fmt.Errorf("PAC script: ambiguous entry point, both FindProxyForURL and FindProxyForURLEx are defined") + return nil, errors.New("PAC script: ambiguous entry point, both FindProxyForURL and FindProxyForURLEx are defined") } if fnx != nil { pr.fn = fnx diff --git a/pac/pac_ipv6.go b/pac/pac_ipv6.go index 89bbbd71..2219f41d 100644 --- a/pac/pac_ipv6.go +++ b/pac/pac_ipv6.go @@ -9,7 +9,7 @@ package pac import ( "bytes" "context" - "fmt" + "errors" "net" "sort" @@ -125,7 +125,7 @@ func (pr *ProxyResolver) sortIPAddressList(call goja.FunctionCall) goja.Value { ips, err := asSlice(s, ";", func(v string) (parsedIP, error) { ip := net.ParseIP(v) if ip == nil { - return parsedIP{}, fmt.Errorf("invalid IP address") + return parsedIP{}, errors.New("invalid IP address") } return parsedIP{ IP: ip, diff --git a/pac/pac_test.go b/pac/pac_test.go index d4c726ec..84caf1b2 100644 --- a/pac/pac_test.go +++ b/pac/pac_test.go @@ -10,6 +10,7 @@ import ( "bufio" "bytes" "context" + "errors" "fmt" "net" "net/url" @@ -79,7 +80,7 @@ func TestProxyResolverChromium(t *testing.T) { //nolint:maintidx // long table fileName: "dns_fail.js", configure: func(t *testing.T, cfg *ProxyResolverConfig) { cfg.testingLookupIP = func(ctx context.Context, network, host string) ([]net.IP, error) { - return nil, fmt.Errorf("test") + return nil, errors.New("test") } cfg.testingMyIPAddress = []net.IP{} cfg.testingMyIPAddressEx = []net.IP{} diff --git a/pac/proxy.go b/pac/proxy.go index d6f453c4..915c4f9b 100644 --- a/pac/proxy.go +++ b/pac/proxy.go @@ -7,6 +7,7 @@ package pac import ( + "errors" "fmt" "net" "net/url" @@ -116,7 +117,7 @@ func parseProxy(s string) (Proxy, error) { mode, hostport, ok := strings.Cut(s, " ") if !ok { - return noProxy, fmt.Errorf("missing host:port") + return noProxy, errors.New("missing host:port") } host, port, err := net.SplitHostPort(hostport) if err != nil { diff --git a/readurl.go b/readurl.go index 1f8ec059..b8206072 100644 --- a/readurl.go +++ b/readurl.go @@ -8,6 +8,7 @@ package forwarder import ( "encoding/base64" + "errors" "fmt" "io" "net/http" @@ -45,7 +46,7 @@ func readData(u *url.URL) ([]byte, error) { idx := strings.IndexByte(v, ',') if idx != -1 { if v[:idx] != "base64" { - return nil, fmt.Errorf("invalid data URI, the only supported format is: data:base64,") + return nil, errors.New("invalid data URI, the only supported format is: data:base64,") } v = v[idx+1:] } diff --git a/resolver.go b/resolver.go index becf0e25..63570f29 100644 --- a/resolver.go +++ b/resolver.go @@ -8,7 +8,7 @@ package forwarder import ( "context" - "fmt" + "errors" "net" ) @@ -16,7 +16,7 @@ func nopResolver() *net.Resolver { return &net.Resolver{ PreferGo: true, Dial: func(ctx context.Context, network, address string) (net.Conn, error) { - return nil, fmt.Errorf("no DNS resolver configured") + return nil, errors.New("no DNS resolver configured") }, } } diff --git a/utils/cobrautil/bind.go b/utils/cobrautil/bind.go index 9271efae..23dfa57e 100644 --- a/utils/cobrautil/bind.go +++ b/utils/cobrautil/bind.go @@ -7,6 +7,7 @@ package cobrautil import ( + "errors" "fmt" "strings" @@ -66,7 +67,7 @@ func BindFromViper(cmd *cobra.Command, v *viper.Viper) error { if f.Shorthand != "" && f.ShorthandDeprecated == "" { flagName = fmt.Sprintf("-%s, --%s", f.Shorthand, f.Name) } else { - flagName = fmt.Sprintf("--%s", f.Name) + flagName = "--" + f.Name } fmt.Fprintf(cmd.ErrOrStderr(), "invalid argument %q for %q flag: %v", value, flagName, err) ok = false @@ -82,11 +83,11 @@ func BindFromViper(cmd *cobra.Command, v *viper.Viper) error { } if !updateFs(cmd.PersistentFlags()) { - return fmt.Errorf("failed to update persistent flags") + return errors.New("failed to update persistent flags") } if !updateFs(cmd.Flags()) { - return fmt.Errorf("failed to update flags") + return errors.New("failed to update flags") } return nil diff --git a/utils/compose/network.go b/utils/compose/network.go index 48009533..607c9c0c 100644 --- a/utils/compose/network.go +++ b/utils/compose/network.go @@ -7,6 +7,7 @@ package compose import ( + "errors" "fmt" "net" "strconv" @@ -53,10 +54,10 @@ type Network struct { func (n *Network) Validate() error { if n.Name == "" { - return fmt.Errorf("network name is required") + return errors.New("network name is required") } if n.Driver == "" { - return fmt.Errorf("network driver is required") + return errors.New("network driver is required") } return n.IPAM.Validate() } diff --git a/utils/compose/service.go b/utils/compose/service.go index db0b49d4..e234276b 100644 --- a/utils/compose/service.go +++ b/utils/compose/service.go @@ -7,7 +7,7 @@ package compose import ( - "fmt" + "errors" "time" ) @@ -29,13 +29,13 @@ type Service struct { func (s *Service) Validate() error { if s == nil { - return fmt.Errorf("service is nil") + return errors.New("service is nil") } if s.Image == "" { - return fmt.Errorf("service image is empty") + return errors.New("service image is empty") } if s.Name == "" { - return fmt.Errorf("service name is empty") + return errors.New("service name is empty") } return nil diff --git a/utils/osdns/configure.go b/utils/osdns/configure.go index 8654860b..9cc5e884 100644 --- a/utils/osdns/configure.go +++ b/utils/osdns/configure.go @@ -5,6 +5,7 @@ package osdns import ( + "errors" "fmt" ) @@ -21,7 +22,7 @@ func configure(cfg *Config) error { procDNSCfg := resolvConf.dnsConfig.Load() if procDNSCfg == nil { - return fmt.Errorf("failed to get system DNS config") + return errors.New("failed to get system DNS config") } if procDNSCfg.err != nil { return fmt.Errorf("failed to get system DNS config: %w", procDNSCfg.err)