chore(all): Providers containing all provider objects

- Share the same providers for updater and vpn
- Initialise all providers at start
- Get from `Providers` instead of constructing on every run
This commit is contained in:
Quentin McGaw
2022-06-10 00:47:56 +00:00
parent ebd94723c1
commit 0378fe4a7b
9 changed files with 136 additions and 112 deletions

View File

@@ -3,37 +3,12 @@ package provider
import (
"context"
"math/rand"
"net"
"net/http"
"time"
"github.com/qdm12/gluetun/internal/configuration/settings"
"github.com/qdm12/gluetun/internal/constants/providers"
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/provider/common"
"github.com/qdm12/gluetun/internal/provider/custom"
"github.com/qdm12/gluetun/internal/provider/cyberghost"
"github.com/qdm12/gluetun/internal/provider/expressvpn"
"github.com/qdm12/gluetun/internal/provider/fastestvpn"
"github.com/qdm12/gluetun/internal/provider/hidemyass"
"github.com/qdm12/gluetun/internal/provider/ipvanish"
"github.com/qdm12/gluetun/internal/provider/ivpn"
"github.com/qdm12/gluetun/internal/provider/mullvad"
"github.com/qdm12/gluetun/internal/provider/nordvpn"
"github.com/qdm12/gluetun/internal/provider/perfectprivacy"
"github.com/qdm12/gluetun/internal/provider/privado"
"github.com/qdm12/gluetun/internal/provider/privateinternetaccess"
"github.com/qdm12/gluetun/internal/provider/privatevpn"
"github.com/qdm12/gluetun/internal/provider/protonvpn"
"github.com/qdm12/gluetun/internal/provider/purevpn"
"github.com/qdm12/gluetun/internal/provider/surfshark"
"github.com/qdm12/gluetun/internal/provider/torguard"
"github.com/qdm12/gluetun/internal/provider/utils"
"github.com/qdm12/gluetun/internal/provider/vpnunlimited"
"github.com/qdm12/gluetun/internal/provider/vyprvpn"
"github.com/qdm12/gluetun/internal/provider/wevpn"
"github.com/qdm12/gluetun/internal/provider/windscribe"
)
// Provider contains methods to read and modify the openvpn configuration to connect as a client.
@@ -53,60 +28,3 @@ type PortForwarder interface {
KeepPortForward(ctx context.Context, client *http.Client,
port uint16, gateway net.IP, serverName string) (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)
}
func New(provider string, storage Storage, timeNow func() time.Time,
updaterWarner common.Warner, client *http.Client, unzipper common.Unzipper) Provider {
randSource := rand.NewSource(timeNow().UnixNano())
switch provider {
case providers.Custom:
return custom.New()
case providers.Cyberghost:
return cyberghost.New(storage, randSource)
case providers.Expressvpn:
return expressvpn.New(storage, randSource, unzipper, updaterWarner)
case providers.Fastestvpn:
return fastestvpn.New(storage, randSource, unzipper, updaterWarner)
case providers.HideMyAss:
return hidemyass.New(storage, randSource, client, updaterWarner)
case providers.Ipvanish:
return ipvanish.New(storage, randSource, unzipper, updaterWarner)
case providers.Ivpn:
return ivpn.New(storage, randSource, client, updaterWarner)
case providers.Mullvad:
return mullvad.New(storage, randSource, client)
case providers.Nordvpn:
return nordvpn.New(storage, randSource, client, updaterWarner)
case providers.Perfectprivacy:
return perfectprivacy.New(storage, randSource, unzipper, updaterWarner)
case providers.Privado:
return privado.New(storage, randSource, client, unzipper, updaterWarner)
case providers.PrivateInternetAccess:
return privateinternetaccess.New(storage, randSource, timeNow, client)
case providers.Privatevpn:
return privatevpn.New(storage, randSource, unzipper, updaterWarner)
case providers.Protonvpn:
return protonvpn.New(storage, randSource, client, updaterWarner)
case providers.Purevpn:
return purevpn.New(storage, randSource, client, unzipper, updaterWarner)
case providers.Surfshark:
return surfshark.New(storage, randSource, client, unzipper, updaterWarner)
case providers.Torguard:
return torguard.New(storage, randSource, unzipper, updaterWarner)
case providers.VPNUnlimited:
return vpnunlimited.New(storage, randSource, unzipper, updaterWarner)
case providers.Vyprvpn:
return vyprvpn.New(storage, randSource, unzipper, updaterWarner)
case providers.Wevpn:
return wevpn.New(storage, randSource, updaterWarner)
case providers.Windscribe:
return windscribe.New(storage, randSource, client, updaterWarner)
default:
panic("provider " + provider + " is unknown") // should never occur
}
}

View File

