Maintenance: refactor servers updater code
- 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
This commit is contained in:
43
internal/updater/providers/purevpn/hosttoserver.go
Normal file
43
internal/updater/providers/purevpn/hosttoserver.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package purevpn
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
type hostToServer map[string]models.PurevpnServer
|
||||
|
||||
func (hts hostToServer) add(host string) {
|
||||
// TODO set TCP and UDP compatibility, set hostname
|
||||
hts[host] = models.PurevpnServer{}
|
||||
}
|
||||
|
||||
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.PurevpnServer) {
|
||||
servers = make([]models.PurevpnServer, 0, len(hts))
|
||||
for _, server := range hts {
|
||||
servers = append(servers, server)
|
||||
}
|
||||
return servers
|
||||
}
|
||||
33
internal/updater/providers/purevpn/locationtoserver.go
Normal file
33
internal/updater/providers/purevpn/locationtoserver.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package purevpn
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
type locationToServer map[string]models.PurevpnServer
|
||||
|
||||
func locationKey(country, region, city string) string {
|
||||
return country + region + city
|
||||
}
|
||||
|
||||
func (lts locationToServer) add(country, region, city string, ips []net.IP) {
|
||||
key := locationKey(country, region, city)
|
||||
server, ok := lts[key]
|
||||
if !ok {
|
||||
server.Country = country
|
||||
server.Region = region
|
||||
server.City = city
|
||||
}
|
||||
server.IPs = append(server.IPs, ips...)
|
||||
lts[key] = server
|
||||
}
|
||||
|
||||
func (lts locationToServer) toServersSlice() (servers []models.PurevpnServer) {
|
||||
servers = make([]models.PurevpnServer, 0, len(lts))
|
||||
for _, server := range lts {
|
||||
servers = append(servers, server)
|
||||
}
|
||||
return servers
|
||||
}
|
||||
33
internal/updater/providers/purevpn/resolve.go
Normal file
33
internal/updater/providers/purevpn/resolve.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package purevpn
|
||||
|
||||
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,
|
||||
SortIPs: true,
|
||||
},
|
||||
}
|
||||
return presolver.Resolve(ctx, hosts, settings)
|
||||
}
|
||||
98
internal/updater/providers/purevpn/servers.go
Normal file
98
internal/updater/providers/purevpn/servers.go
Normal file
@@ -0,0 +1,98 @@
|
||||
// Package purevpn contains code to obtain the server information
|
||||
// for the PureVPN provider.
|
||||
package purevpn
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
"github.com/qdm12/gluetun/internal/publicip"
|
||||
"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, client *http.Client,
|
||||
unzipper unzip.Unzipper, presolver resolver.Parallel, minServers int) (
|
||||
servers []models.PurevpnServer, warnings []string, err error) {
|
||||
const url = "https://s3-us-west-1.amazonaws.com/heartbleed/windows/New+OVPN+Files.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)
|
||||
}
|
||||
|
||||
hts := make(hostToServer)
|
||||
|
||||
for fileName, content := range contents {
|
||||
if !strings.HasSuffix(fileName, ".ovpn") {
|
||||
continue
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
hts.add(host)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
// Dedup by location
|
||||
lts := make(locationToServer)
|
||||
for _, server := range servers {
|
||||
country, region, city, err := publicip.Info(ctx, client, server.IPs[0])
|
||||
if err != nil {
|
||||
return nil, warnings, err
|
||||
}
|
||||
|
||||
// TODO split servers by host
|
||||
lts.add(country, region, city, server.IPs)
|
||||
}
|
||||
|
||||
if len(servers) < minServers {
|
||||
return nil, warnings, fmt.Errorf("%w: %d and expected at least %d",
|
||||
ErrNotEnoughServers, len(servers), minServers)
|
||||
}
|
||||
|
||||
servers = lts.toServersSlice()
|
||||
|
||||
sortServers(servers)
|
||||
|
||||
return servers, warnings, nil
|
||||
}
|
||||
19
internal/updater/providers/purevpn/sort.go
Normal file
19
internal/updater/providers/purevpn/sort.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package purevpn
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
func sortServers(servers []models.PurevpnServer) {
|
||||
sort.Slice(servers, func(i, j int) bool {
|
||||
if servers[i].Country == servers[j].Country {
|
||||
if servers[i].Region == servers[j].Region {
|
||||
return servers[i].City < servers[j].City
|
||||
}
|
||||
return servers[i].Region < servers[j].Region
|
||||
}
|
||||
return servers[i].Country < servers[j].Country
|
||||
})
|
||||
}
|
||||
14
internal/updater/providers/purevpn/string.go
Normal file
14
internal/updater/providers/purevpn/string.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package purevpn
|
||||
|
||||
import "github.com/qdm12/gluetun/internal/models"
|
||||
|
||||
func Stringify(servers []models.PurevpnServer) (s string) {
|
||||
s = "func PurevpnServers() []models.PurevpnServer {\n"
|
||||
s += " return []models.PurevpnServer{\n"
|
||||
for _, server := range servers {
|
||||
s += " " + server.String() + ",\n"
|
||||
}
|
||||
s += " }\n"
|
||||
s += "}"
|
||||
return s
|
||||
}
|
||||
Reference in New Issue
Block a user