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:
267
internal/updater/providers/cyberghost/constants.go
Normal file
267
internal/updater/providers/cyberghost/constants.go
Normal file
@@ -0,0 +1,267 @@
|
||||
package cyberghost
|
||||
|
||||
func getGroups() map[string]string {
|
||||
return map[string]string{
|
||||
"87-1": "Premium UDP Europe",
|
||||
"94-1": "Premium UDP USA",
|
||||
"95-1": "Premium UDP Asia",
|
||||
"87-8": "NoSpy UDP Europe",
|
||||
"97-1": "Premium TCP Europe",
|
||||
"93-1": "Premium TCP USA",
|
||||
"96-1": "Premium TCP Asia",
|
||||
"97-8": "NoSpy TCP Europe",
|
||||
}
|
||||
}
|
||||
|
||||
func getSubdomainToRegion() map[string]string {
|
||||
return map[string]string{
|
||||
"af": "Afghanistan",
|
||||
"ax": "Aland Islands",
|
||||
"al": "Albania",
|
||||
"dz": "Algeria",
|
||||
"as": "American Samoa",
|
||||
"ad": "Andorra",
|
||||
"ao": "Angola",
|
||||
"ai": "Anguilla",
|
||||
"aq": "Antarctica",
|
||||
"ag": "Antigua and Barbuda",
|
||||
"ar": "Argentina",
|
||||
"am": "Armenia",
|
||||
"aw": "Aruba",
|
||||
"au": "Australia",
|
||||
"at": "Austria",
|
||||
"az": "Azerbaijan",
|
||||
"bs": "Bahamas",
|
||||
"bh": "Bahrain",
|
||||
"bd": "Bangladesh",
|
||||
"bb": "Barbados",
|
||||
"by": "Belarus",
|
||||
"be": "Belgium",
|
||||
"bz": "Belize",
|
||||
"bj": "Benin",
|
||||
"bm": "Bermuda",
|
||||
"bt": "Bhutan",
|
||||
"bo": "Bolivia",
|
||||
"bq": "Bonaire",
|
||||
"ba": "Bosnia and Herzegovina",
|
||||
"bw": "Botswana",
|
||||
"bv": "Bouvet Island",
|
||||
"br": "Brazil",
|
||||
"io": "British Indian Ocean Territory",
|
||||
"vg": "British Virgin Islands",
|
||||
"bn": "Brunei Darussalam",
|
||||
"bg": "Bulgaria",
|
||||
"bf": "Burkina Faso",
|
||||
"bi": "Burundi",
|
||||
"kh": "Cambodia",
|
||||
"cm": "Cameroon",
|
||||
"ca": "Canada",
|
||||
"cv": "Cape Verde",
|
||||
"ky": "Cayman Islands",
|
||||
"cf": "Central African Republic",
|
||||
"td": "Chad",
|
||||
"cl": "Chile",
|
||||
"cn": "China",
|
||||
"cx": "Christmas Island",
|
||||
"cc": "Cocos Islands",
|
||||
"co": "Colombia",
|
||||
"km": "Comoros",
|
||||
"cg": "Congo",
|
||||
"ck": "Cook Islands",
|
||||
"cr": "Costa Rica",
|
||||
"ci": "Cote d'Ivoire",
|
||||
"hr": "Croatia",
|
||||
"cu": "Cuba",
|
||||
"cw": "Curacao",
|
||||
"cy": "Cyprus",
|
||||
"cz": "Czech Republic",
|
||||
"cd": "Democratic Republic of the Congo",
|
||||
"dk": "Denmark",
|
||||
"dj": "Djibouti",
|
||||
"dm": "Dominica",
|
||||
"do": "Dominican Republic",
|
||||
"ec": "Ecuador",
|
||||
"eg": "Egypt",
|
||||
"sv": "El Salvador",
|
||||
"gq": "Equatorial Guinea",
|
||||
"er": "Eritrea",
|
||||
"ee": "Estonia",
|
||||
"et": "Ethiopia",
|
||||
"fk": "Falkland Islands",
|
||||
"fo": "Faroe Islands",
|
||||
"fj": "Fiji",
|
||||
"fi": "Finland",
|
||||
"fr": "France",
|
||||
"gf": "French Guiana",
|
||||
"pf": "French Polynesia",
|
||||
"tf": "French Southern Territories",
|
||||
"ga": "Gabon",
|
||||
"gm": "Gambia",
|
||||
"ge": "Georgia",
|
||||
"de": "Germany",
|
||||
"gh": "Ghana",
|
||||
"gi": "Gibraltar",
|
||||
"gr": "Greece",
|
||||
"gl": "Greenland",
|
||||
"gd": "Grenada",
|
||||
"gp": "Guadeloupe",
|
||||
"gu": "Guam",
|
||||
"gt": "Guatemala",
|
||||
"gg": "Guernsey",
|
||||
"gw": "Guinea-Bissau",
|
||||
"gn": "Guinea",
|
||||
"gy": "Guyana",
|
||||
"ht": "Haiti",
|
||||
"hm": "Heard Island and McDonald Islands",
|
||||
"hn": "Honduras",
|
||||
"hk": "Hong Kong",
|
||||
"hu": "Hungary",
|
||||
"is": "Iceland",
|
||||
"in": "India",
|
||||
"id": "Indonesia",
|
||||
"ir": "Iran",
|
||||
"iq": "Iraq",
|
||||
"ie": "Ireland",
|
||||
"im": "Isle of Man",
|
||||
"il": "Israel",
|
||||
"it": "Italy",
|
||||
"jm": "Jamaica",
|
||||
"jp": "Japan",
|
||||
"je": "Jersey",
|
||||
"jo": "Jordan",
|
||||
"kz": "Kazakhstan",
|
||||
"ke": "Kenya",
|
||||
"ki": "Kiribati",
|
||||
"kr": "Korea",
|
||||
"kw": "Kuwait",
|
||||
"kg": "Kyrgyzstan",
|
||||
"la": "Lao People's Democratic Republic",
|
||||
"lv": "Latvia",
|
||||
"lb": "Lebanon",
|
||||
"ls": "Lesotho",
|
||||
"lr": "Liberia",
|
||||
"ly": "Libya",
|
||||
"li": "Liechtenstein",
|
||||
"lt": "Lithuania",
|
||||
"lu": "Luxembourg",
|
||||
"mo": "Macao",
|
||||
"mk": "Macedonia",
|
||||
"mg": "Madagascar",
|
||||
"mw": "Malawi",
|
||||
"my": "Malaysia",
|
||||
"mv": "Maldives",
|
||||
"ml": "Mali",
|
||||
"mt": "Malta",
|
||||
"mh": "Marshall Islands",
|
||||
"mq": "Martinique",
|
||||
"mr": "Mauritania",
|
||||
"mu": "Mauritius",
|
||||
"yt": "Mayotte",
|
||||
"mx": "Mexico",
|
||||
"fm": "Micronesia",
|
||||
"md": "Moldova",
|
||||
"mc": "Monaco",
|
||||
"mn": "Mongolia",
|
||||
"me": "Montenegro",
|
||||
"ms": "Montserrat",
|
||||
"ma": "Morocco",
|
||||
"mz": "Mozambique",
|
||||
"mm": "Myanmar",
|
||||
"na": "Namibia",
|
||||
"nr": "Nauru",
|
||||
"np": "Nepal",
|
||||
"nl": "Netherlands",
|
||||
"nc": "New Caledonia",
|
||||
"nz": "New Zealand",
|
||||
"ni": "Nicaragua",
|
||||
"ne": "Niger",
|
||||
"ng": "Nigeria",
|
||||
"nu": "Niue",
|
||||
"nf": "Norfolk Island",
|
||||
"mp": "Northern Mariana Islands",
|
||||
"no": "Norway",
|
||||
"om": "Oman",
|
||||
"pk": "Pakistan",
|
||||
"pw": "Palau",
|
||||
"ps": "Palestine, State of",
|
||||
"pa": "Panama",
|
||||
"pg": "Papua New Guinea",
|
||||
"py": "Paraguay",
|
||||
"pe": "Peru",
|
||||
"ph": "Philippines",
|
||||
"pn": "Pitcairn",
|
||||
"pl": "Poland",
|
||||
"pt": "Portugal",
|
||||
"pr": "Puerto Rico",
|
||||
"qa": "Qatar",
|
||||
"re": "Reunion",
|
||||
"ro": "Romania",
|
||||
"ru": "Russian Federation",
|
||||
"rw": "Rwanda",
|
||||
"bl": "Saint Barthelemy",
|
||||
"sh": "Saint Helena",
|
||||
"kn": "Saint Kitts and Nevis",
|
||||
"lc": "Saint Lucia",
|
||||
"mf": "Saint Martin",
|
||||
"pm": "Saint Pierre and Miquelon",
|
||||
"vc": "Saint Vincent and the Grenadines",
|
||||
"ws": "Samoa",
|
||||
"sm": "San Marino",
|
||||
"st": "Sao Tome and Principe",
|
||||
"sa": "Saudi Arabia",
|
||||
"sn": "Senegal",
|
||||
"rs": "Serbia",
|
||||
"sc": "Seychelles",
|
||||
"sl": "Sierra Leone",
|
||||
"sg": "Singapore",
|
||||
"sx": "Sint Maarten",
|
||||
"sk": "Slovakia",
|
||||
"si": "Slovenia",
|
||||
"sb": "Solomon Islands",
|
||||
"so": "Somalia",
|
||||
"za": "South Africa",
|
||||
"gs": "South Georgia and the South Sandwich Islands",
|
||||
"ss": "South Sudan",
|
||||
"es": "Spain",
|
||||
"lk": "Sri Lanka",
|
||||
"sd": "Sudan",
|
||||
"sr": "Suriname",
|
||||
"sj": "Svalbard and Jan Mayen",
|
||||
"sz": "Swaziland",
|
||||
"se": "Sweden",
|
||||
"ch": "Switzerland",
|
||||
"sy": "Syrian Arab Republic",
|
||||
"tw": "Taiwan",
|
||||
"tj": "Tajikistan",
|
||||
"tz": "Tanzania",
|
||||
"th": "Thailand",
|
||||
"tl": "Timor-Leste",
|
||||
"tg": "Togo",
|
||||
"tk": "Tokelau",
|
||||
"to": "Tonga",
|
||||
"tt": "Trinidad and Tobago",
|
||||
"tn": "Tunisia",
|
||||
"tr": "Turkey",
|
||||
"tm": "Turkmenistan",
|
||||
"tc": "Turks and Caicos Islands",
|
||||
"tv": "Tuvalu",
|
||||
"ug": "Uganda",
|
||||
"ua": "Ukraine",
|
||||
"ae": "United Arab Emirates",
|
||||
"gb": "United Kingdom",
|
||||
"um": "United States Minor Outlying Islands",
|
||||
"us": "United States",
|
||||
"uy": "Uruguay",
|
||||
"vi": "US Virgin Islands",
|
||||
"uz": "Uzbekistan",
|
||||
"vu": "Vanuatu",
|
||||
"va": "Vatican City State",
|
||||
"ve": "Venezuela",
|
||||
"vn": "Vietnam",
|
||||
"wf": "Wallis and Futuna",
|
||||
"eh": "Western Sahara",
|
||||
"ye": "Yemen",
|
||||
"zm": "Zambia",
|
||||
"zw": "Zimbabwe",
|
||||
}
|
||||
}
|
||||
15
internal/updater/providers/cyberghost/countries.go
Normal file
15
internal/updater/providers/cyberghost/countries.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package cyberghost
|
||||
|
||||
func mergeCountryCodes(base, extend map[string]string) (merged map[string]string) {
|
||||
merged = make(map[string]string, len(base))
|
||||
for countryCode, region := range base {
|
||||
merged[countryCode] = region
|
||||
}
|
||||
for countryCode := range base {
|
||||
delete(extend, countryCode)
|
||||
}
|
||||
for countryCode, region := range extend {
|
||||
merged[countryCode] = region
|
||||
}
|
||||
return merged
|
||||
}
|
||||
65
internal/updater/providers/cyberghost/hosttoserver.go
Normal file
65
internal/updater/providers/cyberghost/hosttoserver.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package cyberghost
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/constants"
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
type hostToServer map[string]models.CyberghostServer
|
||||
|
||||
func getPossibleServers() (possibleServers hostToServer) {
|
||||
groups := getGroups()
|
||||
|
||||
cyberghostCountryCodes := getSubdomainToRegion()
|
||||
allCountryCodes := constants.CountryCodes()
|
||||
possibleCountryCodes := mergeCountryCodes(cyberghostCountryCodes, allCountryCodes)
|
||||
|
||||
n := len(groups) * len(possibleCountryCodes)
|
||||
|
||||
possibleServers = make(hostToServer, n) // key is the host
|
||||
|
||||
for groupID, groupName := range groups {
|
||||
for countryCode, region := range possibleCountryCodes {
|
||||
const domain = "cg-dialup.net"
|
||||
possibleHost := groupID + "-" + countryCode + "." + domain
|
||||
possibleServer := models.CyberghostServer{
|
||||
Region: region,
|
||||
Group: groupName,
|
||||
}
|
||||
possibleServers[possibleHost] = possibleServer
|
||||
}
|
||||
}
|
||||
|
||||
return possibleServers
|
||||
}
|
||||
|
||||
func (hts hostToServer) hostsSlice() (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) toSlice() (servers []models.CyberghostServer) {
|
||||
servers = make([]models.CyberghostServer, 0, len(hts))
|
||||
for _, server := range hts {
|
||||
servers = append(servers, server)
|
||||
}
|
||||
return servers
|
||||
}
|
||||
42
internal/updater/providers/cyberghost/resolve.go
Normal file
42
internal/updater/providers/cyberghost/resolve.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package cyberghost
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/updater/resolver"
|
||||
)
|
||||
|
||||
func resolveHosts(ctx context.Context, presolver resolver.Parallel,
|
||||
possibleHosts []string, minServers int) (
|
||||
hostToIPs map[string][]net.IP, err error) {
|
||||
const (
|
||||
maxFailRatio = 1
|
||||
maxDuration = 10 * time.Second
|
||||
betweenDuration = 500 * time.Millisecond
|
||||
maxNoNew = 2
|
||||
maxFails = 10
|
||||
)
|
||||
settings := resolver.ParallelSettings{
|
||||
MaxFailRatio: maxFailRatio,
|
||||
MinFound: minServers,
|
||||
Repeat: resolver.RepeatSettings{
|
||||
MaxDuration: maxDuration,
|
||||
BetweenDuration: betweenDuration,
|
||||
MaxNoNew: maxNoNew,
|
||||
MaxFails: maxFails,
|
||||
SortIPs: true,
|
||||
},
|
||||
}
|
||||
hostToIPs, _, err = presolver.Resolve(ctx, possibleHosts, settings)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return hostToIPs, nil
|
||||
}
|
||||
28
internal/updater/providers/cyberghost/servers.go
Normal file
28
internal/updater/providers/cyberghost/servers.go
Normal file
@@ -0,0 +1,28 @@
|
||||
// Package cyberghost contains code to obtain the server information
|
||||
// for the Cyberghost provider.
|
||||
package cyberghost
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
"github.com/qdm12/gluetun/internal/updater/resolver"
|
||||
)
|
||||
|
||||
func GetServers(ctx context.Context, presolver resolver.Parallel,
|
||||
minServers int) (servers []models.CyberghostServer, err error) {
|
||||
possibleServers := getPossibleServers()
|
||||
|
||||
possibleHosts := possibleServers.hostsSlice()
|
||||
hostToIPs, err := resolveHosts(ctx, presolver, possibleHosts, minServers)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
possibleServers.adaptWithIPs(hostToIPs)
|
||||
|
||||
servers = possibleServers.toSlice()
|
||||
|
||||
sortServers(servers)
|
||||
return servers, nil
|
||||
}
|
||||
16
internal/updater/providers/cyberghost/sort.go
Normal file
16
internal/updater/providers/cyberghost/sort.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package cyberghost
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
func sortServers(servers []models.CyberghostServer) {
|
||||
sort.Slice(servers, func(i, j int) bool {
|
||||
if servers[i].Region == servers[j].Region {
|
||||
return servers[i].Group < servers[j].Group
|
||||
}
|
||||
return servers[i].Region < servers[j].Region
|
||||
})
|
||||
}
|
||||
15
internal/updater/providers/cyberghost/string.go
Normal file
15
internal/updater/providers/cyberghost/string.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package cyberghost
|
||||
|
||||
import "github.com/qdm12/gluetun/internal/models"
|
||||
|
||||
// Stringify converts servers to code string format.
|
||||
func Stringify(servers []models.CyberghostServer) (s string) {
|
||||
s = "func CyberghostServers() []models.CyberghostServer {\n"
|
||||
s += " return []models.CyberghostServer{\n"
|
||||
for _, server := range servers {
|
||||
s += " " + server.String() + ",\n"
|
||||
}
|
||||
s += " }\n"
|
||||
s += "}"
|
||||
return s
|
||||
}
|
||||
39
internal/updater/providers/fastestvpn/filename.go
Normal file
39
internal/updater/providers/fastestvpn/filename.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package fastestvpn
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var errFilenameNoProtocolSuffix = errors.New("filename does not have a protocol suffix")
|
||||
|
||||
var trailNumberExp = regexp.MustCompile(`[0-9]+$`)
|
||||
|
||||
func parseFilename(fileName string) (
|
||||
country string, tcp, udp bool, err error,
|
||||
) {
|
||||
const (
|
||||
tcpSuffix = "-TCP.ovpn"
|
||||
udpSuffix = "-UDP.ovpn"
|
||||
)
|
||||
var suffix string
|
||||
switch {
|
||||
case strings.HasSuffix(fileName, tcpSuffix):
|
||||
suffix = tcpSuffix
|
||||
tcp = true
|
||||
case strings.HasSuffix(fileName, udpSuffix):
|
||||
suffix = udpSuffix
|
||||
udp = true
|
||||
default:
|
||||
return "", false, false, fmt.Errorf("%w: %s",
|
||||
errFilenameNoProtocolSuffix, fileName)
|
||||
}
|
||||
|
||||
countryWithNumber := strings.TrimSuffix(fileName, suffix)
|
||||
number := trailNumberExp.FindString(countryWithNumber)
|
||||
country = countryWithNumber[:len(countryWithNumber)-len(number)]
|
||||
|
||||
return country, tcp, udp, nil
|
||||
}
|
||||
53
internal/updater/providers/fastestvpn/hosttoserver.go
Normal file
53
internal/updater/providers/fastestvpn/hosttoserver.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package fastestvpn
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
type hostToServer map[string]models.FastestvpnServer
|
||||
|
||||
func (hts hostToServer) add(host, country string, tcp, udp bool) {
|
||||
server, ok := hts[host]
|
||||
if !ok {
|
||||
server.Hostname = host
|
||||
server.Country = country
|
||||
}
|
||||
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][]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.FastestvpnServer) {
|
||||
servers = make([]models.FastestvpnServer, 0, len(hts))
|
||||
for _, server := range hts {
|
||||
servers = append(servers, server)
|
||||
}
|
||||
return servers
|
||||
}
|
||||
30
internal/updater/providers/fastestvpn/resolve.go
Normal file
30
internal/updater/providers/fastestvpn/resolve.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package fastestvpn
|
||||
|
||||
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
|
||||
maxNoNew = 1
|
||||
maxFails = 2
|
||||
)
|
||||
settings := resolver.ParallelSettings{
|
||||
MaxFailRatio: maxFailRatio,
|
||||
MinFound: minServers,
|
||||
Repeat: resolver.RepeatSettings{
|
||||
MaxDuration: time.Second,
|
||||
MaxNoNew: maxNoNew,
|
||||
MaxFails: maxFails,
|
||||
SortIPs: true,
|
||||
},
|
||||
}
|
||||
return presolver.Resolve(ctx, hosts, settings)
|
||||
}
|
||||
82
internal/updater/providers/fastestvpn/servers.go
Normal file
82
internal/updater/providers/fastestvpn/servers.go
Normal file
@@ -0,0 +1,82 @@
|
||||
// Package fastestvpn contains code to obtain the server information
|
||||
// for the FastestVPN provider.
|
||||
package fastestvpn
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
var ErrNotEnoughServers = errors.New("not enough servers found")
|
||||
|
||||
func GetServers(ctx context.Context, unzipper unzip.Unzipper,
|
||||
presolver resolver.Parallel, minServers int) (
|
||||
servers []models.FastestvpnServer, warnings []string, err error) {
|
||||
const url = "https://support.fastestvpn.com/download/openvpn-tcp-udp-config-files"
|
||||
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 // not an OpenVPN file
|
||||
}
|
||||
|
||||
country, tcp, udp, err := parseFilename(fileName)
|
||||
if err != nil {
|
||||
warnings = append(warnings, err.Error())
|
||||
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, country, tcp, udp)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
16
internal/updater/providers/fastestvpn/sort.go
Normal file
16
internal/updater/providers/fastestvpn/sort.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package fastestvpn
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
func sortServers(servers []models.FastestvpnServer) {
|
||||
sort.Slice(servers, func(i, j int) bool {
|
||||
if servers[i].Country == servers[j].Country {
|
||||
return servers[i].Hostname < servers[j].Hostname
|
||||
}
|
||||
return servers[i].Country < servers[j].Country
|
||||
})
|
||||
}
|
||||
14
internal/updater/providers/fastestvpn/string.go
Normal file
14
internal/updater/providers/fastestvpn/string.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package fastestvpn
|
||||
|
||||
import "github.com/qdm12/gluetun/internal/models"
|
||||
|
||||
func Stringify(servers []models.FastestvpnServer) (s string) {
|
||||
s = "func FastestvpnServers() []models.FastestvpnServer {\n"
|
||||
s += " return []models.FastestvpnServer{\n"
|
||||
for _, server := range servers {
|
||||
s += " " + server.String() + ",\n"
|
||||
}
|
||||
s += " }\n"
|
||||
s += "}"
|
||||
return s
|
||||
}
|
||||
19
internal/updater/providers/hidemyass/hosts.go
Normal file
19
internal/updater/providers/hidemyass/hosts.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package hidemyass
|
||||
|
||||
func getUniqueHosts(tcpHostToURL, udpHostToURL map[string]string) (
|
||||
hosts []string) {
|
||||
uniqueHosts := make(map[string]struct{}, len(tcpHostToURL))
|
||||
for host := range tcpHostToURL {
|
||||
uniqueHosts[host] = struct{}{}
|
||||
}
|
||||
for host := range udpHostToURL {
|
||||
uniqueHosts[host] = struct{}{}
|
||||
}
|
||||
|
||||
hosts = make([]string, 0, len(uniqueHosts))
|
||||
for host := range uniqueHosts {
|
||||
hosts = append(hosts, host)
|
||||
}
|
||||
|
||||
return hosts
|
||||
}
|
||||
37
internal/updater/providers/hidemyass/hosttourl.go
Normal file
37
internal/updater/providers/hidemyass/hosttourl.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package hidemyass
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/updater/openvpn"
|
||||
)
|
||||
|
||||
func getAllHostToURL(ctx context.Context, client *http.Client) (
|
||||
tcpHostToURL, udpHostToURL map[string]string, err error) {
|
||||
tcpHostToURL, err = getHostToURL(ctx, client, "TCP")
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
udpHostToURL, err = getHostToURL(ctx, client, "UDP")
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return tcpHostToURL, udpHostToURL, nil
|
||||
}
|
||||
|
||||
func getHostToURL(ctx context.Context, client *http.Client, protocol string) (
|
||||
hostToURL map[string]string, err error) {
|
||||
const baseURL = "https://vpn.hidemyass.com/vpn-config"
|
||||
indexURL := baseURL + "/" + strings.ToUpper(protocol) + "/"
|
||||
|
||||
urls, err := fetchIndex(ctx, client, indexURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return openvpn.FetchMultiFiles(ctx, client, urls)
|
||||
}
|
||||
54
internal/updater/providers/hidemyass/index.go
Normal file
54
internal/updater/providers/hidemyass/index.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package hidemyass
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var indexOpenvpnLinksRegex = regexp.MustCompile(`<a[ ]+href=".+\.ovpn">.+\.ovpn</a>`)
|
||||
|
||||
func fetchIndex(ctx context.Context, client *http.Client, indexURL string) (
|
||||
openvpnURLs []string, err error) {
|
||||
request, err := http.NewRequestWithContext(ctx, http.MethodGet, indexURL, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
htmlCode, err := io.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !strings.HasSuffix(indexURL, "/") {
|
||||
indexURL += "/"
|
||||
}
|
||||
|
||||
lines := strings.Split(string(htmlCode), "\n")
|
||||
for _, line := range lines {
|
||||
found := indexOpenvpnLinksRegex.FindString(line)
|
||||
if len(found) == 0 {
|
||||
continue
|
||||
}
|
||||
const prefix = `.ovpn">`
|
||||
const suffix = `</a>`
|
||||
startIndex := strings.Index(found, prefix) + len(prefix)
|
||||
endIndex := strings.Index(found, suffix)
|
||||
filename := found[startIndex:endIndex]
|
||||
openvpnURL := indexURL + filename
|
||||
if !strings.HasSuffix(openvpnURL, ".ovpn") {
|
||||
continue
|
||||
}
|
||||
openvpnURLs = append(openvpnURLs, openvpnURL)
|
||||
}
|
||||
|
||||
return openvpnURLs, nil
|
||||
}
|
||||
33
internal/updater/providers/hidemyass/resolve.go
Normal file
33
internal/updater/providers/hidemyass/resolve.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package hidemyass
|
||||
|
||||
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 = 15 * time.Second
|
||||
betweenDuration = 2 * 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)
|
||||
}
|
||||
68
internal/updater/providers/hidemyass/servers.go
Normal file
68
internal/updater/providers/hidemyass/servers.go
Normal file
@@ -0,0 +1,68 @@
|
||||
// Package hidemyass contains code to obtain the server information
|
||||
// for the HideMyAss provider.
|
||||
package hidemyass
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
"github.com/qdm12/gluetun/internal/updater/resolver"
|
||||
)
|
||||
|
||||
var ErrNotEnoughServers = errors.New("not enough servers found")
|
||||
|
||||
func GetServers(ctx context.Context, client *http.Client,
|
||||
presolver resolver.Parallel, minServers int) (
|
||||
servers []models.HideMyAssServer, warnings []string, err error) {
|
||||
tcpHostToURL, udpHostToURL, err := getAllHostToURL(ctx, client)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
hosts := getUniqueHosts(tcpHostToURL, udpHostToURL)
|
||||
|
||||
if len(hosts) < minServers {
|
||||
return nil, nil, fmt.Errorf("%w: %d and expected at least %d",
|
||||
ErrNotEnoughServers, len(hosts), minServers)
|
||||
}
|
||||
|
||||
hostToIPs, warnings, err := resolveHosts(ctx, presolver, hosts, minServers)
|
||||
if err != nil {
|
||||
return nil, warnings, err
|
||||
}
|
||||
|
||||
servers = make([]models.HideMyAssServer, 0, len(hostToIPs))
|
||||
for host, IPs := range hostToIPs {
|
||||
tcpURL, tcp := tcpHostToURL[host]
|
||||
udpURL, udp := udpHostToURL[host]
|
||||
|
||||
// These two are only used to extract the country, region and city.
|
||||
var url, protocol string
|
||||
if tcp {
|
||||
url = tcpURL
|
||||
protocol = "TCP"
|
||||
} else if udp {
|
||||
url = udpURL
|
||||
protocol = "UDP"
|
||||
}
|
||||
country, region, city := parseOpenvpnURL(url, protocol)
|
||||
|
||||
server := models.HideMyAssServer{
|
||||
Country: country,
|
||||
Region: region,
|
||||
City: city,
|
||||
Hostname: host,
|
||||
IPs: IPs,
|
||||
TCP: tcp,
|
||||
UDP: udp,
|
||||
}
|
||||
servers = append(servers, server)
|
||||
}
|
||||
|
||||
sortServers(servers)
|
||||
|
||||
return servers, warnings, nil
|
||||
}
|
||||
22
internal/updater/providers/hidemyass/sort.go
Normal file
22
internal/updater/providers/hidemyass/sort.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package hidemyass
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
func sortServers(servers []models.HideMyAssServer) {
|
||||
sort.Slice(servers, func(i, j int) bool {
|
||||
if servers[i].Country == servers[j].Country {
|
||||
if servers[i].Region == servers[j].Region {
|
||||
if servers[i].City == servers[j].City {
|
||||
return servers[i].Hostname < servers[j].Hostname
|
||||
}
|
||||
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/hidemyass/string.go
Normal file
14
internal/updater/providers/hidemyass/string.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package hidemyass
|
||||
|
||||
import "github.com/qdm12/gluetun/internal/models"
|
||||
|
||||
func Stringify(servers []models.HideMyAssServer) (s string) {
|
||||
s = "func HideMyAssServers() []models.HideMyAssServer {\n"
|
||||
s += " return []models.HideMyAssServer{\n"
|
||||
for _, server := range servers {
|
||||
s += " " + server.String() + ",\n"
|
||||
}
|
||||
s += " }\n"
|
||||
s += "}"
|
||||
return s
|
||||
}
|
||||
44
internal/updater/providers/hidemyass/url.go
Normal file
44
internal/updater/providers/hidemyass/url.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package hidemyass
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
func parseOpenvpnURL(url, protocol string) (country, region, city string) {
|
||||
lastSlashIndex := strings.LastIndex(url, "/")
|
||||
url = url[lastSlashIndex+1:]
|
||||
|
||||
suffix := "." + strings.ToUpper(protocol) + ".ovpn"
|
||||
url = strings.TrimSuffix(url, suffix)
|
||||
|
||||
parts := strings.Split(url, ".")
|
||||
|
||||
switch len(parts) {
|
||||
case 1:
|
||||
country = parts[0]
|
||||
return country, "", ""
|
||||
case 2: //nolint:gomnd
|
||||
country = parts[0]
|
||||
city = parts[1]
|
||||
default:
|
||||
country = parts[0]
|
||||
region = parts[1]
|
||||
city = parts[2]
|
||||
}
|
||||
|
||||
return camelCaseToWords(country), camelCaseToWords(region),
|
||||
camelCaseToWords(city)
|
||||
}
|
||||
|
||||
func camelCaseToWords(camelCase string) (words string) {
|
||||
wasLowerCase := false
|
||||
for _, r := range camelCase {
|
||||
if wasLowerCase && unicode.IsUpper(r) {
|
||||
words += " "
|
||||
}
|
||||
wasLowerCase = unicode.IsLower(r)
|
||||
words += string(r)
|
||||
}
|
||||
return words
|
||||
}
|
||||
55
internal/updater/providers/mullvad/api.go
Normal file
55
internal/updater/providers/mullvad/api.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package mullvad
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrHTTPStatusCodeNotOK = errors.New("HTTP status code not OK")
|
||||
ErrUnmarshalResponseBody = errors.New("failed unmarshaling response body")
|
||||
)
|
||||
|
||||
type serverData struct {
|
||||
Hostname string `json:"hostname"`
|
||||
Country string `json:"country_name"`
|
||||
City string `json:"city_name"`
|
||||
Active bool `json:"active"`
|
||||
Owned bool `json:"owned"`
|
||||
Provider string `json:"provider"`
|
||||
IPv4 string `json:"ipv4_addr_in"`
|
||||
IPv6 string `json:"ipv6_addr_in"`
|
||||
}
|
||||
|
||||
func fetchAPI(ctx context.Context, client *http.Client) (data []serverData, err error) {
|
||||
const url = "https://api.mullvad.net/www/relays/openvpn/"
|
||||
|
||||
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("%w: %s", ErrHTTPStatusCodeNotOK, response.Status)
|
||||
}
|
||||
|
||||
decoder := json.NewDecoder(response.Body)
|
||||
if err := decoder.Decode(&data); err != nil {
|
||||
return nil, fmt.Errorf("%w: %s", ErrUnmarshalResponseBody, err)
|
||||
}
|
||||
|
||||
if err := response.Body.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
58
internal/updater/providers/mullvad/hosttoserver.go
Normal file
58
internal/updater/providers/mullvad/hosttoserver.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package mullvad
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
type hostToServer map[string]models.MullvadServer
|
||||
|
||||
var (
|
||||
ErrParseIPv4 = errors.New("cannot parse IPv4 address")
|
||||
ErrParseIPv6 = errors.New("cannot parse IPv6 address")
|
||||
)
|
||||
|
||||
func (hts hostToServer) add(data serverData) (err error) {
|
||||
if !data.Active {
|
||||
return
|
||||
}
|
||||
|
||||
ipv4 := net.ParseIP(data.IPv4)
|
||||
if ipv4 == nil || ipv4.To4() == nil {
|
||||
return fmt.Errorf("%w: %s", ErrParseIPv4, data.IPv4)
|
||||
}
|
||||
|
||||
ipv6 := net.ParseIP(data.IPv6)
|
||||
if ipv6 == nil || ipv6.To4() != nil {
|
||||
return fmt.Errorf("%w: %s", ErrParseIPv6, data.IPv6)
|
||||
}
|
||||
|
||||
server, ok := hts[data.Hostname]
|
||||
if !ok {
|
||||
server.Country = data.Country
|
||||
server.City = strings.ReplaceAll(data.City, ",", "")
|
||||
server.ISP = data.Provider
|
||||
server.Owned = data.Owned
|
||||
}
|
||||
|
||||
server.IPs = append(server.IPs, ipv4)
|
||||
server.IPsV6 = append(server.IPsV6, ipv6)
|
||||
|
||||
hts[data.Hostname] = server
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (hts hostToServer) toServersSlice() (servers []models.MullvadServer) {
|
||||
servers = make([]models.MullvadServer, 0, len(hts))
|
||||
for _, server := range hts {
|
||||
server.IPs = uniqueSortedIPs(server.IPs)
|
||||
server.IPsV6 = uniqueSortedIPs(server.IPsV6)
|
||||
servers = append(servers, server)
|
||||
}
|
||||
return servers
|
||||
}
|
||||
30
internal/updater/providers/mullvad/ips.go
Normal file
30
internal/updater/providers/mullvad/ips.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package mullvad
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net"
|
||||
"sort"
|
||||
)
|
||||
|
||||
func uniqueSortedIPs(ips []net.IP) []net.IP {
|
||||
uniqueIPs := make(map[string]struct{}, len(ips))
|
||||
for _, ip := range ips {
|
||||
key := ip.String()
|
||||
uniqueIPs[key] = struct{}{}
|
||||
}
|
||||
|
||||
ips = make([]net.IP, 0, len(uniqueIPs))
|
||||
for key := range uniqueIPs {
|
||||
ip := net.ParseIP(key)
|
||||
if ipv4 := ip.To4(); ipv4 != nil {
|
||||
ip = ipv4
|
||||
}
|
||||
ips = append(ips, ip)
|
||||
}
|
||||
|
||||
sort.Slice(ips, func(i, j int) bool {
|
||||
return bytes.Compare(ips[i], ips[j]) < 0
|
||||
})
|
||||
|
||||
return ips
|
||||
}
|
||||
41
internal/updater/providers/mullvad/ips_test.go
Normal file
41
internal/updater/providers/mullvad/ips_test.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package mullvad
|
||||
|
||||
import (
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_uniqueSortedIPs(t *testing.T) {
|
||||
t.Parallel()
|
||||
testCases := map[string]struct {
|
||||
inputIPs []net.IP
|
||||
outputIPs []net.IP
|
||||
}{
|
||||
"nil": {
|
||||
inputIPs: nil,
|
||||
outputIPs: []net.IP{},
|
||||
},
|
||||
"empty": {
|
||||
inputIPs: []net.IP{},
|
||||
outputIPs: []net.IP{},
|
||||
},
|
||||
"single IPv4": {
|
||||
inputIPs: []net.IP{{1, 1, 1, 1}},
|
||||
outputIPs: []net.IP{{1, 1, 1, 1}},
|
||||
},
|
||||
"two IPv4s": {
|
||||
inputIPs: []net.IP{{1, 1, 2, 1}, {1, 1, 1, 1}},
|
||||
outputIPs: []net.IP{{1, 1, 1, 1}, {1, 1, 2, 1}},
|
||||
},
|
||||
}
|
||||
for name, testCase := range testCases {
|
||||
testCase := testCase
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
outputIPs := uniqueSortedIPs(testCase.inputIPs)
|
||||
assert.Equal(t, testCase.outputIPs, outputIPs)
|
||||
})
|
||||
}
|
||||
}
|
||||
68
internal/updater/providers/mullvad/servers.go
Normal file
68
internal/updater/providers/mullvad/servers.go
Normal file
@@ -0,0 +1,68 @@
|
||||
// Package mullvad contains code to obtain the server information
|
||||
// for the Mullvad provider.
|
||||
package mullvad
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
var ErrNotEnoughServers = errors.New("not enough servers found")
|
||||
|
||||
func GetServers(ctx context.Context, client *http.Client, minServers int) (
|
||||
servers []models.MullvadServer, err error) {
|
||||
data, err := fetchAPI(ctx, client)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
hts := make(hostToServer)
|
||||
for _, serverData := range data {
|
||||
if err := hts.add(serverData); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if len(hts) < minServers {
|
||||
return nil, fmt.Errorf("%w: %d and expected at least %d",
|
||||
ErrNotEnoughServers, len(hts), minServers)
|
||||
}
|
||||
|
||||
servers = hts.toServersSlice()
|
||||
|
||||
servers = groupByProperties(servers)
|
||||
|
||||
sortServers(servers)
|
||||
|
||||
return servers, nil
|
||||
}
|
||||
|
||||
// TODO group by hostname so remove this.
|
||||
func groupByProperties(serversByHost []models.MullvadServer) (serversByProps []models.MullvadServer) {
|
||||
propsToServer := make(map[string]models.MullvadServer, len(serversByHost))
|
||||
for _, server := range serversByHost {
|
||||
key := server.Country + server.City + server.ISP + strconv.FormatBool(server.Owned)
|
||||
serverByProps, ok := propsToServer[key]
|
||||
if !ok {
|
||||
serverByProps.Country = server.Country
|
||||
serverByProps.City = server.City
|
||||
serverByProps.ISP = server.ISP
|
||||
serverByProps.Owned = server.Owned
|
||||
}
|
||||
serverByProps.IPs = append(serverByProps.IPs, server.IPs...)
|
||||
serverByProps.IPsV6 = append(serverByProps.IPsV6, server.IPsV6...)
|
||||
propsToServer[key] = serverByProps
|
||||
}
|
||||
|
||||
serversByProps = make([]models.MullvadServer, 0, len(propsToServer))
|
||||
for _, serverByProp := range propsToServer {
|
||||
serversByProps = append(serversByProps, serverByProp)
|
||||
}
|
||||
|
||||
return serversByProps
|
||||
}
|
||||
19
internal/updater/providers/mullvad/sort.go
Normal file
19
internal/updater/providers/mullvad/sort.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package mullvad
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
func sortServers(servers []models.MullvadServer) {
|
||||
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].ISP < servers[j].ISP
|
||||
}
|
||||
return servers[i].City < servers[j].City
|
||||
}
|
||||
return servers[i].Country < servers[j].Country
|
||||
})
|
||||
}
|
||||
14
internal/updater/providers/mullvad/string.go
Normal file
14
internal/updater/providers/mullvad/string.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package mullvad
|
||||
|
||||
import "github.com/qdm12/gluetun/internal/models"
|
||||
|
||||
func Stringify(servers []models.MullvadServer) (s string) {
|
||||
s = "func MullvadServers() []models.MullvadServer {\n"
|
||||
s += " return []models.MullvadServer{\n"
|
||||
for _, server := range servers {
|
||||
s += " " + server.String() + ",\n"
|
||||
}
|
||||
s += " }\n"
|
||||
s += "}"
|
||||
return s
|
||||
}
|
||||
32
internal/updater/providers/mullvad/string_test.go
Normal file
32
internal/updater/providers/mullvad/string_test.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package mullvad
|
||||
|
||||
import (
|
||||
"net"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_Stringify(t *testing.T) {
|
||||
servers := []models.MullvadServer{{
|
||||
Country: "webland",
|
||||
City: "webcity",
|
||||
ISP: "not nsa",
|
||||
Owned: true,
|
||||
IPs: []net.IP{{1, 1, 1, 1}},
|
||||
IPsV6: []net.IP{{1, 1, 1, 1}},
|
||||
}}
|
||||
//nolint:lll
|
||||
expected := `
|
||||
func MullvadServers() []models.MullvadServer {
|
||||
return []models.MullvadServer{
|
||||
{Country: "webland", City: "webcity", ISP: "not nsa", Owned: true, IPs: []net.IP{{1, 1, 1, 1}}, IPsV6: []net.IP{{1, 1, 1, 1}}},
|
||||
}
|
||||
}
|
||||
`
|
||||
expected = strings.TrimPrefix(strings.TrimSuffix(expected, "\n"), "\n")
|
||||
s := Stringify(servers)
|
||||
assert.Equal(t, expected, s)
|
||||
}
|
||||
55
internal/updater/providers/nordvpn/api.go
Normal file
55
internal/updater/providers/nordvpn/api.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package nordvpn
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrHTTPStatusCodeNotOK = errors.New("HTTP status code not OK")
|
||||
ErrUnmarshalResponseBody = errors.New("failed unmarshaling response body")
|
||||
)
|
||||
|
||||
type serverData struct {
|
||||
Domain string `json:"domain"`
|
||||
IPAddress string `json:"ip_address"`
|
||||
Name string `json:"name"`
|
||||
Country string `json:"country"`
|
||||
Features struct {
|
||||
UDP bool `json:"openvpn_udp"`
|
||||
TCP bool `json:"openvpn_tcp"`
|
||||
} `json:"features"`
|
||||
}
|
||||
|
||||
func fetchAPI(ctx context.Context, client *http.Client) (data []serverData, err error) {
|
||||
const url = "https://nordvpn.com/api/server"
|
||||
|
||||
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("%w: %s", ErrHTTPStatusCodeNotOK, response.Status)
|
||||
}
|
||||
|
||||
decoder := json.NewDecoder(response.Body)
|
||||
if err := decoder.Decode(&data); err != nil {
|
||||
return nil, fmt.Errorf("%w: %s", ErrUnmarshalResponseBody, err)
|
||||
}
|
||||
|
||||
if err := response.Body.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
16
internal/updater/providers/nordvpn/ip.go
Normal file
16
internal/updater/providers/nordvpn/ip.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package nordvpn
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
)
|
||||
|
||||
func parseIPv4(s string) (ipv4 net.IP, err error) {
|
||||
ip := net.ParseIP(s)
|
||||
if ip == nil {
|
||||
return nil, fmt.Errorf("%w: %q", ErrParseIP, s)
|
||||
} else if ip.To4() == nil {
|
||||
return nil, fmt.Errorf("%w: %s", ErrNotIPv4, ip)
|
||||
}
|
||||
return ip, nil
|
||||
}
|
||||
29
internal/updater/providers/nordvpn/name.go
Normal file
29
internal/updater/providers/nordvpn/name.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package nordvpn
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNoIDInServerName = errors.New("no ID in server name")
|
||||
ErrInvalidIDInServerName = errors.New("invalid ID in server name")
|
||||
)
|
||||
|
||||
func parseServerName(serverName string) (number uint16, err error) {
|
||||
i := strings.IndexRune(serverName, '#')
|
||||
if i < 0 {
|
||||
return 0, fmt.Errorf("%w: %s", ErrNoIDInServerName, serverName)
|
||||
}
|
||||
|
||||
idString := serverName[i+1:]
|
||||
idUint64, err := strconv.ParseUint(idString, 10, 16)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("%w: %s", ErrInvalidIDInServerName, serverName)
|
||||
}
|
||||
|
||||
number = uint16(idUint64)
|
||||
return number, nil
|
||||
}
|
||||
64
internal/updater/providers/nordvpn/servers.go
Normal file
64
internal/updater/providers/nordvpn/servers.go
Normal file
@@ -0,0 +1,64 @@
|
||||
// Package nordvpn contains code to obtain the server information
|
||||
// for the NordVPN provider.
|
||||
package nordvpn
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrParseIP = errors.New("cannot parse IP address")
|
||||
ErrNotIPv4 = errors.New("IP address is not IPv4")
|
||||
ErrNotEnoughServers = errors.New("not enough servers found")
|
||||
)
|
||||
|
||||
func GetServers(ctx context.Context, client *http.Client, minServers int) (
|
||||
servers []models.NordvpnServer, warnings []string, err error) {
|
||||
data, err := fetchAPI(ctx, client)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
servers = make([]models.NordvpnServer, 0, len(data))
|
||||
|
||||
for _, jsonServer := range data {
|
||||
if !jsonServer.Features.TCP && !jsonServer.Features.UDP {
|
||||
warning := "server does not support TCP and UDP for openvpn: " + jsonServer.Name
|
||||
warnings = append(warnings, warning)
|
||||
continue
|
||||
}
|
||||
|
||||
ip, err := parseIPv4(jsonServer.IPAddress)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("%w for server %s", err, jsonServer.Name)
|
||||
}
|
||||
|
||||
number, err := parseServerName(jsonServer.Name)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
server := models.NordvpnServer{
|
||||
Region: jsonServer.Country,
|
||||
Number: number,
|
||||
IP: ip,
|
||||
TCP: jsonServer.Features.TCP,
|
||||
UDP: jsonServer.Features.UDP,
|
||||
}
|
||||
servers = append(servers, server)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
16
internal/updater/providers/nordvpn/sort.go
Normal file
16
internal/updater/providers/nordvpn/sort.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package nordvpn
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
func sortServers(servers []models.NordvpnServer) {
|
||||
sort.Slice(servers, func(i, j int) bool {
|
||||
if servers[i].Region == servers[j].Region {
|
||||
return servers[i].Number < servers[j].Number
|
||||
}
|
||||
return servers[i].Region < servers[j].Region
|
||||
})
|
||||
}
|
||||
14
internal/updater/providers/nordvpn/string.go
Normal file
14
internal/updater/providers/nordvpn/string.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package nordvpn
|
||||
|
||||
import "github.com/qdm12/gluetun/internal/models"
|
||||
|
||||
func Stringify(servers []models.NordvpnServer) (s string) {
|
||||
s = "func NordvpnServers() []models.NordvpnServer {\n"
|
||||
s += " return []models.NordvpnServer{\n"
|
||||
for _, server := range servers {
|
||||
s += " " + server.String() + ",\n"
|
||||
}
|
||||
s += " }\n"
|
||||
s += "}"
|
||||
return s
|
||||
}
|
||||
74
internal/updater/providers/pia/api.go
Normal file
74
internal/updater/providers/pia/api.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package pia
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrHTTPStatusCodeNotOK = errors.New("HTTP status code not OK")
|
||||
)
|
||||
|
||||
type apiData struct {
|
||||
Regions []regionData `json:"regions"`
|
||||
}
|
||||
|
||||
type regionData struct {
|
||||
Name string `json:"name"`
|
||||
PortForward bool `json:"port_forward"`
|
||||
Offline bool `json:"offline"`
|
||||
Servers struct {
|
||||
UDP []serverData `json:"ovpnudp"`
|
||||
TCP []serverData `json:"ovpntcp"`
|
||||
} `json:"servers"`
|
||||
}
|
||||
|
||||
type serverData struct {
|
||||
IP net.IP `json:"ip"`
|
||||
CN string `json:"cn"`
|
||||
}
|
||||
|
||||
func fetchAPI(ctx context.Context, client *http.Client) (
|
||||
data apiData, err error) {
|
||||
const url = "https://serverlist.piaservers.net/vpninfo/servers/v5"
|
||||
|
||||
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return data, err
|
||||
}
|
||||
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
return data, err
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return data, fmt.Errorf("%w: %s", ErrHTTPStatusCodeNotOK, response.Status)
|
||||
}
|
||||
|
||||
b, err := ioutil.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
return data, err
|
||||
}
|
||||
|
||||
if err := response.Body.Close(); err != nil {
|
||||
return data, err
|
||||
}
|
||||
|
||||
// remove key/signature at the bottom
|
||||
i := bytes.IndexRune(b, '\n')
|
||||
b = b[:i]
|
||||
|
||||
if err := json.Unmarshal(b, &data); err != nil {
|
||||
return data, err
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
90
internal/updater/providers/pia/servers.go
Normal file
90
internal/updater/providers/pia/servers.go
Normal file
@@ -0,0 +1,90 @@
|
||||
// Package pia contains code to obtain the server information
|
||||
// for the Private Internet Access provider.
|
||||
package pia
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
var ErrNotEnoughServers = errors.New("not enough servers found")
|
||||
|
||||
func GetServers(ctx context.Context, client *http.Client, minServers int) (
|
||||
servers []models.PIAServer, err error) {
|
||||
data, err := fetchAPI(ctx, client)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, region := range data.Regions {
|
||||
// Deduplicate servers with the same common name
|
||||
commonNameToProtocols := dedupByProtocol(region)
|
||||
|
||||
// newServers can support only UDP or both TCP and UDP
|
||||
newServers := dataToServers(region.Servers.UDP, region.Name,
|
||||
region.PortForward, commonNameToProtocols)
|
||||
servers = append(servers, newServers...)
|
||||
|
||||
// tcpServers only support TCP as mixed servers were found above.
|
||||
tcpServers := dataToServers(region.Servers.TCP, region.Name,
|
||||
region.PortForward, commonNameToProtocols)
|
||||
servers = append(servers, tcpServers...)
|
||||
}
|
||||
|
||||
if len(servers) < minServers {
|
||||
return nil, fmt.Errorf("%w: %d and expected at least %d",
|
||||
ErrNotEnoughServers, len(servers), minServers)
|
||||
}
|
||||
|
||||
sortServers(servers)
|
||||
|
||||
return servers, nil
|
||||
}
|
||||
|
||||
type protocols struct {
|
||||
tcp bool
|
||||
udp bool
|
||||
}
|
||||
|
||||
// Deduplicate servers with the same common name for different protocols.
|
||||
func dedupByProtocol(region regionData) (commonNameToProtocols map[string]protocols) {
|
||||
commonNameToProtocols = make(map[string]protocols)
|
||||
for _, udpServer := range region.Servers.UDP {
|
||||
protocols := commonNameToProtocols[udpServer.CN]
|
||||
protocols.udp = true
|
||||
commonNameToProtocols[udpServer.CN] = protocols
|
||||
}
|
||||
for _, tcpServer := range region.Servers.TCP {
|
||||
protocols := commonNameToProtocols[tcpServer.CN]
|
||||
protocols.tcp = true
|
||||
commonNameToProtocols[tcpServer.CN] = protocols
|
||||
}
|
||||
return commonNameToProtocols
|
||||
}
|
||||
|
||||
func dataToServers(data []serverData, region string, portForward bool,
|
||||
commonNameToProtocols map[string]protocols) (
|
||||
servers []models.PIAServer) {
|
||||
servers = make([]models.PIAServer, 0, len(data))
|
||||
for _, serverData := range data {
|
||||
proto, ok := commonNameToProtocols[serverData.CN]
|
||||
if !ok {
|
||||
continue // server already added
|
||||
}
|
||||
delete(commonNameToProtocols, serverData.CN)
|
||||
server := models.PIAServer{
|
||||
Region: region,
|
||||
ServerName: serverData.CN,
|
||||
TCP: proto.tcp,
|
||||
UDP: proto.udp,
|
||||
PortForward: portForward,
|
||||
IP: serverData.IP,
|
||||
}
|
||||
servers = append(servers, server)
|
||||
}
|
||||
return servers
|
||||
}
|
||||
16
internal/updater/providers/pia/sort.go
Normal file
16
internal/updater/providers/pia/sort.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package pia
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
func sortServers(servers []models.PIAServer) {
|
||||
sort.Slice(servers, func(i, j int) bool {
|
||||
if servers[i].Region == servers[j].Region {
|
||||
return servers[i].ServerName < servers[j].ServerName
|
||||
}
|
||||
return servers[i].Region < servers[j].Region
|
||||
})
|
||||
}
|
||||
14
internal/updater/providers/pia/string.go
Normal file
14
internal/updater/providers/pia/string.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package pia
|
||||
|
||||
import "github.com/qdm12/gluetun/internal/models"
|
||||
|
||||
func Stringify(servers []models.PIAServer) (s string) {
|
||||
s = "func PIAServers() []models.PIAServer {\n"
|
||||
s += " return []models.PIAServer{\n"
|
||||
for _, server := range servers {
|
||||
s += " " + server.String() + ",\n"
|
||||
}
|
||||
s += " }\n"
|
||||
s += "}"
|
||||
return s
|
||||
}
|
||||
53
internal/updater/providers/privado/hosttoserver.go
Normal file
53
internal/updater/providers/privado/hosttoserver.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package privado
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
type hostToServer map[string]models.PrivadoServer
|
||||
|
||||
func (hts hostToServer) add(host string) {
|
||||
server, ok := hts[host]
|
||||
if ok {
|
||||
return
|
||||
}
|
||||
server.Hostname = host
|
||||
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) (
|
||||
warnings []string) {
|
||||
for host, IPs := range hostToIPs {
|
||||
if len(IPs) > 1 {
|
||||
warning := "more than one IP address found for host " + host
|
||||
warnings = append(warnings, warning)
|
||||
}
|
||||
server := hts[host]
|
||||
server.IP = IPs[0]
|
||||
hts[host] = server
|
||||
}
|
||||
for host, server := range hts {
|
||||
if server.IP == nil {
|
||||
delete(hts, host)
|
||||
}
|
||||
}
|
||||
return warnings
|
||||
}
|
||||
|
||||
func (hts hostToServer) toServersSlice() (servers []models.PrivadoServer) {
|
||||
servers = make([]models.PrivadoServer, 0, len(hts))
|
||||
for _, server := range hts {
|
||||
servers = append(servers, server)
|
||||
}
|
||||
return servers
|
||||
}
|
||||
31
internal/updater/providers/privado/resolve.go
Normal file
31
internal/updater/providers/privado/resolve.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package privado
|
||||
|
||||
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 = 3 * time.Second
|
||||
maxNoNew = 1
|
||||
maxFails = 2
|
||||
)
|
||||
settings := resolver.ParallelSettings{
|
||||
MaxFailRatio: maxFailRatio,
|
||||
MinFound: minServers,
|
||||
Repeat: resolver.RepeatSettings{
|
||||
MaxDuration: maxDuration,
|
||||
MaxNoNew: maxNoNew,
|
||||
MaxFails: maxFails,
|
||||
SortIPs: true,
|
||||
},
|
||||
}
|
||||
return presolver.Resolve(ctx, hosts, settings)
|
||||
}
|
||||
72
internal/updater/providers/privado/servers.go
Normal file
72
internal/updater/providers/privado/servers.go
Normal file
@@ -0,0 +1,72 @@
|
||||
// Package privado contains code to obtain the server information
|
||||
// for the Privado provider.
|
||||
package privado
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
var ErrNotEnoughServers = errors.New("not enough servers found")
|
||||
|
||||
func GetServers(ctx context.Context, unzipper unzip.Unzipper,
|
||||
presolver resolver.Parallel, minServers int) (
|
||||
servers []models.PrivadoServer, warnings []string, err error) {
|
||||
const url = "https://privado.io/apps/ovpn_configs.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 // not an OpenVPN file
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
newWarnings = hts.adaptWithIPs(hostToIPs)
|
||||
warnings = append(warnings, newWarnings...)
|
||||
|
||||
servers = hts.toServersSlice()
|
||||
|
||||
sortServers(servers)
|
||||
|
||||
return servers, warnings, nil
|
||||
}
|
||||
13
internal/updater/providers/privado/sort.go
Normal file
13
internal/updater/providers/privado/sort.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package privado
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
func sortServers(servers []models.PrivadoServer) {
|
||||
sort.Slice(servers, func(i, j int) bool {
|
||||
return servers[i].Hostname < servers[j].Hostname
|
||||
})
|
||||
}
|
||||
14
internal/updater/providers/privado/string.go
Normal file
14
internal/updater/providers/privado/string.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package privado
|
||||
|
||||
import "github.com/qdm12/gluetun/internal/models"
|
||||
|
||||
func Stringify(servers []models.PrivadoServer) (s string) {
|
||||
s = "func PrivadoServers() []models.PrivadoServer {\n"
|
||||
s += " return []models.PrivadoServer{\n"
|
||||
for _, server := range servers {
|
||||
s += " " + server.String() + ",\n"
|
||||
}
|
||||
s += " }\n"
|
||||
s += "}"
|
||||
return s
|
||||
}
|
||||
14
internal/updater/providers/privatevpn/countries.go
Normal file
14
internal/updater/providers/privatevpn/countries.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package privatevpn
|
||||
|
||||
import "strings"
|
||||
|
||||
func codeToCountry(countryCode string, countryCodes map[string]string) (
|
||||
country string, warning string) {
|
||||
countryCode = strings.ToLower(countryCode)
|
||||
country, ok := countryCodes[countryCode]
|
||||
if !ok {
|
||||
warning = "unknown country code: " + countryCode
|
||||
country = countryCode
|
||||
}
|
||||
return country, warning
|
||||
}
|
||||
54
internal/updater/providers/privatevpn/filename.go
Normal file
54
internal/updater/providers/privatevpn/filename.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package privatevpn
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
trailingNumber = regexp.MustCompile(` [0-9]+$`)
|
||||
)
|
||||
|
||||
var (
|
||||
errBadPrefix = errors.New("bad prefix in file name")
|
||||
errBadSuffix = errors.New("bad suffix in file name")
|
||||
errNotEnoughParts = errors.New("not enough parts in file name")
|
||||
)
|
||||
|
||||
func parseFilename(fileName string) (
|
||||
countryCode, city string, err error,
|
||||
) {
|
||||
fileName = strings.ReplaceAll(fileName, " ", "") // remove spaces
|
||||
|
||||
const prefix = "PrivateVPN-"
|
||||
if !strings.HasPrefix(fileName, prefix) {
|
||||
return "", "", fmt.Errorf("%w: %s", errBadPrefix, fileName)
|
||||
}
|
||||
s := strings.TrimPrefix(fileName, prefix)
|
||||
|
||||
const tcpSuffix = "-TUN-443.ovpn"
|
||||
const udpSuffix = "-TUN-1194.ovpn"
|
||||
switch {
|
||||
case strings.HasSuffix(fileName, tcpSuffix):
|
||||
s = strings.TrimSuffix(s, tcpSuffix)
|
||||
case strings.HasSuffix(fileName, udpSuffix):
|
||||
s = strings.TrimSuffix(s, udpSuffix)
|
||||
default:
|
||||
return "", "", fmt.Errorf("%w: %s", errBadSuffix, fileName)
|
||||
}
|
||||
|
||||
s = trailingNumber.ReplaceAllString(s, "")
|
||||
|
||||
parts := strings.Split(s, "-")
|
||||
const minParts = 2
|
||||
if len(parts) < minParts {
|
||||
return "", "", fmt.Errorf("%w: %s",
|
||||
errNotEnoughParts, fileName)
|
||||
}
|
||||
countryCode, city = parts[0], parts[1]
|
||||
countryCode = strings.ToLower(countryCode)
|
||||
|
||||
return countryCode, city, nil
|
||||
}
|
||||
50
internal/updater/providers/privatevpn/hosttoserver.go
Normal file
50
internal/updater/providers/privatevpn/hosttoserver.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package privatevpn
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
type hostToServer map[string]models.PrivatevpnServer
|
||||
|
||||
// TODO check if server supports TCP and UDP.
|
||||
func (hts hostToServer) add(host, country, city string) {
|
||||
server, ok := hts[host]
|
||||
if ok {
|
||||
return
|
||||
}
|
||||
server.Hostname = host
|
||||
server.Country = country
|
||||
server.City = city
|
||||
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.PrivatevpnServer) {
|
||||
servers = make([]models.PrivatevpnServer, 0, len(hts))
|
||||
for _, server := range hts {
|
||||
servers = append(servers, server)
|
||||
}
|
||||
return servers
|
||||
}
|
||||
33
internal/updater/providers/privatevpn/resolve.go
Normal file
33
internal/updater/providers/privatevpn/resolve.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package privatevpn
|
||||
|
||||
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 = 6 * 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)
|
||||
}
|
||||
86
internal/updater/providers/privatevpn/servers.go
Normal file
86
internal/updater/providers/privatevpn/servers.go
Normal file
@@ -0,0 +1,86 @@
|
||||
// Package privatevpn contains code to obtain the server information
|
||||
// for the PrivateVPN provider.
|
||||
package privatevpn
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/constants"
|
||||
"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"
|
||||
)
|
||||
|
||||
var ErrNotEnoughServers = errors.New("not enough servers found")
|
||||
|
||||
func GetServers(ctx context.Context, unzipper unzip.Unzipper,
|
||||
presolver resolver.Parallel, minServers int) (
|
||||
servers []models.PrivatevpnServer, warnings []string, err error) {
|
||||
const url = "https://privatevpn.com/client/PrivateVPN-TUN.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)
|
||||
}
|
||||
|
||||
countryCodes := constants.CountryCodes()
|
||||
|
||||
hts := make(hostToServer)
|
||||
|
||||
for fileName, content := range contents {
|
||||
if !strings.HasSuffix(fileName, ".ovpn") {
|
||||
continue // not an OpenVPN file
|
||||
}
|
||||
|
||||
countryCode, city, err := parseFilename(fileName)
|
||||
if err != nil {
|
||||
warnings = append(warnings, err.Error())
|
||||
continue
|
||||
}
|
||||
|
||||
country, warning := codeToCountry(countryCode, countryCodes)
|
||||
if warning != "" {
|
||||
warnings = append(warnings, warning)
|
||||
}
|
||||
|
||||
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, country, city)
|
||||
}
|
||||
|
||||
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()
|
||||
|
||||
sortServers(servers)
|
||||
|
||||
return servers, warnings, nil
|
||||
}
|
||||
19
internal/updater/providers/privatevpn/sort.go
Normal file
19
internal/updater/providers/privatevpn/sort.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package privatevpn
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
func sortServers(servers []models.PrivatevpnServer) {
|
||||
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
|
||||
})
|
||||
}
|
||||
14
internal/updater/providers/privatevpn/string.go
Normal file
14
internal/updater/providers/privatevpn/string.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package privatevpn
|
||||
|
||||
import "github.com/qdm12/gluetun/internal/models"
|
||||
|
||||
func Stringify(servers []models.PrivatevpnServer) (s string) {
|
||||
s = "func PrivatevpnServers() []models.PrivatevpnServer {\n"
|
||||
s += " return []models.PrivatevpnServer{\n"
|
||||
for _, server := range servers {
|
||||
s += " " + server.String() + ",\n"
|
||||
}
|
||||
s += " }\n"
|
||||
s += "}"
|
||||
return s
|
||||
}
|
||||
65
internal/updater/providers/protonvpn/api.go
Normal file
65
internal/updater/providers/protonvpn/api.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package protonvpn
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrHTTPStatusCodeNotOK = errors.New("HTTP status code not OK")
|
||||
ErrUnmarshalResponseBody = errors.New("failed unmarshaling response body")
|
||||
)
|
||||
|
||||
type apiData struct {
|
||||
LogicalServers []logicalServer
|
||||
}
|
||||
|
||||
type logicalServer struct {
|
||||
Name string
|
||||
ExitCountry string
|
||||
Region *string
|
||||
City *string
|
||||
Servers []physicalServer
|
||||
}
|
||||
|
||||
type physicalServer struct {
|
||||
EntryIP net.IP
|
||||
ExitIP net.IP
|
||||
Domain string
|
||||
Status uint8
|
||||
}
|
||||
|
||||
func fetchAPI(ctx context.Context, client *http.Client) (
|
||||
data apiData, err error) {
|
||||
const url = "https://api.protonmail.ch/vpn/logicals"
|
||||
|
||||
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return data, err
|
||||
}
|
||||
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
return data, err
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return data, fmt.Errorf("%w: %s", ErrHTTPStatusCodeNotOK, response.Status)
|
||||
}
|
||||
|
||||
decoder := json.NewDecoder(response.Body)
|
||||
if err := decoder.Decode(&data); err != nil {
|
||||
return data, fmt.Errorf("%w: %s", ErrUnmarshalResponseBody, err)
|
||||
}
|
||||
|
||||
if err := response.Body.Close(); err != nil {
|
||||
return data, err
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
14
internal/updater/providers/protonvpn/countries.go
Normal file
14
internal/updater/providers/protonvpn/countries.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package protonvpn
|
||||
|
||||
import "strings"
|
||||
|
||||
func codeToCountry(countryCode string, countryCodes map[string]string) (
|
||||
country string, warning string) {
|
||||
countryCode = strings.ToLower(countryCode)
|
||||
country, ok := countryCodes[countryCode]
|
||||
if !ok {
|
||||
warning = "unknown country code: " + countryCode
|
||||
country = countryCode
|
||||
}
|
||||
return country, warning
|
||||
}
|
||||
98
internal/updater/providers/protonvpn/servers.go
Normal file
98
internal/updater/providers/protonvpn/servers.go
Normal file
@@ -0,0 +1,98 @@
|
||||
// Package protonvpn contains code to obtain the server information
|
||||
// for the ProtonVPN provider.
|
||||
package protonvpn
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/constants"
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
var ErrNotEnoughServers = errors.New("not enough servers found")
|
||||
|
||||
func GetServers(ctx context.Context, client *http.Client, minServers int) (
|
||||
servers []models.ProtonvpnServer, warnings []string, err error) {
|
||||
data, err := fetchAPI(ctx, client)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
countryCodes := constants.CountryCodes()
|
||||
|
||||
var count int
|
||||
for _, logicalServer := range data.LogicalServers {
|
||||
count += len(logicalServer.Servers)
|
||||
}
|
||||
|
||||
if count < minServers {
|
||||
return nil, warnings, fmt.Errorf("%w: %d and expected at least %d",
|
||||
ErrNotEnoughServers, count, minServers)
|
||||
}
|
||||
|
||||
servers = make([]models.ProtonvpnServer, 0, count)
|
||||
for _, logicalServer := range data.LogicalServers {
|
||||
for _, physicalServer := range logicalServer.Servers {
|
||||
server, warning, err := makeServer(
|
||||
physicalServer, logicalServer, countryCodes)
|
||||
|
||||
if warning != "" {
|
||||
warnings = append(warnings, warning)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
warnings = append(warnings, err.Error())
|
||||
continue
|
||||
}
|
||||
|
||||
servers = append(servers, server)
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
var errServerStatusZero = errors.New("ignoring server with status 0")
|
||||
|
||||
func makeServer(physical physicalServer, logical logicalServer,
|
||||
countryCodes map[string]string) (server models.ProtonvpnServer,
|
||||
warning string, err error) {
|
||||
if physical.Status == 0 {
|
||||
return server, "", fmt.Errorf("%w: %s",
|
||||
errServerStatusZero, physical.Domain)
|
||||
}
|
||||
|
||||
countryCode := logical.ExitCountry
|
||||
country, warning := codeToCountry(countryCode, countryCodes)
|
||||
|
||||
server = models.ProtonvpnServer{
|
||||
// Note: for multi-hop use the server name or hostname
|
||||
// instead of the country
|
||||
Country: country,
|
||||
Region: getStringValue(logical.Region),
|
||||
City: getStringValue(logical.City),
|
||||
Name: logical.Name,
|
||||
Hostname: physical.Domain,
|
||||
EntryIP: physical.EntryIP,
|
||||
ExitIP: physical.ExitIP,
|
||||
}
|
||||
|
||||
return server, warning, nil
|
||||
}
|
||||
|
||||
func getStringValue(ptr *string) string {
|
||||
if ptr == nil {
|
||||
return ""
|
||||
}
|
||||
return *ptr
|
||||
}
|
||||
26
internal/updater/providers/protonvpn/sort.go
Normal file
26
internal/updater/providers/protonvpn/sort.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package protonvpn
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
func sortServers(servers []models.ProtonvpnServer) {
|
||||
sort.Slice(servers, func(i, j int) bool {
|
||||
a, b := servers[i], servers[j]
|
||||
if a.Country == b.Country { //nolint:nestif
|
||||
if a.Region == b.Region {
|
||||
if a.City == b.City {
|
||||
if a.Name == b.Name {
|
||||
return a.Hostname < b.Hostname
|
||||
}
|
||||
return a.Name < b.Name
|
||||
}
|
||||
return a.City < b.City
|
||||
}
|
||||
return a.Region < b.Region
|
||||
}
|
||||
return a.Country < b.Country
|
||||
})
|
||||
}
|
||||
14
internal/updater/providers/protonvpn/string.go
Normal file
14
internal/updater/providers/protonvpn/string.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package protonvpn
|
||||
|
||||
import "github.com/qdm12/gluetun/internal/models"
|
||||
|
||||
func Stringify(servers []models.ProtonvpnServer) (s string) {
|
||||
s = "func ProtonvpnServers() []models.ProtonvpnServer {\n"
|
||||
s += " return []models.ProtonvpnServer{\n"
|
||||
for _, server := range servers {
|
||||
s += " " + server.String() + ",\n"
|
||||
}
|
||||
s += " }\n"
|
||||
s += "}"
|
||||
return s
|
||||
}
|
||||
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
|
||||
}
|
||||
53
internal/updater/providers/surfshark/api.go
Normal file
53
internal/updater/providers/surfshark/api.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package surfshark
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrHTTPStatusCodeNotOK = errors.New("HTTP status code not OK")
|
||||
ErrUnmarshalResponseBody = errors.New("failed unmarshaling response body")
|
||||
)
|
||||
|
||||
//nolint:unused
|
||||
type serverData struct {
|
||||
Host string `json:"connectionName"`
|
||||
Country string `json:"country"`
|
||||
Location string `json:"location"`
|
||||
}
|
||||
|
||||
//nolint:unused,deadcode
|
||||
func fetchAPI(ctx context.Context, client *http.Client) (
|
||||
servers []serverData, err error) {
|
||||
const url = "https://my.surfshark.com/vpn/api/v4/server/clusters"
|
||||
|
||||
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("%w: %s", ErrHTTPStatusCodeNotOK, response.Status)
|
||||
}
|
||||
|
||||
decoder := json.NewDecoder(response.Body)
|
||||
if err := decoder.Decode(&servers); err != nil {
|
||||
return nil, fmt.Errorf("%w: %s", ErrUnmarshalResponseBody, err)
|
||||
}
|
||||
|
||||
if err := response.Body.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return servers, nil
|
||||
}
|
||||
59
internal/updater/providers/surfshark/hosttoserver.go
Normal file
59
internal/updater/providers/surfshark/hosttoserver.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package surfshark
|
||||
|
||||
import (
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
type hostToServer map[string]models.SurfsharkServer
|
||||
|
||||
func (hts hostToServer) add(host, region string) {
|
||||
// TODO set TCP and UDP
|
||||
// TODO set hostname
|
||||
server, ok := hts[host]
|
||||
if !ok {
|
||||
server.Region = region
|
||||
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) toSubdomainsSlice() (subdomains []string) {
|
||||
subdomains = make([]string, 0, len(hts))
|
||||
const suffix = ".prod.surfshark.com"
|
||||
for host := range hts {
|
||||
subdomain := strings.TrimSuffix(host, suffix)
|
||||
subdomains = append(subdomains, subdomain)
|
||||
}
|
||||
return subdomains
|
||||
}
|
||||
|
||||
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.SurfsharkServer) {
|
||||
servers = make([]models.SurfsharkServer, 0, len(hts))
|
||||
for _, server := range hts {
|
||||
servers = append(servers, server)
|
||||
}
|
||||
return servers
|
||||
}
|
||||
200
internal/updater/providers/surfshark/regions.go
Normal file
200
internal/updater/providers/surfshark/regions.go
Normal file
@@ -0,0 +1,200 @@
|
||||
package surfshark
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
errSuffixNotFound = errors.New("suffix not found")
|
||||
errSubdomainNotFound = errors.New("subdomain not found in subdomain to region mapping")
|
||||
)
|
||||
|
||||
func parseHost(host string, subdomainToRegion map[string]string) (
|
||||
region string, err error) {
|
||||
const suffix = ".prod.surfshark.com"
|
||||
if !strings.HasSuffix(host, suffix) {
|
||||
return "", fmt.Errorf("%w: %s in %s",
|
||||
errSuffixNotFound, suffix, host)
|
||||
}
|
||||
|
||||
subdomain := strings.TrimSuffix(host, suffix)
|
||||
region, ok := subdomainToRegion[subdomain]
|
||||
if !ok {
|
||||
return "", fmt.Errorf("%w: %s", errSubdomainNotFound, subdomain)
|
||||
}
|
||||
|
||||
return region, nil
|
||||
}
|
||||
|
||||
func subdomainToRegion() (mapping map[string]string) {
|
||||
return map[string]string{
|
||||
"ae-dub": "United Arab Emirates",
|
||||
"al-tia": "Albania",
|
||||
"at-vie": "Austria",
|
||||
"au-adl": "Australia Adelaide",
|
||||
"au-bne": "Australia Brisbane",
|
||||
"au-mel": "Australia Melbourne",
|
||||
"au-per": "Australia Perth",
|
||||
"au-syd": "Australia Sydney",
|
||||
"au-us": "Australia US",
|
||||
"az-bak": "Azerbaijan",
|
||||
"ba-sjj": "Bosnia and Herzegovina",
|
||||
"be-bru": "Belgium",
|
||||
"bg-sof": "Bulgaria",
|
||||
"br-sao": "Brazil",
|
||||
"ca-mon": "Canada Montreal",
|
||||
"ca-tor": "Canada Toronto",
|
||||
"ca-us": "Canada US",
|
||||
"ca-van": "Canada Vancouver",
|
||||
"ch-zur": "Switzerland",
|
||||
"cl-san": "Chile",
|
||||
"co-bog": "Colombia",
|
||||
"cr-sjn": "Costa Rica",
|
||||
"cy-nic": "Cyprus",
|
||||
"cz-prg": "Czech Republic",
|
||||
"de-ber": "Germany Berlin",
|
||||
"de-fra": "Germany Frankfurt am Main",
|
||||
"de-fra-st001": "Germany Frankfurt am Main st001",
|
||||
"de-fra-st002": "Germany Frankfurt am Main st002",
|
||||
"de-fra-st003": "Germany Frankfurt am Main st003",
|
||||
"de-fra-st004": "Germany Frankfurt am Main st004",
|
||||
"de-fra-st005": "Germany Frankfurt am Main st005",
|
||||
"de-muc": "Germany Munich",
|
||||
"de-nue": "Germany Nuremberg",
|
||||
"de-sg": "Germany Singapour",
|
||||
"de-uk": "Germany UK",
|
||||
"dk-cph": "Denmark",
|
||||
"ee-tll": "Estonia",
|
||||
"es-bcn": "Spain Barcelona",
|
||||
"es-mad": "Spain Madrid",
|
||||
"es-vlc": "Spain Valencia",
|
||||
"fi-hel": "Finland",
|
||||
"fr-bod": "France Bordeaux",
|
||||
"fr-mrs": "France Marseilles",
|
||||
"fr-par": "France Paris",
|
||||
"fr-se": "France Sweden",
|
||||
"gr-ath": "Greece",
|
||||
"hk-hkg": "Hong Kong",
|
||||
"hr-zag": "Croatia",
|
||||
"hu-bud": "Hungary",
|
||||
"id-jak": "Indonesia",
|
||||
"ie-dub": "Ireland",
|
||||
"il-tlv": "Israel",
|
||||
"in-chn": "India Chennai",
|
||||
"in-idr": "India Indore",
|
||||
"in-mum": "India Mumbai",
|
||||
"in-uk": "India UK",
|
||||
"is-rkv": "Iceland",
|
||||
"it-mil": "Italy Milan",
|
||||
"it-rom": "Italy Rome",
|
||||
"jp-tok": "Japan Tokyo",
|
||||
"jp-tok-st001": "Japan Tokyo st001",
|
||||
"jp-tok-st002": "Japan Tokyo st002",
|
||||
"jp-tok-st003": "Japan Tokyo st003",
|
||||
"jp-tok-st004": "Japan Tokyo st004",
|
||||
"jp-tok-st005": "Japan Tokyo st005",
|
||||
"jp-tok-st006": "Japan Tokyo st006",
|
||||
"jp-tok-st007": "Japan Tokyo st007",
|
||||
"jp-tok-st008": "Japan Tokyo st008",
|
||||
"jp-tok-st009": "Japan Tokyo st009",
|
||||
"jp-tok-st010": "Japan Tokyo st010",
|
||||
"jp-tok-st011": "Japan Tokyo st011",
|
||||
"jp-tok-st012": "Japan Tokyo st012",
|
||||
"jp-tok-st013": "Japan Tokyo st013",
|
||||
"kr-seo": "Korea",
|
||||
"kz-ura": "Kazakhstan",
|
||||
"lu-ste": "Luxembourg",
|
||||
"lv-rig": "Latvia",
|
||||
"ly-tip": "Libya",
|
||||
"md-chi": "Moldova",
|
||||
"mk-skp": "North Macedonia",
|
||||
"my-kul": "Malaysia",
|
||||
"ng-lag": "Nigeria",
|
||||
"nl-ams": "Netherlands Amsterdam",
|
||||
"nl-ams-st001": "Netherlands Amsterdam st001",
|
||||
"nl-us": "Netherlands US",
|
||||
"no-osl": "Norway",
|
||||
"nz-akl": "New Zealand",
|
||||
"ph-mnl": "Philippines",
|
||||
"pl-gdn": "Poland Gdansk",
|
||||
"pl-waw": "Poland Warsaw",
|
||||
"pt-lis": "Portugal Lisbon",
|
||||
"pt-lou": "Portugal Loule",
|
||||
"pt-opo": "Portugal Porto",
|
||||
"py-asu": "Paraguay",
|
||||
"ro-buc": "Romania",
|
||||
"rs-beg": "Serbia",
|
||||
"ru-mos": "Russia Moscow",
|
||||
"ru-spt": "Russia St. Petersburg",
|
||||
"se-sto": "Sweden",
|
||||
"sg-hk": "Singapore Hong Kong",
|
||||
"sg-nl": "Singapore Netherlands",
|
||||
"sg-sng": "Singapore",
|
||||
"sg-in": "Singapore in",
|
||||
"sg-sng-st001": "Singapore st001",
|
||||
"sg-sng-st002": "Singapore st002",
|
||||
"sg-sng-st003": "Singapore st003",
|
||||
"sg-sng-st004": "Singapore st004",
|
||||
"sg-sng-mp001": "Singapore mp001",
|
||||
"si-lju": "Slovenia",
|
||||
"sk-bts": "Slovekia",
|
||||
"th-bkk": "Thailand",
|
||||
"tr-bur": "Turkey",
|
||||
"tw-tai": "Taiwan",
|
||||
"ua-iev": "Ukraine",
|
||||
"uk-de": "UK Germany",
|
||||
"uk-fr": "UK France",
|
||||
"uk-gla": "UK Glasgow",
|
||||
"uk-lon": "UK London",
|
||||
"uk-lon-mp001": "UK London mp001",
|
||||
"uk-lon-st001": "UK London st001",
|
||||
"uk-lon-st002": "UK London st002",
|
||||
"uk-lon-st003": "UK London st003",
|
||||
"uk-lon-st004": "UK London st004",
|
||||
"uk-lon-st005": "UK London st005",
|
||||
"uk-man": "UK Manchester",
|
||||
"us-atl": "US Atlanta",
|
||||
"us-bdn": "US Bend",
|
||||
"us-bos": "US Boston",
|
||||
"us-buf": "US Buffalo",
|
||||
"us-chi": "US Chicago",
|
||||
"us-clt": "US Charlotte",
|
||||
"us-dal": "US Dallas",
|
||||
"us-den": "US Denver",
|
||||
"us-dtw": "US Gahanna",
|
||||
"us-hou": "US Houston",
|
||||
"us-kan": "US Kansas City",
|
||||
"us-las": "US Las Vegas",
|
||||
"us-lax": "US Los Angeles",
|
||||
"us-ltm": "US Latham",
|
||||
"us-mia": "US Miami",
|
||||
"us-mnz": "US Maryland",
|
||||
"us-nl": "US Netherlands",
|
||||
"us-nyc": "US New York City",
|
||||
"us-nyc-mp001": "US New York City mp001",
|
||||
"us-nyc-st001": "US New York City st001",
|
||||
"us-nyc-st002": "US New York City st002",
|
||||
"us-nyc-st003": "US New York City st003",
|
||||
"us-nyc-st004": "US New York City st004",
|
||||
"us-nyc-st005": "US New York City st005",
|
||||
"us-orl": "US Orlando",
|
||||
"us-phx": "US Phoenix",
|
||||
"us-pt": "US Portugal",
|
||||
"us-sea": "US Seatle",
|
||||
"us-sfo": "US San Francisco",
|
||||
"us-slc": "US Salt Lake City",
|
||||
"us-stl": "US Saint Louis",
|
||||
"us-tpa": "US Tampa",
|
||||
"vn-hcm": "Vietnam",
|
||||
"za-jnb": "South Africa",
|
||||
"ar-bua": "Argentina Buenos Aires",
|
||||
"tr-ist": "Turkey Istanbul",
|
||||
"mx-mex": "Mexico City Mexico",
|
||||
"ca-tor-mp001": "Canada Toronto mp001",
|
||||
"de-fra-mp001": "Germany Frankfurt mp001",
|
||||
"nl-ams-mp001": "Netherlands Amsterdam mp001",
|
||||
"us-sfo-mp001": "US San Francisco mp001",
|
||||
}
|
||||
}
|
||||
32
internal/updater/providers/surfshark/resolve.go
Normal file
32
internal/updater/providers/surfshark/resolve.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package surfshark
|
||||
|
||||
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,
|
||||
},
|
||||
}
|
||||
return presolver.Resolve(ctx, hosts, settings)
|
||||
}
|
||||
130
internal/updater/providers/surfshark/servers.go
Normal file
130
internal/updater/providers/surfshark/servers.go
Normal file
@@ -0,0 +1,130 @@
|
||||
// Package surfshark contains code to obtain the server information
|
||||
// for the Surshark provider.
|
||||
package surfshark
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
var ErrNotEnoughServers = errors.New("not enough servers found")
|
||||
|
||||
func GetServers(ctx context.Context, unzipper unzip.Unzipper,
|
||||
presolver resolver.Parallel, minServers int) (
|
||||
servers []models.SurfsharkServer, warnings []string, err error) {
|
||||
const url = "https://my.surfshark.com/vpn/api/v1/server/configurations"
|
||||
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)
|
||||
}
|
||||
|
||||
subdomainToRegion := subdomainToRegion()
|
||||
hts := make(hostToServer)
|
||||
|
||||
for fileName, content := range contents {
|
||||
if !strings.HasSuffix(fileName, ".ovpn") {
|
||||
continue // not an OpenVPN file
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
region, err := parseHost(host, subdomainToRegion)
|
||||
if err != nil {
|
||||
// treat error as warning and go to next file
|
||||
warning := err.Error()
|
||||
warnings = append(warnings, warning)
|
||||
continue
|
||||
}
|
||||
|
||||
hts.add(host, region)
|
||||
}
|
||||
|
||||
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()
|
||||
|
||||
// process subdomain entries in mapping that were not in the Zip file
|
||||
subdomainsDone := hts.toSubdomainsSlice()
|
||||
for _, subdomainDone := range subdomainsDone {
|
||||
delete(subdomainToRegion, subdomainDone)
|
||||
}
|
||||
remainingServers, newWarnings, err := getRemainingServers(
|
||||
ctx, subdomainToRegion, presolver)
|
||||
warnings = append(warnings, newWarnings...)
|
||||
if err != nil {
|
||||
return nil, warnings, err
|
||||
}
|
||||
|
||||
servers = append(servers, remainingServers...)
|
||||
|
||||
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 getRemainingServers(ctx context.Context,
|
||||
subdomainToRegionLeft map[string]string, presolver resolver.Parallel) (
|
||||
servers []models.SurfsharkServer, warnings []string, err error) {
|
||||
hosts := make([]string, 0, len(subdomainToRegionLeft))
|
||||
const suffix = ".prod.surfshark.com"
|
||||
for subdomain := range subdomainToRegionLeft {
|
||||
hosts = append(hosts, subdomain+suffix)
|
||||
}
|
||||
|
||||
const minServers = 0
|
||||
hostToIPs, warnings, err := resolveHosts(ctx, presolver, hosts, minServers)
|
||||
if err != nil {
|
||||
return nil, warnings, err
|
||||
}
|
||||
|
||||
servers = make([]models.SurfsharkServer, 0, len(hostToIPs))
|
||||
for host, IPs := range hostToIPs {
|
||||
region, err := parseHost(host, subdomainToRegionLeft)
|
||||
if err != nil {
|
||||
return nil, warnings, err
|
||||
}
|
||||
server := models.SurfsharkServer{
|
||||
Region: region,
|
||||
IPs: IPs,
|
||||
}
|
||||
servers = append(servers, server)
|
||||
}
|
||||
|
||||
return servers, warnings, nil
|
||||
}
|
||||
13
internal/updater/providers/surfshark/sort.go
Normal file
13
internal/updater/providers/surfshark/sort.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package surfshark
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
func sortServers(servers []models.SurfsharkServer) {
|
||||
sort.Slice(servers, func(i, j int) bool {
|
||||
return servers[i].Region < servers[j].Region
|
||||
})
|
||||
}
|
||||
14
internal/updater/providers/surfshark/string.go
Normal file
14
internal/updater/providers/surfshark/string.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package surfshark
|
||||
|
||||
import "github.com/qdm12/gluetun/internal/models"
|
||||
|
||||
func Stringify(servers []models.SurfsharkServer) (s string) {
|
||||
s = "func SurfsharkServers() []models.SurfsharkServer {\n"
|
||||
s += " return []models.SurfsharkServer{\n"
|
||||
for _, server := range servers {
|
||||
s += " " + server.String() + ",\n"
|
||||
}
|
||||
s += " }\n"
|
||||
s += "}"
|
||||
return s
|
||||
}
|
||||
31
internal/updater/providers/torguard/filename.go
Normal file
31
internal/updater/providers/torguard/filename.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package torguard
|
||||
|
||||
import "strings"
|
||||
|
||||
func parseFilename(fileName string) (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 = strings.Title(s)
|
||||
city = s
|
||||
|
||||
default:
|
||||
country = s
|
||||
}
|
||||
|
||||
return country, city
|
||||
}
|
||||
76
internal/updater/providers/torguard/servers.go
Normal file
76
internal/updater/providers/torguard/servers.go
Normal file
@@ -0,0 +1,76 @@
|
||||
// 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/unzip"
|
||||
)
|
||||
|
||||
var ErrNotEnoughServers = errors.New("not enough servers found")
|
||||
|
||||
func GetServers(ctx context.Context, unzipper unzip.Unzipper, minServers int) (
|
||||
servers []models.TorguardServer, warnings []string, err error) {
|
||||
const url = "https://torguard.net/downloads/OpenVPN-TCP-Linux.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)
|
||||
}
|
||||
|
||||
servers = make([]models.TorguardServer, 0, len(contents))
|
||||
for fileName, content := range contents {
|
||||
if !strings.HasSuffix(fileName, ".ovpn") {
|
||||
continue // not an OpenVPN file
|
||||
}
|
||||
|
||||
country, city := parseFilename(fileName)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
ip, warning, err := openvpn.ExtractIP(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
|
||||
}
|
||||
|
||||
server := models.TorguardServer{
|
||||
Country: country,
|
||||
City: city,
|
||||
Hostname: host,
|
||||
IP: ip,
|
||||
}
|
||||
servers = append(servers, server)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
19
internal/updater/providers/torguard/sort.go
Normal file
19
internal/updater/providers/torguard/sort.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package torguard
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
func sortServers(servers []models.TorguardServer) {
|
||||
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
|
||||
})
|
||||
}
|
||||
14
internal/updater/providers/torguard/string.go
Normal file
14
internal/updater/providers/torguard/string.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package torguard
|
||||
|
||||
import "github.com/qdm12/gluetun/internal/models"
|
||||
|
||||
func Stringify(servers []models.TorguardServer) (s string) {
|
||||
s = "func TorguardServers() []models.TorguardServer {\n"
|
||||
s += " return []models.TorguardServer{\n"
|
||||
for _, server := range servers {
|
||||
s += " " + server.String() + ",\n"
|
||||
}
|
||||
s += " }\n"
|
||||
s += "}"
|
||||
return s
|
||||
}
|
||||
22
internal/updater/providers/vyprvpn/filename.go
Normal file
22
internal/updater/providers/vyprvpn/filename.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package vyprvpn
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var errNotOvpnExt = errors.New("filename does not have the openvpn file extension")
|
||||
|
||||
func parseFilename(fileName string) (
|
||||
region string, err error,
|
||||
) {
|
||||
const suffix = ".ovpn"
|
||||
if !strings.HasSuffix(fileName, suffix) {
|
||||
return "", fmt.Errorf("%w: %s", errNotOvpnExt, fileName)
|
||||
}
|
||||
|
||||
region = strings.TrimSuffix(fileName, suffix)
|
||||
region = strings.ReplaceAll(region, " - ", " ")
|
||||
return region, nil
|
||||
}
|
||||
47
internal/updater/providers/vyprvpn/hosttoserver.go
Normal file
47
internal/updater/providers/vyprvpn/hosttoserver.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package vyprvpn
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
type hostToServer map[string]models.VyprvpnServer
|
||||
|
||||
func (hts hostToServer) add(host, region string) {
|
||||
server, ok := hts[host]
|
||||
// TODO set host
|
||||
if !ok {
|
||||
server.Region = region
|
||||
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.VyprvpnServer) {
|
||||
servers = make([]models.VyprvpnServer, 0, len(hts))
|
||||
for _, server := range hts {
|
||||
servers = append(servers, server)
|
||||
}
|
||||
return servers
|
||||
}
|
||||
30
internal/updater/providers/vyprvpn/resolve.go
Normal file
30
internal/updater/providers/vyprvpn/resolve.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package vyprvpn
|
||||
|
||||
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
|
||||
maxNoNew = 2
|
||||
maxFails = 2
|
||||
)
|
||||
settings := resolver.ParallelSettings{
|
||||
MaxFailRatio: maxFailRatio,
|
||||
MinFound: minServers,
|
||||
Repeat: resolver.RepeatSettings{
|
||||
MaxDuration: time.Second,
|
||||
MaxNoNew: maxNoNew,
|
||||
MaxFails: maxFails,
|
||||
SortIPs: true,
|
||||
},
|
||||
}
|
||||
return presolver.Resolve(ctx, hosts, settings)
|
||||
}
|
||||
82
internal/updater/providers/vyprvpn/servers.go
Normal file
82
internal/updater/providers/vyprvpn/servers.go
Normal file
@@ -0,0 +1,82 @@
|
||||
// Package vyprvpn contains code to obtain the server information
|
||||
// for the VyprVPN provider.
|
||||
package vyprvpn
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
var ErrNotEnoughServers = errors.New("not enough servers found")
|
||||
|
||||
func GetServers(ctx context.Context, unzipper unzip.Unzipper,
|
||||
presolver resolver.Parallel, minServers int) (
|
||||
servers []models.VyprvpnServer, warnings []string, err error) {
|
||||
const url = "https://support.vyprvpn.com/hc/article_attachments/360052617332/Vypr_OpenVPN_20200320.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 // not an OpenVPN file
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
region, err := parseFilename(fileName)
|
||||
if err != nil {
|
||||
warnings = append(warnings, err.Error())
|
||||
continue
|
||||
}
|
||||
|
||||
hts.add(host, region)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
13
internal/updater/providers/vyprvpn/sort.go
Normal file
13
internal/updater/providers/vyprvpn/sort.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package vyprvpn
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
func sortServers(servers []models.VyprvpnServer) {
|
||||
sort.Slice(servers, func(i, j int) bool {
|
||||
return servers[i].Region < servers[j].Region
|
||||
})
|
||||
}
|
||||
14
internal/updater/providers/vyprvpn/string.go
Normal file
14
internal/updater/providers/vyprvpn/string.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package vyprvpn
|
||||
|
||||
import "github.com/qdm12/gluetun/internal/models"
|
||||
|
||||
func Stringify(servers []models.VyprvpnServer) (s string) {
|
||||
s = "func VyprvpnServers() []models.VyprvpnServer {\n"
|
||||
s += " return []models.VyprvpnServer{\n"
|
||||
for _, server := range servers {
|
||||
s += " " + server.String() + ",\n"
|
||||
}
|
||||
s += " }\n"
|
||||
s += "}"
|
||||
return s
|
||||
}
|
||||
61
internal/updater/providers/windscribe/api.go
Normal file
61
internal/updater/providers/windscribe/api.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package windscribe
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrHTTPStatusCodeNotOK = errors.New("HTTP status code not OK")
|
||||
ErrUnmarshalResponseBody = errors.New("failed unmarshaling response body")
|
||||
)
|
||||
|
||||
type apiData struct {
|
||||
Data []regionData `json:"data"`
|
||||
}
|
||||
|
||||
type regionData struct {
|
||||
Region string `json:"name"`
|
||||
Groups []groupData `json:"groups"`
|
||||
}
|
||||
|
||||
type groupData struct {
|
||||
City string `json:"city"`
|
||||
Nodes []serverData `json:"nodes"`
|
||||
}
|
||||
|
||||
type serverData struct {
|
||||
Hostname string `json:"hostname"`
|
||||
OpenvpnIP net.IP `json:"ip2"`
|
||||
}
|
||||
|
||||
func fetchAPI(ctx context.Context, client *http.Client) (
|
||||
data apiData, err error) {
|
||||
const baseURL = "https://assets.windscribe.com/serverlist/mob-v2/1/"
|
||||
cacheBreaker := time.Now().Unix()
|
||||
url := baseURL + strconv.Itoa(int(cacheBreaker))
|
||||
|
||||
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return data, err
|
||||
}
|
||||
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
return data, err
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
decoder := json.NewDecoder(response.Body)
|
||||
if err := decoder.Decode(&data); err != nil {
|
||||
return data, fmt.Errorf("%w: %s", ErrUnmarshalResponseBody, err)
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
47
internal/updater/providers/windscribe/servers.go
Normal file
47
internal/updater/providers/windscribe/servers.go
Normal file
@@ -0,0 +1,47 @@
|
||||
// Package windscribe contains code to obtain the server information
|
||||
// for the Windscribe provider.
|
||||
package windscribe
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
var ErrNotEnoughServers = errors.New("not enough servers found")
|
||||
|
||||
func GetServers(ctx context.Context, client *http.Client, minServers int) (
|
||||
servers []models.WindscribeServer, err error) {
|
||||
data, err := fetchAPI(ctx, client)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, regionData := range data.Data {
|
||||
region := regionData.Region
|
||||
for _, group := range regionData.Groups {
|
||||
city := group.City
|
||||
for _, node := range group.Nodes {
|
||||
server := models.WindscribeServer{
|
||||
Region: region,
|
||||
City: city,
|
||||
Hostname: node.Hostname,
|
||||
IP: node.OpenvpnIP,
|
||||
}
|
||||
servers = append(servers, server)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(servers) < minServers {
|
||||
return nil, fmt.Errorf("%w: %d and expected at least %d",
|
||||
ErrNotEnoughServers, len(servers), minServers)
|
||||
}
|
||||
|
||||
sortServers(servers)
|
||||
|
||||
return servers, nil
|
||||
}
|
||||
19
internal/updater/providers/windscribe/sort.go
Normal file
19
internal/updater/providers/windscribe/sort.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package windscribe
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
func sortServers(servers []models.WindscribeServer) {
|
||||
sort.Slice(servers, func(i, j int) bool {
|
||||
if servers[i].Region == servers[j].Region {
|
||||
if servers[i].City == servers[j].City {
|
||||
return servers[i].Hostname < servers[j].Hostname
|
||||
}
|
||||
return servers[i].City < servers[j].City
|
||||
}
|
||||
return servers[i].Region < servers[j].Region
|
||||
})
|
||||
}
|
||||
14
internal/updater/providers/windscribe/string.go
Normal file
14
internal/updater/providers/windscribe/string.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package windscribe
|
||||
|
||||
import "github.com/qdm12/gluetun/internal/models"
|
||||
|
||||
func Stringify(servers []models.WindscribeServer) (s string) {
|
||||
s = "func WindscribeServers() []models.WindscribeServer {\n"
|
||||
s += " return []models.WindscribeServer{\n"
|
||||
for _, server := range servers {
|
||||
s += " " + server.String() + ",\n"
|
||||
}
|
||||
s += " }\n"
|
||||
s += "}"
|
||||
return s
|
||||
}
|
||||
Reference in New Issue
Block a user