Feature: filter by country, region and city for Privado
This commit is contained in:
31
internal/updater/providers/privado/location.go
Normal file
31
internal/updater/providers/privado/location.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package privado
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/http"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
"github.com/qdm12/gluetun/internal/publicip"
|
||||
)
|
||||
|
||||
func setLocationInfo(ctx context.Context, client *http.Client, servers []models.PrivadoServer) (err error) {
|
||||
// Get public IP address information
|
||||
ipsToGetInfo := make([]net.IP, len(servers))
|
||||
for i := range servers {
|
||||
ipsToGetInfo[i] = servers[i].IP
|
||||
}
|
||||
ipsInfo, err := publicip.MultiInfo(ctx, client, ipsToGetInfo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for i := range servers {
|
||||
ipInfo := ipsInfo[i]
|
||||
servers[i].Country = ipInfo.Country
|
||||
servers[i].Region = ipInfo.Region
|
||||
servers[i].City = ipInfo.City
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
@@ -17,7 +18,7 @@ import (
|
||||
var ErrNotEnoughServers = errors.New("not enough servers found")
|
||||
|
||||
func GetServers(ctx context.Context, unzipper unzip.Unzipper,
|
||||
presolver resolver.Parallel, minServers int) (
|
||||
client *http.Client, 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)
|
||||
@@ -66,6 +67,10 @@ func GetServers(ctx context.Context, unzipper unzip.Unzipper,
|
||||
|
||||
servers = hts.toServersSlice()
|
||||
|
||||
if err := setLocationInfo(ctx, client, servers); err != nil {
|
||||
return nil, warnings, err
|
||||
}
|
||||
|
||||
sortServers(servers)
|
||||
|
||||
return servers, warnings, nil
|
||||
|
||||
@@ -8,6 +8,15 @@ import (
|
||||
|
||||
func sortServers(servers []models.PrivadoServer) {
|
||||
sort.Slice(servers, func(i, j int) bool {
|
||||
return servers[i].Hostname < servers[j].Hostname
|
||||
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
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user