Files
gluetun/internal/provider/hidemyass/updater/servers.go
Quentin McGaw 90c6c8485b chore(updater): common GetServers signature
- Log warnings when running outside of CLI mode
- Remove updater CLI bool setting
- Warnings are logged in updating functions
2022-05-28 20:58:50 +00:00

71 lines
1.6 KiB
Go

// Package hidemyass contains code to obtain the server information
// for the HideMyAss provider.
package hidemyass
import (
"context"
"errors"
"fmt"
"github.com/qdm12/gluetun/internal/constants/vpn"
"github.com/qdm12/gluetun/internal/models"
)
var ErrNotEnoughServers = errors.New("not enough servers found")
func (u *Updater) GetServers(ctx context.Context, minServers int) (
servers []models.Server, err error) {
tcpHostToURL, udpHostToURL, err := getAllHostToURL(ctx, u.client)
if err != nil {
return nil, err
}
hosts := getUniqueHosts(tcpHostToURL, udpHostToURL)
if len(hosts) < minServers {
return nil, fmt.Errorf("%w: %d and expected at least %d",
ErrNotEnoughServers, len(hosts), minServers)
}
hostToIPs, warnings, err := resolveHosts(ctx, u.presolver, hosts, minServers)
for _, warning := range warnings {
u.warner.Warn(warning)
}
if err != nil {
return nil, err
}
servers = make([]models.Server, 0, len(hostToIPs))
for host, IPs := range hostToIPs {
tcpURL, tcp := tcpHostToURL[host]
udpURL, udp := udpHostToURL[host]
// These two are only used to extract the country, region and city.
var url, protocol string
if tcp {
url = tcpURL
protocol = "TCP"
} else if udp {
url = udpURL
protocol = "UDP"
}
country, region, city := parseOpenvpnURL(url, protocol)
server := models.Server{
VPN: vpn.OpenVPN,
Country: country,
Region: region,
City: city,
Hostname: host,
IPs: IPs,
TCP: tcp,
UDP: udp,
}
servers = append(servers, server)
}
sortServers(servers)
return servers, nil
}