chore(all): replace net.IP with netip.Addr

This commit is contained in:
Quentin McGaw
2023-05-20 19:58:18 +00:00
parent 00ee6ff9a7
commit 0a29337c3b
91 changed files with 525 additions and 590 deletions

View File

@@ -3,7 +3,7 @@ package extract
import (
"errors"
"fmt"
"net"
"net/netip"
"strconv"
"strings"
@@ -25,12 +25,12 @@ func extractDataFromLines(lines []string) (
connection.UpdateEmptyWith(ip, port, protocol)
if connection.Protocol != "" && connection.IP != nil {
if connection.Protocol != "" && connection.IP.IsValid() {
break
}
}
if connection.IP == nil {
if !connection.IP.IsValid() {
return connection, errRemoteLineNotFound
}
@@ -49,24 +49,24 @@ func extractDataFromLines(lines []string) (
}
func extractDataFromLine(line string) (
ip net.IP, port uint16, protocol string, err error) {
ip netip.Addr, port uint16, protocol string, err error) {
switch {
case strings.HasPrefix(line, "proto "):
protocol, err = extractProto(line)
if err != nil {
return nil, 0, "", fmt.Errorf("extracting protocol from proto line: %w", err)
return ip, 0, "", fmt.Errorf("extracting protocol from proto line: %w", err)
}
return nil, 0, protocol, nil
return ip, 0, protocol, nil
case strings.HasPrefix(line, "remote "):
ip, port, protocol, err = extractRemote(line)
if err != nil {
return nil, 0, "", fmt.Errorf("extracting from remote line: %w", err)
return ip, 0, "", fmt.Errorf("extracting from remote line: %w", err)
}
return ip, port, protocol, nil
}
return nil, 0, "", nil
return ip, 0, "", nil
}
var (
@@ -95,19 +95,19 @@ var (
errPortNotValid = errors.New("port is not valid")
)
func extractRemote(line string) (ip net.IP, port uint16,
func extractRemote(line string) (ip netip.Addr, port uint16,
protocol string, err error) {
fields := strings.Fields(line)
n := len(fields)
if n < 2 || n > 4 {
return nil, 0, "", fmt.Errorf("%w: %s", errRemoteLineFieldsCount, line)
return netip.Addr{}, 0, "", fmt.Errorf("%w: %s", errRemoteLineFieldsCount, line)
}
host := fields[1]
ip = net.ParseIP(host)
if ip == nil {
return nil, 0, "", fmt.Errorf("%w: %s", errHostNotIP, host)
ip, err = netip.ParseAddr(host)
if err != nil {
return netip.Addr{}, 0, "", fmt.Errorf("%w: %s", errHostNotIP, host)
// TODO resolve hostname once there is an option to allow it through
// the firewall before the VPN is up.
}
@@ -115,9 +115,9 @@ func extractRemote(line string) (ip net.IP, port uint16,
if n > 2 { //nolint:gomnd
portInt, err := strconv.Atoi(fields[2])
if err != nil {
return nil, 0, "", fmt.Errorf("%w: %s", errPortNotValid, line)
return netip.Addr{}, 0, "", fmt.Errorf("%w: %s", errPortNotValid, line)
} else if portInt < 1 || portInt > 65535 {
return nil, 0, "", fmt.Errorf("%w: %d must be between 1 and 65535", errPortNotValid, portInt)
return netip.Addr{}, 0, "", fmt.Errorf("%w: %d must be between 1 and 65535", errPortNotValid, portInt)
}
port = uint16(portInt)
}
@@ -127,7 +127,7 @@ func extractRemote(line string) (ip net.IP, port uint16,
case "tcp", "udp":
protocol = fields[3]
default:
return nil, 0, "", fmt.Errorf("%w: %s", errProtocolNotSupported, fields[3])
return netip.Addr{}, 0, "", fmt.Errorf("%w: %s", errProtocolNotSupported, fields[3])
}
}

View File

@@ -2,7 +2,7 @@ package extract
import (
"errors"
"net"
"net/netip"
"testing"
"github.com/qdm12/gluetun/internal/constants"
@@ -22,7 +22,7 @@ func Test_extractDataFromLines(t *testing.T) {
"success": {
lines: []string{"bla bla", "proto tcp", "remote 1.2.3.4 1194 tcp", "dev tun6"},
connection: models.Connection{
IP: net.IPv4(1, 2, 3, 4),
IP: netip.AddrFrom4([4]byte{1, 2, 3, 4}),
Port: 1194,
Protocol: constants.TCP,
},
@@ -34,7 +34,7 @@ func Test_extractDataFromLines(t *testing.T) {
"only use first values found": {
lines: []string{"proto udp", "proto tcp", "remote 1.2.3.4 443 tcp", "remote 5.2.3.4 1194 udp"},
connection: models.Connection{
IP: net.IPv4(1, 2, 3, 4),
IP: netip.AddrFrom4([4]byte{1, 2, 3, 4}),
Port: 443,
Protocol: constants.UDP,
},
@@ -49,7 +49,7 @@ func Test_extractDataFromLines(t *testing.T) {
"default TCP port": {
lines: []string{"remote 1.2.3.4", "proto tcp"},
connection: models.Connection{
IP: net.IPv4(1, 2, 3, 4),
IP: netip.AddrFrom4([4]byte{1, 2, 3, 4}),
Port: 443,
Protocol: constants.TCP,
},
@@ -57,7 +57,7 @@ func Test_extractDataFromLines(t *testing.T) {
"default UDP port": {
lines: []string{"remote 1.2.3.4", "proto udp"},
connection: models.Connection{
IP: net.IPv4(1, 2, 3, 4),
IP: netip.AddrFrom4([4]byte{1, 2, 3, 4}),
Port: 1194,
Protocol: constants.UDP,
},
@@ -88,7 +88,7 @@ func Test_extractDataFromLine(t *testing.T) {
testCases := map[string]struct {
line string
ip net.IP
ip netip.Addr
port uint16
protocol string
isErr error
@@ -110,7 +110,7 @@ func Test_extractDataFromLine(t *testing.T) {
},
"extract remote success": {
line: "remote 1.2.3.4 1194 udp",
ip: net.IPv4(1, 2, 3, 4),
ip: netip.AddrFrom4([4]byte{1, 2, 3, 4}),
port: 1194,
protocol: constants.UDP,
},
@@ -186,7 +186,7 @@ func Test_extractRemote(t *testing.T) {
testCases := map[string]struct {
line string
ip net.IP
ip netip.Addr
port uint16
protocol string
err error
@@ -205,7 +205,7 @@ func Test_extractRemote(t *testing.T) {
},
"only IP host": {
line: "remote 1.2.3.4",
ip: net.IPv4(1, 2, 3, 4),
ip: netip.AddrFrom4([4]byte{1, 2, 3, 4}),
},
"port not an integer": {
line: "remote 1.2.3.4 bad",
@@ -225,7 +225,7 @@ func Test_extractRemote(t *testing.T) {
},
"IP host and port": {
line: "remote 1.2.3.4 8000",
ip: net.IPv4(1, 2, 3, 4),
ip: netip.AddrFrom4([4]byte{1, 2, 3, 4}),
port: 8000,
},
"invalid protocol": {
@@ -234,7 +234,7 @@ func Test_extractRemote(t *testing.T) {
},
"IP host and port and protocol": {
line: "remote 1.2.3.4 8000 udp",
ip: net.IPv4(1, 2, 3, 4),
ip: netip.AddrFrom4([4]byte{1, 2, 3, 4}),
port: 8000,
protocol: constants.UDP,
},