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 Aug 3, 2024
1 parent ee41434 commit a3b2c67
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 42 deletions.
13 changes: 3 additions & 10 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 All @@ -54,7 +50,6 @@ var (
ErrServerNameNotSet = errors.New("server name not set")
ErrUsernameNotSet = errors.New("username not set")
ErrPasswordNotSet = errors.New("password not set")
ErrServerIPNotSet = errors.New("server ip not set")
ErrFilepathNotSet = errors.New("file path not set")
ErrInterfaceNotSet = errors.New("interface not set")
)
Expand Down Expand Up @@ -86,8 +81,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
}
1 change: 0 additions & 1 deletion internal/portforward/service/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ func (s *Service) Start(ctx context.Context) (runError <-chan error, err error)
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
2 changes: 0 additions & 2 deletions internal/provider/utils/portforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ type PortForwardObjects struct {
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
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 a3b2c67

Please sign in to comment.