Skip to content

Commit

Permalink
Use internal IP instead of server entry IP
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Jul 29, 2024
1 parent bb10b9f commit 5cc29a7
Show file tree
Hide file tree
Showing 14 changed files with 42 additions and 44 deletions.
1 change: 1 addition & 0 deletions internal/portforward/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Service interface {

type Routing interface {
VPNLocalGatewayIP(vpnInterface string) (gateway netip.Addr, err error)
AssignedIP(interfaceName string, family int) (ip netip.Addr, err error)
}

type PortAllower interface {
Expand Down
1 change: 1 addition & 0 deletions internal/portforward/service/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type PortAllower interface {

type Routing interface {
VPNLocalGatewayIP(vpnInterface string) (gateway netip.Addr, err error)
AssignedIP(interfaceName string, family int) (ip netip.Addr, err error)
}

type Logger interface {
Expand Down
12 changes: 3 additions & 9 deletions internal/portforward/service/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package service
import (
"errors"
"fmt"
"net/netip"

"github.com/qdm12/gluetun/internal/constants/providers"
"github.com/qdm12/gosettings"
Expand All @@ -13,10 +12,9 @@ type Settings struct {
Enabled *bool
PortForwarder PortForwarder
Filepath string
Interface string // needed for PIA and ProtonVPN, tun0 for example
ServerName string // needed for PIA
ServerIP netip.Addr // needed for PrivateVPN
CanPortForward bool // needed for PIA
Interface string // needed for PIA, PrivateVPN and ProtonVPN, tun0 for example
ServerName string // needed for PIA
CanPortForward bool // needed for PIA
ListeningPort uint16
Username string // needed for PIA
Password string // needed for PIA
Expand All @@ -28,7 +26,6 @@ func (s Settings) Copy() (copied Settings) {
copied.Filepath = s.Filepath
copied.Interface = s.Interface
copied.ServerName = s.ServerName
copied.ServerIP = s.ServerIP
copied.CanPortForward = s.CanPortForward
copied.ListeningPort = s.ListeningPort
copied.Username = s.Username
Expand All @@ -42,7 +39,6 @@ func (s *Settings) OverrideWith(update Settings) {
s.Filepath = gosettings.OverrideWithComparable(s.Filepath, update.Filepath)
s.Interface = gosettings.OverrideWithComparable(s.Interface, update.Interface)
s.ServerName = gosettings.OverrideWithComparable(s.ServerName, update.ServerName)
s.ServerIP = gosettings.OverrideWithComparable(s.ServerIP, update.ServerIP)
s.CanPortForward = gosettings.OverrideWithComparable(s.CanPortForward, update.CanPortForward)
s.ListeningPort = gosettings.OverrideWithComparable(s.ListeningPort, update.ListeningPort)
s.Username = gosettings.OverrideWithComparable(s.Username, update.Username)
Expand Down Expand Up @@ -86,8 +82,6 @@ func (s *Settings) Validate(forStartup bool) (err error) {
case s.Password == "":
return fmt.Errorf("%w", ErrPasswordNotSet)
}
case s.PortForwarder.Name() == providers.Privatevpn && !s.ServerIP.IsValid():
return fmt.Errorf("%w", ErrServerIPNotSet)
}
return nil
}
12 changes: 11 additions & 1 deletion internal/portforward/service/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"github.com/qdm12/gluetun/internal/netlink"
"github.com/qdm12/gluetun/internal/provider/utils"
)

Expand All @@ -22,12 +23,21 @@ func (s *Service) Start(ctx context.Context) (runError <-chan error, err error)
return nil, fmt.Errorf("getting VPN local gateway IP: %w", err)
}

family := netlink.FamilyV4
if gateway.Is6() {
family = netlink.FamilyV6
}
internalIP, err := s.routing.AssignedIP(s.settings.Interface, family)
if err != nil {
return nil, fmt.Errorf("getting VPN assigned IP address: %w", err)
}

obj := utils.PortForwardObjects{
Logger: s.logger,
Gateway: gateway,
InternalIP: internalIP,
Client: s.client,
ServerName: s.settings.ServerName,
ServerIP: s.settings.ServerIP,
CanPortForward: s.settings.CanPortForward,
Username: s.settings.Username,
Password: s.settings.Password,
Expand Down
6 changes: 3 additions & 3 deletions internal/provider/privatevpn/portforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var (
// It returns 0 if all ports are to forwarded on a dedicated server IP.
func (p *Provider) PortForward(ctx context.Context, objects utils.PortForwardObjects) (
port uint16, err error) {
url := "https://connect.pvdatanet.com/v3/Api/port?ip[]=" + objects.ServerIP.String()
url := "https://connect.pvdatanet.com/v3/Api/port?ip[]=" + objects.InternalIP.String()
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return 0, fmt.Errorf("creating HTTP request: %w", err)
Expand All @@ -51,8 +51,8 @@ func (p *Provider) PortForward(ctx context.Context, objects utils.PortForwardObj
if err != nil {
return 0, fmt.Errorf("decoding JSON response: %w", err)
} else if !data.Supported {
return 0, fmt.Errorf("%w: for server IP %s",
common.ErrPortForwardNotSupported, objects.ServerIP)
return 0, fmt.Errorf("%w: for VPN internal IP %s",
common.ErrPortForwardNotSupported, objects.InternalIP)
}

portString := regexPort.FindString(data.Status)
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/utils/portforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ type PortForwardObjects struct {
// Gateway is the VPN gateway IP address, used by Private Internet Access
// and ProtonVPN.
Gateway netip.Addr
// InternalIP is the VPN internal IP address assigned, used by Perfect Privacy.
InternalIP netip.Addr
// Client is used to query the VPN gateway for Private Internet Access.
Client *http.Client
// ServerName is used by Private Internet Access for port forwarding.
ServerName string
// ServerIP is used by PrivateVPN for port forwarding.
ServerIP netip.Addr
// CanPortForward is used by Private Internet Access for port forwarding.
CanPortForward bool
// Username is used by Private Internet Access for port forwarding.
Expand Down
2 changes: 1 addition & 1 deletion internal/routing/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (r *Routing) DefaultRoutes() (defaultRoutes []DefaultRoute, err error) {
if route.Gw.Is4() {
family = netlink.FamilyV4
}
defaultRoute.AssignedIP, err = r.assignedIP(defaultRoute.NetInterface, family)
defaultRoute.AssignedIP, err = r.AssignedIP(defaultRoute.NetInterface, family)
if err != nil {
return nil, fmt.Errorf("getting assigned IP of %s: %w", defaultRoute.NetInterface, err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/routing/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func ipMatchesFamily(ip netip.Addr, family int) bool {
(family == netlink.FamilyV6 && ip.Is6())
}

func (r *Routing) assignedIP(interfaceName string, family int) (ip netip.Addr, err error) {
func (r *Routing) AssignedIP(interfaceName string, family int) (ip netip.Addr, err error) {
iface, err := net.InterfaceByName(interfaceName)
if err != nil {
return ip, fmt.Errorf("network interface %s not found: %w", interfaceName, err)
Expand Down
2 changes: 1 addition & 1 deletion internal/routing/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (r *Routing) LocalNetworks() (localNetworks []LocalNetwork, err error) {
if localNet.IPNet.Addr().Is4() {
family = netlink.FamilyV4
}
ip, err := r.assignedIP(localNet.InterfaceName, family)
ip, err := r.AssignedIP(localNet.InterfaceName, family)
if err != nil {
return localNetworks, err
}
Expand Down
15 changes: 7 additions & 8 deletions internal/vpn/openvpn.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package vpn
import (
"context"
"fmt"
"net/netip"

"github.com/qdm12/gluetun/internal/configuration/settings"
"github.com/qdm12/gluetun/internal/openvpn"
Expand All @@ -17,37 +16,37 @@ func setupOpenVPN(ctx context.Context, fw Firewall,
openvpnConf OpenVPN, providerConf provider.Provider,
settings settings.VPN, ipv6Supported bool, starter command.Starter,
logger openvpn.Logger) (runner *openvpn.Runner, serverName string,
serverIP netip.Addr, canPortForward bool, err error) {
canPortForward bool, err error) {
connection, err := providerConf.GetConnection(settings.Provider.ServerSelection, ipv6Supported)
if err != nil {
return nil, "", netip.Addr{}, false, fmt.Errorf("finding a valid server connection: %w", err)
return nil, "", false, fmt.Errorf("finding a valid server connection: %w", err)
}

lines := providerConf.OpenVPNConfig(connection, settings.OpenVPN, ipv6Supported)

if err := openvpnConf.WriteConfig(lines); err != nil {
return nil, "", netip.Addr{}, false, fmt.Errorf("writing configuration to file: %w", err)
return nil, "", false, fmt.Errorf("writing configuration to file: %w", err)
}

if *settings.OpenVPN.User != "" {
err := openvpnConf.WriteAuthFile(*settings.OpenVPN.User, *settings.OpenVPN.Password)
if err != nil {
return nil, "", netip.Addr{}, false, fmt.Errorf("writing auth to file: %w", err)
return nil, "", false, fmt.Errorf("writing auth to file: %w", err)
}
}

if *settings.OpenVPN.KeyPassphrase != "" {
err := openvpnConf.WriteAskPassFile(*settings.OpenVPN.KeyPassphrase)
if err != nil {
return nil, "", netip.Addr{}, false, fmt.Errorf("writing askpass file: %w", err)
return nil, "", false, fmt.Errorf("writing askpass file: %w", err)
}
}

if err := fw.SetVPNConnection(ctx, connection, settings.OpenVPN.Interface); err != nil {
return nil, "", netip.Addr{}, false, fmt.Errorf("allowing VPN connection through firewall: %w", err)
return nil, "", false, fmt.Errorf("allowing VPN connection through firewall: %w", err)
}

runner = openvpn.NewRunner(settings.OpenVPN, starter, logger)

return runner, connection.ServerName, connection.IP, connection.PortForward, nil
return runner, connection.ServerName, connection.PortForward, nil
}
1 change: 0 additions & 1 deletion internal/vpn/portforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func (l *Loop) startPortForwarding(data tunnelUpData) (err error) {
PortForwarder: data.portForwarder,
Interface: data.vpnIntf,
ServerName: data.serverName,
ServerIP: data.serverIP,
CanPortForward: data.canPortForward,
Username: data.username,
Password: data.password,
Expand Down
7 changes: 2 additions & 5 deletions internal/vpn/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package vpn

import (
"context"
"net/netip"

"github.com/qdm12/gluetun/internal/constants"
"github.com/qdm12/gluetun/internal/constants/vpn"
Expand Down Expand Up @@ -30,17 +29,16 @@ func (l *Loop) Run(ctx context.Context, done chan<- struct{}) {
Run(ctx context.Context, waitError chan<- error, tunnelReady chan<- struct{})
}
var serverName, vpnInterface string
var serverIP netip.Addr
var canPortForward bool
var err error
subLogger := l.logger.New(log.SetComponent(settings.Type))
if settings.Type == vpn.OpenVPN {
vpnInterface = settings.OpenVPN.Interface
vpnRunner, serverName, serverIP, canPortForward, err = setupOpenVPN(ctx, l.fw,
vpnRunner, serverName, canPortForward, err = setupOpenVPN(ctx, l.fw,
l.openvpnConf, providerConf, settings, l.ipv6Supported, l.starter, subLogger)
} else { // Wireguard
vpnInterface = settings.Wireguard.Interface
vpnRunner, serverName, serverIP, canPortForward, err = setupWireguard(ctx, l.netLinker, l.fw,
vpnRunner, serverName, canPortForward, err = setupWireguard(ctx, l.netLinker, l.fw,
providerConf, settings, l.ipv6Supported, subLogger)
}
if err != nil {
Expand All @@ -49,7 +47,6 @@ func (l *Loop) Run(ctx context.Context, done chan<- struct{}) {
}
tunnelUpData := tunnelUpData{
serverName: serverName,
serverIP: serverIP,
canPortForward: canPortForward,
portForwarder: portForwarder,
vpnIntf: vpnInterface,
Expand Down
10 changes: 4 additions & 6 deletions internal/vpn/tunnelup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package vpn

import (
"context"
"net/netip"

"github.com/qdm12/gluetun/internal/constants"
"github.com/qdm12/gluetun/internal/version"
Expand All @@ -11,11 +10,10 @@ import (
type tunnelUpData struct {
// Port forwarding
vpnIntf string
serverName string // used for PIA
serverIP netip.Addr // used for PrivateVPN
canPortForward bool // used for PIA
username string // used for PIA
password string // used for PIA
serverName string // used for PIA
canPortForward bool // used for PIA
username string // used for PIA
password string // used for PIA
portForwarder PortForwarder
}

Expand Down
11 changes: 5 additions & 6 deletions internal/vpn/wireguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package vpn
import (
"context"
"fmt"
"net/netip"

"github.com/qdm12/gluetun/internal/configuration/settings"
"github.com/qdm12/gluetun/internal/provider"
Expand All @@ -17,10 +16,10 @@ import (
func setupWireguard(ctx context.Context, netlinker NetLinker,
fw Firewall, providerConf provider.Provider,
settings settings.VPN, ipv6Supported bool, logger wireguard.Logger) (
wireguarder *wireguard.Wireguard, serverName string, serverIP netip.Addr, canPortForward bool, err error) {
wireguarder *wireguard.Wireguard, serverName string, canPortForward bool, err error) {
connection, err := providerConf.GetConnection(settings.Provider.ServerSelection, ipv6Supported)
if err != nil {
return nil, "", netip.Addr{}, false, fmt.Errorf("finding a VPN server: %w", err)
return nil, "", false, fmt.Errorf("finding a VPN server: %w", err)
}

wireguardSettings := utils.BuildWireguardSettings(connection, settings.Wireguard, ipv6Supported)
Expand All @@ -31,13 +30,13 @@ func setupWireguard(ctx context.Context, netlinker NetLinker,

wireguarder, err = wireguard.New(wireguardSettings, netlinker, logger)
if err != nil {
return nil, "", netip.Addr{}, false, fmt.Errorf("creating Wireguard: %w", err)
return nil, "", false, fmt.Errorf("creating Wireguard: %w", err)
}

err = fw.SetVPNConnection(ctx, connection, settings.Wireguard.Interface)
if err != nil {
return nil, "", netip.Addr{}, false, fmt.Errorf("setting firewall: %w", err)
return nil, "", false, fmt.Errorf("setting firewall: %w", err)
}

return wireguarder, connection.ServerName, connection.IP, connection.PortForward, nil
return wireguarder, connection.ServerName, connection.PortForward, nil
}

0 comments on commit 5cc29a7

Please sign in to comment.