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,59 @@
|
||||
package purevpn
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"golang.org/x/text/runes"
|
||||
"golang.org/x/text/transform"
|
||||
"golang.org/x/text/unicode/norm"
|
||||
)
|
||||
|
||||
// comparePlaceNames returns true if strings are within 1 edit
|
||||
// distance after normalization.
|
||||
func comparePlaceNames(a, b string) bool {
|
||||
normA := normalize(a)
|
||||
normB := normalize(b)
|
||||
return normA == normB || levenshteinDistance(normA, normB) <= 1
|
||||
}
|
||||
|
||||
// normalize removes accents, trims space, and lowercases the string.
|
||||
func normalize(s string) string {
|
||||
transformer := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
|
||||
result, _, err := transform.String(transformer, s)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return strings.ToLower(strings.TrimSpace(result))
|
||||
}
|
||||
|
||||
// levenshteinDistance calculates the edit distance
|
||||
// between two strings a and b.
|
||||
func levenshteinDistance(a, b string) int {
|
||||
switch {
|
||||
case len(a) == 0:
|
||||
return len(b)
|
||||
case len(b) == 0:
|
||||
return len(a)
|
||||
}
|
||||
|
||||
column := make([]int, len(b)+1)
|
||||
for i := 0; i <= len(b); i++ {
|
||||
column[i] = i
|
||||
}
|
||||
|
||||
for i := 1; i <= len(a); i++ {
|
||||
column[0] = i
|
||||
lastValue := i - 1
|
||||
for j := 1; j <= len(b); j++ {
|
||||
oldValue := column[j]
|
||||
cost := 0
|
||||
if a[i-1] != b[j-1] {
|
||||
cost = 1
|
||||
}
|
||||
column[j] = min(column[j]+1, min(column[j-1]+1, lastValue+cost))
|
||||
lastValue = oldValue
|
||||
}
|
||||
}
|
||||
return column[len(b)]
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package purevpn
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_comparePlaceNames(t *testing.T) {
|
||||
t.Parallel() // Allow the top-level test to run in parallel
|
||||
|
||||
testCases := map[string]struct {
|
||||
a string
|
||||
b string
|
||||
want bool
|
||||
}{
|
||||
"exact_match": {
|
||||
a: "Paris",
|
||||
b: "Paris",
|
||||
want: true,
|
||||
},
|
||||
"difference_in_casing_and_whitespace": {
|
||||
a: " Montreal",
|
||||
b: "montreal ",
|
||||
want: true,
|
||||
},
|
||||
"accent_normalization": {
|
||||
a: "Montréal",
|
||||
b: "Montreal",
|
||||
want: true,
|
||||
},
|
||||
"single_character_typo": {
|
||||
a: "Lyon",
|
||||
b: "Lyonn",
|
||||
want: true,
|
||||
},
|
||||
"too_many_differences": {
|
||||
a: "London",
|
||||
b: "Londres",
|
||||
want: false,
|
||||
},
|
||||
}
|
||||
|
||||
for name, testCase := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
result := comparePlaceNames(testCase.a, testCase.b)
|
||||
assert.Equal(t, testCase.want, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package purevpn
|
||||
|
||||
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 string, tcp, udp bool) {
|
||||
server, ok := hts[host]
|
||||
if !ok {
|
||||
server.VPN = vpn.OpenVPN
|
||||
server.Hostname = host
|
||||
}
|
||||
if tcp {
|
||||
server.TCP = true
|
||||
}
|
||||
if udp {
|
||||
server.UDP = true
|
||||
}
|
||||
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,65 @@
|
||||
package purevpn
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/constants"
|
||||
)
|
||||
|
||||
var countryCodeToName = constants.CountryCodes() //nolint:gochecknoglobals
|
||||
|
||||
//nolint:gochecknoglobals
|
||||
var countryCityCodeToCityName = map[string]string{
|
||||
"aume": "Melbourne",
|
||||
"aupe": "Perth",
|
||||
"ausd": "Sydney",
|
||||
"ukl": "London",
|
||||
"ukm": "Manchester",
|
||||
"usca": "Los Angeles",
|
||||
"usfl": "Miami",
|
||||
"usga": "Atlanta",
|
||||
"usil": "Chicago",
|
||||
"usnj": "Newark",
|
||||
"usny": "New York",
|
||||
"uspe": "Perth",
|
||||
"usphx": "Phoenix",
|
||||
"ussa": "Seattle",
|
||||
"ussf": "San Francisco",
|
||||
"ustx": "Houston",
|
||||
"usut": "Salt Lake City",
|
||||
"usva": "Ashburn",
|
||||
"uswdc": "Washington DC",
|
||||
}
|
||||
|
||||
func parseHostname(hostname string) (country, city string, warnings []string) {
|
||||
const minHostnameLength = 2 + 3 + 2 // 2 country code + 3 city code + "2-"
|
||||
if len(hostname) < minHostnameLength {
|
||||
warnings = append(warnings,
|
||||
fmt.Sprintf("hostname %q is too short to parse country and city codes", hostname))
|
||||
}
|
||||
countryCode := strings.ToLower(hostname[0:2])
|
||||
country, ok := countryCodeToName[countryCode]
|
||||
if !ok {
|
||||
warnings = append(warnings, fmt.Sprintf("unknown country code %q in hostname %q",
|
||||
countryCode, hostname))
|
||||
}
|
||||
|
||||
twoMinusIndex := strings.Index(hostname, "2-")
|
||||
switch twoMinusIndex {
|
||||
case -1:
|
||||
warnings = append(warnings,
|
||||
fmt.Sprintf("hostname %q does not contain '2-'", hostname))
|
||||
return country, city, warnings
|
||||
case 2: //nolint:mnd
|
||||
// no city code
|
||||
return country, "", warnings
|
||||
}
|
||||
countryCityCode := strings.ToLower(hostname[:twoMinusIndex])
|
||||
city, ok = countryCityCodeToCityName[countryCityCode]
|
||||
if !ok {
|
||||
warnings = append(warnings, fmt.Sprintf("unknown country-city code %q in hostname %q",
|
||||
countryCityCode, hostname))
|
||||
}
|
||||
return country, city, warnings
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package purevpn
|
||||
|
||||
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,119 @@
|
||||
package purevpn
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/netip"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
"github.com/qdm12/gluetun/internal/provider/common"
|
||||
"github.com/qdm12/gluetun/internal/publicip/api"
|
||||
"github.com/qdm12/gluetun/internal/updater/openvpn"
|
||||
)
|
||||
|
||||
func (u *Updater) FetchServers(ctx context.Context, minServers int) (
|
||||
servers []models.Server, err error,
|
||||
) {
|
||||
if !u.ipFetcher.CanFetchAnyIP() {
|
||||
return nil, fmt.Errorf("%w: %s", common.ErrIPFetcherUnsupported, u.ipFetcher.String())
|
||||
}
|
||||
|
||||
const url = "https://d11a57lttb2ffq.cloudfront.net/heartbleed/router/Recommended-CA2.zip"
|
||||
contents, err := u.unzipper.FetchAndExtract(ctx, url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if len(contents) < minServers {
|
||||
return nil, fmt.Errorf("%w: %d and expected at least %d",
|
||||
common.ErrNotEnoughServers, len(contents), minServers)
|
||||
}
|
||||
|
||||
hts := make(hostToServer)
|
||||
|
||||
for fileName, content := range contents {
|
||||
if !strings.HasSuffix(fileName, ".ovpn") {
|
||||
continue
|
||||
}
|
||||
|
||||
tcp, udp, err := openvpn.ExtractProto(content)
|
||||
if err != nil {
|
||||
// treat error as warning and go to next file
|
||||
u.warner.Warn(err.Error() + " in " + fileName)
|
||||
continue
|
||||
}
|
||||
|
||||
host, warning, err := openvpn.ExtractHost(content)
|
||||
if warning != "" {
|
||||
u.warner.Warn(warning)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
// treat error as warning and go to next file
|
||||
u.warner.Warn(err.Error() + " in " + fileName)
|
||||
continue
|
||||
}
|
||||
|
||||
hts.add(host, tcp, udp)
|
||||
}
|
||||
|
||||
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)
|
||||
for _, warning := range warnings {
|
||||
u.warner.Warn(warning)
|
||||
}
|
||||
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()
|
||||
|
||||
// Get public IP address information
|
||||
ipsToGetInfo := make([]netip.Addr, len(servers))
|
||||
for i := range servers {
|
||||
ipsToGetInfo[i] = servers[i].IPs[0]
|
||||
}
|
||||
ipsInfo, err := api.FetchMultiInfo(ctx, u.ipFetcher, ipsToGetInfo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for i := range servers {
|
||||
parsedCountry, parsedCity, warnings := parseHostname(servers[i].Hostname)
|
||||
for _, warning := range warnings {
|
||||
u.warner.Warn(warning)
|
||||
}
|
||||
servers[i].Country = parsedCountry
|
||||
if servers[i].Country == "" {
|
||||
servers[i].Country = ipsInfo[i].Country
|
||||
}
|
||||
servers[i].City = parsedCity
|
||||
if servers[i].City == "" {
|
||||
servers[i].City = ipsInfo[i].City
|
||||
}
|
||||
|
||||
if (parsedCountry == "" ||
|
||||
comparePlaceNames(parsedCountry, ipsInfo[i].Country)) &&
|
||||
(parsedCity == "" ||
|
||||
comparePlaceNames(parsedCity, ipsInfo[i].City)) {
|
||||
servers[i].Region = ipsInfo[i].Region
|
||||
}
|
||||
}
|
||||
|
||||
sort.Sort(models.SortableServers(servers))
|
||||
|
||||
return servers, nil
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package purevpn
|
||||
|
||||
import (
|
||||
"github.com/qdm12/gluetun/internal/provider/common"
|
||||
)
|
||||
|
||||
type Updater struct {
|
||||
ipFetcher common.IPFetcher
|
||||
unzipper common.Unzipper
|
||||
parallelResolver common.ParallelResolver
|
||||
warner common.Warner
|
||||
}
|
||||
|
||||
func New(ipFetcher common.IPFetcher, unzipper common.Unzipper,
|
||||
warner common.Warner, parallelResolver common.ParallelResolver,
|
||||
) *Updater {
|
||||
return &Updater{
|
||||
ipFetcher: ipFetcher,
|
||||
unzipper: unzipper,
|
||||
parallelResolver: parallelResolver,
|
||||
warner: warner,
|
||||
}
|
||||
}
|
||||
|
||||
func (u *Updater) Version() uint16 {
|
||||
return 3 //nolint:mnd
|
||||
}
|
||||
Reference in New Issue
Block a user