From 0edf4e92c3938fef4568fceafb012c4712cb77f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Thu, 11 Jul 2024 18:14:24 +0900 Subject: [PATCH] chore: make lint happy --- .golangci.yml | 73 +++++++++++++++++++++++++++++++++++++++++ gold/link/listen.go | 8 +++-- gold/link/me.go | 4 +-- gold/link/nat.go | 8 +++-- upper/services/wg/wg.go | 7 ++-- 5 files changed, 90 insertions(+), 10 deletions(-) create mode 100644 .golangci.yml diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..f32f75c --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,73 @@ +linters-settings: + errcheck: + ignore: fmt:.*,io/ioutil:^Read.* + ignoretests: true + exclude-functions: + - github.com/fumiama/WireGold/helper.(*Writer).Write.* + - github.com/fumiama/WireGold/upper/services/tunnel.(*Tunnel).(Write|Read) + + goimports: + local-prefixes: github.com/fumiama/WireGold + +linters: + # please, do not use `enable-all`: it's deprecated and will be removed soon. + # inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint + disable-all: true + fast: false + enable: + - bodyclose + #- deadcode + #- depguard + - dogsled + - dupl + - errcheck + - exportloopref + - exhaustive + #- funlen + #- goconst + #- gocritic + #- gocyclo + - gofumpt + - goimports + - goprintffuncname + #- gosec + - gosimple + - govet + - ineffassign + - misspell + - nolintlint + - rowserrcheck + - staticcheck + #- structcheck + #- stylecheck + - typecheck + - unconvert + - unparam + - unused + #- varcheck + - whitespace + - prealloc + - predeclared + - asciicheck + #- revive + - makezero + #- interfacer + + +run: + # default concurrency is a available CPU number. + # concurrency: 4 # explicitly omit this value to fully utilize available resources. + deadline: 5m + issues-exit-code: 1 + tests: false + +# output configuration options +output: + format: 'colored-line-number' + print-issued-lines: true + print-linter-name: true + uniq-by-line: true + +issues: + # Fix found issues (if it's supported by the linter) + fix: true diff --git a/gold/link/listen.go b/gold/link/listen.go index 965f236..2d1d0c6 100644 --- a/gold/link/listen.go +++ b/gold/link/listen.go @@ -157,8 +157,12 @@ func (m *Me) listenthread(packet *head.Packet, addr *net.UDPAddr, index int, fin p.pipe <- packet logrus.Debugln("[listen] @", index, "deliver to pipe of", p.peerip) } else { - m.nic.Write(packet.Data) - logrus.Debugln("[listen] @", index, "deliver", len(packet.Data), "bytes data to nic") + _, err := m.nic.Write(packet.Data) + if err != nil { + logrus.Errorln("[listen] @", index, "deliver", len(packet.Data), "bytes data to nic err:", err) + } else { + logrus.Debugln("[listen] @", index, "deliver", len(packet.Data), "bytes data to nic") + } packet.Put() } default: diff --git a/gold/link/me.go b/gold/link/me.go index 70d4012..c89d6fa 100644 --- a/gold/link/me.go +++ b/gold/link/me.go @@ -121,7 +121,7 @@ func (m *Me) MTU() uint16 { return m.mtu } -func (m *Me) Close() error { +func (m *Me) CloseNIC() error { m.nic.Down() return m.nic.Close() } @@ -132,7 +132,7 @@ func (m *Me) Write(packet []byte) (n int, err error) { return } -func (m *Me) ListenFromNIC() (written int64, err error) { +func (m *Me) ListenNIC() (written int64, err error) { m.nic.Up() return io.Copy(m, m.nic) } diff --git a/gold/link/nat.go b/gold/link/nat.go index 7df186e..b05072b 100644 --- a/gold/link/nat.go +++ b/gold/link/nat.go @@ -86,8 +86,12 @@ func (l *Link) onQuery(packet []byte) { if len(notify) > 0 { logrus.Infoln("[nat] query wrap", len(notify), "notify") w := helper.SelectWriter() - json.NewEncoder(w).Encode(¬ify) - l.WriteAndPut(head.NewPacket(head.ProtoNotify, l.me.srcport, l.peerip, l.me.dstport, w.Bytes()), false) + _ = json.NewEncoder(w).Encode(¬ify) + _, err = l.WriteAndPut(head.NewPacket(head.ProtoNotify, l.me.srcport, l.peerip, l.me.dstport, w.Bytes()), false) + if err != nil { + logrus.Errorln("[nat] notify peer", l, "err:", err) + return + } helper.PutWriter(w) } } diff --git a/upper/services/wg/wg.go b/upper/services/wg/wg.go index 1208134..cc8dba8 100644 --- a/upper/services/wg/wg.go +++ b/upper/services/wg/wg.go @@ -49,20 +49,19 @@ func NewWireGold(c *config.Config) (wg WG, err error) { } func (wg *WG) Start(srcport, destport uint16) { - wg.init(srcport, destport) - go wg.me.ListenFromNIC() + go wg.Run(srcport, destport) } func (wg *WG) Run(srcport, destport uint16) { wg.init(srcport, destport) - _, err := wg.me.ListenFromNIC() + _, err := wg.me.ListenNIC() if err != nil { logrus.Panicln(err) } } func (wg *WG) Stop() { - _ = wg.me.Close() + _ = wg.me.CloseNIC() } func (wg *WG) init(srcport, dstport uint16) {