chore: simplify provider GetConnection

This commit is contained in:
Quentin McGaw
2022-04-19 14:28:57 +00:00
parent 306d8494d6
commit 0c0f1663b1
36 changed files with 243 additions and 707 deletions

View File

@@ -6,40 +6,8 @@ import (
"github.com/qdm12/gluetun/internal/provider/utils"
)
func (w *Windscribe) GetConnection(selection settings.ServerSelection) (
func (p *Windscribe) GetConnection(selection settings.ServerSelection) (
connection models.Connection, err error) {
port := getPort(selection)
protocol := utils.GetProtocol(selection)
servers, err := utils.FilterServers(w.servers, selection)
if err != nil {
return connection, err
}
var connections []models.Connection
for _, server := range servers {
for _, IP := range server.IPs {
connection := models.Connection{
Type: selection.VPN,
IP: IP,
Port: port,
Protocol: protocol,
Hostname: server.OvpnX509,
PubKey: server.WgPubKey,
}
connections = append(connections, connection)
}
}
return utils.PickConnection(connections, selection, w.randSource)
}
func getPort(selection settings.ServerSelection) (port uint16) {
const (
defaultOpenVPNTCP = 443
defaultOpenVPNUDP = 1194
defaultWireguard = 1194
)
return utils.GetPort(selection, defaultOpenVPNTCP,
defaultOpenVPNUDP, defaultWireguard)
defaults := utils.NewConnectionDefaults(443, 1194, 1194) //nolint:gomnd
return utils.GetConnection(p.servers, selection, defaults, p.randSource)
}

View File

@@ -27,7 +27,7 @@ func Test_Windscribe_GetConnection(t *testing.T) {
"no server available": {
selection: settings.ServerSelection{}.WithDefaults(providers.Windscribe),
errWrapped: utils.ErrNoServerFound,
errMessage: "no server found: for VPN openvpn; protocol udp",
errMessage: "cannot filter servers: no server found: for VPN openvpn; protocol udp",
},
"no filter": {
servers: []models.Server{
@@ -72,6 +72,7 @@ func Test_Windscribe_GetConnection(t *testing.T) {
Type: vpn.OpenVPN,
IP: net.IPv4(2, 2, 2, 2),
Port: 1194,
Hostname: "b",
Protocol: constants.UDP,
},
},