- Require at least 80% of number of servers now to pass - Each provider is in its own package with a common structure - Unzip package with unzipper interface - Openvpn package with extraction and download functions
77 lines
2.0 KiB
Go
77 lines
2.0 KiB
Go
// Package torguard contains code to obtain the server information
|
|
// for the Torguard provider.
|
|
package torguard
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/qdm12/gluetun/internal/models"
|
|
"github.com/qdm12/gluetun/internal/updater/openvpn"
|
|
"github.com/qdm12/gluetun/internal/updater/unzip"
|
|
)
|
|
|
|
var ErrNotEnoughServers = errors.New("not enough servers found")
|
|
|
|
func GetServers(ctx context.Context, unzipper unzip.Unzipper, minServers int) (
|
|
servers []models.TorguardServer, warnings []string, err error) {
|
|
const url = "https://torguard.net/downloads/OpenVPN-TCP-Linux.zip"
|
|
contents, err := unzipper.FetchAndExtract(ctx, url)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
} else if len(contents) < minServers {
|
|
return nil, nil, fmt.Errorf("%w: %d and expected at least %d",
|
|
ErrNotEnoughServers, len(contents), minServers)
|
|
}
|
|
|
|
servers = make([]models.TorguardServer, 0, len(contents))
|
|
for fileName, content := range contents {
|
|
if !strings.HasSuffix(fileName, ".ovpn") {
|
|
continue // not an OpenVPN file
|
|
}
|
|
|
|
country, city := parseFilename(fileName)
|
|
|
|
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)
|
|
continue
|
|
}
|
|
|
|
ip, warning, err := openvpn.ExtractIP(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)
|
|
continue
|
|
}
|
|
|
|
server := models.TorguardServer{
|
|
Country: country,
|
|
City: city,
|
|
Hostname: host,
|
|
IP: ip,
|
|
}
|
|
servers = append(servers, server)
|
|
}
|
|
|
|
if len(servers) < minServers {
|
|
return nil, warnings, fmt.Errorf("%w: %d and expected at least %d",
|
|
ErrNotEnoughServers, len(servers), minServers)
|
|
}
|
|
|
|
sortServers(servers)
|
|
|
|
return servers, warnings, nil
|
|
}
|