2021-05-08 00:59:42 +00:00
|
|
|
// Package protonvpn contains code to obtain the server information
|
|
|
|
|
// for the ProtonVPN provider.
|
|
|
|
|
package protonvpn
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
2022-06-06 01:57:44 +00:00
|
|
|
"sort"
|
2021-05-08 00:59:42 +00:00
|
|
|
|
|
|
|
|
"github.com/qdm12/gluetun/internal/constants"
|
|
|
|
|
"github.com/qdm12/gluetun/internal/models"
|
2022-05-28 22:02:18 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/provider/common"
|
2021-05-08 00:59:42 +00:00
|
|
|
)
|
|
|
|
|
|
2022-05-28 20:58:50 +00:00
|
|
|
func (u *Updater) GetServers(ctx context.Context, minServers int) (
|
|
|
|
|
servers []models.Server, err error) {
|
|
|
|
|
data, err := fetchAPI(ctx, u.client)
|
2021-05-08 00:59:42 +00:00
|
|
|
if err != nil {
|
2022-05-28 20:58:50 +00:00
|
|
|
return nil, err
|
2021-05-08 00:59:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
countryCodes := constants.CountryCodes()
|
|
|
|
|
|
|
|
|
|
var count int
|
|
|
|
|
for _, logicalServer := range data.LogicalServers {
|
|
|
|
|
count += len(logicalServer.Servers)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if count < minServers {
|
2022-05-28 20:58:50 +00:00
|
|
|
return nil, fmt.Errorf("%w: %d and expected at least %d",
|
2022-05-28 22:02:18 +00:00
|
|
|
common.ErrNotEnoughServers, count, minServers)
|
2021-05-08 00:59:42 +00:00
|
|
|
}
|
|
|
|
|
|
2021-09-30 15:23:18 +00:00
|
|
|
ipToServer := make(ipToServer, count)
|
2021-05-08 00:59:42 +00:00
|
|
|
for _, logicalServer := range data.LogicalServers {
|
2021-09-30 15:23:18 +00:00
|
|
|
region := getStringValue(logicalServer.Region)
|
|
|
|
|
city := getStringValue(logicalServer.City)
|
|
|
|
|
name := logicalServer.Name
|
2021-05-08 00:59:42 +00:00
|
|
|
for _, physicalServer := range logicalServer.Servers {
|
2021-09-30 15:23:18 +00:00
|
|
|
if physicalServer.Status == 0 { // disabled so skip server
|
2022-05-28 20:58:50 +00:00
|
|
|
u.warner.Warn("ignoring server " + physicalServer.Domain + " with status 0")
|
2021-09-30 15:23:18 +00:00
|
|
|
continue
|
|
|
|
|
}
|
2021-05-08 00:59:42 +00:00
|
|
|
|
2021-09-30 15:23:18 +00:00
|
|
|
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)
|
2021-05-08 00:59:42 +00:00
|
|
|
if warning != "" {
|
2022-05-28 20:58:50 +00:00
|
|
|
u.warner.Warn(warning)
|
2021-05-08 00:59:42 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-16 09:44:57 +00:00
|
|
|
ipToServer.add(country, region, city, name, hostname, entryIP)
|
2021-05-08 00:59:42 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-30 15:23:18 +00:00
|
|
|
if len(ipToServer) < minServers {
|
2022-05-28 20:58:50 +00:00
|
|
|
return nil, fmt.Errorf("%w: %d and expected at least %d",
|
2022-05-28 22:02:18 +00:00
|
|
|
common.ErrNotEnoughServers, len(ipToServer), minServers)
|
2021-05-08 00:59:42 +00:00
|
|
|
}
|
|
|
|
|
|
2021-09-30 15:23:18 +00:00
|
|
|
servers = ipToServer.toServersSlice()
|
|
|
|
|
|
2022-06-06 01:57:44 +00:00
|
|
|
sort.Sort(models.SortableServers(servers))
|
2021-05-08 00:59:42 +00:00
|
|
|
|
2022-05-28 20:58:50 +00:00
|
|
|
return servers, nil
|
2021-05-08 00:59:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getStringValue(ptr *string) string {
|
|
|
|
|
if ptr == nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
return *ptr
|
|
|
|
|
}
|