From 0c4f01a89287b9258fe9f8d434f46f1cad947796 Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Sun, 23 May 2021 21:51:12 +0000 Subject: [PATCH] Feature: Protonvpn filter servers with FREE_ONLY --- Dockerfile | 2 ++ internal/configuration/openvpn_test.go | 3 ++- internal/configuration/protonvpn.go | 10 ++++++++++ internal/configuration/selection.go | 3 +++ internal/provider/protonvpn/filter.go | 5 ++++- internal/provider/utils/formatting.go | 4 ++++ 6 files changed, 25 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 187dbdc4..613f8c25 100644 --- a/Dockerfile +++ b/Dockerfile @@ -99,6 +99,8 @@ ENV VPNSP=pia \ SERVER_NUMBER= \ # NordVPN and ProtonVPN only: SERVER_NAME= \ + # ProtonVPN only: + FREE_ONLY= \ # Openvpn OPENVPN_CIPHER= \ OPENVPN_AUTH= \ diff --git a/internal/configuration/openvpn_test.go b/internal/configuration/openvpn_test.go index ac6eb835..6a78caa6 100644 --- a/internal/configuration/openvpn_test.go +++ b/internal/configuration/openvpn_test.go @@ -40,7 +40,8 @@ func Test_OpenVPN_JSON(t *testing.T) { "owned": false, "custom_port": 0, "numbers": null, - "encryption_preset": "" + "encryption_preset": "", + "free_only": false }, "extra_config": { "encryption_preset": "", diff --git a/internal/configuration/protonvpn.go b/internal/configuration/protonvpn.go index 8d1f364f..f6236b85 100644 --- a/internal/configuration/protonvpn.go +++ b/internal/configuration/protonvpn.go @@ -2,6 +2,7 @@ package configuration import ( "github.com/qdm12/gluetun/internal/constants" + "github.com/qdm12/golibs/params" ) func (settings *Provider) protonvpnLines() (lines []string) { @@ -25,6 +26,10 @@ func (settings *Provider) protonvpnLines() (lines []string) { lines = append(lines, lastIndent+"Hostnames: "+commaJoin(settings.ServerSelection.Hostnames)) } + if settings.ServerSelection.FreeOnly { + lines = append(lines, lastIndent+"Free only: yes") + } + return lines } @@ -71,5 +76,10 @@ func (settings *Provider) readProtonvpn(r reader) (err error) { return err } + settings.ServerSelection.FreeOnly, err = r.env.YesNo("FREE_ONLY", params.Default("no")) + if err != nil { + return err + } + return nil } diff --git a/internal/configuration/selection.go b/internal/configuration/selection.go index 6d0624e9..babf64f8 100644 --- a/internal/configuration/selection.go +++ b/internal/configuration/selection.go @@ -32,6 +32,9 @@ type ServerSelection struct { //nolint:maligned // PIA EncryptionPreset string `json:"encryption_preset"` + + // ProtonVPN + FreeOnly bool `json:"free_only"` } type ExtraConfigOptions struct { diff --git a/internal/provider/protonvpn/filter.go b/internal/provider/protonvpn/filter.go index 143c2c40..def15efa 100644 --- a/internal/provider/protonvpn/filter.go +++ b/internal/provider/protonvpn/filter.go @@ -1,6 +1,8 @@ package protonvpn import ( + "strings" + "github.com/qdm12/gluetun/internal/configuration" "github.com/qdm12/gluetun/internal/models" "github.com/qdm12/gluetun/internal/provider/utils" @@ -15,7 +17,8 @@ func (p *Protonvpn) filterServers(selection configuration.ServerSelection) ( utils.FilterByPossibilities(server.Region, selection.Regions), utils.FilterByPossibilities(server.City, selection.Cities), utils.FilterByPossibilities(server.Hostname, selection.Hostnames), - utils.FilterByPossibilities(server.Name, selection.Names): + utils.FilterByPossibilities(server.Name, selection.Names), + selection.FreeOnly && !strings.Contains(strings.ToLower(server.Name), "free"): default: servers = append(servers, server) } diff --git a/internal/provider/utils/formatting.go b/internal/provider/utils/formatting.go index c35a481d..00fb0a22 100644 --- a/internal/provider/utils/formatting.go +++ b/internal/provider/utils/formatting.go @@ -113,6 +113,10 @@ func NoServerFoundError(selection configuration.ServerSelection) (err error) { messageParts = append(messageParts, part) } + if selection.FreeOnly { + messageParts = append(messageParts, "free tier only") + } + message := "for " + strings.Join(messageParts, "; ") return fmt.Errorf("%w: %s", ErrNoServerFound, message)