Feature: Multiple IPs for each Torguard server

- Fallback on IP from configuration file if DNS resolution fails
- Download both TCP and UDP zip files to detect support for each
- Filter servers by supported network protocol
-
This commit is contained in:
Quentin McGaw
2021-05-10 01:48:52 +00:00
parent eff65dce00
commit 95b0fb81d6
9 changed files with 248 additions and 119 deletions

View File

@@ -0,0 +1,55 @@
package torguard
import (
"net"
"github.com/qdm12/gluetun/internal/models"
)
type hostToServer map[string]models.TorguardServer
func (hts hostToServer) add(host, country, city string, tcp, udp bool, ip net.IP) {
server, ok := hts[host]
if !ok {
server.Hostname = host
server.Country = country
server.City = city
server.IPs = append(server.IPs, ip) // used if DNS resolution fails downstream
}
if tcp {
server.TCP = tcp
}
if udp {
server.UDP = udp
}
hts[host] = server
}
func (hts hostToServer) toHostsSlice() (hosts []string) {
hosts = make([]string, 0, len(hts))
for host := range hts {
hosts = append(hosts, host)
}
return hosts
}
func (hts hostToServer) adaptWithIPs(hostToIPs map[string][]net.IP) {
for host, IPs := range hostToIPs {
server := hts[host]
server.IPs = IPs
hts[host] = server
}
for host, server := range hts {
if len(server.IPs) == 0 {
delete(hts, host)
}
}
}
func (hts hostToServer) toServersSlice() (servers []models.TorguardServer) {
servers = make([]models.TorguardServer, 0, len(hts))
for _, server := range hts {
servers = append(servers, server)
}
return servers
}

View File

@@ -0,0 +1,32 @@
package torguard
import (
"context"
"net"
"time"
"github.com/qdm12/gluetun/internal/updater/resolver"
)
func resolveHosts(ctx context.Context, presolver resolver.Parallel,
hosts []string, minServers int) (hostToIPs map[string][]net.IP,
warnings []string, err error) {
const (
maxFailRatio = 0.1
maxDuration = 20 * time.Second
betweenDuration = time.Second
maxNoNew = 2
maxFails = 2
)
settings := resolver.ParallelSettings{
MaxFailRatio: maxFailRatio,
MinFound: minServers,
Repeat: resolver.RepeatSettings{
MaxDuration: maxDuration,
BetweenDuration: betweenDuration,
MaxNoNew: maxNoNew,
MaxFails: maxFails,
},
}
return presolver.Resolve(ctx, hosts, settings)
}

View File

@@ -10,61 +10,57 @@ import (
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/updater/openvpn"
"github.com/qdm12/gluetun/internal/updater/resolver"
"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) (
func GetServers(ctx context.Context, unzipper unzip.Unzipper,
presolver resolver.Parallel, 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)
const tcpURL = "https://torguard.net/downloads/OpenVPN-TCP-Linux.zip"
tcpContents, err := unzipper.FetchAndExtract(ctx, tcpURL)
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)
const udpURL = "https://torguard.net/downloads/OpenVPN-UDP-Linux.zip"
udpContents, err := unzipper.FetchAndExtract(ctx, udpURL)
if err != nil {
return nil, nil, err
}
hts := make(hostToServer)
for fileName, content := range tcpContents {
const tcp, udp = true, false
newWarnings := addServerFromOvpn(fileName, content, hts, tcp, udp)
warnings = append(warnings, newWarnings...)
}
for fileName, content := range udpContents {
const tcp, udp = false, true
newWarnings := addServerFromOvpn(fileName, content, hts, tcp, udp)
warnings = append(warnings, newWarnings...)
}
if len(hts) < minServers {
return nil, warnings, fmt.Errorf("%w: %d and expected at least %d",
ErrNotEnoughServers, len(hts), minServers)
}
hosts := hts.toHostsSlice()
hostToIPs, newWarnings, err := resolveHosts(ctx, presolver, hosts, minServers)
warnings = append(warnings, newWarnings...)
if err != nil {
return nil, warnings, err
}
hts.adaptWithIPs(hostToIPs)
servers = hts.toServersSlice()
if len(servers) < minServers {
return nil, warnings, fmt.Errorf("%w: %d and expected at least %d",
ErrNotEnoughServers, len(servers), minServers)
@@ -74,3 +70,37 @@ func GetServers(ctx context.Context, unzipper unzip.Unzipper, minServers int) (
return servers, warnings, nil
}
func addServerFromOvpn(fileName string, content []byte,
hts hostToServer, tcp, udp bool) (warnings []string) {
if !strings.HasSuffix(fileName, ".ovpn") {
return nil // 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)
return warnings
}
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)
return warnings
}
hts.add(host, country, city, tcp, udp, ip)
return warnings
}