Files
gluetun/internal/provider/mullvad/connection.go
Quentin McGaw aa729515b9 chore(models): streamline all server models IPs (#942)
- Use `IPs []net.IP` for all server models
- Use `ips` JSON field for all server models
- Merge IPv4 and IPv6 addresses together for Mullvad
2022-04-17 16:18:34 +00:00

49 lines
1.2 KiB
Go

package mullvad
import (
"github.com/qdm12/gluetun/internal/configuration/settings"
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/provider/utils"
)
func (m *Mullvad) GetConnection(selection settings.ServerSelection) (
connection models.Connection, err error) {
port := getPort(selection)
protocol := utils.GetProtocol(selection)
servers, err := m.filterServers(selection)
if err != nil {
return connection, err
}
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
}
connection := models.Connection{
Type: selection.VPN,
IP: IP,
Port: port,
Protocol: protocol,
PubKey: server.WgPubKey, // Wireguard only
}
connections = append(connections, connection)
}
}
return utils.PickConnection(connections, selection, m.randSource)
}
func getPort(selection settings.ServerSelection) (port uint16) {
const (
defaultOpenVPNTCP = 443
defaultOpenVPNUDP = 1194
defaultWireguard = 51820
)
return utils.GetPort(selection, defaultOpenVPNTCP,
defaultOpenVPNUDP, defaultWireguard)
}