diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..dd9bcca --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,58 @@ +run: + # timeout for analysis, e.g. 30s, 5m, default is 1m + timeout: 5m + + # include test files or not, default is true + tests: true + + # by default isn't set. If set we pass it to "go list -mod={option}". From "go help modules": + # If invoked with -mod=readonly, the go command is disallowed from the implicit + # automatic updating of go.mod described above. Instead, it fails when any changes + # to go.mod are needed. This setting is most useful to check that go.mod does + # not need updates, such as in a continuous integration and testing system. + # If invoked with -mod=vendor, the go command assumes that the vendor + # directory holds the correct copies of dependencies and ignores + # the dependency descriptions in go.mod. + modules-download-mode: readonly + +# output configuration options +output: + # colored-line-number|line-number|json|tab|checkstyle|code-climate, default is "colored-line-number" + formats: + - format: colored-line-number + +linters: + enable: + - bodyclose + - errcheck + - errorlint + - gocheckcompilerdirectives + - gochecknoinits + - gochecksumtype + - goconst + - gofmt + - gosimple + - gosec + - govet + - nilerr + - nilnil + - staticcheck + - typecheck + - unused + - whitespace + +issues: + # Excluding configuration per-path, per-linter, per-text and per-source + exclude-rules: + # Exclude some linters from running on tests files. + - path: _test\.go + linters: + - errcheck + - goconst + - gosec + + # checking for errors in defers seldom makes sense... + - source: "^\\s*defer\\s" + linters: + - errcheck + - staticcheck diff --git a/hub/hub_connections.go b/hub/hub_connections.go index 5e3a372..63eb908 100644 --- a/hub/hub_connections.go +++ b/hub/hub_connections.go @@ -167,13 +167,16 @@ func (h *Hub) connectFoundService(remoteService *api.ServiceDetails, host, port, } address := fmt.Sprintf("wss://%s:%s%s", host, port, path) - conn, _, err := dialer.Dial(address, nil) - if err != nil { + conn, resp, err := dialer.Dial(address, nil) + if err == nil { + defer resp.Body.Close() + } else { address = fmt.Sprintf("wss://%s:%s", host, port) - conn, _, err = dialer.Dial(address, nil) - } - if err != nil { - return err + conn, resp, err = dialer.Dial(address, nil) + if err != nil { + return err + } + defer resp.Body.Close() } tlsConn := conn.UnderlyingConn().(*tls.Conn) diff --git a/hub/hub_pairing.go b/hub/hub_pairing.go index 6fed753..f7d8ef9 100644 --- a/hub/hub_pairing.go +++ b/hub/hub_pairing.go @@ -102,7 +102,6 @@ func (h *Hub) RegisterRemoteSKI(ski string, enable bool) { // Disconnect a connection to an SKI, used by a service implementation // e.g. if heartbeats go wrong func (h *Hub) DisconnectSKI(ski string, reason string) { - con := h.connectionForSKI(ski) if con == nil { return diff --git a/hub/hub_shipconnection.go b/hub/hub_shipconnection.go index 5c10e78..64aa6d1 100644 --- a/hub/hub_shipconnection.go +++ b/hub/hub_shipconnection.go @@ -1,6 +1,7 @@ package hub import ( + "errors" "time" "github.com/enbility/ship-go/api" @@ -78,7 +79,7 @@ func (h *Hub) HandleShipHandshakeStateUpdate(ski string, state model.ShipState) } pairingState := h.mapShipMessageExchangeState(state.State, ski) - if state.Error != nil && state.Error != api.ErrConnectionNotFound { + if state.Error != nil && !errors.Is(state.Error, api.ErrConnectionNotFound) { pairingState = api.ConnectionStateError } @@ -88,7 +89,7 @@ func (h *Hub) HandleShipHandshakeStateUpdate(ski string, state model.ShipState) existingDetails := service.ConnectionStateDetail() existingState := existingDetails.State() - if existingState != pairingState || existingDetails.Error() != state.Error { + if existingState != pairingState || !errors.Is(existingDetails.Error(), state.Error) { service.SetConnectionStateDetail(pairingDetail) // always send a delayed update, as the processing of the new state has to be done diff --git a/hub/hub_test.go b/hub/hub_test.go index dd2429e..b81ce7d 100644 --- a/hub/hub_test.go +++ b/hub/hub_test.go @@ -152,7 +152,7 @@ func (s *HubSuite) Test_SendWSCloseMessage() { wsURL := strings.Replace(server.URL, "http://", "ws://", -1) // Connect to the server - con, _, err := websocket.DefaultDialer.Dial(wsURL, nil) + con, resp, err := websocket.DefaultDialer.Dial(wsURL, nil) assert.Nil(s.T(), err) ski := "12af9e" @@ -163,6 +163,7 @@ func (s *HubSuite) Test_SendWSCloseMessage() { hub.sendWSCloseMessage(con) + resp.Body.Close() _ = con.Close() server.CloseClientConnections() server.Close() @@ -334,16 +335,18 @@ func (s *HubSuite) Test_ServeHTTP_01() { wsURL := strings.Replace(server.URL, "http://", "ws://", -1) // Connect to the server - con, _, err := websocket.DefaultDialer.Dial(wsURL, nil) + con, resp, err := websocket.DefaultDialer.Dial(wsURL, nil) assert.Nil(s.T(), err) + resp.Body.Close() _ = con.Close() dialer := &websocket.Dialer{ Subprotocols: []string{api.ShipWebsocketSubProtocol}, } - con, _, err = dialer.Dial(wsURL, nil) + con, resp, err = dialer.Dial(wsURL, nil) assert.Nil(s.T(), err) + resp.Body.Close() _ = con.Close() server.CloseClientConnections() server.Close() @@ -373,9 +376,10 @@ func (s *HubSuite) Test_ServeHTTP_02() { }, Subprotocols: []string{api.ShipWebsocketSubProtocol}, } - con, _, err := dialer.Dial(wsURL, nil) + con, resp, err := dialer.Dial(wsURL, nil) assert.Nil(s.T(), err) + resp.Body.Close() _ = con.Close() validCert, _ := cert.CreateCertificate("unit", "org", "DE", "CN") @@ -389,9 +393,10 @@ func (s *HubSuite) Test_ServeHTTP_02() { }, Subprotocols: []string{api.ShipWebsocketSubProtocol}, } - con, _, err = dialer.Dial(wsURL, nil) + con, resp, err = dialer.Dial(wsURL, nil) assert.Nil(s.T(), err) + resp.Body.Close() _ = con.Close() server.CloseClientConnections() server.Close() diff --git a/mdns/avahi.go b/mdns/avahi.go index fd6d320..404b330 100644 --- a/mdns/avahi.go +++ b/mdns/avahi.go @@ -181,7 +181,6 @@ func (a *AvahiProvider) ResolveEntries(callback api.MdnsResolveCB) { } } } - } // process an avahi mDNS service @@ -211,14 +210,13 @@ func (a *AvahiProvider) processService(service avahi.Service, remove bool, cb ap // resolve the new service resolved, err := a.avServer.ResolveService(service.Interface, service.Protocol, service.Name, service.Type, service.Domain, avahi.ProtoUnspec, 0) if err != nil { - return fmt.Errorf("error resolving service: %s error: %s", service.Name, err) + return fmt.Errorf("error resolving service: %s error: %w", service.Name, err) } return a.processAddedService(resolved, cb) } func (a *AvahiProvider) processRemovedService(service avahi.Service, cb api.MdnsResolveCB) error { - // get the elements for the service elements := a.serviceElements[getServiceUniqueKey(service)] @@ -228,7 +226,6 @@ func (a *AvahiProvider) processRemovedService(service avahi.Service, cb api.Mdns } func (a *AvahiProvider) processAddedService(service avahi.Service, cb api.MdnsResolveCB) error { - // convert [][]byte to []string manually var txt []string for _, element := range service.Txt { diff --git a/mdns/mdns.go b/mdns/mdns.go index f96c76f..134c074 100644 --- a/mdns/mdns.go +++ b/mdns/mdns.go @@ -358,7 +358,6 @@ func (m *MdnsManager) processMdnsEntry(elements map[string]string, name, host st if updated { m.setMdnsEntry(ski, entry) } - } else if !exists && !remove { updated = true // new