Feat: Wireguard support for Ivpn (#584)
This commit is contained in:
@@ -26,10 +26,12 @@ type apiServer struct {
|
||||
Country string `json:"country"`
|
||||
City string `json:"city"`
|
||||
ISP string `json:"isp"`
|
||||
WgPubKey string `json:"wg_public_key"`
|
||||
}
|
||||
|
||||
type apiHostnames struct {
|
||||
OpenVPN string `json:"openvpn"`
|
||||
OpenVPN string `json:"openvpn"`
|
||||
Wireguard string `json:"wireguard"`
|
||||
}
|
||||
|
||||
func fetchAPI(ctx context.Context, client *http.Client) (
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/constants"
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
"github.com/qdm12/gluetun/internal/updater/resolver"
|
||||
)
|
||||
@@ -28,13 +29,15 @@ func GetServers(ctx context.Context, client *http.Client,
|
||||
hosts := make([]string, 0, len(data.Servers))
|
||||
|
||||
for _, serverData := range data.Servers {
|
||||
host := serverData.Hostnames.OpenVPN
|
||||
|
||||
if host == "" {
|
||||
continue // Wireguard
|
||||
openVPNHost := serverData.Hostnames.OpenVPN
|
||||
if openVPNHost != "" {
|
||||
hosts = append(hosts, openVPNHost)
|
||||
}
|
||||
|
||||
hosts = append(hosts, host)
|
||||
wireguardHost := serverData.Hostnames.Wireguard
|
||||
if wireguardHost != "" {
|
||||
hosts = append(hosts, wireguardHost)
|
||||
}
|
||||
}
|
||||
|
||||
if len(hosts) < minServers {
|
||||
@@ -49,19 +52,27 @@ func GetServers(ctx context.Context, client *http.Client,
|
||||
|
||||
servers = make([]models.IvpnServer, 0, len(hosts))
|
||||
for _, serverData := range data.Servers {
|
||||
host := serverData.Hostnames.OpenVPN
|
||||
if serverData.Hostnames.OpenVPN == "" {
|
||||
continue // Wireguard
|
||||
vpnType := constants.OpenVPN
|
||||
hostname := serverData.Hostnames.OpenVPN
|
||||
tcp := true
|
||||
wgPubKey := ""
|
||||
if hostname == "" {
|
||||
vpnType = constants.Wireguard
|
||||
hostname = serverData.Hostnames.Wireguard
|
||||
tcp = false
|
||||
wgPubKey = serverData.WgPubKey
|
||||
}
|
||||
|
||||
server := models.IvpnServer{
|
||||
VPN: vpnType,
|
||||
Country: serverData.Country,
|
||||
City: serverData.City,
|
||||
ISP: serverData.ISP,
|
||||
Hostname: serverData.Hostnames.OpenVPN,
|
||||
TCP: true,
|
||||
Hostname: hostname,
|
||||
WgPubKey: wgPubKey,
|
||||
TCP: tcp,
|
||||
UDP: true,
|
||||
IPs: hostToIPs[host],
|
||||
IPs: hostToIPs[hostname],
|
||||
}
|
||||
servers = append(servers, server)
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/qdm12/gluetun/internal/constants"
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
"github.com/qdm12/gluetun/internal/updater/resolver"
|
||||
"github.com/qdm12/gluetun/internal/updater/resolver/mock_resolver"
|
||||
@@ -70,23 +71,31 @@ func Test_GetServers(t *testing.T) {
|
||||
minServers: 1,
|
||||
responseBody: `{"servers":[
|
||||
{"country":"Country1","city":"City A","hostnames":{"openvpn":"hosta"}},
|
||||
{"country":"Country2","city":"City B","hostnames":{"openvpn":"hostb"}},
|
||||
{"country":"Country3","city":"City C","hostnames":{"wireguard":"hostc"}}
|
||||
{"country":"Country2","city":"City B","hostnames":{"openvpn":"hostb"},"wg_public_key":"xyz"},
|
||||
{"country":"Country3","city":"City C","hostnames":{"wireguard":"hostc"},"wg_public_key":"xyz"}
|
||||
]}`,
|
||||
responseStatus: http.StatusOK,
|
||||
expectResolve: true,
|
||||
hostsToResolve: []string{"hosta", "hostb"},
|
||||
hostsToResolve: []string{"hosta", "hostb", "hostc"},
|
||||
resolveSettings: getResolveSettings(1),
|
||||
hostToIPs: map[string][]net.IP{
|
||||
"hosta": {{1, 1, 1, 1}, {2, 2, 2, 2}},
|
||||
"hostb": {{3, 3, 3, 3}, {4, 4, 4, 4}},
|
||||
"hostc": {{5, 5, 5, 5}, {6, 6, 6, 6}},
|
||||
},
|
||||
resolveWarnings: []string{"resolve warning"},
|
||||
servers: []models.IvpnServer{
|
||||
{Country: "Country1", City: "City A", Hostname: "hosta",
|
||||
TCP: true, UDP: true, IPs: []net.IP{{1, 1, 1, 1}, {2, 2, 2, 2}}},
|
||||
{Country: "Country2", City: "City B", Hostname: "hostb",
|
||||
TCP: true, UDP: true, IPs: []net.IP{{3, 3, 3, 3}, {4, 4, 4, 4}}},
|
||||
{VPN: constants.OpenVPN, Country: "Country1",
|
||||
City: "City A", Hostname: "hosta", TCP: true, UDP: true,
|
||||
IPs: []net.IP{{1, 1, 1, 1}, {2, 2, 2, 2}}},
|
||||
{VPN: constants.OpenVPN, Country: "Country2",
|
||||
City: "City B", Hostname: "hostb", TCP: true, UDP: true,
|
||||
IPs: []net.IP{{3, 3, 3, 3}, {4, 4, 4, 4}}},
|
||||
{VPN: constants.Wireguard,
|
||||
Country: "Country3", City: "City C",
|
||||
Hostname: "hostc", UDP: true,
|
||||
WgPubKey: "xyz",
|
||||
IPs: []net.IP{{5, 5, 5, 5}, {6, 6, 6, 6}}},
|
||||
},
|
||||
warnings: []string{"resolve warning"},
|
||||
},
|
||||
|
||||
@@ -10,6 +10,9 @@ func sortServers(servers []models.IvpnServer) {
|
||||
sort.Slice(servers, func(i, j int) bool {
|
||||
if servers[i].Country == servers[j].Country {
|
||||
if servers[i].City == servers[j].City {
|
||||
if servers[i].Hostname == servers[j].Hostname {
|
||||
return servers[i].VPN < servers[j].VPN
|
||||
}
|
||||
return servers[i].Hostname < servers[j].Hostname
|
||||
}
|
||||
return servers[i].City < servers[j].City
|
||||
|
||||
Reference in New Issue
Block a user