Skip to content

Commit

Permalink
Add wireguard servers to list from api.
Browse files Browse the repository at this point in the history
  • Loading branch information
FireMasterK committed Sep 4, 2023
1 parent fdca594 commit 73355f3
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 31 deletions.
1 change: 1 addition & 0 deletions internal/provider/privateinternetaccess/updater/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type regionData struct {
Servers struct {
UDP []serverData `json:"ovpnudp"`
TCP []serverData `json:"ovpntcp"`
WG []serverData `json:"wg"`
} `json:"servers"`
}

Expand Down
99 changes: 70 additions & 29 deletions internal/provider/privateinternetaccess/updater/hosttoserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
11 changes: 9 additions & 2 deletions internal/provider/privateinternetaccess/updater/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package updater
import (
"context"
"fmt"
"github.com/qdm12/gluetun/internal/constants/vpn"
"sort"
"time"

Expand Down Expand Up @@ -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
}
}
Expand Down

0 comments on commit 73355f3

Please sign in to comment.