From 73355f346f0130e10850b684ff4d57bde64616d1 Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Mon, 4 Sep 2023 20:52:06 +0100 Subject: [PATCH] Add wireguard servers to list from api. --- .../privateinternetaccess/updater/api.go | 1 + .../updater/hosttoserver.go | 99 +++++++++++++------ .../privateinternetaccess/updater/servers.go | 11 ++- 3 files changed, 80 insertions(+), 31 deletions(-) diff --git a/internal/provider/privateinternetaccess/updater/api.go b/internal/provider/privateinternetaccess/updater/api.go index fc17d2aa6..5c249d0f0 100644 --- a/internal/provider/privateinternetaccess/updater/api.go +++ b/internal/provider/privateinternetaccess/updater/api.go @@ -27,6 +27,7 @@ type regionData struct { Servers struct { UDP []serverData `json:"ovpnudp"` TCP []serverData `json:"ovpntcp"` + WG []serverData `json:"wg"` } `json:"servers"` } diff --git a/internal/provider/privateinternetaccess/updater/hosttoserver.go b/internal/provider/privateinternetaccess/updater/hosttoserver.go index 76cd6efc8..83d816fef 100644 --- a/internal/provider/privateinternetaccess/updater/hosttoserver.go +++ b/internal/provider/privateinternetaccess/updater/hosttoserver.go @@ -7,51 +7,92 @@ import ( "github.com/qdm12/gluetun/internal/models" ) -type nameToServer map[string]models.Server +type nameToServer map[string][]models.Server func (nts nameToServer) add(name, hostname, region string, - tcp, udp, portForward bool, ip netip.Addr) (change bool) { - server, ok := nts[name] - if !ok { - change = true - server.VPN = vpn.OpenVPN - server.ServerName = name - server.Hostname = hostname - server.Region = region - server.PortForward = portForward - } + serverType string, tcp, udp, portForward bool, ip netip.Addr) (change bool) { - if !server.TCP && tcp { - change = true - server.TCP = tcp - } - if !server.UDP && udp { - change = true - server.UDP = udp - } + var server models.Server - ipFound := false - for _, existingIP := range server.IPs { - if ip == existingIP { - ipFound = true + // Check for existing server for this name. + servers := nts[name] + for i, existingServer := range servers { + if existingServer.ServerName != name || existingServer.VPN != serverType { + continue + } + + server = existingServer + + switch existingServer.VPN { + case vpn.OpenVPN: + // Update OpenVPN supported protocols and return + if !existingServer.TCP { + servers[i].TCP = tcp + change = true + } + if !existingServer.UDP { + servers[i].UDP = udp + change = true + } + ipFound := false + for _, existingIP := range server.IPs { + if ip == existingIP { + ipFound = true + break + } + } + if !ipFound { + server.IPs = append(server.IPs, ip) + change = true + } + break + case vpn.Wireguard: + // Update IPs and return + ipFound := false + for _, existingIP := range server.IPs { + if ip == existingIP { + ipFound = true + break + } + } + if !ipFound { + server.IPs = append(server.IPs, ip) + change = true + } break } + + break } - if !ipFound { + if server.ServerName == "" { change = true - server.IPs = append(server.IPs, ip) + nts[name] = append(servers, models.Server{ + VPN: vpn.OpenVPN, + Region: region, + Hostname: hostname, + PortForward: portForward, + ServerName: name, + TCP: tcp, + UDP: udp, + }) + nts[name] = append(servers, models.Server{ + VPN: vpn.Wireguard, + Region: region, + Hostname: hostname, + PortForward: portForward, + ServerName: name, + IPs: []netip.Addr{ip}, + }) } - nts[name] = server - return change } func (nts nameToServer) toServersSlice() (servers []models.Server) { servers = make([]models.Server, 0, len(nts)) - for _, server := range nts { - servers = append(servers, server) + for _, hostServers := range nts { + servers = append(servers, hostServers...) } return servers } diff --git a/internal/provider/privateinternetaccess/updater/servers.go b/internal/provider/privateinternetaccess/updater/servers.go index 96d75187c..5659920a4 100644 --- a/internal/provider/privateinternetaccess/updater/servers.go +++ b/internal/provider/privateinternetaccess/updater/servers.go @@ -3,6 +3,7 @@ package updater import ( "context" "fmt" + "github.com/qdm12/gluetun/internal/constants/vpn" "sort" "time" @@ -78,14 +79,20 @@ func addData(regions []regionData, nts nameToServer) (change bool) { for _, region := range regions { for _, server := range region.Servers.UDP { const tcp, udp = false, true - if nts.add(server.CN, region.DNS, region.Name, tcp, udp, region.PortForward, server.IP) { + if nts.add(server.CN, region.DNS, region.Name, vpn.OpenVPN, tcp, udp, region.PortForward, server.IP) { change = true } } for _, server := range region.Servers.TCP { const tcp, udp = true, false - if nts.add(server.CN, region.DNS, region.Name, tcp, udp, region.PortForward, server.IP) { + if nts.add(server.CN, region.DNS, region.Name, vpn.OpenVPN, tcp, udp, region.PortForward, server.IP) { + change = true + } + } + + for _, server := range region.Servers.WG { + if nts.add(server.CN, region.DNS, region.Name, vpn.Wireguard, false, false, region.PortForward, server.IP) { change = true } }