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

@@ -9,6 +9,7 @@ import (
"io"
"net"
"net/http"
"net/netip"
"net/url"
"os"
"strconv"
@@ -21,14 +22,14 @@ import (
)
var (
ErrServerNameNotFound = errors.New("server name not found in servers")
ErrGatewayIPIsNil = errors.New("gateway IP address is nil")
ErrServerNameEmpty = errors.New("server name is empty")
ErrServerNameNotFound = errors.New("server name not found in servers")
ErrGatewayIPIsNotValid = errors.New("gateway IP address is not valid")
ErrServerNameEmpty = errors.New("server name is empty")
)
// PortForward obtains a VPN server side port forwarded from PIA.
func (p *Provider) PortForward(ctx context.Context, client *http.Client,
logger utils.Logger, gateway net.IP, serverName string) (
logger utils.Logger, gateway netip.Addr, serverName string) (
port uint16, err error) {
server, ok := p.storage.GetServerByName(providers.PrivateInternetAccess, serverName)
if !ok {
@@ -40,8 +41,8 @@ func (p *Provider) PortForward(ctx context.Context, client *http.Client,
" (region " + server.Region + ") does not support port forwarding")
return 0, nil
}
if gateway == nil {
return 0, ErrGatewayIPIsNil
if !gateway.IsValid() {
return 0, fmt.Errorf("%w: %s", ErrGatewayIPIsNotValid, gateway)
} else if serverName == "" {
return 0, ErrServerNameEmpty
}
@@ -91,7 +92,7 @@ var (
)
func (p *Provider) KeepPortForward(ctx context.Context,
gateway net.IP, serverName string) (err error) {
gateway netip.Addr, serverName string) (err error) {
privateIPClient, err := newHTTPClient(serverName)
if err != nil {
return fmt.Errorf("creating custom HTTP client: %w", err)
@@ -132,7 +133,7 @@ func (p *Provider) KeepPortForward(ctx context.Context,
}
func refreshPIAPortForwardData(ctx context.Context, client, privateIPClient *http.Client,
gateway net.IP, portForwardPath, authFilePath string) (data piaPortForwardData, err error) {
gateway netip.Addr, portForwardPath, authFilePath string) (data piaPortForwardData, err error) {
data.Token, err = fetchToken(ctx, client, authFilePath)
if err != nil {
return data, fmt.Errorf("fetching token: %w", err)
@@ -314,7 +315,7 @@ func getOpenvpnCredentials(authFilePath string) (
return username, password, nil
}
func fetchPortForwardData(ctx context.Context, client *http.Client, gateway net.IP, token string) (
func fetchPortForwardData(ctx context.Context, client *http.Client, gateway netip.Addr, token string) (
port uint16, signature string, expiration time.Time, err error) {
errSubstitutions := map[string]string{url.QueryEscape(token): "<token>"}
@@ -368,7 +369,7 @@ var (
ErrBadResponse = errors.New("bad response received")
)
func bindPort(ctx context.Context, client *http.Client, gateway net.IP, data piaPortForwardData) (err error) {
func bindPort(ctx context.Context, client *http.Client, gateway netip.Addr, data piaPortForwardData) (err error) {
payload, err := packPayload(data.Port, data.Token, data.Expiration)
if err != nil {
return fmt.Errorf("serializing payload: %w", err)

View File

@@ -7,8 +7,8 @@ import (
"errors"
"fmt"
"io"
"net"
"net/http"
"net/netip"
)
var (
@@ -31,8 +31,8 @@ type regionData struct {
}
type serverData struct {
IP net.IP `json:"ip"`
CN string `json:"cn"`
IP netip.Addr `json:"ip"`
CN string `json:"cn"`
}
func fetchAPI(ctx context.Context, client *http.Client) (

View File

@@ -1,7 +1,7 @@
package updater
import (
"net"
"net/netip"
"github.com/qdm12/gluetun/internal/constants/vpn"
"github.com/qdm12/gluetun/internal/models"
@@ -10,7 +10,7 @@ import (
type nameToServer map[string]models.Server
func (nts nameToServer) add(name, hostname, region string,
tcp, udp, portForward bool, ip net.IP) (change bool) {
tcp, udp, portForward bool, ip netip.Addr) (change bool) {
server, ok := nts[name]
if !ok {
change = true
@@ -32,7 +32,7 @@ func (nts nameToServer) add(name, hostname, region string,
ipFound := false
for _, existingIP := range server.IPs {
if ip.Equal(existingIP) {
if ip == existingIP {
ipFound = true
break
}