Skip to content

Commit 26d3e75

Browse files
authored
Fix linter configuration & issues (#735)
* Fix linter configuration * Fix linter issues * Fix additional linter issues
1 parent 5f52967 commit 26d3e75

File tree

6 files changed

+93
-74
lines changed

6 files changed

+93
-74
lines changed

.golangci.yml

Lines changed: 64 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
1+
version: "2"
12
run:
2-
timeout: 5m
33
modules-download-mode: readonly
44

55
# List from https://golangci-lint.run/usage/linters/
66
linters:
77
enable:
8-
# Default linters
9-
- errcheck
10-
- gosimple
11-
- govet
12-
- ineffassign
13-
- staticcheck
14-
- unused
15-
# Other linters
168
- asasalint
179
- asciicheck
1810
- bidichk
@@ -27,13 +19,10 @@ linters:
2719
- fatcontext
2820
- forbidigo
2921
- forcetypeassert
30-
- gci
3122
- gocheckcompilerdirectives
3223
- gochecksumtype
3324
- gocritic
3425
- godot
35-
- gofmt
36-
- gofumpt
3726
- goheader
3827
- gomodguard
3928
- goprintffuncname
@@ -57,9 +46,8 @@ linters:
5746
- predeclared
5847
- reassign
5948
- revive
60-
- stylecheck
49+
- staticcheck
6150
- tagalign
62-
- tenv
6351
- testableexamples
6452
- testifylint
6553
- testpackage
@@ -69,7 +57,6 @@ linters:
6957
- usestdlibvars
7058
- wastedassign
7159
- whitespace
72-
7360
disable:
7461
- bodyclose
7562
- canonicalheader
@@ -89,7 +76,6 @@ linters:
8976
- goconst
9077
- gocyclo
9178
- godox
92-
- goimports
9379
- gomoddirectives
9480
- inamedparam
9581
- intrange
@@ -115,46 +101,65 @@ linters:
115101
- wrapcheck
116102
- wsl
117103
- zerologlint
118-
119-
linters-settings:
120-
gci:
121-
sections:
122-
- standard
123-
- default
124-
skip-generated: false
125-
custom-order: true
126-
gosec:
127-
excludes:
128-
- G402 # InsecureSkipVerify
129-
- G102 # Binds to all network interfaces
130-
- G403 # RSA keys should be at least 2048 bits
131-
- G115 # Integer overflow conversion (uint64 -> int64)
132-
- G404 # Use of weak random number generator (math/rand)
133-
- G204 # Subprocess launched with a potential tainted input or cmd arguments
134-
135-
issues:
136-
exclude-rules:
137-
- linters:
138-
- gocritic
139-
text: "ifElseChain"
140-
- linters:
141-
- lll
142-
source: "^// "
143-
- linters:
144-
- revive
145-
text: "add-constant: "
146-
- linters:
147-
- revive
148-
text: "unused-parameter: "
149-
- linters:
150-
- revive
151-
text: "empty-block: "
152-
- linters:
153-
- revive
154-
text: "var-naming: " # TODO: Re-enable in V2
155-
- linters:
156-
- stylecheck
157-
text: " should be " # TODO: Re-enable in V2
158-
- linters:
159-
- stylecheck
160-
text: "ST1003: should not use ALL_CAPS in Go names; use CamelCase instead" # TODO: Re-enable in V2
104+
settings:
105+
gosec:
106+
excludes:
107+
- G402 # InsecureSkipVerify
108+
- G102 # Binds to all network interfaces
109+
- G403 # RSA keys should be at least 2048 bits
110+
- G115 # Integer overflow conversion (uint64 -> int64)
111+
- G404 # Use of weak random number generator (math/rand)
112+
- G204 # Subprocess launched with a potential tainted input or cmd arguments
113+
- G602 # Slice index out of range
114+
exclusions:
115+
generated: lax
116+
presets:
117+
- comments
118+
- common-false-positives
119+
- legacy
120+
- std-error-handling
121+
rules:
122+
- linters:
123+
- gocritic
124+
text: ifElseChain
125+
- linters:
126+
- lll
127+
source: '^// '
128+
- linters:
129+
- revive
130+
text: 'add-constant: '
131+
- linters:
132+
- revive
133+
text: 'unused-parameter: '
134+
- linters:
135+
- revive
136+
text: 'empty-block: '
137+
- linters:
138+
- revive
139+
text: 'var-naming: ' # TODO: Re-enable in V2
140+
- linters:
141+
- staticcheck
142+
text: ' should be ' # TODO: Re-enable in V2
143+
- linters:
144+
- staticcheck
145+
text: 'ST1003: should not use ALL_CAPS in Go names; use CamelCase instead'
146+
paths:
147+
- examples$
148+
- transport
149+
formatters:
150+
enable:
151+
- gci
152+
- gofmt
153+
- gofumpt
154+
settings:
155+
gci:
156+
sections:
157+
- standard
158+
- default
159+
custom-order: true
160+
exclusions:
161+
generated: lax
162+
paths:
163+
- third_party$
164+
- builtin$
165+
- examples$

h2.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package goproxy
22

33
import (
44
"bufio"
5+
"context"
56
"crypto/tls"
67
"errors"
78
"io"
@@ -45,7 +46,7 @@ func (r *H2Transport) RoundTrip(_ *http.Request) (*http.Response, error) {
4546
if !ok {
4647
return nil, errors.New("invalid TLS connection")
4748
}
48-
if err = rawTLSConn.Handshake(); err != nil {
49+
if err = rawTLSConn.HandshakeContext(context.Background()); err != nil {
4950
return nil, err
5051
}
5152
if r.TLSConfig == nil || !r.TLSConfig.InsecureSkipVerify {

https.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ func (proxy *ProxyHttpServer) dial(ctx *ProxyCtx, network, addr string) (c net.C
8282

8383
// if the user didn't specify any dialer, we just use the default one,
8484
// provided by net package
85-
return net.Dial(network, addr)
85+
var d net.Dialer
86+
return d.DialContext(ctx.Req.Context(), network, addr)
8687
}
8788

8889
func (proxy *ProxyHttpServer) connectDial(ctx *ProxyCtx, network, addr string) (c net.Conn, err error) {
@@ -282,7 +283,7 @@ func (proxy *ProxyHttpServer) handleHttps(w http.ResponseWriter, r *http.Request
282283
go func() {
283284
rawClientTls := tls.Server(proxyClient, tlsConfig)
284285
defer rawClientTls.Close()
285-
if err := rawClientTls.Handshake(); err != nil {
286+
if err := rawClientTls.HandshakeContext(context.Background()); err != nil {
286287
ctx.Warnf("Cannot handshake client %v %v", r.Host, err)
287288
return
288289
}

internal/signer/signer.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,12 @@ func SignHost(ca tls.Certificate, hosts []string) (cert *tls.Certificate, err er
108108
return nil, err
109109
}
110110

111-
certBytes := [][]byte{derBytes}
112-
certBytes = append(certBytes, ca.Certificate...)
111+
certBytes := make([][]byte, 1+len(ca.Certificate))
112+
certBytes[0] = derBytes
113+
for i, singleCertBytes := range ca.Certificate {
114+
certBytes[i+1] = singleCertBytes
115+
}
116+
113117
return &tls.Certificate{
114118
Certificate: certBytes,
115119
PrivateKey: certpriv,

internal/signer/signer_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ func testSignerTLS(t *testing.T, ca tls.Certificate) {
9090
}
9191
browser := getBrowser(os.Args)
9292
if browser != "" {
93-
_ = exec.Command(browser, asLocalhost).Run()
93+
ctx := context.Background()
94+
_ = exec.CommandContext(ctx, browser, asLocalhost).Run()
9495
time.Sleep(10 * time.Second)
9596
}
9697
}

proxy_test.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func getOrFail(t *testing.T, url string, client *http.Client) []byte {
9999

100100
func getCert(t *testing.T, c *tls.Conn) []byte {
101101
t.Helper()
102-
if err := c.Handshake(); err != nil {
102+
if err := c.HandshakeContext(context.Background()); err != nil {
103103
t.Fatal("cannot handshake", err)
104104
}
105105
return c.ConnectionState().PeerCertificates[0].Raw
@@ -318,14 +318,19 @@ func TestSimpleMitm(t *testing.T) {
318318
client, l := oneShotProxy(proxy)
319319
defer l.Close()
320320

321-
c, err := tls.Dial("tcp", https.Listener.Addr().String(), &tls.Config{InsecureSkipVerify: true})
321+
ctx := context.Background()
322+
c, err := (&tls.Dialer{
323+
Config: &tls.Config{InsecureSkipVerify: true},
324+
}).DialContext(ctx, "tcp", https.Listener.Addr().String())
322325
if err != nil {
323326
t.Fatal("cannot dial to tcp server", err)
324327
}
325-
origCert := getCert(t, c)
328+
tlsConn, ok := c.(*tls.Conn)
329+
assert.True(t, ok)
330+
origCert := getCert(t, tlsConn)
326331
_ = c.Close()
327332

328-
c2, err := net.Dial("tcp", l.Listener.Addr().String())
333+
c2, err := (&net.Dialer{}).DialContext(ctx, "tcp", l.Listener.Addr().String())
329334
if err != nil {
330335
t.Fatal("dialing to proxy", err)
331336
}
@@ -555,7 +560,9 @@ func TestHeadReqHasContentLength(t *testing.T) {
555560
}
556561

557562
func TestChunkedResponse(t *testing.T) {
558-
l, err := net.Listen("tcp", ":10234")
563+
ctx := context.Background()
564+
565+
l, err := (&net.ListenConfig{}).Listen(ctx, "tcp", ":10234")
559566
panicOnErr(err, "listen")
560567
defer l.Close()
561568
go func() {
@@ -579,10 +586,10 @@ func TestChunkedResponse(t *testing.T) {
579586
}
580587
}()
581588

582-
c, err := net.Dial("tcp", "localhost:10234")
589+
c, err := (&net.Dialer{}).DialContext(ctx, "tcp", "localhost:10234")
583590
panicOnErr(err, "dial")
584591
defer c.Close()
585-
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/", nil)
592+
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "/", nil)
586593
_ = req.Write(c)
587594
resp, err := http.ReadResponse(bufio.NewReader(c), req)
588595
panicOnErr(err, "readresp")
@@ -609,7 +616,7 @@ func TestChunkedResponse(t *testing.T) {
609616
client, s := oneShotProxy(proxy)
610617
defer s.Close()
611618

612-
req, err = http.NewRequestWithContext(context.Background(), http.MethodGet, "http://localhost:10234/", nil)
619+
req, err = http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost:10234/", nil)
613620
if err != nil {
614621
t.Fatal("Cannot create request", err)
615622
}
@@ -694,7 +701,7 @@ func TestGoproxyHijackConnect(t *testing.T) {
694701
client, l := oneShotProxy(proxy)
695702
defer l.Close()
696703
proxyAddr := l.Listener.Addr().String()
697-
conn, err := net.Dial("tcp", proxyAddr)
704+
conn, err := (&net.Dialer{}).DialContext(context.Background(), "tcp", proxyAddr)
698705
panicOnErr(err, "conn "+proxyAddr)
699706
buf := bufio.NewReader(conn)
700707
writeConnect(conn)

0 commit comments

Comments
 (0)