Feat: OPENVPN_TARGET_IP overrides IP

- Check target IP matches a server for Wireguard since we need the public key
- Streamline connection picking for all providers
This commit is contained in:
Quentin McGaw (desktop)
2021-08-28 19:07:44 +00:00
parent c6fedd9214
commit 119cac5a67
20 changed files with 61 additions and 118 deletions

View File

@@ -33,9 +33,5 @@ func (c *Cyberghost) GetConnection(selection configuration.ServerSelection) (
}
}
if selection.TargetIP != nil {
return utils.GetTargetIPConnection(connections, selection.TargetIP)
}
return utils.PickRandomConnection(connections, c.randSource), nil
return utils.PickConnection(connections, selection, c.randSource)
}

View File

@@ -33,9 +33,5 @@ func (f *Fastestvpn) GetConnection(selection configuration.ServerSelection) (
}
}
if selection.TargetIP != nil {
return utils.GetTargetIPConnection(connections, selection.TargetIP)
}
return utils.PickRandomConnection(connections, f.randSource), nil
return utils.PickConnection(connections, selection, f.randSource)
}

View File

@@ -38,9 +38,5 @@ func (h *HideMyAss) GetConnection(selection configuration.ServerSelection) (
}
}
if selection.TargetIP != nil {
return utils.GetTargetIPConnection(connections, selection.TargetIP)
}
return utils.PickRandomConnection(connections, h.randSource), nil
return utils.PickConnection(connections, selection, h.randSource)
}

View File

@@ -38,9 +38,5 @@ func (i *Ipvanish) GetConnection(selection configuration.ServerSelection) (
}
}
if selection.TargetIP != nil {
return utils.GetTargetIPConnection(connections, selection.TargetIP)
}
return utils.PickRandomConnection(connections, i.randSource), nil
return utils.PickConnection(connections, selection, i.randSource)
}

View File

@@ -31,11 +31,7 @@ func (i *Ivpn) GetConnection(selection configuration.ServerSelection) (
}
}
if selection.TargetIP != nil {
return utils.GetTargetIPConnection(connections, selection.TargetIP)
}
return utils.PickRandomConnection(connections, i.randSource), nil
return utils.PickConnection(connections, selection, i.randSource)
}
func getPort(selection configuration.ServerSelection) (port uint16) {

View File

@@ -30,11 +30,7 @@ func (m *Mullvad) GetConnection(selection configuration.ServerSelection) (
}
}
if selection.TargetIP != nil {
return utils.GetTargetIPConnection(connections, selection.TargetIP)
}
return utils.PickRandomConnection(connections, m.randSource), nil
return utils.PickConnection(connections, selection, m.randSource)
}
func getPort(selection configuration.ServerSelection) (port uint16) {

View File

@@ -32,9 +32,5 @@ func (n *Nordvpn) GetConnection(selection configuration.ServerSelection) (
connections[i] = connection
}
if selection.TargetIP != nil {
return utils.GetTargetIPConnection(connections, selection.TargetIP)
}
return utils.PickRandomConnection(connections, n.randSource), nil
return utils.PickConnection(connections, selection, n.randSource)
}

View File

@@ -37,9 +37,5 @@ func (p *Privado) GetConnection(selection configuration.ServerSelection) (
connections[i] = connection
}
if selection.TargetIP != nil {
return utils.GetTargetIPConnection(connections, selection.TargetIP)
}
return utils.PickRandomConnection(connections, p.randSource), nil
return utils.PickConnection(connections, selection, p.randSource)
}

View File

@@ -38,15 +38,5 @@ func (p *PIA) GetConnection(selection configuration.ServerSelection) (
}
}
if selection.TargetIP != nil {
connection, err = utils.GetTargetIPConnection(connections, selection.TargetIP)
} else {
connection, err = utils.PickRandomConnection(connections, p.randSource), nil
}
if err != nil {
return connection, err
}
return connection, nil
return utils.PickConnection(connections, selection, p.randSource)
}

View File

@@ -34,9 +34,5 @@ func (p *Privatevpn) GetConnection(selection configuration.ServerSelection) (
}
}
if selection.TargetIP != nil {
return utils.GetTargetIPConnection(connections, selection.TargetIP)
}
return utils.PickRandomConnection(connections, p.randSource), nil
return utils.PickConnection(connections, selection, p.randSource)
}

View File

@@ -34,9 +34,5 @@ func (p *Protonvpn) GetConnection(selection configuration.ServerSelection) (
}
}
if selection.TargetIP != nil {
return utils.GetTargetIPConnection(connections, selection.TargetIP)
}
return utils.PickRandomConnection(connections, p.randSource), nil
return utils.PickConnection(connections, selection, p.randSource)
}

View File

@@ -34,9 +34,5 @@ func (p *Purevpn) GetConnection(selection configuration.ServerSelection) (
}
}
if selection.TargetIP != nil {
return utils.GetTargetIPConnection(connections, selection.TargetIP)
}
return utils.PickRandomConnection(connections, p.randSource), nil
return utils.PickConnection(connections, selection, p.randSource)
}

