chore(all): move sub-packages to internal/provider
This commit is contained in:
65
internal/provider/protonvpn/updater/api.go
Normal file
65
internal/provider/protonvpn/updater/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")
|
||||
)
|
||||
|
||||
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: %d %s", ErrHTTPStatusCodeNotOK,
|
||||
response.StatusCode, response.Status)
|
||||
}
|
||||
|
||||
decoder := json.NewDecoder(response.Body)
|
||||
if err := decoder.Decode(&data); err != nil {
|
||||
return data, fmt.Errorf("failed unmarshaling response body: %w", err)
|
||||
}
|
||||
|
||||
if err := response.Body.Close(); err != nil {
|
||||
return data, err
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
14
internal/provider/protonvpn/updater/countries.go
Normal file
14
internal/provider/protonvpn/updater/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
|
||||
}
|
||||
40
internal/provider/protonvpn/updater/iptoserver.go
Normal file
40
internal/provider/protonvpn/updater/iptoserver.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package protonvpn
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/constants/vpn"
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
type ipToServer map[string]models.Server
|
||||
|
||||
func (its ipToServer) add(country, region, city, name, hostname string,
|
||||
entryIP net.IP) {
|
||||
key := entryIP.String()
|
||||
|
||||
server, ok := its[key]
|
||||
if !ok {
|
||||
server.VPN = vpn.OpenVPN
|
||||
server.Country = country
|
||||
server.Region = region
|
||||
server.City = city
|
||||
server.ServerName = name
|
||||
server.Hostname = hostname
|
||||
server.UDP = true
|
||||
server.TCP = true
|
||||
server.IPs = []net.IP{entryIP}
|
||||
} else {
|
||||
server.IPs = append(server.IPs, entryIP)
|
||||
}
|
||||
|
||||
its[key] = server
|
||||
}
|
||||
|
||||
func (its ipToServer) toServersSlice() (servers []models.Server) {
|
||||
servers = make([]models.Server, 0, len(its))
|
||||
for _, server := range its {
|
||||
servers = append(servers, server)
|
||||
}
|
||||
return servers
|
||||
}
|
||||
80
internal/provider/protonvpn/updater/servers.go
Normal file
80
internal/provider/protonvpn/updater/servers.go
Normal file
@@ -0,0 +1,80 @@
|
||||
// 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.Server, 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)
|
||||
}
|
||||
|
||||
ipToServer := make(ipToServer, count)
|
||||
for _, logicalServer := range data.LogicalServers {
|
||||
region := getStringValue(logicalServer.Region)
|
||||
city := getStringValue(logicalServer.City)
|
||||
name := logicalServer.Name
|
||||
for _, physicalServer := range logicalServer.Servers {
|
||||
if physicalServer.Status == 0 { // disabled so skip server
|
||||
warnings = append(warnings,
|
||||
"ignoring server "+physicalServer.Domain+" with status 0")
|
||||
continue
|
||||
}
|
||||
|
||||
hostname := physicalServer.Domain
|
||||
entryIP := physicalServer.EntryIP
|
||||
|
||||
// Note: for multi-hop use the server name or hostname
|
||||
// instead of the country
|
||||
countryCode := logicalServer.ExitCountry
|
||||
country, warning := codeToCountry(countryCode, countryCodes)
|
||||
if warning != "" {
|
||||
warnings = append(warnings, warning)
|
||||
}
|
||||
|
||||
ipToServer.add(country, region, city, name, hostname, entryIP)
|
||||
}
|
||||
}
|
||||
|
||||
if len(ipToServer) < minServers {
|
||||
return nil, warnings, fmt.Errorf("%w: %d and expected at least %d",
|
||||
ErrNotEnoughServers, len(ipToServer), minServers)
|
||||
}
|
||||
|
||||
servers = ipToServer.toServersSlice()
|
||||
|
||||
sortServers(servers)
|
||||
|
||||
return servers, warnings, nil
|
||||
}
|
||||
|
||||
func getStringValue(ptr *string) string {
|
||||
if ptr == nil {
|
||||
return ""
|
||||
}
|
||||
return *ptr
|
||||
}
|
||||
26
internal/provider/protonvpn/updater/sort.go
Normal file
26
internal/provider/protonvpn/updater/sort.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package protonvpn
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
func sortServers(servers []models.Server) {
|
||||
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.ServerName == b.ServerName {
|
||||
return a.Hostname < b.Hostname
|
||||
}
|
||||
return a.ServerName < b.ServerName
|
||||
}
|
||||
return a.City < b.City
|
||||
}
|
||||
return a.Region < b.Region
|
||||
}
|
||||
return a.Country < b.Country
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user