- Log warnings when running outside of CLI mode - Remove updater CLI bool setting - Warnings are logged in updating functions
79 lines
1.9 KiB
Go
79 lines
1.9 KiB
Go
// Package protonvpn contains code to obtain the server information
|
|
// for the ProtonVPN provider.
|
|
package protonvpn
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/qdm12/gluetun/internal/constants"
|
|
"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) {
|
|
data, err := fetchAPI(ctx, u.client)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
countryCodes := constants.CountryCodes()
|
|
|
|
var count int
|
|
for _, logicalServer := range data.LogicalServers {
|
|
count += len(logicalServer.Servers)
|
|
}
|
|
|
|
if count < minServers {
|
|
return nil, fmt.Errorf("%w: %d and expected at least %d",
|
|
ErrNotEnoughServers, count, minServers)
|
|
}
|
|
|
|
ipToServer := make(ipToServer, count)
|
|
for _, logicalServer := range data.LogicalServers {
|
|
region := getStringValue(logicalServer.Region)
|
|
city := getStringValue(logicalServer.City)
|
|
name := logicalServer.Name
|
|
for _, physicalServer := range logicalServer.Servers {
|
|
if physicalServer.Status == 0 { // disabled so skip server
|
|
u.warner.Warn("ignoring server " + physicalServer.Domain + " with status 0")
|
|
continue
|
|
}
|
|
|
|
hostname := physicalServer.Domain
|
|
entryIP := physicalServer.EntryIP
|
|
|
|
// Note: for multi-hop use the server name or hostname
|
|
// instead of the country
|
|
countryCode := logicalServer.ExitCountry
|
|
country, warning := codeToCountry(countryCode, countryCodes)
|
|
if warning != "" {
|
|
u.warner.Warn(warning)
|
|
}
|
|
|
|
ipToServer.add(country, region, city, name, hostname, entryIP)
|
|
}
|
|
}
|
|
|
|
if len(ipToServer) < minServers {
|
|
return nil, fmt.Errorf("%w: %d and expected at least %d",
|
|
ErrNotEnoughServers, len(ipToServer), minServers)
|
|
}
|
|
|
|
servers = ipToServer.toServersSlice()
|
|
|
|
sortServers(servers)
|
|
|
|
return servers, nil
|
|
}
|
|
|
|
func getStringValue(ptr *string) string {
|
|
if ptr == nil {
|
|
return ""
|
|
}
|
|
return *ptr
|
|
}
|