chore(all): move sub-packages to internal/provider
This commit is contained in:
35
internal/provider/torguard/updater/filename.go
Normal file
35
internal/provider/torguard/updater/filename.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package torguard
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"golang.org/x/text/cases"
|
||||
)
|
||||
|
||||
func parseFilename(fileName string, titleCaser cases.Caser) (country, city string) {
|
||||
const prefix = "TorGuard."
|
||||
const suffix = ".ovpn"
|
||||
s := strings.TrimPrefix(fileName, prefix)
|
||||
s = strings.TrimSuffix(s, suffix)
|
||||
|
||||
switch {
|
||||
case strings.Count(s, ".") == 1 && !strings.HasPrefix(s, "USA"):
|
||||
parts := strings.Split(s, ".")
|
||||
country = parts[0]
|
||||
city = parts[1]
|
||||
|
||||
case strings.HasPrefix(s, "USA"):
|
||||
country = "USA"
|
||||
s = strings.TrimPrefix(s, "USA-")
|
||||
s = strings.ReplaceAll(s, "-", " ")
|
||||
s = strings.ReplaceAll(s, ".", " ")
|
||||
s = strings.ToLower(s)
|
||||
s = titleCaser.String(s)
|
||||
city = s
|
||||
|
||||
default:
|
||||
country = s
|
||||
}
|
||||
|
||||
return country, city
|
||||
}
|
||||
61
internal/provider/torguard/updater/hosttoserver.go
Normal file
61
internal/provider/torguard/updater/hosttoserver.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package torguard
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/constants/vpn"
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
type hostToServer map[string]models.Server
|
||||
|
||||
func (hts hostToServer) add(host, country, city string,
|
||||
tcp, udp bool, ips []net.IP) {
|
||||
server, ok := hts[host]
|
||||
if !ok {
|
||||
server.VPN = vpn.OpenVPN
|
||||
server.Hostname = host
|
||||
server.Country = country
|
||||
server.City = city
|
||||
server.IPs = ips // used if DNS resolution fails downstream
|
||||
} else {
|
||||
server.IPs = append(server.IPs, ips...)
|
||||
}
|
||||
|
||||
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.Server) {
|
||||
servers = make([]models.Server, 0, len(hts))
|
||||
for _, server := range hts {
|
||||
servers = append(servers, server)
|
||||
}
|
||||
return servers
|
||||
}
|
||||
33
internal/provider/torguard/updater/resolve.go
Normal file
33
internal/provider/torguard/updater/resolve.go
Normal file
@@ -0,0 +1,33 @@
|
||||
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,
|
||||
SortIPs: true,
|
||||
},
|
||||
}
|
||||
return presolver.Resolve(ctx, hosts, settings)
|
||||
}
|
||||
106
internal/provider/torguard/updater/servers.go
Normal file
106
internal/provider/torguard/updater/servers.go
Normal file
@@ -0,0 +1,106 @@
|
||||
// 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/resolver"
|
||||
"github.com/qdm12/gluetun/internal/updater/unzip"
|
||||
"golang.org/x/text/cases"
|
||||
"golang.org/x/text/language"
|
||||
)
|
||||
|
||||
var ErrNotEnoughServers = errors.New("not enough servers found")
|
||||
|
||||
func GetServers(ctx context.Context, unzipper unzip.Unzipper,
|
||||
presolver resolver.Parallel, minServers int) (
|
||||
servers []models.Server, warnings []string, err error) {
|
||||
const tcpURL = "https://torguard.net/downloads/OpenVPN-TCP-Linux.zip"
|
||||
tcpContents, err := unzipper.FetchAndExtract(ctx, tcpURL)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
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)
|
||||
titleCaser := cases.Title(language.English)
|
||||
|
||||
for fileName, content := range tcpContents {
|
||||
const tcp, udp = true, false
|
||||
newWarnings := addServerFromOvpn(fileName, content, hts, tcp, udp, titleCaser)
|
||||
warnings = append(warnings, newWarnings...)
|
||||
}
|
||||
|
||||
for fileName, content := range udpContents {
|
||||
const tcp, udp = false, true
|
||||
newWarnings := addServerFromOvpn(fileName, content, hts, tcp, udp, titleCaser)
|
||||
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)
|
||||
}
|
||||
|
||||
sortServers(servers)
|
||||
|
||||
return servers, warnings, 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
|
||||
}
|
||||
19
internal/provider/torguard/updater/sort.go
Normal file
19
internal/provider/torguard/updater/sort.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package torguard
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
func sortServers(servers []models.Server) {
|
||||
sort.Slice(servers, func(i, j int) bool {
|
||||
if servers[i].Country == servers[j].Country {
|
||||
if servers[i].City == servers[j].City {
|
||||
return servers[i].Hostname < servers[j].Hostname
|
||||
}
|
||||
return servers[i].City < servers[j].City
|
||||
}
|
||||
return servers[i].Country < servers[j].Country
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user