Uniformize server selection filtering
This commit is contained in:
@@ -2,7 +2,6 @@ package provider
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"github.com/qdm12/golibs/network"
|
||||
@@ -16,32 +15,48 @@ func newCyberghost() *cyberghost {
|
||||
return &cyberghost{}
|
||||
}
|
||||
|
||||
func (c *cyberghost) GetOpenVPNConnections(selection models.ServerSelection) (connections []models.OpenVPNConnection, err error) {
|
||||
var IPs []net.IP
|
||||
for _, server := range constants.CyberghostServers() {
|
||||
if strings.EqualFold(server.Region, selection.Region) && strings.EqualFold(server.Group, selection.Group) {
|
||||
IPs = server.IPs
|
||||
func (c *cyberghost) filterServers(region, group string) (servers []models.CyberghostServer) {
|
||||
allServers := constants.CyberghostServers()
|
||||
for i, server := range allServers {
|
||||
if len(region) == 0 {
|
||||
server.Region = ""
|
||||
}
|
||||
if len(group) == 0 {
|
||||
server.Group = ""
|
||||
}
|
||||
if strings.EqualFold(server.Region, region) && strings.EqualFold(server.Group, group) {
|
||||
servers = append(servers, allServers[i])
|
||||
}
|
||||
}
|
||||
if len(IPs) == 0 {
|
||||
return nil, fmt.Errorf("no IP found for group %q and region %q", selection.Group, selection.Region)
|
||||
return servers
|
||||
}
|
||||
|
||||
func (c *cyberghost) GetOpenVPNConnections(selection models.ServerSelection) (connections []models.OpenVPNConnection, err error) {
|
||||
servers := c.filterServers(selection.Region, selection.Group)
|
||||
if len(servers) == 0 {
|
||||
return nil, fmt.Errorf("no server found for region %q and group %q", selection.Region, selection.Group)
|
||||
}
|
||||
if selection.TargetIP != nil {
|
||||
found := false
|
||||
for i := range IPs {
|
||||
if IPs[i].Equal(selection.TargetIP) {
|
||||
found = true
|
||||
break
|
||||
|
||||
for _, server := range servers {
|
||||
for _, IP := range server.IPs {
|
||||
if selection.TargetIP != nil {
|
||||
if selection.TargetIP.Equal(IP) {
|
||||
return []models.OpenVPNConnection{{IP: IP, Port: 443, Protocol: selection.Protocol}}, nil
|
||||
}
|
||||
} else {
|
||||
connections = append(connections, models.OpenVPNConnection{IP: IP, Port: 443, Protocol: selection.Protocol})
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return nil, fmt.Errorf("target IP address %q not found in IP addresses", selection.TargetIP)
|
||||
}
|
||||
IPs = []net.IP{selection.TargetIP}
|
||||
}
|
||||
for _, IP := range IPs {
|
||||
connections = append(connections, models.OpenVPNConnection{IP: IP, Port: 443, Protocol: selection.Protocol})
|
||||
|
||||
if selection.TargetIP != nil {
|
||||
return nil, fmt.Errorf("target IP %s not found in IP addresses", selection.TargetIP)
|
||||
}
|
||||
|
||||
if len(connections) > 64 {
|
||||
connections = connections[:64]
|
||||
}
|
||||
|
||||
return connections, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user