mirror of
https://github.com/qdm12/gluetun.git
synced 2026-06-18 17:34:02 +02:00
chore(updater): move updater packages to pkg/updaters/<name>
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
package surfshark
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/provider/surfshark/servers"
|
||||
)
|
||||
|
||||
func addServersFromAPI(ctx context.Context, client *http.Client,
|
||||
hts hostToServers,
|
||||
) (err error) {
|
||||
data, err := fetchAPI(ctx, client)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
locationData := servers.LocationData()
|
||||
hostToLocation := hostToLocation(locationData)
|
||||
|
||||
for _, serverData := range data {
|
||||
locationData := hostToLocation[serverData.Host] // TODO remove in v4
|
||||
retroLoc := locationData.RetroLoc // empty string if the host has no retro-compatible region
|
||||
|
||||
tcp, udp := true, true // OpenVPN servers from API supports both TCP and UDP
|
||||
hts.addOpenVPN(serverData.Host, serverData.Region, serverData.Country,
|
||||
serverData.Location, retroLoc, tcp, udp)
|
||||
|
||||
if serverData.PubKey != "" {
|
||||
hts.addWireguard(serverData.Host, serverData.Region, serverData.Country,
|
||||
serverData.Location, retroLoc, serverData.PubKey)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
var ErrHTTPStatusCodeNotOK = errors.New("HTTP status code not OK")
|
||||
|
||||
type serverData struct {
|
||||
Host string `json:"connectionName"`
|
||||
Region string `json:"region"`
|
||||
Country string `json:"country"`
|
||||
Location string `json:"location"`
|
||||
PubKey string `json:"pubKey"`
|
||||
}
|
||||
|
||||
func fetchAPI(ctx context.Context, client *http.Client) (
|
||||
servers []serverData, err error,
|
||||
) {
|
||||
const url = "https://api.surfshark.com/v4/server/clusters"
|
||||
|
||||
for _, clustersType := range [...]string{"generic", "double", "static", "obfuscated"} {
|
||||
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url+"/"+clustersType, 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: %d %s", ErrHTTPStatusCodeNotOK,
|
||||
response.StatusCode, response.Status)
|
||||
}
|
||||
|
||||
decoder := json.NewDecoder(response.Body)
|
||||
var newServers []serverData
|
||||
err = decoder.Decode(&newServers)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("decoding response body: %w", err)
|
||||
}
|
||||
|
||||
err = response.Body.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
servers = append(servers, newServers...)
|
||||
}
|
||||
|
||||
return servers, nil
|
||||
}
|
||||
@@ -0,0 +1,240 @@
|
||||
package surfshark
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/constants/vpn"
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type httpExchange struct {
|
||||
requestURL string
|
||||
responseStatus int
|
||||
responseBody io.ReadCloser
|
||||
}
|
||||
|
||||
func Test_addServersFromAPI(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
testCases := map[string]struct {
|
||||
hts hostToServers
|
||||
exchanges []httpExchange
|
||||
expected hostToServers
|
||||
err error
|
||||
}{
|
||||
"fetch API error": {
|
||||
exchanges: []httpExchange{{
|
||||
requestURL: "https://api.surfshark.com/v4/server/clusters/generic",
|
||||
responseStatus: http.StatusNoContent,
|
||||
}},
|
||||
err: errors.New("HTTP status code not OK: 204 No Content"),
|
||||
},
|
||||
"success": {
|
||||
hts: hostToServers{
|
||||
"existinghost": []models.Server{{Hostname: "existinghost"}},
|
||||
},
|
||||
exchanges: []httpExchange{{
|
||||
requestURL: "https://api.surfshark.com/v4/server/clusters/generic",
|
||||
responseStatus: http.StatusOK,
|
||||
responseBody: io.NopCloser(strings.NewReader(`[
|
||||
{"connectionName":"host1","region":"region1","country":"country1","location":"location1"},
|
||||
{"connectionName":"host1","region":"region1","country":"country1","location":"location1","pubkey":"pubKeyValue"},
|
||||
{"connectionName":"host2","region":"region2","country":"country1","location":"location2"}
|
||||
]`)),
|
||||
}, {
|
||||
requestURL: "https://api.surfshark.com/v4/server/clusters/double",
|
||||
responseStatus: http.StatusOK,
|
||||
responseBody: io.NopCloser(strings.NewReader(`[]`)),
|
||||
}, {
|
||||
requestURL: "https://api.surfshark.com/v4/server/clusters/static",
|
||||
responseStatus: http.StatusOK,
|
||||
responseBody: io.NopCloser(strings.NewReader(`[]`)),
|
||||
}, {
|
||||
requestURL: "https://api.surfshark.com/v4/server/clusters/obfuscated",
|
||||
responseStatus: http.StatusOK,
|
||||
responseBody: io.NopCloser(strings.NewReader(`[]`)),
|
||||
}},
|
||||
expected: map[string][]models.Server{
|
||||
"existinghost": {{Hostname: "existinghost"}},
|
||||
"host1": {{
|
||||
VPN: vpn.OpenVPN,
|
||||
Region: "region1",
|
||||
Country: "country1",
|
||||
City: "location1",
|
||||
Hostname: "host1",
|
||||
TCP: true,
|
||||
UDP: true,
|
||||
}, {
|
||||
VPN: vpn.Wireguard,
|
||||
Region: "region1",
|
||||
Country: "country1",
|
||||
City: "location1",
|
||||
Hostname: "host1",
|
||||
WgPubKey: "pubKeyValue",
|
||||
}},
|
||||
"host2": {{
|
||||
VPN: vpn.OpenVPN,
|
||||
Region: "region2",
|
||||
Country: "country1",
|
||||
City: "location2",
|
||||
Hostname: "host2",
|
||||
TCP: true,
|
||||
UDP: true,
|
||||
}},
|
||||
},
|
||||
},
|
||||
}
|
||||
for name, testCase := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
currentExchangeIndex := 0
|
||||
|
||||
client := &http.Client{
|
||||
Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) {
|
||||
assert.Equal(t, http.MethodGet, r.Method)
|
||||
exchange := testCase.exchanges[currentExchangeIndex]
|
||||
currentExchangeIndex++
|
||||
assert.Equal(t, exchange.requestURL, r.URL.String())
|
||||
return &http.Response{
|
||||
StatusCode: exchange.responseStatus,
|
||||
Status: http.StatusText(exchange.responseStatus),
|
||||
Body: exchange.responseBody,
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
|
||||
err := addServersFromAPI(ctx, client, testCase.hts)
|
||||
|
||||
assert.Equal(t, testCase.expected, testCase.hts)
|
||||
if testCase.err != nil {
|
||||
require.Error(t, err)
|
||||
assert.Equal(t, testCase.err.Error(), err.Error())
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_fetchAPI(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
testCases := map[string]struct {
|
||||
exchanges []httpExchange
|
||||
data []serverData
|
||||
err error
|
||||
}{
|
||||
"http response status not ok": {
|
||||
exchanges: []httpExchange{{
|
||||
requestURL: "https://api.surfshark.com/v4/server/clusters/generic",
|
||||
responseStatus: http.StatusNoContent,
|
||||
}},
|
||||
err: errors.New("HTTP status code not OK: 204 No Content"),
|
||||
},
|
||||
"nil body": {
|
||||
exchanges: []httpExchange{{
|
||||
requestURL: "https://api.surfshark.com/v4/server/clusters/generic",
|
||||
responseStatus: http.StatusOK,
|
||||
}},
|
||||
err: errors.New("decoding response body: EOF"),
|
||||
},
|
||||
"no server": {
|
||||
exchanges: []httpExchange{{
|
||||
requestURL: "https://api.surfshark.com/v4/server/clusters/generic",
|
||||
responseStatus: http.StatusOK,
|
||||
responseBody: io.NopCloser(strings.NewReader(`[]`)),
|
||||
}, {
|
||||
requestURL: "https://api.surfshark.com/v4/server/clusters/double",
|
||||
responseStatus: http.StatusOK,
|
||||
responseBody: io.NopCloser(strings.NewReader(`[]`)),
|
||||
}, {
|
||||
requestURL: "https://api.surfshark.com/v4/server/clusters/static",
|
||||
responseStatus: http.StatusOK,
|
||||
responseBody: io.NopCloser(strings.NewReader(`[]`)),
|
||||
}, {
|
||||
requestURL: "https://api.surfshark.com/v4/server/clusters/obfuscated",
|
||||
responseStatus: http.StatusOK,
|
||||
responseBody: io.NopCloser(strings.NewReader(`[]`)),
|
||||
}},
|
||||
},
|
||||
"success": {
|
||||
exchanges: []httpExchange{{
|
||||
requestURL: "https://api.surfshark.com/v4/server/clusters/generic",
|
||||
responseStatus: http.StatusOK,
|
||||
responseBody: io.NopCloser(strings.NewReader(`[
|
||||
{"connectionName":"host1","region":"region1","country":"country1","location":"location1"},
|
||||
{"connectionName":"host2","region":"region2","country":"country1","location":"location2"}
|
||||
]`)),
|
||||
}, {
|
||||
requestURL: "https://api.surfshark.com/v4/server/clusters/double",
|
||||
responseStatus: http.StatusOK,
|
||||
responseBody: io.NopCloser(strings.NewReader(`[]`)),
|
||||
}, {
|
||||
requestURL: "https://api.surfshark.com/v4/server/clusters/static",
|
||||
responseStatus: http.StatusOK,
|
||||
responseBody: io.NopCloser(strings.NewReader(`[]`)),
|
||||
}, {
|
||||
requestURL: "https://api.surfshark.com/v4/server/clusters/obfuscated",
|
||||
responseStatus: http.StatusOK,
|
||||
responseBody: io.NopCloser(strings.NewReader(`[]`)),
|
||||
}},
|
||||
data: []serverData{
|
||||
{
|
||||
Region: "region1",
|
||||
Country: "country1",
|
||||
Location: "location1",
|
||||
Host: "host1",
|
||||
},
|
||||
{
|
||||
Region: "region2",
|
||||
Country: "country1",
|
||||
Location: "location2",
|
||||
Host: "host2",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
for name, testCase := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
currentExchangeIndex := 0
|
||||
|
||||
client := &http.Client{
|
||||
Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) {
|
||||
assert.Equal(t, http.MethodGet, r.Method)
|
||||
exchange := testCase.exchanges[currentExchangeIndex]
|
||||
currentExchangeIndex++
|
||||
assert.Equal(t, exchange.requestURL, r.URL.String())
|
||||
return &http.Response{
|
||||
StatusCode: exchange.responseStatus,
|
||||
Status: http.StatusText(exchange.responseStatus),
|
||||
Body: exchange.responseBody,
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
|
||||
data, err := fetchAPI(ctx, client)
|
||||
|
||||
assert.Equal(t, testCase.data, data)
|
||||
if testCase.err != nil {
|
||||
require.Error(t, err)
|
||||
assert.Equal(t, testCase.err.Error(), err.Error())
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package surfshark
|
||||
|
||||
import (
|
||||
"net/netip"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/constants/vpn"
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
type hostToServers map[string][]models.Server
|
||||
|
||||
func (hts hostToServers) addOpenVPN(host, region, country, city,
|
||||
retroLoc string, tcp, udp bool,
|
||||
) {
|
||||
// Check for existing server for this host and OpenVPN.
|
||||
servers := hts[host]
|
||||
for i, existingServer := range servers {
|
||||
if existingServer.Hostname != host ||
|
||||
existingServer.VPN != vpn.OpenVPN {
|
||||
continue
|
||||
}
|
||||
|
||||
// Update OpenVPN supported protocols and return
|
||||
if !existingServer.TCP {
|
||||
servers[i].TCP = tcp
|
||||
}
|
||||
if !existingServer.UDP {
|
||||
servers[i].UDP = udp
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
server := models.Server{
|
||||
VPN: vpn.OpenVPN,
|
||||
Region: region,
|
||||
Country: country,
|
||||
City: city,
|
||||
RetroLoc: retroLoc,
|
||||
Hostname: host,
|
||||
TCP: tcp,
|
||||
UDP: udp,
|
||||
}
|
||||
hts[host] = append(servers, server)
|
||||
}
|
||||
|
||||
func (hts hostToServers) addWireguard(host, region, country, city, retroLoc,
|
||||
wgPubKey string,
|
||||
) {
|
||||
// Check for existing server for this host and Wireguard.
|
||||
servers := hts[host]
|
||||
for _, existingServer := range servers {
|
||||
if existingServer.Hostname == host &&
|
||||
existingServer.VPN == vpn.Wireguard {
|
||||
// No update necessary for Wireguard
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
server := models.Server{
|
||||
VPN: vpn.Wireguard,
|
||||
Region: region,
|
||||
Country: country,
|
||||
City: city,
|
||||
RetroLoc: retroLoc,
|
||||
Hostname: host,
|
||||
WgPubKey: wgPubKey,
|
||||
}
|
||||
hts[host] = append(servers, server)
|
||||
}
|
||||
|
||||
func (hts hostToServers) toHostsSlice() (hosts []string) {
|
||||
const vpnServerTypes = 2 // OpenVPN + Wireguard
|
||||
hosts = make([]string, 0, vpnServerTypes*len(hts))
|
||||
for host := range hts {
|
||||
hosts = append(hosts, host)
|
||||
}
|
||||
return hosts
|
||||
}
|
||||
|
||||
func (hts hostToServers) adaptWithIPs(hostToIPs map[string][]netip.Addr) {
|
||||
for host, IPs := range hostToIPs {
|
||||
servers := hts[host]
|
||||
for i := range servers {
|
||||
servers[i].IPs = IPs
|
||||
}
|
||||
hts[host] = servers
|
||||
}
|
||||
for host, servers := range hts {
|
||||
if len(servers[0].IPs) == 0 {
|
||||
delete(hts, host)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (hts hostToServers) toServersSlice() (servers []models.Server) {
|
||||
const vpnServerTypes = 2 // OpenVPN + Wireguard
|
||||
servers = make([]models.Server, 0, vpnServerTypes*len(hts))
|
||||
for _, serversForHost := range hts {
|
||||
servers = append(servers, serversForHost...)
|
||||
}
|
||||
return servers
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package surfshark
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/provider/surfshark/servers"
|
||||
)
|
||||
|
||||
var errHostnameNotFound = errors.New("hostname not found in hostname to location mapping")
|
||||
|
||||
func getHostInformation(host string, hostnameToLocation map[string]servers.ServerLocation) (
|
||||
data servers.ServerLocation, err error,
|
||||
) {
|
||||
locationData, ok := hostnameToLocation[host]
|
||||
if !ok {
|
||||
return locationData, fmt.Errorf("%w: %s", errHostnameNotFound, host)
|
||||
}
|
||||
|
||||
return locationData, nil
|
||||
}
|
||||
|
||||
func hostToLocation(locationData []servers.ServerLocation) (
|
||||
hostToLocation map[string]servers.ServerLocation,
|
||||
) {
|
||||
hostToLocation = make(map[string]servers.ServerLocation, len(locationData))
|
||||
for _, data := range locationData {
|
||||
hostToLocation[data.Hostname] = data
|
||||
}
|
||||
return hostToLocation
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package surfshark
|
||||
|
||||
import (
|
||||
"github.com/qdm12/gluetun/internal/provider/surfshark/servers"
|
||||
)
|
||||
|
||||
// getRemainingServers finds extra servers not found in the API or in the ZIP file.
|
||||
func getRemainingServers(hts hostToServers) {
|
||||
locationData := servers.LocationData()
|
||||
hostnameToLocationLeft := hostToLocation(locationData)
|
||||
for _, hostnameDone := range hts.toHostsSlice() {
|
||||
delete(hostnameToLocationLeft, hostnameDone)
|
||||
}
|
||||
|
||||
for hostname, locationData := range hostnameToLocationLeft {
|
||||
// we assume the OpenVPN server supports both TCP and UDP
|
||||
const tcp, udp = true, true
|
||||
hts.addOpenVPN(hostname, locationData.Region, locationData.Country,
|
||||
locationData.City, locationData.RetroLoc, tcp, udp)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package surfshark
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/updater/resolver"
|
||||
)
|
||||
|
||||
func parallelResolverSettings(hosts []string) (settings resolver.ParallelSettings) {
|
||||
const (
|
||||
maxFailRatio = 0.1
|
||||
maxDuration = 20 * time.Second
|
||||
betweenDuration = time.Second
|
||||
maxNoNew = 2
|
||||
maxFails = 2
|
||||
)
|
||||
return resolver.ParallelSettings{
|
||||
Hosts: hosts,
|
||||
MaxFailRatio: maxFailRatio,
|
||||
Repeat: resolver.RepeatSettings{
|
||||
MaxDuration: maxDuration,
|
||||
BetweenDuration: betweenDuration,
|
||||
MaxNoNew: maxNoNew,
|
||||
MaxFails: maxFails,
|
||||
SortIPs: true,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package surfshark
|
||||
|
||||
import "net/http"
|
||||
|
||||
type roundTripFunc func(r *http.Request) (*http.Response, error)
|
||||
|
||||
func (f roundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) {
|
||||
return f(r)
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package surfshark
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
"github.com/qdm12/gluetun/internal/provider/common"
|
||||
)
|
||||
|
||||
func (u *Updater) FetchServers(ctx context.Context, minServers int) (
|
||||
servers []models.Server, err error,
|
||||
) {
|
||||
hts := make(hostToServers)
|
||||
|
||||
err = addServersFromAPI(ctx, u.client, hts)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("fetching server information from API: %w", err)
|
||||
}
|
||||
|
||||
warnings, err := addOpenVPNServersFromZip(ctx, u.unzipper, hts)
|
||||
for _, warning := range warnings {
|
||||
u.warner.Warn(warning)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("getting OpenVPN ZIP file: %w", err)
|
||||
}
|
||||
|
||||
getRemainingServers(hts)
|
||||
|
||||
hosts := hts.toHostsSlice()
|
||||
resolveSettings := parallelResolverSettings(hosts)
|
||||
hostToIPs, warnings, err := u.parallelResolver.Resolve(ctx, resolveSettings)
|
||||
for _, warning := range warnings {
|
||||
u.warner.Warn(warning)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
hts.adaptWithIPs(hostToIPs)
|
||||
|
||||
if len(hts) < minServers {
|
||||
return nil, fmt.Errorf("%w: %d and expected at least %d",
|
||||
common.ErrNotEnoughServers, len(hts), minServers)
|
||||
}
|
||||
|
||||
servers = hts.toServersSlice()
|
||||
|
||||
sort.Sort(models.SortableServers(servers))
|
||||
|
||||
return servers, nil
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package surfshark
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/provider/common"
|
||||
)
|
||||
|
||||
type Updater struct {
|
||||
client *http.Client
|
||||
unzipper common.Unzipper
|
||||
parallelResolver common.ParallelResolver
|
||||
warner common.Warner
|
||||
}
|
||||
|
||||
func New(client *http.Client, unzipper common.Unzipper,
|
||||
warner common.Warner, parallelResolver common.ParallelResolver,
|
||||
) *Updater {
|
||||
return &Updater{
|
||||
client: client,
|
||||
unzipper: unzipper,
|
||||
parallelResolver: parallelResolver,
|
||||
warner: warner,
|
||||
}
|
||||
}
|
||||
|
||||
func (u *Updater) Version() uint16 {
|
||||
return 4 //nolint:mnd
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package surfshark
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/provider/common"
|
||||
"github.com/qdm12/gluetun/internal/provider/surfshark/servers"
|
||||
"github.com/qdm12/gluetun/internal/updater/openvpn"
|
||||
)
|
||||
|
||||
func addOpenVPNServersFromZip(ctx context.Context,
|
||||
unzipper common.Unzipper, hts hostToServers) (
|
||||
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, err
|
||||
}
|
||||
|
||||
hostnamesDone := hts.toHostsSlice()
|
||||
hostnamesDoneSet := make(map[string]struct{}, len(hostnamesDone))
|
||||
for _, hostname := range hostnamesDone {
|
||||
hostnamesDoneSet[hostname] = struct{}{}
|
||||
}
|
||||
|
||||
locationData := servers.LocationData()
|
||||
hostToLocation := hostToLocation(locationData)
|
||||
|
||||
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
|
||||
// TODO gather location data for IP address Openvpn files
|
||||
// and process those when this error triggers.
|
||||
warnings = append(warnings, warning)
|
||||
continue
|
||||
}
|
||||
|
||||
_, ok := hostnamesDoneSet[host]
|
||||
if ok {
|
||||
continue // already done in API
|
||||
}
|
||||
|
||||
tcp, udp, err := openvpn.ExtractProto(content)
|
||||
if err != nil {
|
||||
// treat error as warning and go to next file
|
||||
warning := err.Error() + " in " + fileName
|
||||
warnings = append(warnings, warning)
|
||||
continue
|
||||
}
|
||||
|
||||
data, err := getHostInformation(host, hostToLocation)
|
||||
if err != nil {
|
||||
// treat error as warning and go to next file
|
||||
warning := err.Error()
|
||||
warnings = append(warnings, warning)
|
||||
continue
|
||||
}
|
||||
|
||||
hts.addOpenVPN(host, data.Region, data.Country, data.City,
|
||||
data.RetroLoc, tcp, udp)
|
||||
}
|
||||
|
||||
return warnings, nil
|
||||
}
|
||||
Reference in New Issue
Block a user