- settings: get filter choices from storage for settings validation - updater: update servers to the storage - storage: minimal deep copying and data duplication - storage: add merged servers mutex for thread safety - connection: filter servers in storage - formatter: format servers to Markdown in storage - PIA: get server by name from storage directly - Updater: get servers count from storage directly - Updater: equality check done in storage, fix #882
76 lines
1.9 KiB
Go
76 lines
1.9 KiB
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"math/rand"
|
|
|
|
"github.com/qdm12/gluetun/internal/configuration/settings"
|
|
"github.com/qdm12/gluetun/internal/constants/vpn"
|
|
"github.com/qdm12/gluetun/internal/models"
|
|
)
|
|
|
|
type ConnectionDefaults struct {
|
|
OpenVPNTCPPort uint16
|
|
OpenVPNUDPPort uint16
|
|
WireguardPort uint16
|
|
}
|
|
|
|
func NewConnectionDefaults(openvpnTCPPort, openvpnUDPPort,
|
|
wireguardPort uint16) ConnectionDefaults {
|
|
return ConnectionDefaults{
|
|
OpenVPNTCPPort: openvpnTCPPort,
|
|
OpenVPNUDPPort: openvpnUDPPort,
|
|
WireguardPort: wireguardPort,
|
|
}
|
|
}
|
|
|
|
type Storage interface {
|
|
FilterServers(provider string, selection settings.ServerSelection) (
|
|
servers []models.Server, err error)
|
|
}
|
|
|
|
func GetConnection(provider string,
|
|
storage Storage,
|
|
selection settings.ServerSelection,
|
|
defaults ConnectionDefaults,
|
|
randSource rand.Source) (
|
|
connection models.Connection, err error) {
|
|
servers, err := storage.FilterServers(provider, selection)
|
|
if err != nil {
|
|
return connection, fmt.Errorf("cannot filter servers: %w", err)
|
|
}
|
|
|
|
protocol := getProtocol(selection)
|
|
port := getPort(selection, defaults.OpenVPNTCPPort,
|
|
defaults.OpenVPNUDPPort, defaults.WireguardPort)
|
|
|
|
connections := make([]models.Connection, 0, len(servers))
|
|
for _, server := range servers {
|
|
for _, ip := range server.IPs {
|
|
if ip.To4() == nil {
|
|
// do not use IPv6 connections for now
|
|
continue
|
|
}
|
|
|
|
hostname := server.Hostname
|
|
if selection.VPN == vpn.OpenVPN && server.OvpnX509 != "" {
|
|
// For Windscribe where hostname and
|
|
// OpenVPN x509 are not the same.
|
|
hostname = server.OvpnX509
|
|
}
|
|
|
|
connection := models.Connection{
|
|
Type: selection.VPN,
|
|
IP: ip,
|
|
Port: port,
|
|
Protocol: protocol,
|
|
Hostname: hostname,
|
|
PubKey: server.WgPubKey, // Wireguard
|
|
}
|
|
connections = append(connections, connection)
|
|
}
|
|
}
|
|
|
|
return pickConnection(connections, selection, randSource)
|
|
}
|