mirror of
https://github.com/qdm12/gluetun.git
synced 2026-05-07 04:20:12 +02:00
chore(models): streamline all server models IPs (#942)
- Use `IPs []net.IP` for all server models - Use `ips` JSON field for all server models - Merge IPv4 and IPv6 addresses together for Mullvad
This commit is contained in:
@@ -108,7 +108,6 @@ func (a *AllServers) GetMullvad() (servers []MullvadServer) {
|
||||
for i, serverToCopy := range a.Mullvad.Servers {
|
||||
servers[i] = serverToCopy
|
||||
servers[i].IPs = copyIPs(serverToCopy.IPs)
|
||||
servers[i].IPsV6 = copyIPs(serverToCopy.IPsV6)
|
||||
}
|
||||
return servers
|
||||
}
|
||||
@@ -120,7 +119,7 @@ func (a *AllServers) GetNordvpn() (servers []NordvpnServer) {
|
||||
servers = make([]NordvpnServer, len(a.Nordvpn.Servers))
|
||||
for i, serverToCopy := range a.Nordvpn.Servers {
|
||||
servers[i] = serverToCopy
|
||||
servers[i].IP = copyIP(serverToCopy.IP)
|
||||
servers[i].IPs = copyIPs(serverToCopy.IPs)
|
||||
}
|
||||
return servers
|
||||
}
|
||||
@@ -156,7 +155,7 @@ func (a *AllServers) GetPrivado() (servers []PrivadoServer) {
|
||||
servers = make([]PrivadoServer, len(a.Privado.Servers))
|
||||
for i, serverToCopy := range a.Privado.Servers {
|
||||
servers[i] = serverToCopy
|
||||
servers[i].IP = copyIP(serverToCopy.IP)
|
||||
servers[i].IPs = copyIPs(serverToCopy.IPs)
|
||||
}
|
||||
return servers
|
||||
}
|
||||
@@ -180,7 +179,7 @@ func (a *AllServers) GetProtonvpn() (servers []ProtonvpnServer) {
|
||||
servers = make([]ProtonvpnServer, len(a.Protonvpn.Servers))
|
||||
for i, serverToCopy := range a.Protonvpn.Servers {
|
||||
servers[i] = serverToCopy
|
||||
servers[i].EntryIP = copyIP(serverToCopy.EntryIP)
|
||||
servers[i].IPs = copyIPs(serverToCopy.IPs)
|
||||
}
|
||||
return servers
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ func Test_AllServers_GetCopy(t *testing.T) {
|
||||
},
|
||||
Nordvpn: NordvpnServers{
|
||||
Servers: []NordvpnServer{{
|
||||
IP: net.IP{1, 2, 3, 4},
|
||||
IPs: []net.IP{{1, 2, 3, 4}},
|
||||
}},
|
||||
},
|
||||
Perfectprivacy: PerfectprivacyServers{
|
||||
@@ -58,7 +58,7 @@ func Test_AllServers_GetCopy(t *testing.T) {
|
||||
},
|
||||
Privado: PrivadoServers{
|
||||
Servers: []PrivadoServer{{
|
||||
IP: net.IP{1, 2, 3, 4},
|
||||
IPs: []net.IP{{1, 2, 3, 4}},
|
||||
}},
|
||||
},
|
||||
Pia: PiaServers{
|
||||
@@ -73,7 +73,7 @@ func Test_AllServers_GetCopy(t *testing.T) {
|
||||
},
|
||||
Protonvpn: ProtonvpnServers{
|
||||
Servers: []ProtonvpnServer{{
|
||||
EntryIP: net.IP{1, 2, 3, 4},
|
||||
IPs: []net.IP{{1, 2, 3, 4}},
|
||||
}},
|
||||
},
|
||||
Purevpn: PurevpnServers{
|
||||
|
||||
+18
-19
@@ -63,7 +63,6 @@ type IvpnServer struct {
|
||||
type MullvadServer struct {
|
||||
VPN string `json:"vpn,omitempty"`
|
||||
IPs []net.IP `json:"ips,omitempty"`
|
||||
IPsV6 []net.IP `json:"ipsv6,omitempty"`
|
||||
Country string `json:"country,omitempty"`
|
||||
City string `json:"city,omitempty"`
|
||||
Hostname string `json:"hostname,omitempty"`
|
||||
@@ -73,12 +72,12 @@ type MullvadServer struct {
|
||||
}
|
||||
|
||||
type NordvpnServer struct { //nolint:maligned
|
||||
Region string `json:"region,omitempty"`
|
||||
Hostname string `json:"hostname,omitempty"`
|
||||
Number uint16 `json:"number,omitempty"`
|
||||
IP net.IP `json:"ip,omitempty"`
|
||||
TCP bool `json:"tcp,omitempty"`
|
||||
UDP bool `json:"udp,omitempty"`
|
||||
Region string `json:"region,omitempty"`
|
||||
Hostname string `json:"hostname,omitempty"`
|
||||
Number uint16 `json:"number,omitempty"`
|
||||
IPs []net.IP `json:"ips,omitempty"`
|
||||
TCP bool `json:"tcp,omitempty"`
|
||||
UDP bool `json:"udp,omitempty"`
|
||||
}
|
||||
|
||||
type PerfectprivacyServer struct {
|
||||
@@ -89,11 +88,11 @@ type PerfectprivacyServer struct {
|
||||
}
|
||||
|
||||
type PrivadoServer struct {
|
||||
Country string `json:"country,omitempty"`
|
||||
Region string `json:"region,omitempty"`
|
||||
City string `json:"city,omitempty"`
|
||||
Hostname string `json:"hostname,omitempty"`
|
||||
IP net.IP `json:"ip,omitempty"`
|
||||
Country string `json:"country,omitempty"`
|
||||
Region string `json:"region,omitempty"`
|
||||
City string `json:"city,omitempty"`
|
||||
Hostname string `json:"hostname,omitempty"`
|
||||
IPs []net.IP `json:"ips,omitempty"`
|
||||
}
|
||||
|
||||
type PIAServer struct {
|
||||
@@ -110,16 +109,16 @@ type PrivatevpnServer struct {
|
||||
Country string `json:"country,omitempty"`
|
||||
City string `json:"city,omitempty"`
|
||||
Hostname string `json:"hostname,omitempty"`
|
||||
IPs []net.IP `json:"ip,omitempty"`
|
||||
IPs []net.IP `json:"ips,omitempty"`
|
||||
}
|
||||
|
||||
type ProtonvpnServer struct {
|
||||
Country string `json:"country,omitempty"`
|
||||
Region string `json:"region,omitempty"`
|
||||
City string `json:"city,omitempty"`
|
||||
Name string `json:"server_name,omitempty"`
|
||||
Hostname string `json:"hostname,omitempty"`
|
||||
EntryIP net.IP `json:"entry_ip,omitempty"`
|
||||
Country string `json:"country,omitempty"`
|
||||
Region string `json:"region,omitempty"`
|
||||
City string `json:"city,omitempty"`
|
||||
Name string `json:"server_name,omitempty"`
|
||||
Hostname string `json:"hostname,omitempty"`
|
||||
IPs []net.IP `json:"ips,omitempty"`
|
||||
}
|
||||
|
||||
type PurevpnServer struct {
|
||||
|
||||
@@ -16,9 +16,13 @@ func (m *Mullvad) GetConnection(selection settings.ServerSelection) (
|
||||
return connection, err
|
||||
}
|
||||
|
||||
var connections []models.Connection
|
||||
connections := make([]models.Connection, 0, len(servers))
|
||||
for _, server := range servers {
|
||||
for _, IP := range server.IPs {
|
||||
if IP.To4() == nil {
|
||||
// do not use IPv6 connections for now
|
||||
continue
|
||||
}
|
||||
connection := models.Connection{
|
||||
Type: selection.VPN,
|
||||
IP: IP,
|
||||
|
||||
@@ -21,15 +21,17 @@ func (n *Nordvpn) GetConnection(selection settings.ServerSelection) (
|
||||
return connection, err
|
||||
}
|
||||
|
||||
connections := make([]models.Connection, len(servers))
|
||||
for i := range servers {
|
||||
connection := models.Connection{
|
||||
Type: selection.VPN,
|
||||
IP: servers[i].IP,
|
||||
Port: port,
|
||||
Protocol: protocol,
|
||||
connections := make([]models.Connection, 0, len(servers))
|
||||
for _, server := range servers {
|
||||
for _, ip := range server.IPs {
|
||||
connection := models.Connection{
|
||||
Type: selection.VPN,
|
||||
IP: ip,
|
||||
Port: port,
|
||||
Protocol: protocol,
|
||||
}
|
||||
connections = append(connections, connection)
|
||||
}
|
||||
connections[i] = connection
|
||||
}
|
||||
|
||||
return utils.PickConnection(connections, selection, n.randSource)
|
||||
|
||||
@@ -17,16 +17,18 @@ func (p *Privado) GetConnection(selection settings.ServerSelection) (
|
||||
return connection, err
|
||||
}
|
||||
|
||||
connections := make([]models.Connection, len(servers))
|
||||
for i := range servers {
|
||||
connection := models.Connection{
|
||||
Type: selection.VPN,
|
||||
IP: servers[i].IP,
|
||||
Port: port,
|
||||
Protocol: protocol,
|
||||
Hostname: servers[i].Hostname,
|
||||
connections := make([]models.Connection, 0, len(servers))
|
||||
for _, server := range servers {
|
||||
for _, ip := range server.IPs {
|
||||
connection := models.Connection{
|
||||
Type: selection.VPN,
|
||||
IP: ip,
|
||||
Port: port,
|
||||
Protocol: protocol,
|
||||
Hostname: server.Hostname,
|
||||
}
|
||||
connections = append(connections, connection)
|
||||
}
|
||||
connections[i] = connection
|
||||
}
|
||||
|
||||
return utils.PickConnection(connections, selection, p.randSource)
|
||||
|
||||
@@ -24,13 +24,16 @@ func (p *Protonvpn) GetConnection(selection settings.ServerSelection) (
|
||||
return connection, err
|
||||
}
|
||||
|
||||
connections := make([]models.Connection, len(servers))
|
||||
for i := range servers {
|
||||
connections[i] = models.Connection{
|
||||
Type: selection.VPN,
|
||||
IP: servers[i].EntryIP,
|
||||
Port: port,
|
||||
Protocol: protocol,
|
||||
connections := make([]models.Connection, 0, len(servers))
|
||||
for _, server := range servers {
|
||||
for _, ip := range server.IPs {
|
||||
connection := models.Connection{
|
||||
Type: selection.VPN,
|
||||
IP: ip,
|
||||
Port: port,
|
||||
Protocol: protocol,
|
||||
}
|
||||
connections = append(connections, connection)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+26193
-12139
File diff suppressed because it is too large
Load Diff
@@ -73,21 +73,6 @@ func ExtractIPs(b []byte) (ips []net.IP, err error) {
|
||||
return ips, nil
|
||||
}
|
||||
|
||||
func ExtractIP(b []byte) (ip net.IP, warning string, err error) {
|
||||
ips, err := ExtractIPs(b)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
if len(ips) > 1 {
|
||||
warning = fmt.Sprintf(
|
||||
"only using the first IP address %s and discarding %d other hosts",
|
||||
ips[0], len(ips)-1)
|
||||
}
|
||||
|
||||
return ips[0], warning, nil
|
||||
}
|
||||
|
||||
func extractRemoteHosts(content []byte, rejectIP, rejectDomain bool) (hosts []string) {
|
||||
lines := strings.Split(string(content), "\n")
|
||||
for _, line := range lines {
|
||||
|
||||
@@ -50,7 +50,7 @@ func (hts hostToServer) add(data serverData) (err error) {
|
||||
if ipv4 == nil || ipv4.To4() == nil {
|
||||
return fmt.Errorf("%w: %s", ErrParseIPv4, data.IPv4)
|
||||
}
|
||||
server.IPs = []net.IP{ipv4}
|
||||
server.IPs = append(server.IPs, ipv4)
|
||||
}
|
||||
|
||||
if data.IPv6 != "" {
|
||||
@@ -58,7 +58,7 @@ func (hts hostToServer) add(data serverData) (err error) {
|
||||
if ipv6 == nil || ipv6.To4() != nil {
|
||||
return fmt.Errorf("%w: %s", ErrParseIPv6, data.IPv6)
|
||||
}
|
||||
server.IPsV6 = []net.IP{ipv6}
|
||||
server.IPs = append(server.IPs, ipv6)
|
||||
}
|
||||
|
||||
server.Country = data.Country
|
||||
@@ -77,7 +77,6 @@ func (hts hostToServer) toServersSlice() (servers []models.MullvadServer) {
|
||||
servers = make([]models.MullvadServer, 0, len(hts))
|
||||
for _, server := range hts {
|
||||
server.IPs = uniqueSortedIPs(server.IPs)
|
||||
server.IPsV6 = uniqueSortedIPs(server.IPsV6)
|
||||
servers = append(servers, server)
|
||||
}
|
||||
return servers
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
@@ -47,7 +48,7 @@ func GetServers(ctx context.Context, client *http.Client, minServers int) (
|
||||
Region: jsonServer.Country,
|
||||
Hostname: jsonServer.Domain,
|
||||
Number: number,
|
||||
IP: ip,
|
||||
IPs: []net.IP{ip},
|
||||
TCP: jsonServer.Features.TCP,
|
||||
UDP: jsonServer.Features.UDP,
|
||||
}
|
||||
|
||||
@@ -25,23 +25,17 @@ func (hts hostToServer) toHostsSlice() (hosts []string) {
|
||||
return hosts
|
||||
}
|
||||
|
||||
func (hts hostToServer) adaptWithIPs(hostToIPs map[string][]net.IP) (
|
||||
warnings []string) {
|
||||
func (hts hostToServer) adaptWithIPs(hostToIPs map[string][]net.IP) {
|
||||
for host, IPs := range hostToIPs {
|
||||
if len(IPs) > 1 {
|
||||
warning := "more than one IP address found for host " + host
|
||||
warnings = append(warnings, warning)
|
||||
}
|
||||
server := hts[host]
|
||||
server.IP = IPs[0]
|
||||
server.IPs = IPs
|
||||
hts[host] = server
|
||||
}
|
||||
for host, server := range hts {
|
||||
if server.IP == nil {
|
||||
if len(server.IPs) == 0 {
|
||||
delete(hts, host)
|
||||
}
|
||||
}
|
||||
return warnings
|
||||
}
|
||||
|
||||
func (hts hostToServer) toServersSlice() (servers []models.PrivadoServer) {
|
||||
|
||||
@@ -11,9 +11,9 @@ import (
|
||||
|
||||
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
|
||||
ipsToGetInfo := make([]net.IP, 0, len(servers))
|
||||
for _, server := range servers {
|
||||
ipsToGetInfo = append(ipsToGetInfo, server.IPs...)
|
||||
}
|
||||
ipsInfo, err := publicip.MultiInfo(ctx, client, ipsToGetInfo)
|
||||
if err != nil {
|
||||
|
||||
@@ -62,8 +62,7 @@ func GetServers(ctx context.Context, unzipper unzip.Unzipper,
|
||||
return nil, warnings, err
|
||||
}
|
||||
|
||||
newWarnings = hts.adaptWithIPs(hostToIPs)
|
||||
warnings = append(warnings, newWarnings...)
|
||||
hts.adaptWithIPs(hostToIPs)
|
||||
|
||||
servers = hts.toServersSlice()
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/constants"
|
||||
@@ -60,7 +59,7 @@ func GetServers(ctx context.Context, unzipper unzip.Unzipper,
|
||||
continue
|
||||
}
|
||||
|
||||
ip, warning, extractIPErr := openvpn.ExtractIP(content)
|
||||
ips, extractIPErr := openvpn.ExtractIPs(content)
|
||||
if warning != "" {
|
||||
warnings = append(warnings, warning)
|
||||
}
|
||||
@@ -73,7 +72,7 @@ func GetServers(ctx context.Context, unzipper unzip.Unzipper,
|
||||
server := models.PrivatevpnServer{
|
||||
Country: country,
|
||||
City: city,
|
||||
IPs: []net.IP{ip},
|
||||
IPs: ips,
|
||||
}
|
||||
noHostnameServers = append(noHostnameServers, server)
|
||||
}
|
||||
|
||||
@@ -19,7 +19,9 @@ func (its ipToServer) add(country, region, city, name, hostname string,
|
||||
server.City = city
|
||||
server.Name = name
|
||||
server.Hostname = hostname
|
||||
server.EntryIP = entryIP
|
||||
server.IPs = []net.IP{entryIP}
|
||||
} else {
|
||||
server.IPs = append(server.IPs, entryIP)
|
||||
}
|
||||
|
||||
its[key] = server
|
||||
|
||||
@@ -8,14 +8,18 @@ import (
|
||||
|
||||
type hostToServer map[string]models.TorguardServer
|
||||
|
||||
func (hts hostToServer) add(host, country, city string, tcp, udp bool, ip net.IP) {
|
||||
func (hts hostToServer) add(host, country, city string,
|
||||
tcp, udp bool, ips []net.IP) {
|
||||
server, ok := hts[host]
|
||||
if !ok {
|
||||
server.Hostname = host
|
||||
server.Country = country
|
||||
server.City = city
|
||||
server.IPs = append(server.IPs, ip) // used if DNS resolution fails downstream
|
||||
server.IPs = ips // used if DNS resolution fails downstream
|
||||
} else {
|
||||
server.IPs = append(server.IPs, ips...)
|
||||
}
|
||||
|
||||
if tcp {
|
||||
server.TCP = tcp
|
||||
}
|
||||
|
||||
@@ -90,10 +90,7 @@ func addServerFromOvpn(fileName string, content []byte,
|
||||
return warnings
|
||||
}
|
||||
|
||||
ip, warning, err := openvpn.ExtractIP(content)
|
||||
if warning != "" {
|
||||
warnings = append(warnings, warning)
|
||||
}
|
||||
ips, err := openvpn.ExtractIPs(content)
|
||||
if err != nil {
|
||||
// treat error as warning and go to next file
|
||||
warning := err.Error() + " in " + fileName
|
||||
@@ -101,6 +98,6 @@ func addServerFromOvpn(fileName string, content []byte,
|
||||
return warnings
|
||||
}
|
||||
|
||||
hts.add(host, country, city, tcp, udp, ip)
|
||||
hts.add(host, country, city, tcp, udp, ips)
|
||||
return warnings
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user