@@ -0,0 +1,91 @@
package provider
import (
"fmt"
"math/rand"
"net/http"
"time"
"github.com/qdm12/gluetun/internal/configuration/settings"
"github.com/qdm12/gluetun/internal/constants/providers"
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/provider/common"
"github.com/qdm12/gluetun/internal/provider/custom"
"github.com/qdm12/gluetun/internal/provider/cyberghost"
"github.com/qdm12/gluetun/internal/provider/expressvpn"
"github.com/qdm12/gluetun/internal/provider/fastestvpn"
"github.com/qdm12/gluetun/internal/provider/hidemyass"
"github.com/qdm12/gluetun/internal/provider/ipvanish"
"github.com/qdm12/gluetun/internal/provider/ivpn"
"github.com/qdm12/gluetun/internal/provider/mullvad"
"github.com/qdm12/gluetun/internal/provider/nordvpn"
"github.com/qdm12/gluetun/internal/provider/perfectprivacy"
"github.com/qdm12/gluetun/internal/provider/privado"
"github.com/qdm12/gluetun/internal/provider/privateinternetaccess"
"github.com/qdm12/gluetun/internal/provider/privatevpn"
"github.com/qdm12/gluetun/internal/provider/protonvpn"
"github.com/qdm12/gluetun/internal/provider/purevpn"
"github.com/qdm12/gluetun/internal/provider/surfshark"
"github.com/qdm12/gluetun/internal/provider/torguard"
"github.com/qdm12/gluetun/internal/provider/vpnunlimited"
"github.com/qdm12/gluetun/internal/provider/vyprvpn"
"github.com/qdm12/gluetun/internal/provider/wevpn"
"github.com/qdm12/gluetun/internal/provider/windscribe"
)
type Providers struct {
providerNameToProvider map[string]Provider
}
type Storage interface {
FilterServers(provider string, selection settings.ServerSelection) (
servers []models.Server, err error)
GetServerByName(provider, name string) (server models.Server, ok bool)
}
func NewProviders(storage Storage, timeNow func() time.Time,
updaterWarner common.Warner, client *http.Client, unzipper common.Unzipper) *Providers {
randSource := rand.NewSource(timeNow().UnixNano())
targetLength := len(providers.AllWithCustom())
providerNameToProvider := make(map[string]Provider, targetLength)
providerNameToProvider[providers.Custom] = custom.New()
providerNameToProvider[providers.Cyberghost] = cyberghost.New(storage, randSource)
providerNameToProvider[providers.Expressvpn] = expressvpn.New(storage, randSource, unzipper, updaterWarner)
providerNameToProvider[providers.Fastestvpn] = fastestvpn.New(storage, randSource, unzipper, updaterWarner)
providerNameToProvider[providers.HideMyAss] = hidemyass.New(storage, randSource, client, updaterWarner)
providerNameToProvider[providers.Ipvanish] = ipvanish.New(storage, randSource, unzipper, updaterWarner)
providerNameToProvider[providers.Ivpn] = ivpn.New(storage, randSource, client, updaterWarner)
providerNameToProvider[providers.Mullvad] = mullvad.New(storage, randSource, client)
providerNameToProvider[providers.Nordvpn] = nordvpn.New(storage, randSource, client, updaterWarner)
providerNameToProvider[providers.Perfectprivacy] = perfectprivacy.New(storage, randSource, unzipper, updaterWarner)
providerNameToProvider[providers.Privado] = privado.New(storage, randSource, client, unzipper, updaterWarner)
providerNameToProvider[providers.PrivateInternetAccess] = privateinternetaccess.New(storage, randSource, timeNow, client) //nolint:lll
providerNameToProvider[providers.Privatevpn] = privatevpn.New(storage, randSource, unzipper, updaterWarner)
providerNameToProvider[providers.Protonvpn] = protonvpn.New(storage, randSource, client, updaterWarner)
providerNameToProvider[providers.Purevpn] = purevpn.New(storage, randSource, client, unzipper, updaterWarner)
providerNameToProvider[providers.Surfshark] = surfshark.New(storage, randSource, client, unzipper, updaterWarner)
providerNameToProvider[providers.Torguard] = torguard.New(storage, randSource, unzipper, updaterWarner)
providerNameToProvider[providers.VPNUnlimited] = vpnunlimited.New(storage, randSource, unzipper, updaterWarner)
providerNameToProvider[providers.Vyprvpn] = vyprvpn.New(storage, randSource, unzipper, updaterWarner)
providerNameToProvider[providers.Wevpn] = wevpn.New(storage, randSource, updaterWarner)
providerNameToProvider[providers.Windscribe] = windscribe.New(storage, randSource, client, updaterWarner)
if len(providerNameToProvider) != targetLength {
// Programming sanity check
panic(fmt.Sprintf("invalid number of providers, expected %d but got %d",
targetLength, len(providerNameToProvider)))
}
return &Providers{
providerNameToProvider: providerNameToProvider,
}
}
func (p *Providers) Get(providerName string) (provider Provider) {
provider, ok := p.providerNameToProvider[providerName]
if !ok {
panic(fmt.Sprintf("provider %q not found", providerName))
}
return provider
}