Files
gluetun/gluetun-servers/pkg/updaters/providers/torguard/servers.go
T
Quentin McGaw d9cc7dcffb refactor(storage): new storage file structure
- new directory structure containing manifest.json and one json file per provider, by default.
- the manifest.json file can specify a filepath for each vpn provider
- each vpn provider json data file can contain the `"preferred": true` field to enforce it is used even if outdated, unless there is a version mismatch
- `STORAGE_SERVERS_DIRECTORY_PATH` replaces `STORAGE_FILEPATH` (which is now a migration source only). It sets the directory where server manifest and per-provider JSON files are stored (default: `/gluetun/servers/`).
- First-run migration: On startup, gluetun checks for the old /gluetun/servers.json file; if found and no new manifest exists, it automatically migrates all data to /gluetun/servers/ directory structure
- Silent fallback: If legacy file isn't found, uses the new directory path normally
- Legacy cleanup: After successful migration, attempts to remove the old fat JSON file (logs warning only if removal fails, e.g., read-only bind mounts)
2026-05-11 04:53:04 +00:00

115 lines
2.9 KiB
Go

package torguard
import (
"context"
"fmt"
"sort"
"strings"
"github.com/qdm12/gluetun-servers/pkg/models"
"github.com/qdm12/gluetun-servers/pkg/updaters/common"
"github.com/qdm12/gluetun-servers/pkg/updaters/openvpn"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
func (u *Updater) FetchServers(ctx context.Context, minServers int) (
servers []models.Server, err error,
) {
const tcpURL = "https://torguard.net/downloads/OpenVPN-TCP-Linux.zip"
tcpContents, err := u.unzipper.FetchAndExtract(ctx, tcpURL)
if err != nil {
return nil, err
}
const udpURL = "https://torguard.net/downloads/OpenVPN-UDP-Linux.zip"
udpContents, err := u.unzipper.FetchAndExtract(ctx, udpURL)
if err != nil {
return nil, err
}
hts := make(hostToServer)
titleCaser := cases.Title(language.English)
for fileName, content := range tcpContents {
const tcp, udp = true, false
warnings := addServerFromOvpn(fileName, content, hts, tcp, udp, titleCaser)
u.warnWarnings(warnings)
}
for fileName, content := range udpContents {
const tcp, udp = false, true
warnings := addServerFromOvpn(fileName, content, hts, tcp, udp, titleCaser)
u.warnWarnings(warnings)
}
if len(hts) < minServers {
return nil, fmt.Errorf("%w: %d and expected at least %d",
common.ErrNotEnoughServers, len(hts), minServers)
}
hosts := hts.toHostsSlice()
resolveSettings := parallelResolverSettings(hosts)
hostToIPs, warnings, err := u.parallelResolver.Resolve(ctx, resolveSettings)
u.warnWarnings(warnings)
if err != nil {
return nil, err
}
if len(hostToIPs) < minServers {
return nil, fmt.Errorf("%w: %d and expected at least %d",
common.ErrNotEnoughServers, len(servers), minServers)
}
hts.adaptWithIPs(hostToIPs)
servers = hts.toServersSlice()
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 addServerFromOvpn(fileName string, content []byte,
hts hostToServer, tcp, udp bool, titleCaser cases.Caser,
) (warnings []string) {
if !strings.HasSuffix(fileName, ".ovpn") {
return nil // not an OpenVPN file
}
country, city := parseFilename(fileName, titleCaser)
host, warning, err := openvpn.ExtractHost(content)
if warning != "" {
warnings = append(warnings, warning)
}
if err != nil {
// treat error as warning and go to next file
warning := err.Error() + " in " + fileName
warnings = append(warnings, warning)
return warnings
}
ips, err := openvpn.ExtractIPs(content)
if err != nil {
// treat error as warning and go to next file
warning := err.Error() + " in " + fileName
warnings = append(warnings, warning)
return warnings
}
hts.add(host, country, city, tcp, udp, ips)
return warnings
}
func (u *Updater) warnWarnings(warnings []string) {
for _, warning := range warnings {
u.warner.Warn(warning)
}
}