Add linters and fix lint issues
This commit is contained in:
@@ -5,7 +5,6 @@ import (
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -28,7 +27,8 @@ func parseRoutingEntry(s string) (r routingEntry, err error) {
|
||||
return fmt.Errorf("line %q: %w", s, err)
|
||||
}
|
||||
fields := strings.Fields(s)
|
||||
if len(fields) < 11 {
|
||||
const minFields = 11
|
||||
if len(fields) < minFields {
|
||||
return r, wrapError(fmt.Errorf("not enough fields"))
|
||||
}
|
||||
r.iface = fields[0]
|
||||
@@ -74,20 +74,22 @@ func parseRoutingEntry(s string) (r routingEntry, err error) {
|
||||
|
||||
func reversedHexToIPv4(reversedHex string) (ip net.IP, err error) {
|
||||
bytes, err := hex.DecodeString(reversedHex)
|
||||
const nBytesRequired = 4
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot parse reversed IP hex %q: %s", reversedHex, err)
|
||||
} else if len(bytes) != 4 {
|
||||
return nil, fmt.Errorf("hex string contains %d bytes instead of 4", len(bytes))
|
||||
} else if L := len(bytes); L != nBytesRequired {
|
||||
return nil, fmt.Errorf("hex string contains %d bytes instead of %d", L, nBytesRequired)
|
||||
}
|
||||
return []byte{bytes[3], bytes[2], bytes[1], bytes[0]}, nil
|
||||
}
|
||||
|
||||
func hexToIPv4Mask(hexString string) (mask net.IPMask, err error) {
|
||||
bytes, err := hex.DecodeString(hexString)
|
||||
const nBytesRequired = 4
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot parse hex mask %q: %s", hexString, err)
|
||||
} else if len(bytes) != 4 {
|
||||
return nil, fmt.Errorf("hex string contains %d bytes instead of 4", len(bytes))
|
||||
} else if L := len(bytes); L != nBytesRequired {
|
||||
return nil, fmt.Errorf("hex string contains %d bytes instead of %d", L, nBytesRequired)
|
||||
}
|
||||
return []byte{bytes[3], bytes[2], bytes[1], bytes[0]}, nil
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
//nolint:lll
|
||||
func Test_parseRoutingEntry(t *testing.T) {
|
||||
t.Parallel()
|
||||
tests := map[string]struct {
|
||||
|
||||
@@ -2,12 +2,12 @@ package routing
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
|
||||
"fmt"
|
||||
"net"
|
||||
)
|
||||
|
||||
func (r *routing) AddRouteVia(ctx context.Context, subnet net.IPNet, defaultGateway net.IP, defaultInterface string) error {
|
||||
func (r *routing) AddRouteVia(ctx context.Context,
|
||||
subnet net.IPNet, defaultGateway net.IP, defaultInterface string) error {
|
||||
subnetStr := subnet.String()
|
||||
r.logger.Info("adding %s as route via %s %s", subnetStr, defaultGateway, defaultInterface)
|
||||
exists, err := r.routeExists(subnet)
|
||||
@@ -19,9 +19,11 @@ func (r *routing) AddRouteVia(ctx context.Context, subnet net.IPNet, defaultGate
|
||||
if r.debug {
|
||||
fmt.Printf("ip route add %s via %s dev %s\n", subnetStr, defaultGateway, defaultInterface)
|
||||
}
|
||||
output, err := r.commander.Run(ctx, "ip", "route", "add", subnetStr, "via", defaultGateway.String(), "dev", defaultInterface)
|
||||
output, err := r.commander.Run(ctx,
|
||||
"ip", "route", "add", subnetStr, "via", defaultGateway.String(), "dev", defaultInterface)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot add route for %s via %s %s %s: %s: %w", subnetStr, defaultGateway, "dev", defaultInterface, output, err)
|
||||
return fmt.Errorf("cannot add route for %s via %s %s %s: %s: %w",
|
||||
subnetStr, defaultGateway, "dev", defaultInterface, output, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -63,6 +63,7 @@ func Test_DeleteRouteVia(t *testing.T) {
|
||||
commander.EXPECT().Run(ctx, "ip", "route", "del", subnetStr).
|
||||
Return(tc.runOutput, tc.runErr).Times(1)
|
||||
fileManager := mock_files.NewMockFileManager(mockCtrl)
|
||||
//nolint:lll
|
||||
routesData := []byte(`Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
|
||||
eth0 0002A8C0 0100000A 0003 0 0 0 00FFFFFF 0 0 0
|
||||
`)
|
||||
|
||||
@@ -2,9 +2,8 @@ package routing
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net"
|
||||
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/constants"
|
||||
@@ -37,7 +36,8 @@ func (r *routing) DefaultRoute() (defaultInterface string, defaultGateway net.IP
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
if len(entries) < 2 {
|
||||
const minEntries = 2
|
||||
if len(entries) < minEntries {
|
||||
return "", nil, fmt.Errorf("not enough entries (%d) found in %s", len(entries), constants.NetRoute)
|
||||
}
|
||||
var defaultRouteEntry routingEntry
|
||||
@@ -61,7 +61,8 @@ func (r *routing) LocalSubnet() (defaultSubnet net.IPNet, err error) {
|
||||
if err != nil {
|
||||
return defaultSubnet, err
|
||||
}
|
||||
if len(entries) < 2 {
|
||||
const minEntries = 2
|
||||
if len(entries) < minEntries {
|
||||
return defaultSubnet, fmt.Errorf("not enough entries (%d) found in %s", len(entries), constants.NetRoute)
|
||||
}
|
||||
var localSubnetEntry routingEntry
|
||||
|
||||
@@ -6,14 +6,14 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/constants"
|
||||
"github.com/qdm12/golibs/files/mock_files"
|
||||
"github.com/qdm12/golibs/logging/mock_logging"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
//nolint:lll
|
||||
const exampleRouteData = `Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
|
||||
tun0 00000000 050A030A 0003 0 0 0 00000080 0 0 0
|
||||
eth0 00000000 010011AC 0003 0 0 0 00000000 0 0 0
|
||||
@@ -24,6 +24,7 @@ tun0 00000080 050A030A 0003 0 0 0 00000080
|
||||
eth0 000011AC 00000000 0001 0 0 0 0000FFFF 0 0 0
|
||||
`
|
||||
|
||||
//nolint:lll
|
||||
func Test_parseRoutingTable(t *testing.T) {
|
||||
t.Parallel()
|
||||
tests := map[string]struct {
|
||||
@@ -96,6 +97,7 @@ eth0 x 0100000A 0003 0 0 0 00FFFFFF 0 0 0
|
||||
}
|
||||
}
|
||||
|
||||
//nolint:lll
|
||||
func Test_DefaultRoute(t *testing.T) {
|
||||
t.Parallel()
|
||||
tests := map[string]struct {
|
||||
@@ -164,6 +166,7 @@ eth0 000011AC 00000000 0001 0 0 0 0000FFFF
|
||||
}
|
||||
}
|
||||
|
||||
//nolint:lll
|
||||
func Test_LocalSubnet(t *testing.T) {
|
||||
t.Parallel()
|
||||
tests := map[string]struct {
|
||||
@@ -229,6 +232,7 @@ eth0 000011AC 10000000 0001 0 0 0 0000FFFF
|
||||
}
|
||||
}
|
||||
|
||||
//nolint:lll
|
||||
func Test_routeExists(t *testing.T) {
|
||||
t.Parallel()
|
||||
tests := map[string]struct {
|
||||
@@ -291,6 +295,7 @@ eth0 0002A8C0 0100000A 0003 0 0 0 00FFFFFF
|
||||
}
|
||||
}
|
||||
|
||||
//nolint:lll
|
||||
func Test_VPNDestinationIP(t *testing.T) {
|
||||
t.Parallel()
|
||||
tests := map[string]struct {
|
||||
|
||||
@@ -26,7 +26,7 @@ type routing struct {
|
||||
debug bool
|
||||
}
|
||||
|
||||
// NewConfigurator creates a new Configurator instance
|
||||
// NewConfigurator creates a new Configurator instance.
|
||||
func NewRouting(logger logging.Logger, fileManager files.FileManager) Routing {
|
||||
return &routing{
|
||||
commander: command.NewCommander(),
|
||||
|
||||
Reference in New Issue
Block a user