Fix & feat: Cyberghost server groups
- Allow multiple comma separated values for CYBERGHOST_GROUP - Defaults to all UDP groups - If TCP is enabled, defaults to all TCP groups - Check groups specified match the protocol - Default Cyberghost group to empty - Adjust formatting and messages
This commit is contained in:
@@ -1,18 +1,41 @@
|
||||
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"
|
||||
)
|
||||
|
||||
var ErrGroupMismatchesProtocol = errors.New("server group does not match protocol")
|
||||
|
||||
func (c *Cyberghost) filterServers(selection configuration.ServerSelection) (
|
||||
servers []models.CyberghostServer, err error) {
|
||||
if len(selection.Groups) == 0 {
|
||||
if selection.TCP {
|
||||
selection.Groups = tcpGroupChoices()
|
||||
} else {
|
||||
selection.Groups = udpGroupChoices()
|
||||
}
|
||||
}
|
||||
|
||||
// Check each group match the protocol
|
||||
groupsCheckFn := groupsAreAllUDP
|
||||
if selection.TCP {
|
||||
groupsCheckFn = groupsAreAllTCP
|
||||
}
|
||||
if err := groupsCheckFn(selection.Groups); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, server := range c.servers {
|
||||
switch {
|
||||
case selection.Group != "" && !strings.EqualFold(selection.Group, server.Group), // TODO make CSV
|
||||
case
|
||||
utils.FilterByPossibilities(server.Group, selection.Groups),
|
||||
utils.FilterByPossibilities(server.Region, selection.Regions),
|
||||
utils.FilterByPossibilities(server.Hostname, selection.Hostnames):
|
||||
default:
|
||||
@@ -26,3 +49,51 @@ func (c *Cyberghost) filterServers(selection configuration.ServerSelection) (
|
||||
|
||||
return servers, nil
|
||||
}
|
||||
|
||||
func tcpGroupChoices() (choices []string) {
|
||||
const tcp = true
|
||||
return groupsForTCP(tcp)
|
||||
}
|
||||
|
||||
func udpGroupChoices() (choices []string) {
|
||||
const tcp = false
|
||||
return groupsForTCP(tcp)
|
||||
}
|
||||
|
||||
func groupsForTCP(tcp bool) (choices []string) {
|
||||
allGroups := constants.CyberghostGroupChoices()
|
||||
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
|
||||
}
|
||||
|
||||
@@ -21,77 +21,102 @@ func Test_Cyberghost_filterServers(t *testing.T) {
|
||||
"no servers": {
|
||||
err: errors.New("no server found: for protocol udp"),
|
||||
},
|
||||
"servers without filter": {
|
||||
"servers without filter defaults to UDP": {
|
||||
servers: []models.CyberghostServer{
|
||||
{Region: "a", Group: "1"},
|
||||
{Region: "b", Group: "1"},
|
||||
{Region: "c", Group: "2"},
|
||||
{Region: "d", Group: "2"},
|
||||
{Region: "a", Group: "Premium TCP Asia"},
|
||||
{Region: "b", Group: "Premium TCP Europe"},
|
||||
{Region: "c", Group: "Premium UDP Asia"},
|
||||
{Region: "d", Group: "Premium UDP Europe"},
|
||||
},
|
||||
filteredServers: []models.CyberghostServer{
|
||||
{Region: "a", Group: "1"},
|
||||
{Region: "b", Group: "1"},
|
||||
{Region: "c", Group: "2"},
|
||||
{Region: "d", Group: "2"},
|
||||
{Region: "c", Group: "Premium UDP Asia"},
|
||||
{Region: "d", Group: "Premium UDP Europe"},
|
||||
},
|
||||
},
|
||||
"servers with TCP selection": {
|
||||
servers: []models.CyberghostServer{
|
||||
{Region: "a", Group: "Premium TCP Asia"},
|
||||
{Region: "b", Group: "Premium TCP Europe"},
|
||||
{Region: "c", Group: "Premium UDP Asia"},
|
||||
{Region: "d", Group: "Premium UDP Europe"},
|
||||
},
|
||||
selection: configuration.ServerSelection{
|
||||
TCP: true,
|
||||
},
|
||||
filteredServers: []models.CyberghostServer{
|
||||
{Region: "a", Group: "Premium TCP Asia"},
|
||||
{Region: "b", Group: "Premium TCP Europe"},
|
||||
},
|
||||
},
|
||||
"servers with regions filter": {
|
||||
servers: []models.CyberghostServer{
|
||||
{Region: "a", Group: "1"},
|
||||
{Region: "b", Group: "1"},
|
||||
{Region: "c", Group: "2"},
|
||||
{Region: "d", Group: "2"},
|
||||
{Region: "a", Group: "Premium UDP Asia"},
|
||||
{Region: "b", Group: "Premium UDP Asia"},
|
||||
{Region: "c", Group: "Premium UDP Asia"},
|
||||
{Region: "d", Group: "Premium UDP Asia"},
|
||||
},
|
||||
selection: configuration.ServerSelection{
|
||||
Regions: []string{"a", "c"},
|
||||
},
|
||||
filteredServers: []models.CyberghostServer{
|
||||
{Region: "a", Group: "1"},
|
||||
{Region: "c", Group: "2"},
|
||||
{Region: "a", Group: "Premium UDP Asia"},
|
||||
{Region: "c", Group: "Premium UDP Asia"},
|
||||
},
|
||||
},
|
||||
"servers with group filter": {
|
||||
servers: []models.CyberghostServer{
|
||||
{Region: "a", Group: "1"},
|
||||
{Region: "b", Group: "1"},
|
||||
{Region: "c", Group: "2"},
|
||||
{Region: "d", Group: "2"},
|
||||
{Region: "a", Group: "Premium UDP Europe"},
|
||||
{Region: "b", Group: "Premium UDP Europe"},
|
||||
{Region: "c", Group: "Premium TCP Europe"},
|
||||
{Region: "d", Group: "Premium TCP Europe"},
|
||||
},
|
||||
selection: configuration.ServerSelection{
|
||||
Group: "1",
|
||||
Groups: []string{"Premium UDP Europe"},
|
||||
},
|
||||
filteredServers: []models.CyberghostServer{
|
||||
{Region: "a", Group: "1"},
|
||||
{Region: "b", Group: "1"},
|
||||
{Region: "a", Group: "Premium UDP Europe"},
|
||||
{Region: "b", Group: "Premium UDP Europe"},
|
||||
},
|
||||
},
|
||||
"servers with bad group filter": {
|
||||
servers: []models.CyberghostServer{
|
||||
{Region: "a", Group: "Premium TCP Europe"},
|
||||
{Region: "b", Group: "Premium TCP Europe"},
|
||||
{Region: "c", Group: "Premium UDP Europe"},
|
||||
{Region: "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{
|
||||
{Region: "a", Group: "1"},
|
||||
{Region: "b", Group: "1"},
|
||||
{Region: "c", Group: "2"},
|
||||
{Region: "d", Group: "2"},
|
||||
{Region: "a", Group: "Premium UDP Europe"},
|
||||
{Region: "b", Group: "Premium TCP Europe"},
|
||||
{Region: "c", Group: "Premium UDP Asia"},
|
||||
{Region: "d", Group: "Premium TCP Asia"},
|
||||
},
|
||||
selection: configuration.ServerSelection{
|
||||
Regions: []string{"a", "c"},
|
||||
Group: "1",
|
||||
Groups: []string{"Premium UDP Europe"},
|
||||
},
|
||||
filteredServers: []models.CyberghostServer{
|
||||
{Region: "a", Group: "1"},
|
||||
{Region: "a", Group: "Premium UDP Europe"},
|
||||
},
|
||||
},
|
||||
"servers with hostnames filter": {
|
||||
servers: []models.CyberghostServer{
|
||||
{Hostname: "a"},
|
||||
{Hostname: "b"},
|
||||
{Hostname: "c"},
|
||||
{Hostname: "a", Group: "Premium UDP Asia"},
|
||||
{Hostname: "b", Group: "Premium UDP Asia"},
|
||||
{Hostname: "c", Group: "Premium UDP Asia"},
|
||||
},
|
||||
selection: configuration.ServerSelection{
|
||||
Hostnames: []string{"a", "c"},
|
||||
},
|
||||
filteredServers: []models.CyberghostServer{
|
||||
{Hostname: "a"},
|
||||
{Hostname: "c"},
|
||||
{Hostname: "a", Group: "Premium UDP Asia"},
|
||||
{Hostname: "c", Group: "Premium UDP Asia"},
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -113,3 +138,25 @@ func Test_Cyberghost_filterServers(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_tcpGroupChoices(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
expected := []string{
|
||||
"Premium TCP Asia", "Premium TCP Europe", "Premium TCP USA",
|
||||
}
|
||||
choices := tcpGroupChoices()
|
||||
|
||||
assert.Equal(t, expected, choices)
|
||||
}
|
||||
|
||||
func Test_udpGroupChoices(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
expected := []string{
|
||||
"Premium UDP Asia", "Premium UDP Europe", "Premium UDP USA",
|
||||
}
|
||||
choices := udpGroupChoices()
|
||||
|
||||
assert.Equal(t, expected, choices)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user