mirror of
https://github.com/qdm12/gluetun.git
synced 2026-06-19 01:43:56 +02:00
chore(updater): move updater packages to pkg/updaters/<name>
This commit is contained in:
@@ -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
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package torguard
|
||||
|
||||
import (
|
||||
"net/netip"
|
||||
|
||||
"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 []netip.Addr,
|
||||
) {
|
||||
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][]netip.Addr) {
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package torguard
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/updater/resolver"
|
||||
)
|
||||
|
||||
func parallelResolverSettings(hosts []string) (settings resolver.ParallelSettings) {
|
||||
const (
|
||||
maxFailRatio = 0.1
|
||||
maxDuration = 20 * time.Second
|
||||
betweenDuration = time.Second
|
||||
maxNoNew = 2
|
||||
maxFails = 2
|
||||
)
|
||||
return resolver.ParallelSettings{
|
||||
Hosts: hosts,
|
||||
MaxFailRatio: maxFailRatio,
|
||||
Repeat: resolver.RepeatSettings{
|
||||
MaxDuration: maxDuration,
|
||||
BetweenDuration: betweenDuration,
|
||||
MaxNoNew: maxNoNew,
|
||||
MaxFails: maxFails,
|
||||
SortIPs: true,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
package torguard
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
"github.com/qdm12/gluetun/internal/provider/common"
|
||||
"github.com/qdm12/gluetun/internal/updater/openvpn"
|
||||
"golang.org/x/text/cases"
|
||||
"golang.org/x/text/language"
|
||||
)
|
||||
|
||||
func (u *Updater) FetchServers(ctx context.Context, minServers int) (
|
||||
servers []models.Server, err error,
|
||||
) {
|
||||
const tcpURL = "https://torguard.net/downloads/OpenVPN-TCP-Linux.zip"
|
||||
tcpContents, err := u.unzipper.FetchAndExtract(ctx, tcpURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
const udpURL = "https://torguard.net/downloads/OpenVPN-UDP-Linux.zip"
|
||||
udpContents, err := u.unzipper.FetchAndExtract(ctx, udpURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
hts := make(hostToServer)
|
||||
titleCaser := cases.Title(language.English)
|
||||
|
||||
for fileName, content := range tcpContents {
|
||||
const tcp, udp = true, false
|
||||
warnings := addServerFromOvpn(fileName, content, hts, tcp, udp, titleCaser)
|
||||
u.warnWarnings(warnings)
|
||||
}
|
||||
|
||||
for fileName, content := range udpContents {
|
||||
const tcp, udp = false, true
|
||||
warnings := addServerFromOvpn(fileName, content, hts, tcp, udp, titleCaser)
|
||||
u.warnWarnings(warnings)
|
||||
}
|
||||
|
||||
if len(hts) < minServers {
|
||||
return nil, fmt.Errorf("%w: %d and expected at least %d",
|
||||
common.ErrNotEnoughServers, len(hts), minServers)
|
||||
}
|
||||
|
||||
hosts := hts.toHostsSlice()
|
||||
resolveSettings := parallelResolverSettings(hosts)
|
||||
hostToIPs, warnings, err := u.parallelResolver.Resolve(ctx, resolveSettings)
|
||||
u.warnWarnings(warnings)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(hostToIPs) < minServers {
|
||||
return nil, fmt.Errorf("%w: %d and expected at least %d",
|
||||
common.ErrNotEnoughServers, len(servers), minServers)
|
||||
}
|
||||
|
||||
hts.adaptWithIPs(hostToIPs)
|
||||
|
||||
servers = hts.toServersSlice()
|
||||
|
||||
if len(servers) < minServers {
|
||||
return nil, fmt.Errorf("%w: %d and expected at least %d",
|
||||
common.ErrNotEnoughServers, len(servers), minServers)
|
||||
}
|
||||
|
||||
sort.Sort(models.SortableServers(servers))
|
||||
|
||||
return servers, 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
|
||||
}
|
||||
|
||||
func (u *Updater) warnWarnings(warnings []string) {
|
||||
for _, warning := range warnings {
|
||||
u.warner.Warn(warning)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package torguard
|
||||
|
||||
import (
|
||||
"github.com/qdm12/gluetun/internal/provider/common"
|
||||
)
|
||||
|
||||
type Updater struct {
|
||||
unzipper common.Unzipper
|
||||
parallelResolver common.ParallelResolver
|
||||
warner common.Warner
|
||||
}
|
||||
|
||||
func New(unzipper common.Unzipper, warner common.Warner,
|
||||
parallelResolver common.ParallelResolver,
|
||||
) *Updater {
|
||||
return &Updater{
|
||||
unzipper: unzipper,
|
||||
parallelResolver: parallelResolver,
|
||||
warner: warner,
|
||||
}
|
||||
}
|
||||
|
||||
func (u *Updater) Version() uint16 {
|
||||
return 3 //nolint:mnd
|
||||
}
|
||||
Reference in New Issue
Block a user