chore(updater): move updater packages to pkg/updaters/<name>

This commit is contained in:
Quentin McGaw
2026-04-23 03:47:57 +00:00
parent 628b0a22e2
commit d96752c734
164 changed files with 732 additions and 343 deletions
+44
View File
@@ -0,0 +1,44 @@
package nordvpn
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
)
var ErrHTTPStatusCodeNotOK = errors.New("HTTP status code not OK")
func fetchAPI(ctx context.Context, client *http.Client,
limit uint,
) (data serversData, err error) {
url := "https://api.nordvpn.com/v2/servers"
url += fmt.Sprintf("?limit=%d", limit) // 0 means no limit
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return serversData{}, err
}
response, err := client.Do(request)
if err != nil {
return serversData{}, err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return serversData{}, fmt.Errorf("%w: %s", ErrHTTPStatusCodeNotOK, response.Status)
}
decoder := json.NewDecoder(response.Body)
if err := decoder.Decode(&data); err != nil {
return serversData{}, fmt.Errorf("decoding response body: %w", err)
}
if err := response.Body.Close(); err != nil {
return serversData{}, err
}
return data, nil
}
+199
View File
@@ -0,0 +1,199 @@
package nordvpn
import (
"encoding/base64"
"errors"
"fmt"
"net/netip"
"strings"
)
// Check out the JSON data from https://api.nordvpn.com/v2/servers?limit=10
type serversData struct {
Servers []serverData `json:"servers"`
Groups []groupData `json:"groups"`
Services []serviceData `json:"services"`
Locations []locationData `json:"locations"`
Technologies []technologyData `json:"technologies"`
}
type serverData struct {
// Name is the server name, for example 'Poland #128'
Name string `json:"name"`
// Stations is, it seems, the entry IP address.
// However it is ignored in favor of the 'ips' entry field.
Station netip.Addr `json:"station"`
// IPv6Station is mostly empty, so we ignore it for now.
IPv6Station netip.Addr `json:"station_ipv6"`
// Hostname is the server hostname, for example 'pl128.nordvpn.com'
Hostname string `json:"hostname"`
// Status is the server status, for example 'online'
Status string `json:"status"`
// Locations is the list of location IDs for the server.
// Only the first location is taken into account for now.
LocationIDs []uint32 `json:"location_ids"`
Technologies []struct {
ID uint32 `json:"id"`
Status string `json:"status"`
Metadata []struct {
// Name can notably be 'public_key'.
Name string `json:"name"`
// Value can notably the Wireguard public key value.
Value string `json:"value"`
} `json:"metadata"`
} `json:"technologies"`
GroupIDs []uint32 `json:"group_ids"`
ServiceIDs []uint32 `json:"service_ids"`
// IPs is the list of IP addresses for the server.
IPs []struct {
// Type can notably be 'entry'.
Type string `json:"type"`
IP struct {
IP netip.Addr `json:"ip"`
} `json:"ip"`
} `json:"ips"`
}
type groupData struct {
ID uint32 `json:"id"`
Title string `json:"title"` // "Europe", "Standard VPN servers", etc.
Type struct {
Identifier string `json:"identifier"` // 'regions', 'legacy_group_category', etc.
} `json:"type"`
}
type serviceData struct {
ID uint32 `json:"id"`
Identifier string `json:"identifier"` // 'vpn', 'proxy', etc.
}
type locationData struct {
ID uint32 `json:"id"`
Country struct {
Name string `json:"name"` // for example "Poland"
City struct {
Name string `json:"name"` // for example "Warsaw"
} `json:"city"`
} `json:"country"`
}
type technologyData struct {
ID uint32 `json:"id"`
// Identifier is the technology identifier name and relevant values are:
// 'openvpn_udp', 'openvpn_tcp', 'openvpn_dedicated_udp',
// 'openvpn_dedicated_tcp' and 'wireguard_udp'
Identifier string `json:"identifier"`
}
func (s serversData) idToData() (
groups map[uint32]groupData,
services map[uint32]serviceData,
locations map[uint32]locationData,
technologies map[uint32]technologyData,
) {
groups = make(map[uint32]groupData, len(s.Groups))
for _, group := range s.Groups {
if group.Type.Identifier == "regions" { //nolint:goconst
group.Title = strings.ReplaceAll(group.Title, ",", "")
}
groups[group.ID] = group
}
services = make(map[uint32]serviceData, len(s.Services))
for _, service := range s.Services {
services[service.ID] = service
}
locations = make(map[uint32]locationData, len(s.Locations))
for _, location := range s.Locations {
locations[location.ID] = location
}
technologies = make(map[uint32]technologyData, len(s.Technologies))
for _, technology := range s.Technologies {
technologies[technology.ID] = technology
}
return groups, services, locations, technologies
}
func (s *serverData) region(groups map[uint32]groupData) (region string) {
for _, groupID := range s.GroupIDs {
group, ok := groups[groupID]
if !ok {
continue
}
if group.Type.Identifier == "regions" {
return group.Title
}
}
return ""
}
func (s *serverData) hasVPNService(services map[uint32]serviceData) (ok bool) {
for _, serviceID := range s.ServiceIDs {
service, ok := services[serviceID]
if !ok {
continue
}
if service.Identifier == "vpn" {
return true
}
}
return false
}
// categories returns the list of categories for the server.
func (s *serverData) categories(groups map[uint32]groupData) (categories []string) {
categories = make([]string, 0, len(s.GroupIDs))
for _, groupID := range s.GroupIDs {
data, ok := groups[groupID]
if !ok || data.Type.Identifier == "regions" {
continue
}
categories = append(categories, data.Title)
}
return categories
}
// ips returns the list of IP addresses for the server.
func (s *serverData) ips() (ips []netip.Addr) {
ips = make([]netip.Addr, 0, len(s.IPs))
for _, ipObject := range s.IPs {
if ipObject.Type != "entry" {
continue
}
ips = append(ips, ipObject.IP.IP)
}
return ips
}
var (
ErrWireguardPublicKeyMalformed = errors.New("wireguard public key is malformed")
ErrWireguardPublicKeyNotFound = errors.New("wireguard public key not found")
)
// wireguardPublicKey returns the Wireguard public key for the server.
func (s *serverData) wireguardPublicKey(technologies map[uint32]technologyData) (
wgPubKey string, err error,
) {
for _, technology := range s.Technologies {
data, ok := technologies[technology.ID]
if !ok || data.Identifier != "wireguard_udp" {
continue
}
for _, metadata := range technology.Metadata {
if metadata.Name != "public_key" {
continue
}
wgPubKey = metadata.Value
_, err = base64.StdEncoding.DecodeString(wgPubKey)
if err != nil {
return "", fmt.Errorf("%w: %s cannot be decoded: %s",
ErrWireguardPublicKeyMalformed, wgPubKey, err)
}
return metadata.Value, nil
}
}
return "", fmt.Errorf("%w", ErrWireguardPublicKeyNotFound)
}
+29
View File
@@ -0,0 +1,29 @@
package nordvpn
import (
"errors"
"fmt"
"strconv"
"strings"
)
var (
ErrNoIDInServerName = errors.New("no ID in server name")
ErrInvalidIDInServerName = errors.New("invalid ID in server name")
)
func parseServerName(serverName string) (number uint16, err error) {
i := strings.IndexRune(serverName, '#')
if i < 0 {
return 0, fmt.Errorf("%w: %s", ErrNoIDInServerName, serverName)
}
idString := serverName[i+1:]
idUint64, err := strconv.ParseUint(idString, 10, 16)
if err != nil {
return 0, fmt.Errorf("%w: %s", ErrInvalidIDInServerName, serverName)
}
number = uint16(idUint64)
return number, nil
}
+150
View File
@@ -0,0 +1,150 @@
package nordvpn
import (
"context"
"errors"
"fmt"
"sort"
"github.com/qdm12/gluetun/internal/constants/vpn"
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/provider/common"
)
var ErrNotIPv4 = errors.New("IP address is not IPv4")
func (u *Updater) FetchServers(ctx context.Context, minServers int) (
servers []models.Server, err error,
) {
const limit = 0
data, err := fetchAPI(ctx, u.client, limit)
if err != nil {
return nil, err
}
servers = make([]models.Server, 0, len(data.Servers))
groups, services, locations, technologies := data.idToData()
for _, jsonServer := range data.Servers {
newServers, warnings := extractServers(jsonServer, groups, services, locations, technologies)
for _, warning := range warnings {
u.warner.Warn(warning)
}
servers = append(servers, newServers...)
}
if len(servers) < minServers {
return nil, fmt.Errorf("%w: %d and expected at least %d",
common.ErrNotEnoughServers, len(servers), minServers)
}
sort.Sort(models.SortableServers(servers))
return servers, nil
}
func extractServers(jsonServer serverData, groups map[uint32]groupData,
services map[uint32]serviceData, locations map[uint32]locationData,
technologies map[uint32]technologyData) (servers []models.Server,
warnings []string,
) {
ignoreReason := ""
switch {
case jsonServer.Status != "online":
ignoreReason = "status is " + jsonServer.Status
case len(jsonServer.LocationIDs) == 0:
ignoreReason = "no location"
case len(jsonServer.IPs) == 0:
ignoreReason = "no IP address"
case !jsonServer.hasVPNService(services):
ignoreReason = "no VPN service"
}
if ignoreReason != "" {
warning := fmt.Sprintf("ignoring server %s: %s", jsonServer.Name, ignoreReason)
return nil, []string{warning}
}
location, ok := locations[jsonServer.LocationIDs[0]]
if !ok {
warning := fmt.Sprintf("location with id %d not found in %v",
jsonServer.LocationIDs[0], locations)
return nil, []string{warning}
}
region := jsonServer.region(groups)
if region == "" {
warning := fmt.Sprintf("no region found for server %s", jsonServer.Name)
return nil, []string{warning}
}
server := models.Server{
Country: location.Country.Name,
Region: region,
City: location.Country.City.Name,
Categories: jsonServer.categories(groups),
Hostname: jsonServer.Hostname,
IPs: jsonServer.ips(),
}
number, err := parseServerName(jsonServer.Name)
switch {
case errors.Is(err, ErrNoIDInServerName):
warning := fmt.Sprintf("%s - leaving server number as 0", err)
warnings = append(warnings, warning)
case err != nil:
warning := fmt.Sprintf("failed parsing server name: %s", err)
return nil, []string{warning}
default: // no error
server.Number = number
}
var wireguardFound, openvpnFound bool
wireguardServer := server
wireguardServer.VPN = vpn.Wireguard
openVPNServer := server // accumulate UDP+TCP technologies
openVPNServer.VPN = vpn.OpenVPN
for _, technology := range jsonServer.Technologies {
if technology.Status != "online" {
continue
}
technologyData, ok := technologies[technology.ID]
if !ok {
warning := fmt.Sprintf("technology with id %d not found in %v",
technology.ID, technologies)
warnings = append(warnings, warning)
continue
}
switch technologyData.Identifier {
case "openvpn_udp", "openvpn_dedicated_udp":
openvpnFound = true
openVPNServer.UDP = true
case "openvpn_tcp", "openvpn_dedicated_tcp":
openvpnFound = true
openVPNServer.TCP = true
case "wireguard_udp":
wireguardFound = true
wireguardServer.WgPubKey, err = jsonServer.wireguardPublicKey(technologies)
if err != nil {
warning := fmt.Sprintf("ignoring Wireguard server %s: %s", jsonServer.Name, err)
warnings = append(warnings, warning)
wireguardFound = false
continue
}
default: // Ignore other technologies
continue
}
}
const maxServers = 2
servers = make([]models.Server, 0, maxServers)
if openvpnFound {
servers = append(servers, openVPNServer)
}
if wireguardFound {
servers = append(servers, wireguardServer)
}
return servers, warnings
}
+23
View File
@@ -0,0 +1,23 @@
package nordvpn
import (
"net/http"
"github.com/qdm12/gluetun/internal/provider/common"
)
type Updater struct {
client *http.Client
warner common.Warner
}
func New(client *http.Client, warner common.Warner) *Updater {
return &Updater{
client: client,
warner: warner,
}
}
func (u *Updater) Version() uint16 {
return 5 //nolint:mnd
}