View File

@@ -34,9 +34,5 @@ func (s *Surfshark) GetConnection(selection configuration.ServerSelection) (
}
}
if selection.TargetIP != nil {
return utils.GetTargetIPConnection(connections, selection.TargetIP)
}
return utils.PickRandomConnection(connections, s.randSource), nil
return utils.PickConnection(connections, selection, s.randSource)
}

View File

@@ -37,9 +37,5 @@ func (t *Torguard) GetConnection(selection configuration.ServerSelection) (
}
}
if selection.TargetIP != nil {
return utils.GetTargetIPConnection(connections, selection.TargetIP)
}
return utils.PickRandomConnection(connections, t.randSource), nil
return utils.PickConnection(connections, selection, t.randSource)
}

View File

@@ -1,12 +1,51 @@
package utils
import (
"errors"
"fmt"
"math/rand"
"net"
"github.com/qdm12/gluetun/internal/configuration"
"github.com/qdm12/gluetun/internal/constants"
"github.com/qdm12/gluetun/internal/models"
)
func PickRandomConnection(connections []models.Connection,
// PickConnection picks a connection from a pool of connections.
// If the VPN protocol is Wireguard and the target IP is set,
// it finds the connection corresponding to this target IP.
// Otherwise, it picks a random connection from the pool of connections
// and sets the target IP address as the IP if this one is set.
func PickConnection(connections []models.Connection,
selection configuration.ServerSelection, randSource rand.Source) (
connection models.Connection, err error) {
if selection.TargetIP != nil && selection.VPN == constants.Wireguard {
// we need the right public key
return getTargetIPConnection(connections, selection.TargetIP)
}
connection = pickRandomConnection(connections, randSource)
if selection.TargetIP != nil {
connection.IP = selection.TargetIP
}
return connection, nil
}
func pickRandomConnection(connections []models.Connection,
source rand.Source) models.Connection {
return connections[rand.New(source).Intn(len(connections))] //nolint:gosec
}
var errTargetIPNotFound = errors.New("target IP address not found")
func getTargetIPConnection(connections []models.Connection,
targetIP net.IP) (connection models.Connection, err error) {
for _, connection := range connections {
if targetIP.Equal(connection.IP) {
return connection, nil
}
}
return connection, fmt.Errorf("%w: in %d filtered connections",
errTargetIPNotFound, len(connections))
}

View File

@@ -8,19 +8,19 @@ import (
"github.com/stretchr/testify/assert"
)
func Test_PickRandomConnection(t *testing.T) {
func Test_pickRandomConnection(t *testing.T) {
t.Parallel()
connections := []models.Connection{
{Port: 1}, {Port: 2}, {Port: 3}, {Port: 4},
}
source := rand.NewSource(0)
connection := PickRandomConnection(connections, source)
connection := pickRandomConnection(connections, source)
assert.Equal(t, models.Connection{Port: 3}, connection)
connection = PickRandomConnection(connections, source)
connection = pickRandomConnection(connections, source)
assert.Equal(t, models.Connection{Port: 3}, connection)
connection = PickRandomConnection(connections, source)
connection = pickRandomConnection(connections, source)
assert.Equal(t, models.Connection{Port: 2}, connection)
}

View File

@@ -1,22 +0,0 @@
package utils
import (
"errors"
"fmt"
"net"
"github.com/qdm12/gluetun/internal/models"
)
var ErrTargetIPNotFound = errors.New("target IP address not found")
func GetTargetIPConnection(connections []models.Connection,
targetIP net.IP) (connection models.Connection, err error) {
for _, connection := range connections {
if targetIP.Equal(connection.IP) {
return connection, nil
}
}
return connection, fmt.Errorf("%w: in %d filtered connections",
ErrTargetIPNotFound, len(connections))
}

View File

@@ -37,9 +37,5 @@ func (p *Provider) GetConnection(selection configuration.ServerSelection) (
}
}
if selection.TargetIP != nil {
return utils.GetTargetIPConnection(connections, selection.TargetIP)
}
return utils.PickRandomConnection(connections, p.randSource), nil
return utils.PickConnection(connections, selection, p.randSource)
}

View File

@@ -38,9 +38,5 @@ func (v *Vyprvpn) GetConnection(selection configuration.ServerSelection) (
}
}
if selection.TargetIP != nil {
return utils.GetTargetIPConnection(connections, selection.TargetIP)
}
return utils.PickRandomConnection(connections, v.randSource), nil
return utils.PickConnection(connections, selection, v.randSource)
}

View File

@@ -31,11 +31,7 @@ func (w *Windscribe) GetConnection(selection configuration.ServerSelection) (
}
}
if selection.TargetIP != nil {
return utils.GetTargetIPConnection(connections, selection.TargetIP)
}
return utils.PickRandomConnection(connections, w.randSource), nil
return utils.PickConnection(connections, selection, w.randSource)
}
func getPort(selection configuration.ServerSelection) (port uint16) {