Maint: Remove CYBERGHOST_GROUP (change)

- It does not make any sense with newer server data
- It was to be deprecated anyway
This commit is contained in:
Quentin McGaw (desktop)
2021-09-23 13:54:24 +00:00
parent 625de1c834
commit f9aadeef1c
17 changed files with 1910 additions and 1846 deletions

View File

@@ -2,11 +2,8 @@ package cyberghost
import (
"errors"
"fmt"
"strings"
"github.com/qdm12/gluetun/internal/configuration"
"github.com/qdm12/gluetun/internal/constants"
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/provider/utils"
)
@@ -15,27 +12,10 @@ var ErrGroupMismatchesProtocol = errors.New("server group does not match protoco
func (c *Cyberghost) filterServers(selection configuration.ServerSelection) (
servers []models.CyberghostServer, err error) {
if len(selection.Groups) == 0 {
if selection.OpenVPN.TCP {
selection.Groups = tcpGroupChoices(c.servers)
} else {
selection.Groups = udpGroupChoices(c.servers)
}
}
// Check each group match the protocol
groupsCheckFn := groupsAreAllUDP
if selection.OpenVPN.TCP {
groupsCheckFn = groupsAreAllTCP
}
if err := groupsCheckFn(selection.Groups); err != nil {
return nil, err
}
for _, server := range c.servers {
switch {
case
utils.FilterByPossibilities(server.Group, selection.Groups),
utils.FilterByProtocol(selection, server.TCP, server.UDP),
utils.FilterByPossibilities(server.Country, selection.Countries),
utils.FilterByPossibilities(server.Hostname, selection.Hostnames):
default:
@@ -49,51 +29,3 @@ func (c *Cyberghost) filterServers(selection configuration.ServerSelection) (
return servers, nil
}
func tcpGroupChoices(servers []models.CyberghostServer) (choices []string) {
const tcp = true
return groupsForTCP(servers, tcp)
}
func udpGroupChoices(servers []models.CyberghostServer) (choices []string) {
const tcp = false
return groupsForTCP(servers, tcp)
}
func groupsForTCP(servers []models.CyberghostServer, tcp bool) (choices []string) {
allGroups := constants.CyberghostGroupChoices(servers)
choices = make([]string, 0, len(allGroups))
for _, group := range allGroups {
switch {
case tcp && groupIsTCP(group):
choices = append(choices, group)
case !tcp && !groupIsTCP(group):
choices = append(choices, group)
}
}
return choices
}
func groupIsTCP(group string) bool {
return strings.Contains(strings.ToLower(group), "tcp")
}
func groupsAreAllTCP(groups []string) error {
for _, group := range groups {
if !groupIsTCP(group) {
return fmt.Errorf("%w: group %s for protocol TCP",
ErrGroupMismatchesProtocol, group)
}
}
return nil
}
func groupsAreAllUDP(groups []string) error {
for _, group := range groups {
if groupIsTCP(group) {
return fmt.Errorf("%w: group %s for protocol UDP",
ErrGroupMismatchesProtocol, group)
}
}
return nil
}

View File

@@ -25,22 +25,22 @@ func Test_Cyberghost_filterServers(t *testing.T) {
},
"servers without filter defaults to UDP": {
servers: []models.CyberghostServer{
{Country: "a", Group: "Premium TCP Asia"},
{Country: "b", Group: "Premium TCP Europe"},
{Country: "c", Group: "Premium UDP Asia"},
{Country: "d", Group: "Premium UDP Europe"},
{Country: "a", TCP: true},
{Country: "b", TCP: true},
{Country: "c", UDP: true},
{Country: "d", UDP: true},
},
filteredServers: []models.CyberghostServer{
{Country: "c", Group: "Premium UDP Asia"},
{Country: "d", Group: "Premium UDP Europe"},
{Country: "c", UDP: true},
{Country: "d", UDP: true},
},
},
"servers with TCP selection": {
servers: []models.CyberghostServer{
{Country: "a", Group: "Premium TCP Asia"},
{Country: "b", Group: "Premium TCP Europe"},
{Country: "c", Group: "Premium UDP Asia"},
{Country: "d", Group: "Premium UDP Europe"},
{Country: "a", TCP: true},
{Country: "b", TCP: true},
{Country: "c", UDP: true},
{Country: "d", UDP: true},
},
selection: configuration.ServerSelection{
OpenVPN: configuration.OpenVPNSelection{
@@ -48,79 +48,37 @@ func Test_Cyberghost_filterServers(t *testing.T) {
},
},
filteredServers: []models.CyberghostServer{
{Country: "a", Group: "Premium TCP Asia"},
{Country: "b", Group: "Premium TCP Europe"},
{Country: "a", TCP: true},
{Country: "b", TCP: true},
},
},
"servers with regions filter": {
servers: []models.CyberghostServer{
{Country: "a", Group: "Premium UDP Asia"},
{Country: "b", Group: "Premium UDP Asia"},
{Country: "c", Group: "Premium UDP Asia"},
{Country: "d", Group: "Premium UDP Asia"},
{Country: "a", UDP: true},
{Country: "b", UDP: true},
{Country: "c", UDP: true},
{Country: "d", UDP: true},
},
selection: configuration.ServerSelection{
Countries: []string{"a", "c"},
},
filteredServers: []models.CyberghostServer{
{Country: "a", Group: "Premium UDP Asia"},
{Country: "c", Group: "Premium UDP Asia"},
},
},
"servers with group filter": {
servers: []models.CyberghostServer{
{Country: "a", Group: "Premium UDP Europe"},
{Country: "b", Group: "Premium UDP Europe"},
{Country: "c", Group: "Premium TCP Europe"},
{Country: "d", Group: "Premium TCP Europe"},
},
selection: configuration.ServerSelection{
Groups: []string{"Premium UDP Europe"},
},
filteredServers: []models.CyberghostServer{
{Country: "a", Group: "Premium UDP Europe"},
{Country: "b", Group: "Premium UDP Europe"},
},
},
"servers with bad group filter": {
servers: []models.CyberghostServer{
{Country: "a", Group: "Premium TCP Europe"},
{Country: "b", Group: "Premium TCP Europe"},
{Country: "c", Group: "Premium UDP Europe"},
{Country: "d", Group: "Premium UDP Europe"},
},
selection: configuration.ServerSelection{
Groups: []string{"Premium TCP Europe"},
},
err: errors.New("server group does not match protocol: group Premium TCP Europe for protocol UDP"),
},
"servers with regions and group filter": {
servers: []models.CyberghostServer{
{Country: "a", Group: "Premium UDP Europe"},
{Country: "b", Group: "Premium TCP Europe"},
{Country: "c", Group: "Premium UDP Asia"},
{Country: "d", Group: "Premium TCP Asia"},
},
selection: configuration.ServerSelection{
Countries: []string{"a", "c"},
Groups: []string{"Premium UDP Europe"},
},
filteredServers: []models.CyberghostServer{
{Country: "a", Group: "Premium UDP Europe"},
{Country: "a", UDP: true},
{Country: "c", UDP: true},
},
},
"servers with hostnames filter": {
servers: []models.CyberghostServer{
{Hostname: "a", Group: "Premium UDP Asia"},
{Hostname: "b", Group: "Premium UDP Asia"},
{Hostname: "c", Group: "Premium UDP Asia"},
{Hostname: "a", UDP: true},
{Hostname: "b", UDP: true},
{Hostname: "c", UDP: true},
},
selection: configuration.ServerSelection{
Hostnames: []string{"a", "c"},
},
filteredServers: []models.CyberghostServer{
{Hostname: "a", Group: "Premium UDP Asia"},
{Hostname: "c", Group: "Premium UDP Asia"},
{Hostname: "a", UDP: true},
{Hostname: "c", UDP: true},
},
},
}
@@ -142,41 +100,3 @@ func Test_Cyberghost_filterServers(t *testing.T) {
})
}
}
func Test_tcpGroupChoices(t *testing.T) {
t.Parallel()
servers := []models.CyberghostServer{
{Group: "Premium TCP Asia"},
{Group: "Premium TCP Europe"},
{Group: "Premium TCP USA"},
{Group: "Premium UDP Asia"},
{Group: "Premium UDP Europe"},
{Group: "Premium UDP USA"},
}
expected := []string{
"Premium TCP Asia", "Premium TCP Europe", "Premium TCP USA",
}
choices := tcpGroupChoices(servers)
assert.Equal(t, expected, choices)
}
func Test_udpGroupChoices(t *testing.T) {
t.Parallel()
servers := []models.CyberghostServer{
{Group: "Premium TCP Asia"},
{Group: "Premium TCP Europe"},
{Group: "Premium TCP USA"},
{Group: "Premium UDP Asia"},
{Group: "Premium UDP Europe"},
{Group: "Premium UDP USA"},
}
expected := []string{
"Premium UDP Asia", "Premium UDP Europe", "Premium UDP USA",
}
choices := udpGroupChoices(servers)
assert.Equal(t, expected, choices)
}

View File

@@ -27,16 +27,6 @@ func NoServerFoundError(selection configuration.ServerSelection) (err error) {
}
messageParts = append(messageParts, "protocol "+protocol)
switch len(selection.Groups) {
case 0:
case 1:
part := "group " + selection.Groups[0]
messageParts = append(messageParts, part)
default:
part := "groups " + commaJoin(selection.Groups)
messageParts = append(messageParts, part)
}
switch len(selection.Countries) {
case 0:
case 1: