chore: simplify provider GetConnection
This commit is contained in:
@@ -8,36 +8,6 @@ import (
|
||||
|
||||
func (w *Wevpn) 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,
|
||||
}
|
||||
connections = append(connections, connection)
|
||||
}
|
||||
}
|
||||
|
||||
return utils.PickConnection(connections, selection, w.randSource)
|
||||
}
|
||||
|
||||
func getPort(selection settings.ServerSelection) (port uint16) {
|
||||
const (
|
||||
defaultOpenVPNTCP = 1195
|
||||
defaultOpenVPNUDP = 1194
|
||||
defaultWireguard = 0 // Wireguard not supported
|
||||
)
|
||||
return utils.GetPort(selection, defaultOpenVPNTCP,
|
||||
defaultOpenVPNUDP, defaultWireguard)
|
||||
defaults := utils.NewConnectionDefaults(1195, 1194, 0) //nolint:gomnd
|
||||
return utils.GetConnection(w.servers, selection, defaults, w.randSource)
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ func Test_Wevpn_GetConnection(t *testing.T) {
|
||||
VPN: vpn.OpenVPN,
|
||||
}.WithDefaults(providers.Wevpn),
|
||||
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{
|
||||
@@ -74,6 +74,7 @@ func Test_Wevpn_GetConnection(t *testing.T) {
|
||||
Type: vpn.OpenVPN,
|
||||
IP: net.IPv4(2, 2, 2, 2),
|
||||
Port: 1194,
|
||||
Hostname: "b",
|
||||
Protocol: constants.UDP,
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user