feat(protonvpn): check udp vs tcp port forwarded
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/qdm12/gluetun/internal/natpmp"
|
"github.com/qdm12/gluetun/internal/natpmp"
|
||||||
@@ -32,29 +33,47 @@ func (p *Provider) PortForward(ctx context.Context, _ *http.Client,
|
|||||||
}
|
}
|
||||||
|
|
||||||
logger.Info("gateway external IPv4 address is " + externalIPv4Address.String())
|
logger.Info("gateway external IPv4 address is " + externalIPv4Address.String())
|
||||||
networkProtocols := []string{"udp", "tcp"}
|
|
||||||
const internalPort, externalPort = 0, 0
|
const internalPort, externalPort = 0, 0
|
||||||
const lifetime = 60 * time.Second
|
const lifetime = 60 * time.Second
|
||||||
for _, networkProtocol := range networkProtocols {
|
|
||||||
_, _, assignedExternalPort, assignedLiftetime, err :=
|
|
||||||
client.AddPortMapping(ctx, gateway, networkProtocol,
|
|
||||||
internalPort, externalPort, lifetime)
|
|
||||||
if err != nil {
|
|
||||||
return 0, fmt.Errorf("adding port mapping: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if assignedLiftetime != lifetime {
|
_, _, assignedUDPExternalPort, assignedLifetime, err :=
|
||||||
logger.Warn(fmt.Sprintf("assigned lifetime %s differs"+
|
client.AddPortMapping(ctx, gateway, "udp",
|
||||||
" from requested lifetime %s",
|
internalPort, externalPort, lifetime)
|
||||||
assignedLiftetime, lifetime))
|
if err != nil {
|
||||||
}
|
return 0, fmt.Errorf("adding UDP port mapping: %w", err)
|
||||||
|
|
||||||
port = assignedExternalPort
|
|
||||||
}
|
}
|
||||||
|
checkLifetime(logger, "UDP", lifetime, assignedLifetime)
|
||||||
|
|
||||||
|
_, _, assignedTCPExternalPort, assignedLifetime, err :=
|
||||||
|
client.AddPortMapping(ctx, gateway, "tcp",
|
||||||
|
internalPort, externalPort, lifetime)
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("adding TCP port mapping: %w", err)
|
||||||
|
}
|
||||||
|
checkLifetime(logger, "TCP", lifetime, assignedLifetime)
|
||||||
|
|
||||||
|
checkExternalPorts(logger, assignedUDPExternalPort, assignedTCPExternalPort)
|
||||||
|
port = assignedTCPExternalPort
|
||||||
|
|
||||||
return port, nil
|
return port, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func checkLifetime(logger utils.Logger, protocol string,
|
||||||
|
requested, actual time.Duration) {
|
||||||
|
if requested != actual {
|
||||||
|
logger.Warn(fmt.Sprintf("assigned %s port lifetime %s differs"+
|
||||||
|
" from requested lifetime %s", strings.ToUpper(protocol),
|
||||||
|
actual, requested))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkExternalPorts(logger utils.Logger, udpPort, tcpPort uint16) {
|
||||||
|
if udpPort != tcpPort {
|
||||||
|
logger.Warn(fmt.Sprintf("UDP external port %d differs from TCP external port %d",
|
||||||
|
udpPort, tcpPort))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (p *Provider) KeepPortForward(ctx context.Context, port uint16,
|
func (p *Provider) KeepPortForward(ctx context.Context, port uint16,
|
||||||
gateway netip.Addr, _ string, logger utils.Logger) (err error) {
|
gateway netip.Addr, _ string, logger utils.Logger) (err error) {
|
||||||
client := natpmp.New()
|
client := natpmp.New()
|
||||||
|
|||||||
Reference in New Issue
Block a user