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:
@@ -33,9 +33,5 @@ func (c *Cyberghost) GetConnection(selection configuration.ServerSelection) (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if selection.TargetIP != nil {
|
return utils.PickConnection(connections, selection, c.randSource)
|
||||||
return utils.GetTargetIPConnection(connections, selection.TargetIP)
|
|
||||||
}
|
|
||||||
|
|
||||||
return utils.PickRandomConnection(connections, c.randSource), nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,9 +33,5 @@ func (f *Fastestvpn) GetConnection(selection configuration.ServerSelection) (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if selection.TargetIP != nil {
|
return utils.PickConnection(connections, selection, f.randSource)
|
||||||
return utils.GetTargetIPConnection(connections, selection.TargetIP)
|
|
||||||
}
|
|
||||||
|
|
||||||
return utils.PickRandomConnection(connections, f.randSource), nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,9 +38,5 @@ func (h *HideMyAss) GetConnection(selection configuration.ServerSelection) (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if selection.TargetIP != nil {
|
return utils.PickConnection(connections, selection, h.randSource)
|
||||||
return utils.GetTargetIPConnection(connections, selection.TargetIP)
|
|
||||||
}
|
|
||||||
|
|
||||||
return utils.PickRandomConnection(connections, h.randSource), nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,9 +38,5 @@ func (i *Ipvanish) GetConnection(selection configuration.ServerSelection) (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if selection.TargetIP != nil {
|
return utils.PickConnection(connections, selection, i.randSource)
|
||||||
return utils.GetTargetIPConnection(connections, selection.TargetIP)
|
|
||||||
}
|
|
||||||
|
|
||||||
return utils.PickRandomConnection(connections, i.randSource), nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,11 +31,7 @@ func (i *Ivpn) GetConnection(selection configuration.ServerSelection) (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if selection.TargetIP != nil {
|
return utils.PickConnection(connections, selection, i.randSource)
|
||||||
return utils.GetTargetIPConnection(connections, selection.TargetIP)
|
|
||||||
}
|
|
||||||
|
|
||||||
return utils.PickRandomConnection(connections, i.randSource), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getPort(selection configuration.ServerSelection) (port uint16) {
|
func getPort(selection configuration.ServerSelection) (port uint16) {
|
||||||
|
|||||||
@@ -30,11 +30,7 @@ func (m *Mullvad) GetConnection(selection configuration.ServerSelection) (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if selection.TargetIP != nil {
|
return utils.PickConnection(connections, selection, m.randSource)
|
||||||
return utils.GetTargetIPConnection(connections, selection.TargetIP)
|
|
||||||
}
|
|
||||||
|
|
||||||
return utils.PickRandomConnection(connections, m.randSource), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getPort(selection configuration.ServerSelection) (port uint16) {
|
func getPort(selection configuration.ServerSelection) (port uint16) {
|
||||||
|
|||||||
@@ -32,9 +32,5 @@ func (n *Nordvpn) GetConnection(selection configuration.ServerSelection) (
|
|||||||
connections[i] = connection
|
connections[i] = connection
|
||||||
}
|
}
|
||||||
|
|
||||||
if selection.TargetIP != nil {
|
return utils.PickConnection(connections, selection, n.randSource)
|
||||||
return utils.GetTargetIPConnection(connections, selection.TargetIP)
|
|
||||||
}
|
|
||||||
|
|
||||||
return utils.PickRandomConnection(connections, n.randSource), nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,9 +37,5 @@ func (p *Privado) GetConnection(selection configuration.ServerSelection) (
|
|||||||
connections[i] = connection
|
connections[i] = connection
|
||||||
}
|
}
|
||||||
|
|
||||||
if selection.TargetIP != nil {
|
return utils.PickConnection(connections, selection, p.randSource)
|
||||||
return utils.GetTargetIPConnection(connections, selection.TargetIP)
|
|
||||||
}
|
|
||||||
|
|
||||||
return utils.PickRandomConnection(connections, p.randSource), nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,15 +38,5 @@ func (p *PIA) GetConnection(selection configuration.ServerSelection) (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if selection.TargetIP != nil {
|
return utils.PickConnection(connections, selection, p.randSource)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,9 +34,5 @@ func (p *Privatevpn) GetConnection(selection configuration.ServerSelection) (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if selection.TargetIP != nil {
|
return utils.PickConnection(connections, selection, p.randSource)
|
||||||
return utils.GetTargetIPConnection(connections, selection.TargetIP)
|
|
||||||
}
|
|
||||||
|
|
||||||
return utils.PickRandomConnection(connections, p.randSource), nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,9 +34,5 @@ func (p *Protonvpn) GetConnection(selection configuration.ServerSelection) (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if selection.TargetIP != nil {
|
return utils.PickConnection(connections, selection, p.randSource)
|
||||||
return utils.GetTargetIPConnection(connections, selection.TargetIP)
|
|
||||||
}
|
|
||||||
|
|
||||||
return utils.PickRandomConnection(connections, p.randSource), nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,9 +34,5 @@ func (p *Purevpn) GetConnection(selection configuration.ServerSelection) (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if selection.TargetIP != nil {
|
return utils.PickConnection(connections, selection, p.randSource)
|
||||||
return utils.GetTargetIPConnection(connections, selection.TargetIP)
|
|
||||||
}
|
|
||||||
|
|
||||||
return utils.PickRandomConnection(connections, p.randSource), nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,9 +34,5 @@ func (s *Surfshark) GetConnection(selection configuration.ServerSelection) (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if selection.TargetIP != nil {
|
return utils.PickConnection(connections, selection, s.randSource)
|
||||||
return utils.GetTargetIPConnection(connections, selection.TargetIP)
|
|
||||||
}
|
|
||||||
|
|
||||||
return utils.PickRandomConnection(connections, s.randSource), nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,9 +37,5 @@ func (t *Torguard) GetConnection(selection configuration.ServerSelection) (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if selection.TargetIP != nil {
|
return utils.PickConnection(connections, selection, t.randSource)
|
||||||
return utils.GetTargetIPConnection(connections, selection.TargetIP)
|
|
||||||
}
|
|
||||||
|
|
||||||
return utils.PickRandomConnection(connections, t.randSource), nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,51 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"net"
|
||||||
|
|
||||||
|
"github.com/qdm12/gluetun/internal/configuration"
|
||||||
|
"github.com/qdm12/gluetun/internal/constants"
|
||||||
"github.com/qdm12/gluetun/internal/models"
|
"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 {
|
source rand.Source) models.Connection {
|
||||||
return connections[rand.New(source).Intn(len(connections))] //nolint:gosec
|
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))
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,19 +8,19 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_PickRandomConnection(t *testing.T) {
|
func Test_pickRandomConnection(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
connections := []models.Connection{
|
connections := []models.Connection{
|
||||||
{Port: 1}, {Port: 2}, {Port: 3}, {Port: 4},
|
{Port: 1}, {Port: 2}, {Port: 3}, {Port: 4},
|
||||||
}
|
}
|
||||||
source := rand.NewSource(0)
|
source := rand.NewSource(0)
|
||||||
|
|
||||||
connection := PickRandomConnection(connections, source)
|
connection := pickRandomConnection(connections, source)
|
||||||
assert.Equal(t, models.Connection{Port: 3}, connection)
|
assert.Equal(t, models.Connection{Port: 3}, connection)
|
||||||
|
|
||||||
connection = PickRandomConnection(connections, source)
|
connection = pickRandomConnection(connections, source)
|
||||||
assert.Equal(t, models.Connection{Port: 3}, connection)
|
assert.Equal(t, models.Connection{Port: 3}, connection)
|
||||||
|
|
||||||
connection = PickRandomConnection(connections, source)
|
connection = pickRandomConnection(connections, source)
|
||||||
assert.Equal(t, models.Connection{Port: 2}, connection)
|
assert.Equal(t, models.Connection{Port: 2}, connection)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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))
|
|
||||||
}
|
|
||||||
@@ -37,9 +37,5 @@ func (p *Provider) GetConnection(selection configuration.ServerSelection) (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if selection.TargetIP != nil {
|
return utils.PickConnection(connections, selection, p.randSource)
|
||||||
return utils.GetTargetIPConnection(connections, selection.TargetIP)
|
|
||||||
}
|
|
||||||
|
|
||||||
return utils.PickRandomConnection(connections, p.randSource), nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,9 +38,5 @@ func (v *Vyprvpn) GetConnection(selection configuration.ServerSelection) (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if selection.TargetIP != nil {
|
return utils.PickConnection(connections, selection, v.randSource)
|
||||||
return utils.GetTargetIPConnection(connections, selection.TargetIP)
|
|
||||||
}
|
|
||||||
|
|
||||||
return utils.PickRandomConnection(connections, v.randSource), nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,11 +31,7 @@ func (w *Windscribe) GetConnection(selection configuration.ServerSelection) (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if selection.TargetIP != nil {
|
return utils.PickConnection(connections, selection, w.randSource)
|
||||||
return utils.GetTargetIPConnection(connections, selection.TargetIP)
|
|
||||||
}
|
|
||||||
|
|
||||||
return utils.PickRandomConnection(connections, w.randSource), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getPort(selection configuration.ServerSelection) (port uint16) {
|
func getPort(selection configuration.ServerSelection) (port uint16) {
|
||||||
|
|||||||
Reference in New Issue
Block a user