Maint: internal/storage rework

- No more global variables
- Inject merged servers to configuration package
- Fix #566: configuration parsing to use persisted servers.json
- Move server data files from `internal/constants` to `internal/storage`
This commit is contained in:
Quentin McGaw (desktop)
2021-08-27 19:10:03 +00:00
parent b1cfc03fc5
commit 3863cc439e
59 changed files with 850 additions and 490 deletions

View File

@@ -17,9 +17,9 @@ func (c *Cyberghost) filterServers(selection configuration.ServerSelection) (
servers []models.CyberghostServer, err error) {
if len(selection.Groups) == 0 {
if selection.OpenVPN.TCP {
selection.Groups = tcpGroupChoices()
selection.Groups = tcpGroupChoices(c.servers)
} else {
selection.Groups = udpGroupChoices()
selection.Groups = udpGroupChoices(c.servers)
}
}
@@ -50,18 +50,18 @@ func (c *Cyberghost) filterServers(selection configuration.ServerSelection) (
return servers, nil
}
func tcpGroupChoices() (choices []string) {
func tcpGroupChoices(servers []models.CyberghostServer) (choices []string) {
const tcp = true
return groupsForTCP(tcp)
return groupsForTCP(servers, tcp)
}
func udpGroupChoices() (choices []string) {
func udpGroupChoices(servers []models.CyberghostServer) (choices []string) {
const tcp = false
return groupsForTCP(tcp)
return groupsForTCP(servers, tcp)
}
func groupsForTCP(tcp bool) (choices []string) {
allGroups := constants.CyberghostGroupChoices()
func groupsForTCP(servers []models.CyberghostServer, tcp bool) (choices []string) {
allGroups := constants.CyberghostGroupChoices(servers)
choices = make([]string, 0, len(allGroups))
for _, group := range allGroups {
switch {

View File

@@ -21,7 +21,7 @@ func Test_Cyberghost_filterServers(t *testing.T) {
}{
"no server": {
selection: configuration.ServerSelection{VPN: constants.OpenVPN},
err: errors.New("no server found: for VPN openvpn; protocol udp; groups Premium UDP Asia, Premium UDP Europe, Premium UDP USA"), //nolint:lll
err: errors.New("no server found: for VPN openvpn; protocol udp"),
},
"servers without filter defaults to UDP": {
servers: []models.CyberghostServer{
@@ -146,10 +146,18 @@ 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()
choices := tcpGroupChoices(servers)
assert.Equal(t, expected, choices)
}
@@ -157,10 +165,18 @@ func Test_tcpGroupChoices(t *testing.T) {
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()
choices := udpGroupChoices(servers)
assert.Equal(t, expected, choices)
}

View File

@@ -33,7 +33,7 @@ var (
func (p *PIA) PortForward(ctx context.Context, client *http.Client,
logger logging.Logger, gateway net.IP, serverName string) (
port uint16, err error) {
server := constants.PIAServerWhereName(serverName)
server := constants.PIAServerWhereName(p.servers, serverName)
if !server.PortForward {
logger.Error("The server " + serverName +
" (region " + server.Region + ") does not support port forwarding")