chore(all): move sub-packages to internal/provider

This commit is contained in:
Quentin McGaw
2022-05-27 17:48:51 +00:00
parent 364f9de756
commit 42904b6749
118 changed files with 25 additions and 25 deletions

View 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
}

View File

@@ -0,0 +1,57 @@
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)
if countryCode == "co" && strings.HasPrefix(city, "Bogot") {
city = "Bogota"
}
return countryCode, city, nil
}

View File

@@ -0,0 +1,54 @@
package privatevpn
import (
"net"
"github.com/qdm12/gluetun/internal/constants/vpn"
"github.com/qdm12/gluetun/internal/models"
)
type hostToServer map[string]models.Server
// TODO check if server supports TCP and UDP.
func (hts hostToServer) add(host, country, city string) {
server, ok := hts[host]
if ok {
return
}
server.VPN = vpn.OpenVPN
server.Hostname = host
server.Country = country
server.City = city
server.UDP = true
server.TCP = 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.Server) {
servers = make([]models.Server, 0, len(hts))
for _, server := range hts {
servers = append(servers, server)
}
return servers
}

View 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)
}

View File

@@ -0,0 +1,103 @@
// 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.Server, 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)
noHostnameServers := make([]models.Server, 0, 1) // there is only one for now
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 { // found host
hts.add(host, country, city)
continue
}
ips, extractIPErr := openvpn.ExtractIPs(content)
if warning != "" {
warnings = append(warnings, warning)
}
if extractIPErr != nil {
// treat extract host error as warning and go to next file
warning := err.Error() + " in " + fileName
warnings = append(warnings, warning)
continue
}
server := models.Server{
Country: country,
City: city,
IPs: ips,
UDP: true,
TCP: true,
}
noHostnameServers = append(noHostnameServers, server)
}
if len(noHostnameServers)+len(hts) < minServers {
return nil, warnings, fmt.Errorf("%w: %d and expected at least %d",
ErrNotEnoughServers, len(servers)+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()
servers = append(servers, noHostnameServers...)
sortServers(servers)
return servers, warnings, nil
}

View File

@@ -0,0 +1,19 @@
package privatevpn
import (
"sort"
"github.com/qdm12/gluetun/internal/models"
)
func sortServers(servers []models.Server) {
sort.Slice(servers, func(i, j int) bool {
if servers[i].Country == servers[j].Country {
if servers[i].City == servers[j].City {
return servers[i].Hostname < servers[j].Hostname
}
return servers[i].City < servers[j].City
}
return servers[i].Country < servers[j].Country
})
}