Feat: Perfect privacy support (#606)
This commit is contained in:
45
internal/updater/providers/perfectprivacy/citytoserver.go
Normal file
45
internal/updater/providers/perfectprivacy/citytoserver.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package perfectprivacy
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
type cityToServer map[string]models.PerfectprivacyServer
|
||||
|
||||
func (cts cityToServer) add(city string, ips []net.IP) {
|
||||
server, ok := cts[city]
|
||||
if !ok {
|
||||
server.City = city
|
||||
server.IPs = ips
|
||||
server.TCP = true
|
||||
server.UDP = true
|
||||
} else {
|
||||
// Do not insert duplicate IP addresses
|
||||
existingIPs := make(map[string]struct{}, len(server.IPs))
|
||||
for _, ip := range server.IPs {
|
||||
existingIPs[ip.String()] = struct{}{}
|
||||
}
|
||||
|
||||
for _, ip := range ips {
|
||||
ipString := ip.String()
|
||||
_, ok := existingIPs[ipString]
|
||||
if ok {
|
||||
continue
|
||||
}
|
||||
existingIPs[ipString] = struct{}{}
|
||||
server.IPs = append(server.IPs, ip)
|
||||
}
|
||||
}
|
||||
|
||||
cts[city] = server
|
||||
}
|
||||
|
||||
func (cts cityToServer) toServersSlice() (servers []models.PerfectprivacyServer) {
|
||||
servers = make([]models.PerfectprivacyServer, 0, len(cts))
|
||||
for _, server := range cts {
|
||||
servers = append(servers, server)
|
||||
}
|
||||
return servers
|
||||
}
|
||||
20
internal/updater/providers/perfectprivacy/filename.go
Normal file
20
internal/updater/providers/perfectprivacy/filename.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package perfectprivacy
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
func parseFilename(fileName string) (city string) {
|
||||
const suffix = ".conf"
|
||||
s := strings.TrimSuffix(fileName, suffix)
|
||||
|
||||
for i, r := range s {
|
||||
if unicode.IsDigit(r) {
|
||||
s = s[:i]
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
73
internal/updater/providers/perfectprivacy/servers.go
Normal file
73
internal/updater/providers/perfectprivacy/servers.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package perfectprivacy
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"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.PerfectprivacyServer, warnings []string, err error) {
|
||||
zipURL := url.URL{
|
||||
Scheme: "https",
|
||||
Host: "www.perfect-privacy.com",
|
||||
Path: "/downloads/openvpn/get",
|
||||
}
|
||||
values := make(url.Values)
|
||||
values.Set("system", "linux")
|
||||
values.Set("scope", "server")
|
||||
values.Set("filetype", "zip")
|
||||
values.Set("protocol", "udp") // all support both TCP and UDP
|
||||
zipURL.RawQuery = values.Encode()
|
||||
|
||||
contents, err := unzipper.FetchAndExtract(ctx, zipURL.String())
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
cts := make(cityToServer)
|
||||
|
||||
for fileName, content := range contents {
|
||||
err := addServerFromOvpn(cts, fileName, content)
|
||||
if err != nil {
|
||||
warnings = append(warnings, fileName+": "+err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
if len(cts) < minServers {
|
||||
return nil, warnings, fmt.Errorf("%w: %d and expected at least %d",
|
||||
ErrNotEnoughServers, len(cts), minServers)
|
||||
}
|
||||
|
||||
servers = cts.toServersSlice()
|
||||
|
||||
sortServers(servers)
|
||||
|
||||
return servers, warnings, nil
|
||||
}
|
||||
|
||||
func addServerFromOvpn(cts cityToServer,
|
||||
fileName string, content []byte) (err error) {
|
||||
if !strings.HasSuffix(fileName, ".conf") {
|
||||
return nil // not an OpenVPN file
|
||||
}
|
||||
|
||||
ips, err := openvpn.ExtractIPs(content)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
city := parseFilename(fileName)
|
||||
|
||||
cts.add(city, ips)
|
||||
|
||||
return nil
|
||||
}
|
||||
13
internal/updater/providers/perfectprivacy/sort.go
Normal file
13
internal/updater/providers/perfectprivacy/sort.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package perfectprivacy
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
func sortServers(servers []models.PerfectprivacyServer) {
|
||||
sort.Slice(servers, func(i, j int) bool {
|
||||
return servers[i].City < servers[j].City
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user