From aa6dc786a48f4f051dc5e5876ac69dee62e5ac72 Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Sat, 23 Sep 2023 13:02:09 +0000 Subject: [PATCH] chore(provider): use type assertion for port forwarders --- internal/provider/airvpn/provider.go | 9 ++--- internal/provider/custom/provider.go | 6 +-- internal/provider/cyberghost/provider.go | 9 ++--- internal/provider/example/provider.go | 9 ++--- internal/provider/expressvpn/provider.go | 9 ++--- internal/provider/fastestvpn/provider.go | 9 ++--- internal/provider/hidemyass/provider.go | 9 ++--- internal/provider/ipvanish/provider.go | 9 ++--- internal/provider/ivpn/provider.go | 9 ++--- internal/provider/mullvad/provider.go | 9 ++--- internal/provider/nordvpn/provider.go | 9 ++--- internal/provider/perfectprivacy/provider.go | 9 ++--- internal/provider/privado/provider.go | 9 ++--- internal/provider/privatevpn/provider.go | 9 ++--- internal/provider/provider.go | 9 ----- internal/provider/purevpn/provider.go | 9 ++--- internal/provider/slickvpn/provider.go | 9 ++--- internal/provider/surfshark/provider.go | 9 ++--- internal/provider/torguard/provider.go | 9 ++--- internal/provider/utils/noportforward.go | 33 --------------- internal/provider/vpnsecure/provider.go | 9 ++--- internal/provider/vpnunlimited/provider.go | 9 ++--- internal/provider/vyprvpn/provider.go | 9 ++--- internal/provider/wevpn/provider.go | 9 ++--- internal/provider/windscribe/provider.go | 9 ++--- internal/vpn/interfaces.go | 16 ++++++++ internal/vpn/portforward.go | 42 ++++++++++++++++++++ internal/vpn/run.go | 7 +--- internal/vpn/tunnelup.go | 3 +- 29 files changed, 129 insertions(+), 185 deletions(-) delete mode 100644 internal/provider/utils/noportforward.go diff --git a/internal/provider/airvpn/provider.go b/internal/provider/airvpn/provider.go index 591dda0a9..1e2f76dd0 100644 --- a/internal/provider/airvpn/provider.go +++ b/internal/provider/airvpn/provider.go @@ -7,23 +7,20 @@ import ( "github.com/qdm12/gluetun/internal/constants/providers" "github.com/qdm12/gluetun/internal/provider/airvpn/updater" "github.com/qdm12/gluetun/internal/provider/common" - "github.com/qdm12/gluetun/internal/provider/utils" ) type Provider struct { storage common.Storage randSource rand.Source - utils.NoPortForwarder common.Fetcher } func New(storage common.Storage, randSource rand.Source, client *http.Client) *Provider { return &Provider{ - storage: storage, - randSource: randSource, - NoPortForwarder: utils.NewNoPortForwarding(providers.Example), - Fetcher: updater.New(client), + storage: storage, + randSource: randSource, + Fetcher: updater.New(client), } } diff --git a/internal/provider/custom/provider.go b/internal/provider/custom/provider.go index d052f4bb3..4c923e78d 100644 --- a/internal/provider/custom/provider.go +++ b/internal/provider/custom/provider.go @@ -8,15 +8,13 @@ import ( type Provider struct { extractor Extractor - utils.NoPortForwarder common.Fetcher } func New(extractor Extractor) *Provider { return &Provider{ - extractor: extractor, - NoPortForwarder: utils.NewNoPortForwarding(providers.Custom), - Fetcher: utils.NewNoFetcher(providers.Custom), + extractor: extractor, + Fetcher: utils.NewNoFetcher(providers.Custom), } } diff --git a/internal/provider/cyberghost/provider.go b/internal/provider/cyberghost/provider.go index 2ea8f9871..4fcaf27f3 100644 --- a/internal/provider/cyberghost/provider.go +++ b/internal/provider/cyberghost/provider.go @@ -6,23 +6,20 @@ import ( "github.com/qdm12/gluetun/internal/constants/providers" "github.com/qdm12/gluetun/internal/provider/common" "github.com/qdm12/gluetun/internal/provider/cyberghost/updater" - "github.com/qdm12/gluetun/internal/provider/utils" ) type Provider struct { storage common.Storage randSource rand.Source - utils.NoPortForwarder common.Fetcher } func New(storage common.Storage, randSource rand.Source, parallelResolver common.ParallelResolver) *Provider { return &Provider{ - storage: storage, - randSource: randSource, - NoPortForwarder: utils.NewNoPortForwarding(providers.Cyberghost), - Fetcher: updater.New(parallelResolver), + storage: storage, + randSource: randSource, + Fetcher: updater.New(parallelResolver), } } diff --git a/internal/provider/example/provider.go b/internal/provider/example/provider.go index ea5c2f940..416a1c410 100644 --- a/internal/provider/example/provider.go +++ b/internal/provider/example/provider.go @@ -7,13 +7,11 @@ import ( "github.com/qdm12/gluetun/internal/constants/providers" "github.com/qdm12/gluetun/internal/provider/common" "github.com/qdm12/gluetun/internal/provider/example/updater" - "github.com/qdm12/gluetun/internal/provider/utils" ) type Provider struct { storage common.Storage randSource rand.Source - utils.NoPortForwarder common.Fetcher } @@ -22,10 +20,9 @@ func New(storage common.Storage, randSource rand.Source, updaterWarner common.Warner, client *http.Client, unzipper common.Unzipper, parallelResolver common.ParallelResolver) *Provider { return &Provider{ - storage: storage, - randSource: randSource, - NoPortForwarder: utils.NewNoPortForwarding(providers.Example), - Fetcher: updater.New(updaterWarner, unzipper, client, parallelResolver), + storage: storage, + randSource: randSource, + Fetcher: updater.New(updaterWarner, unzipper, client, parallelResolver), } } diff --git a/internal/provider/expressvpn/provider.go b/internal/provider/expressvpn/provider.go index 2c746d643..3fddc69b0 100644 --- a/internal/provider/expressvpn/provider.go +++ b/internal/provider/expressvpn/provider.go @@ -6,13 +6,11 @@ import ( "github.com/qdm12/gluetun/internal/constants/providers" "github.com/qdm12/gluetun/internal/provider/common" "github.com/qdm12/gluetun/internal/provider/expressvpn/updater" - "github.com/qdm12/gluetun/internal/provider/utils" ) type Provider struct { storage common.Storage randSource rand.Source - utils.NoPortForwarder common.Fetcher } @@ -20,10 +18,9 @@ func New(storage common.Storage, randSource rand.Source, unzipper common.Unzipper, updaterWarner common.Warner, parallelResolver common.ParallelResolver) *Provider { return &Provider{ - storage: storage, - randSource: randSource, - NoPortForwarder: utils.NewNoPortForwarding(providers.Expressvpn), - Fetcher: updater.New(unzipper, updaterWarner, parallelResolver), + storage: storage, + randSource: randSource, + Fetcher: updater.New(unzipper, updaterWarner, parallelResolver), } } diff --git a/internal/provider/fastestvpn/provider.go b/internal/provider/fastestvpn/provider.go index 937107965..017ccceaa 100644 --- a/internal/provider/fastestvpn/provider.go +++ b/internal/provider/fastestvpn/provider.go @@ -6,13 +6,11 @@ import ( "github.com/qdm12/gluetun/internal/constants/providers" "github.com/qdm12/gluetun/internal/provider/common" "github.com/qdm12/gluetun/internal/provider/fastestvpn/updater" - "github.com/qdm12/gluetun/internal/provider/utils" ) type Provider struct { storage common.Storage randSource rand.Source - utils.NoPortForwarder common.Fetcher } @@ -20,10 +18,9 @@ func New(storage common.Storage, randSource rand.Source, unzipper common.Unzipper, updaterWarner common.Warner, parallelResolver common.ParallelResolver) *Provider { return &Provider{ - storage: storage, - randSource: randSource, - NoPortForwarder: utils.NewNoPortForwarding(providers.Fastestvpn), - Fetcher: updater.New(unzipper, updaterWarner, parallelResolver), + storage: storage, + randSource: randSource, + Fetcher: updater.New(unzipper, updaterWarner, parallelResolver), } } diff --git a/internal/provider/hidemyass/provider.go b/internal/provider/hidemyass/provider.go index f4bc37dce..ca6dd015b 100644 --- a/internal/provider/hidemyass/provider.go +++ b/internal/provider/hidemyass/provider.go @@ -7,13 +7,11 @@ import ( "github.com/qdm12/gluetun/internal/constants/providers" "github.com/qdm12/gluetun/internal/provider/common" "github.com/qdm12/gluetun/internal/provider/hidemyass/updater" - "github.com/qdm12/gluetun/internal/provider/utils" ) type Provider struct { storage common.Storage randSource rand.Source - utils.NoPortForwarder common.Fetcher } @@ -21,10 +19,9 @@ func New(storage common.Storage, randSource rand.Source, client *http.Client, updaterWarner common.Warner, parallelResolver common.ParallelResolver) *Provider { return &Provider{ - storage: storage, - randSource: randSource, - NoPortForwarder: utils.NewNoPortForwarding(providers.HideMyAss), - Fetcher: updater.New(client, updaterWarner, parallelResolver), + storage: storage, + randSource: randSource, + Fetcher: updater.New(client, updaterWarner, parallelResolver), } } diff --git a/internal/provider/ipvanish/provider.go b/internal/provider/ipvanish/provider.go index cb17137e7..ece3aad1a 100644 --- a/internal/provider/ipvanish/provider.go +++ b/internal/provider/ipvanish/provider.go @@ -6,13 +6,11 @@ import ( "github.com/qdm12/gluetun/internal/constants/providers" "github.com/qdm12/gluetun/internal/provider/common" "github.com/qdm12/gluetun/internal/provider/ipvanish/updater" - "github.com/qdm12/gluetun/internal/provider/utils" ) type Provider struct { storage common.Storage randSource rand.Source - utils.NoPortForwarder common.Fetcher } @@ -20,10 +18,9 @@ func New(storage common.Storage, randSource rand.Source, unzipper common.Unzipper, updaterWarner common.Warner, parallelResolver common.ParallelResolver) *Provider { return &Provider{ - storage: storage, - randSource: randSource, - NoPortForwarder: utils.NewNoPortForwarding(providers.Ipvanish), - Fetcher: updater.New(unzipper, updaterWarner, parallelResolver), + storage: storage, + randSource: randSource, + Fetcher: updater.New(unzipper, updaterWarner, parallelResolver), } } diff --git a/internal/provider/ivpn/provider.go b/internal/provider/ivpn/provider.go index 1b19ff037..3792eeb4b 100644 --- a/internal/provider/ivpn/provider.go +++ b/internal/provider/ivpn/provider.go @@ -7,13 +7,11 @@ import ( "github.com/qdm12/gluetun/internal/constants/providers" "github.com/qdm12/gluetun/internal/provider/common" "github.com/qdm12/gluetun/internal/provider/ivpn/updater" - "github.com/qdm12/gluetun/internal/provider/utils" ) type Provider struct { storage common.Storage randSource rand.Source - utils.NoPortForwarder common.Fetcher } @@ -21,10 +19,9 @@ func New(storage common.Storage, randSource rand.Source, client *http.Client, updaterWarner common.Warner, parallelResolver common.ParallelResolver) *Provider { return &Provider{ - storage: storage, - randSource: randSource, - NoPortForwarder: utils.NewNoPortForwarding(providers.Ivpn), - Fetcher: updater.New(client, updaterWarner, parallelResolver), + storage: storage, + randSource: randSource, + Fetcher: updater.New(client, updaterWarner, parallelResolver), } } diff --git a/internal/provider/mullvad/provider.go b/internal/provider/mullvad/provider.go index 154593c36..ab08c94c5 100644 --- a/internal/provider/mullvad/provider.go +++ b/internal/provider/mullvad/provider.go @@ -7,23 +7,20 @@ import ( "github.com/qdm12/gluetun/internal/constants/providers" "github.com/qdm12/gluetun/internal/provider/common" "github.com/qdm12/gluetun/internal/provider/mullvad/updater" - "github.com/qdm12/gluetun/internal/provider/utils" ) type Provider struct { storage common.Storage randSource rand.Source - utils.NoPortForwarder common.Fetcher } func New(storage common.Storage, randSource rand.Source, client *http.Client) *Provider { return &Provider{ - storage: storage, - randSource: randSource, - NoPortForwarder: utils.NewNoPortForwarding(providers.Mullvad), - Fetcher: updater.New(client), + storage: storage, + randSource: randSource, + Fetcher: updater.New(client), } } diff --git a/internal/provider/nordvpn/provider.go b/internal/provider/nordvpn/provider.go index f8ffa2620..c9ddf1ef6 100644 --- a/internal/provider/nordvpn/provider.go +++ b/internal/provider/nordvpn/provider.go @@ -7,23 +7,20 @@ import ( "github.com/qdm12/gluetun/internal/constants/providers" "github.com/qdm12/gluetun/internal/provider/common" "github.com/qdm12/gluetun/internal/provider/nordvpn/updater" - "github.com/qdm12/gluetun/internal/provider/utils" ) type Provider struct { storage common.Storage randSource rand.Source - utils.NoPortForwarder common.Fetcher } func New(storage common.Storage, randSource rand.Source, client *http.Client, updaterWarner common.Warner) *Provider { return &Provider{ - storage: storage, - randSource: randSource, - NoPortForwarder: utils.NewNoPortForwarding(providers.Nordvpn), - Fetcher: updater.New(client, updaterWarner), + storage: storage, + randSource: randSource, + Fetcher: updater.New(client, updaterWarner), } } diff --git a/internal/provider/perfectprivacy/provider.go b/internal/provider/perfectprivacy/provider.go index 9b9f6e524..198a148fa 100644 --- a/internal/provider/perfectprivacy/provider.go +++ b/internal/provider/perfectprivacy/provider.go @@ -6,23 +6,20 @@ import ( "github.com/qdm12/gluetun/internal/constants/providers" "github.com/qdm12/gluetun/internal/provider/common" "github.com/qdm12/gluetun/internal/provider/perfectprivacy/updater" - "github.com/qdm12/gluetun/internal/provider/utils" ) type Provider struct { storage common.Storage randSource rand.Source - utils.NoPortForwarder common.Fetcher } func New(storage common.Storage, randSource rand.Source, unzipper common.Unzipper, updaterWarner common.Warner) *Provider { return &Provider{ - storage: storage, - randSource: randSource, - NoPortForwarder: utils.NewNoPortForwarding(providers.Perfectprivacy), - Fetcher: updater.New(unzipper, updaterWarner), + storage: storage, + randSource: randSource, + Fetcher: updater.New(unzipper, updaterWarner), } } diff --git a/internal/provider/privado/provider.go b/internal/provider/privado/provider.go index 6d70f8610..38f9e8279 100644 --- a/internal/provider/privado/provider.go +++ b/internal/provider/privado/provider.go @@ -6,13 +6,11 @@ import ( "github.com/qdm12/gluetun/internal/constants/providers" "github.com/qdm12/gluetun/internal/provider/common" "github.com/qdm12/gluetun/internal/provider/privado/updater" - "github.com/qdm12/gluetun/internal/provider/utils" ) type Provider struct { storage common.Storage randSource rand.Source - utils.NoPortForwarder common.Fetcher } @@ -21,10 +19,9 @@ func New(storage common.Storage, randSource rand.Source, updaterWarner common.Warner, parallelResolver common.ParallelResolver) *Provider { return &Provider{ - storage: storage, - randSource: randSource, - NoPortForwarder: utils.NewNoPortForwarding(providers.Privado), - Fetcher: updater.New(ipFetcher, unzipper, updaterWarner, parallelResolver), + storage: storage, + randSource: randSource, + Fetcher: updater.New(ipFetcher, unzipper, updaterWarner, parallelResolver), } } diff --git a/internal/provider/privatevpn/provider.go b/internal/provider/privatevpn/provider.go index 10b8c82e3..6e2835627 100644 --- a/internal/provider/privatevpn/provider.go +++ b/internal/provider/privatevpn/provider.go @@ -6,13 +6,11 @@ import ( "github.com/qdm12/gluetun/internal/constants/providers" "github.com/qdm12/gluetun/internal/provider/common" "github.com/qdm12/gluetun/internal/provider/privatevpn/updater" - "github.com/qdm12/gluetun/internal/provider/utils" ) type Provider struct { storage common.Storage randSource rand.Source - utils.NoPortForwarder common.Fetcher } @@ -20,10 +18,9 @@ func New(storage common.Storage, randSource rand.Source, unzipper common.Unzipper, updaterWarner common.Warner, parallelResolver common.ParallelResolver) *Provider { return &Provider{ - storage: storage, - randSource: randSource, - NoPortForwarder: utils.NewNoPortForwarding(providers.Privatevpn), - Fetcher: updater.New(unzipper, updaterWarner, parallelResolver), + storage: storage, + randSource: randSource, + Fetcher: updater.New(unzipper, updaterWarner, parallelResolver), } } diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 96b48df75..d9b0bebb9 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -5,7 +5,6 @@ import ( "github.com/qdm12/gluetun/internal/configuration/settings" "github.com/qdm12/gluetun/internal/models" - "github.com/qdm12/gluetun/internal/provider/utils" ) // Provider contains methods to read and modify the openvpn configuration to connect as a client. @@ -13,14 +12,6 @@ type Provider interface { GetConnection(selection settings.ServerSelection, ipv6Supported bool) (connection models.Connection, err error) OpenVPNConfig(connection models.Connection, settings settings.OpenVPN, ipv6Supported bool) (lines []string) Name() string - PortForwarder FetchServers(ctx context.Context, minServers int) ( servers []models.Server, err error) } - -type PortForwarder interface { - Name() string - PortForward(ctx context.Context, objects utils.PortForwardObjects) ( - port uint16, err error) - KeepPortForward(ctx context.Context, objects utils.PortForwardObjects) (err error) -} diff --git a/internal/provider/purevpn/provider.go b/internal/provider/purevpn/provider.go index 6442b6e8b..31570f4a7 100644 --- a/internal/provider/purevpn/provider.go +++ b/internal/provider/purevpn/provider.go @@ -6,13 +6,11 @@ import ( "github.com/qdm12/gluetun/internal/constants/providers" "github.com/qdm12/gluetun/internal/provider/common" "github.com/qdm12/gluetun/internal/provider/purevpn/updater" - "github.com/qdm12/gluetun/internal/provider/utils" ) type Provider struct { storage common.Storage randSource rand.Source - utils.NoPortForwarder common.Fetcher } @@ -20,10 +18,9 @@ func New(storage common.Storage, randSource rand.Source, ipFetcher common.IPFetcher, unzipper common.Unzipper, updaterWarner common.Warner, parallelResolver common.ParallelResolver) *Provider { return &Provider{ - storage: storage, - randSource: randSource, - NoPortForwarder: utils.NewNoPortForwarding(providers.Purevpn), - Fetcher: updater.New(ipFetcher, unzipper, updaterWarner, parallelResolver), + storage: storage, + randSource: randSource, + Fetcher: updater.New(ipFetcher, unzipper, updaterWarner, parallelResolver), } } diff --git a/internal/provider/slickvpn/provider.go b/internal/provider/slickvpn/provider.go index c3b02174a..f513a17fe 100644 --- a/internal/provider/slickvpn/provider.go +++ b/internal/provider/slickvpn/provider.go @@ -7,13 +7,11 @@ import ( "github.com/qdm12/gluetun/internal/constants/providers" "github.com/qdm12/gluetun/internal/provider/common" "github.com/qdm12/gluetun/internal/provider/slickvpn/updater" - "github.com/qdm12/gluetun/internal/provider/utils" ) type Provider struct { storage common.Storage randSource rand.Source - utils.NoPortForwarder common.Fetcher } @@ -21,10 +19,9 @@ func New(storage common.Storage, randSource rand.Source, client *http.Client, updaterWarner common.Warner, parallelResolver common.ParallelResolver) *Provider { return &Provider{ - storage: storage, - randSource: randSource, - NoPortForwarder: utils.NewNoPortForwarding(providers.SlickVPN), - Fetcher: updater.New(client, updaterWarner, parallelResolver), + storage: storage, + randSource: randSource, + Fetcher: updater.New(client, updaterWarner, parallelResolver), } } diff --git a/internal/provider/surfshark/provider.go b/internal/provider/surfshark/provider.go index 58872deb6..47d8c9c4e 100644 --- a/internal/provider/surfshark/provider.go +++ b/internal/provider/surfshark/provider.go @@ -7,13 +7,11 @@ import ( "github.com/qdm12/gluetun/internal/constants/providers" "github.com/qdm12/gluetun/internal/provider/common" "github.com/qdm12/gluetun/internal/provider/surfshark/updater" - "github.com/qdm12/gluetun/internal/provider/utils" ) type Provider struct { storage common.Storage randSource rand.Source - utils.NoPortForwarder common.Fetcher } @@ -21,10 +19,9 @@ func New(storage common.Storage, randSource rand.Source, client *http.Client, unzipper common.Unzipper, updaterWarner common.Warner, parallelResolver common.ParallelResolver) *Provider { return &Provider{ - storage: storage, - randSource: randSource, - NoPortForwarder: utils.NewNoPortForwarding(providers.Surfshark), - Fetcher: updater.New(client, unzipper, updaterWarner, parallelResolver), + storage: storage, + randSource: randSource, + Fetcher: updater.New(client, unzipper, updaterWarner, parallelResolver), } } diff --git a/internal/provider/torguard/provider.go b/internal/provider/torguard/provider.go index e5a85ffba..54f237cad 100644 --- a/internal/provider/torguard/provider.go +++ b/internal/provider/torguard/provider.go @@ -6,13 +6,11 @@ import ( "github.com/qdm12/gluetun/internal/constants/providers" "github.com/qdm12/gluetun/internal/provider/common" "github.com/qdm12/gluetun/internal/provider/torguard/updater" - "github.com/qdm12/gluetun/internal/provider/utils" ) type Provider struct { storage common.Storage randSource rand.Source - utils.NoPortForwarder common.Fetcher } @@ -20,10 +18,9 @@ func New(storage common.Storage, randSource rand.Source, unzipper common.Unzipper, updaterWarner common.Warner, parallelResolver common.ParallelResolver) *Provider { return &Provider{ - storage: storage, - randSource: randSource, - NoPortForwarder: utils.NewNoPortForwarding(providers.Torguard), - Fetcher: updater.New(unzipper, updaterWarner, parallelResolver), + storage: storage, + randSource: randSource, + Fetcher: updater.New(unzipper, updaterWarner, parallelResolver), } } diff --git a/internal/provider/utils/noportforward.go b/internal/provider/utils/noportforward.go deleted file mode 100644 index 4d49df81f..000000000 --- a/internal/provider/utils/noportforward.go +++ /dev/null @@ -1,33 +0,0 @@ -package utils - -import ( - "context" - "errors" - "fmt" -) - -type NoPortForwarder interface { - PortForward(ctx context.Context, objects PortForwardObjects) (port uint16, err error) - KeepPortForward(ctx context.Context, objects PortForwardObjects) (err error) -} - -type NoPortForwarding struct { - providerName string -} - -func NewNoPortForwarding(providerName string) *NoPortForwarding { - return &NoPortForwarding{ - providerName: providerName, - } -} - -var ErrPortForwardingNotSupported = errors.New("custom port forwarding obtention is not supported") - -func (n *NoPortForwarding) PortForward(context.Context, PortForwardObjects) ( - port uint16, err error) { - return 0, fmt.Errorf("%w: for %s", ErrPortForwardingNotSupported, n.providerName) -} - -func (n *NoPortForwarding) KeepPortForward(context.Context, PortForwardObjects) (err error) { - return fmt.Errorf("%w: for %s", ErrPortForwardingNotSupported, n.providerName) -} diff --git a/internal/provider/vpnsecure/provider.go b/internal/provider/vpnsecure/provider.go index 7f8d0a29e..48c56cbb7 100644 --- a/internal/provider/vpnsecure/provider.go +++ b/internal/provider/vpnsecure/provider.go @@ -6,14 +6,12 @@ import ( "github.com/qdm12/gluetun/internal/constants/providers" "github.com/qdm12/gluetun/internal/provider/common" - "github.com/qdm12/gluetun/internal/provider/utils" "github.com/qdm12/gluetun/internal/provider/vpnsecure/updater" ) type Provider struct { storage common.Storage randSource rand.Source - utils.NoPortForwarder common.Fetcher } @@ -21,10 +19,9 @@ func New(storage common.Storage, randSource rand.Source, client *http.Client, updaterWarner common.Warner, parallelResolver common.ParallelResolver) *Provider { return &Provider{ - storage: storage, - randSource: randSource, - NoPortForwarder: utils.NewNoPortForwarding(providers.VPNSecure), - Fetcher: updater.New(client, updaterWarner, parallelResolver), + storage: storage, + randSource: randSource, + Fetcher: updater.New(client, updaterWarner, parallelResolver), } } diff --git a/internal/provider/vpnunlimited/provider.go b/internal/provider/vpnunlimited/provider.go index 33f3412cc..35ff34d7c 100644 --- a/internal/provider/vpnunlimited/provider.go +++ b/internal/provider/vpnunlimited/provider.go @@ -5,14 +5,12 @@ import ( "github.com/qdm12/gluetun/internal/constants/providers" "github.com/qdm12/gluetun/internal/provider/common" - "github.com/qdm12/gluetun/internal/provider/utils" "github.com/qdm12/gluetun/internal/provider/vpnunlimited/updater" ) type Provider struct { storage common.Storage randSource rand.Source - utils.NoPortForwarder common.Fetcher } @@ -20,10 +18,9 @@ func New(storage common.Storage, randSource rand.Source, unzipper common.Unzipper, updaterWarner common.Warner, parallelResolver common.ParallelResolver) *Provider { return &Provider{ - storage: storage, - randSource: randSource, - NoPortForwarder: utils.NewNoPortForwarding(providers.VPNUnlimited), - Fetcher: updater.New(unzipper, updaterWarner, parallelResolver), + storage: storage, + randSource: randSource, + Fetcher: updater.New(unzipper, updaterWarner, parallelResolver), } } diff --git a/internal/provider/vyprvpn/provider.go b/internal/provider/vyprvpn/provider.go index d5eddfcb9..6ad6534cb 100644 --- a/internal/provider/vyprvpn/provider.go +++ b/internal/provider/vyprvpn/provider.go @@ -5,14 +5,12 @@ import ( "github.com/qdm12/gluetun/internal/constants/providers" "github.com/qdm12/gluetun/internal/provider/common" - "github.com/qdm12/gluetun/internal/provider/utils" "github.com/qdm12/gluetun/internal/provider/vyprvpn/updater" ) type Provider struct { storage common.Storage randSource rand.Source - utils.NoPortForwarder common.Fetcher } @@ -20,10 +18,9 @@ func New(storage common.Storage, randSource rand.Source, unzipper common.Unzipper, updaterWarner common.Warner, parallelResolver common.ParallelResolver) *Provider { return &Provider{ - storage: storage, - randSource: randSource, - NoPortForwarder: utils.NewNoPortForwarding(providers.Vyprvpn), - Fetcher: updater.New(unzipper, updaterWarner, parallelResolver), + storage: storage, + randSource: randSource, + Fetcher: updater.New(unzipper, updaterWarner, parallelResolver), } } diff --git a/internal/provider/wevpn/provider.go b/internal/provider/wevpn/provider.go index 6c89da2f7..0251ef464 100644 --- a/internal/provider/wevpn/provider.go +++ b/internal/provider/wevpn/provider.go @@ -5,14 +5,12 @@ import ( "github.com/qdm12/gluetun/internal/constants/providers" "github.com/qdm12/gluetun/internal/provider/common" - "github.com/qdm12/gluetun/internal/provider/utils" "github.com/qdm12/gluetun/internal/provider/wevpn/updater" ) type Provider struct { storage common.Storage randSource rand.Source - utils.NoPortForwarder common.Fetcher } @@ -20,10 +18,9 @@ func New(storage common.Storage, randSource rand.Source, updaterWarner common.Warner, parallelResolver common.ParallelResolver) *Provider { return &Provider{ - storage: storage, - randSource: randSource, - NoPortForwarder: utils.NewNoPortForwarding(providers.Wevpn), - Fetcher: updater.New(updaterWarner, parallelResolver), + storage: storage, + randSource: randSource, + Fetcher: updater.New(updaterWarner, parallelResolver), } } diff --git a/internal/provider/windscribe/provider.go b/internal/provider/windscribe/provider.go index fd8436178..658501617 100644 --- a/internal/provider/windscribe/provider.go +++ b/internal/provider/windscribe/provider.go @@ -6,24 +6,21 @@ import ( "github.com/qdm12/gluetun/internal/constants/providers" "github.com/qdm12/gluetun/internal/provider/common" - "github.com/qdm12/gluetun/internal/provider/utils" "github.com/qdm12/gluetun/internal/provider/windscribe/updater" ) type Provider struct { storage common.Storage randSource rand.Source - utils.NoPortForwarder common.Fetcher } func New(storage common.Storage, randSource rand.Source, client *http.Client, updaterWarner common.Warner) *Provider { return &Provider{ - storage: storage, - randSource: randSource, - NoPortForwarder: utils.NewNoPortForwarding(providers.Windscribe), - Fetcher: updater.New(client, updaterWarner), + storage: storage, + randSource: randSource, + Fetcher: updater.New(client, updaterWarner), } } diff --git a/internal/vpn/interfaces.go b/internal/vpn/interfaces.go index 792404d3e..8990c2974 100644 --- a/internal/vpn/interfaces.go +++ b/internal/vpn/interfaces.go @@ -9,6 +9,7 @@ import ( "github.com/qdm12/gluetun/internal/netlink" portforward "github.com/qdm12/gluetun/internal/portforward/service" "github.com/qdm12/gluetun/internal/provider" + "github.com/qdm12/gluetun/internal/provider/utils" ) type Firewall interface { @@ -35,6 +36,21 @@ type Providers interface { Get(providerName string) provider.Provider } +type Provider interface { + GetConnection(selection settings.ServerSelection, ipv6Supported bool) (connection models.Connection, err error) + OpenVPNConfig(connection models.Connection, settings settings.OpenVPN, ipv6Supported bool) (lines []string) + Name() string + FetchServers(ctx context.Context, minServers int) ( + servers []models.Server, err error) +} + +type PortForwarder interface { + Name() string + PortForward(ctx context.Context, objects utils.PortForwardObjects) ( + port uint16, err error) + KeepPortForward(ctx context.Context, objects utils.PortForwardObjects) (err error) +} + type Storage interface { FilterServers(provider string, selection settings.ServerSelection) (servers []models.Server, err error) GetServerByName(provider, name string) (server models.Server, ok bool) diff --git a/internal/vpn/portforward.go b/internal/vpn/portforward.go index 9ac3eba6e..6be920293 100644 --- a/internal/vpn/portforward.go +++ b/internal/vpn/portforward.go @@ -1,10 +1,27 @@ package vpn import ( + "context" + "errors" + "fmt" + "github.com/qdm12/gluetun/internal/configuration/settings" "github.com/qdm12/gluetun/internal/portforward/service" + pfutils "github.com/qdm12/gluetun/internal/provider/utils" ) +func getPortForwarder(provider Provider, providers Providers, //nolint:ireturn + customPortForwarderName string) (portForwarder PortForwarder) { + if customPortForwarderName != "" { + provider = providers.Get(customPortForwarderName) + } + portForwarder, ok := provider.(PortForwarder) + if ok { + return portForwarder + } + return newNoPortForwarder(provider.Name()) +} + func (l *Loop) startPortForwarding(data tunnelUpData) (err error) { partialUpdate := service.Settings{ PortForwarder: data.portForwarder, @@ -24,3 +41,28 @@ func (l *Loop) stopPortForwarding(vpnProvider string) (err error) { } return l.portForward.UpdateWith(partialUpdate) } + +type noPortForwarder struct { + providerName string +} + +func newNoPortForwarder(providerName string) *noPortForwarder { + return &noPortForwarder{ + providerName: providerName, + } +} + +var ErrPortForwardingNotSupported = errors.New("custom port forwarding obtention is not supported") + +func (n *noPortForwarder) Name() string { + return n.providerName +} + +func (n *noPortForwarder) PortForward(context.Context, pfutils.PortForwardObjects) ( + port uint16, err error) { + return 0, fmt.Errorf("%w: for %s", ErrPortForwardingNotSupported, n.providerName) +} + +func (n *noPortForwarder) KeepPortForward(context.Context, pfutils.PortForwardObjects) (err error) { + return fmt.Errorf("%w: for %s", ErrPortForwardingNotSupported, n.providerName) +} diff --git a/internal/vpn/run.go b/internal/vpn/run.go index db4560887..c8e7ff214 100644 --- a/internal/vpn/run.go +++ b/internal/vpn/run.go @@ -22,11 +22,8 @@ func (l *Loop) Run(ctx context.Context, done chan<- struct{}) { providerConf := l.providers.Get(*settings.Provider.Name) - customPortForwardingProvider := *settings.Provider.PortForwarding.Provider - portForwarder := providerConf - if customPortForwardingProvider != "" { - portForwarder = l.providers.Get(customPortForwardingProvider) - } + portForwarder := getPortForwarder(providerConf, l.providers, + *settings.Provider.PortForwarding.Provider) var vpnRunner interface { Run(ctx context.Context, waitError chan<- error, tunnelReady chan<- struct{}) diff --git a/internal/vpn/tunnelup.go b/internal/vpn/tunnelup.go index e58c7c80d..fcd2ede39 100644 --- a/internal/vpn/tunnelup.go +++ b/internal/vpn/tunnelup.go @@ -4,7 +4,6 @@ import ( "context" "github.com/qdm12/gluetun/internal/constants" - "github.com/qdm12/gluetun/internal/provider" "github.com/qdm12/gluetun/internal/version" ) @@ -12,7 +11,7 @@ type tunnelUpData struct { // Port forwarding vpnIntf string serverName string - portForwarder provider.PortForwarder + portForwarder PortForwarder } func (l *Loop) onTunnelUp(ctx context.Context, data tunnelUpData) {