Filter Privado servers by hostnames only

This commit is contained in:
Quentin McGaw
2020-11-09 23:17:22 +00:00
parent f1e4b9937b
commit 40ed070f21
12 changed files with 222 additions and 289 deletions

View File

@@ -5,8 +5,6 @@ import (
"fmt"
"net"
"sort"
"strconv"
"strings"
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/golibs/network"
@@ -45,59 +43,41 @@ func findPrivadoServersFromZip(ctx context.Context, client network.Client, looku
if len(remoteLines) == 0 {
return nil, warnings, fmt.Errorf("cannot find any remote lines in %s", fileName)
}
hosts := extractHostnamesFromRemoteLines(remoteLines)
if len(hosts) == 0 {
hostnames := extractHostnamesFromRemoteLines(remoteLines)
if len(hostnames) == 0 {
return nil, warnings, fmt.Errorf("cannot find any hosts in %s", fileName)
} else if len(hosts) > 1 {
warning := fmt.Sprintf("more than one host in %q, only taking first one %q into account", fileName, hosts[0])
} else if len(hostnames) > 1 {
warning := fmt.Sprintf("more than one host in %q, only taking first one %q into account", fileName, hostnames[0])
warnings = append(warnings, warning)
}
host := hosts[0]
if net.ParseIP(host) != nil {
warning := fmt.Sprintf("ignoring IP address host %q in %s", host, fileName)
hostname := hostnames[0]
if net.ParseIP(hostname) != nil {
warning := fmt.Sprintf("ignoring IP address host %q in %s", hostname, fileName)
warnings = append(warnings, warning)
continue
}
const repetition = 1
IPs, err := resolveRepeat(ctx, lookupIP, host, repetition)
IPs, err := resolveRepeat(ctx, lookupIP, hostname, repetition)
switch {
case err != nil:
return nil, warnings, err
case len(IPs) == 0:
warning := fmt.Sprintf("no IP address found for host %q", host)
warning := fmt.Sprintf("no IP address found for host %q", hostname)
warnings = append(warnings, warning)
continue
case len(IPs) > 1:
warning := fmt.Sprintf("more than one IP address found for host %q", host)
warning := fmt.Sprintf("more than one IP address found for host %q", hostname)
warnings = append(warnings, warning)
}
subdomain := strings.TrimSuffix(host, ".vpn.privado.io")
parts := strings.Split(subdomain, "-")
const expectedParts = 2
if len(parts) != expectedParts {
warning := fmt.Sprintf("malformed subdomain %q: cannot find city and server number", subdomain)
warnings = append(warnings, warning)
continue
}
city, serverNumberString := parts[0], parts[1]
serverNumberInt, err := strconv.ParseInt(serverNumberString, 10, 16)
if err != nil {
warning := fmt.Sprintf("malformed server number %q: %s", serverNumberString, err)
warnings = append(warnings, warning)
continue
}
server := models.PrivadoServer{
City: city,
Number: uint16(serverNumberInt),
IP: IPs[0],
Hostname: hostname,
IP: IPs[0],
}
servers = append(servers, server)
}
sort.Slice(servers, func(i, j int) bool {
keyA := servers[i].City + fmt.Sprintf("%d", servers[i].Number)
keyB := servers[j].City + fmt.Sprintf("%d", servers[j].Number)
return keyA < keyB
return servers[i].Hostname < servers[j].Hostname
})
return servers, warnings, nil
}