2021-02-06 16:26:23 +00:00
|
|
|
// Package updater implements update mechanisms for each VPN provider servers.
|
2020-08-28 08:17:04 -04:00
|
|
|
package updater
|
|
|
|
|
|
|
|
|
|
import (
|
2020-09-05 12:57:16 -04:00
|
|
|
"context"
|
2020-08-29 13:19:34 -04:00
|
|
|
"net/http"
|
2022-01-06 06:40:23 -05:00
|
|
|
"strings"
|
2020-08-28 08:17:04 -04:00
|
|
|
"time"
|
|
|
|
|
|
2022-01-06 06:40:23 -05:00
|
|
|
"github.com/qdm12/gluetun/internal/configuration/settings"
|
2022-04-16 19:30:26 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/constants/providers"
|
2020-09-05 12:57:16 -04:00
|
|
|
"github.com/qdm12/gluetun/internal/models"
|
2021-05-06 18:48:14 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/updater/resolver"
|
2021-05-08 00:59:42 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/updater/unzip"
|
2020-08-28 08:17:04 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Updater interface {
|
2020-09-12 14:04:54 -04:00
|
|
|
UpdateServers(ctx context.Context) (allServers models.AllServers, err error)
|
2020-08-28 08:17:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type updater struct {
|
2020-09-05 12:57:16 -04:00
|
|
|
// configuration
|
2022-01-06 06:40:23 -05:00
|
|
|
options settings.Updater
|
2020-09-05 12:57:16 -04:00
|
|
|
|
|
|
|
|
// state
|
|
|
|
|
servers models.AllServers
|
|
|
|
|
|
|
|
|
|
// Functions for tests
|
2021-09-23 16:58:21 +00:00
|
|
|
logger Logger
|
2021-05-06 18:48:14 +00:00
|
|
|
timeNow func() time.Time
|
|
|
|
|
presolver resolver.Parallel
|
|
|
|
|
client *http.Client
|
2021-05-08 00:59:42 +00:00
|
|
|
unzipper unzip.Unzipper
|
2020-08-28 08:17:04 -04:00
|
|
|
}
|
|
|
|
|
|
2022-01-06 06:40:23 -05:00
|
|
|
func New(settings settings.Updater, httpClient *http.Client,
|
2021-09-23 16:58:21 +00:00
|
|
|
currentServers models.AllServers, logger Logger) Updater {
|
2021-05-08 00:59:42 +00:00
|
|
|
unzipper := unzip.New(httpClient)
|
2020-08-28 08:17:04 -04:00
|
|
|
return &updater{
|
2021-05-06 18:48:14 +00:00
|
|
|
logger: logger,
|
|
|
|
|
timeNow: time.Now,
|
2022-01-06 06:40:23 -05:00
|
|
|
presolver: resolver.NewParallelResolver(settings.DNSAddress.String()),
|
2021-05-06 18:48:14 +00:00
|
|
|
client: httpClient,
|
2021-05-08 00:59:42 +00:00
|
|
|
unzipper: unzipper,
|
2021-05-06 18:48:14 +00:00
|
|
|
options: settings,
|
|
|
|
|
servers: currentServers,
|
2020-08-28 08:17:04 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-06 06:40:23 -05:00
|
|
|
type updateFunc func(ctx context.Context) (err error)
|
2021-03-05 22:58:57 -05:00
|
|
|
|
2022-01-06 06:40:23 -05:00
|
|
|
func (u *updater) UpdateServers(ctx context.Context) (allServers models.AllServers, err error) {
|
|
|
|
|
for _, provider := range u.options.Providers {
|
|
|
|
|
u.logger.Info("updating " + strings.Title(provider) + " servers...")
|
|
|
|
|
updateProvider := u.getUpdateFunction(provider)
|
2021-04-25 15:44:45 -04:00
|
|
|
|
2020-09-05 12:57:16 -04:00
|
|
|
// TODO support servers offering only TCP or only UDP
|
2022-01-06 06:40:23 -05:00
|
|
|
// for NordVPN and PureVPN
|
|
|
|
|
err = updateProvider(ctx)
|
|
|
|
|
if err != nil {
|
2021-02-17 20:36:30 -05:00
|
|
|
if ctxErr := ctx.Err(); ctxErr != nil {
|
|
|
|
|
return allServers, ctxErr
|
|
|
|
|
}
|
2021-07-23 17:36:08 +00:00
|
|
|
u.logger.Error(err.Error())
|
2021-02-17 20:36:30 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-06 06:40:23 -05:00
|
|
|
return u.servers, nil
|
|
|
|
|
}
|
2021-09-23 07:58:13 -07:00
|
|
|
|
2022-01-06 06:40:23 -05:00
|
|
|
func (u *updater) getUpdateFunction(provider string) (updateFunction updateFunc) {
|
|
|
|
|
switch provider {
|
2022-04-16 19:30:26 +00:00
|
|
|
case providers.Custom:
|
2022-01-06 06:40:23 -05:00
|
|
|
panic("cannot update custom provider")
|
2022-04-16 19:30:26 +00:00
|
|
|
case providers.Cyberghost:
|
2022-01-06 06:40:23 -05:00
|
|
|
return func(ctx context.Context) (err error) { return u.updateCyberghost(ctx) }
|
2022-04-16 19:30:26 +00:00
|
|
|
case providers.Expressvpn:
|
2022-01-06 06:40:23 -05:00
|
|
|
return func(ctx context.Context) (err error) { return u.updateExpressvpn(ctx) }
|
2022-04-16 19:30:26 +00:00
|
|
|
case providers.Fastestvpn:
|
2022-01-06 06:40:23 -05:00
|
|
|
return func(ctx context.Context) (err error) { return u.updateFastestvpn(ctx) }
|
2022-04-16 19:30:26 +00:00
|
|
|
case providers.HideMyAss:
|
2022-01-06 06:40:23 -05:00
|
|
|
return func(ctx context.Context) (err error) { return u.updateHideMyAss(ctx) }
|
2022-04-16 19:30:26 +00:00
|
|
|
case providers.Ipvanish:
|
2022-01-06 06:40:23 -05:00
|
|
|
return func(ctx context.Context) (err error) { return u.updateIpvanish(ctx) }
|
2022-04-16 19:30:26 +00:00
|
|
|
case providers.Ivpn:
|
2022-01-06 06:40:23 -05:00
|
|
|
return func(ctx context.Context) (err error) { return u.updateIvpn(ctx) }
|
2022-04-16 19:30:26 +00:00
|
|
|
case providers.Mullvad:
|
2022-01-06 06:40:23 -05:00
|
|
|
return func(ctx context.Context) (err error) { return u.updateMullvad(ctx) }
|
2022-04-16 19:30:26 +00:00
|
|
|
case providers.Nordvpn:
|
2022-01-06 06:40:23 -05:00
|
|
|
return func(ctx context.Context) (err error) { return u.updateNordvpn(ctx) }
|
2022-04-16 19:30:26 +00:00
|
|
|
case providers.Perfectprivacy:
|
2022-01-06 06:40:23 -05:00
|
|
|
return func(ctx context.Context) (err error) { return u.updatePerfectprivacy(ctx) }
|
2022-04-16 19:30:26 +00:00
|
|
|
case providers.Privado:
|
2022-01-06 06:40:23 -05:00
|
|
|
return func(ctx context.Context) (err error) { return u.updatePrivado(ctx) }
|
2022-04-16 19:30:26 +00:00
|
|
|
case providers.PrivateInternetAccess:
|
2022-01-06 06:40:23 -05:00
|
|
|
return func(ctx context.Context) (err error) { return u.updatePIA(ctx) }
|
2022-04-16 19:30:26 +00:00
|
|
|
case providers.Privatevpn:
|
2022-01-06 06:40:23 -05:00
|
|
|
return func(ctx context.Context) (err error) { return u.updatePrivatevpn(ctx) }
|
2022-04-16 19:30:26 +00:00
|
|
|
case providers.Protonvpn:
|
2022-01-06 06:40:23 -05:00
|
|
|
return func(ctx context.Context) (err error) { return u.updateProtonvpn(ctx) }
|
2022-04-16 19:30:26 +00:00
|
|
|
case providers.Purevpn:
|
2022-01-06 06:40:23 -05:00
|
|
|
return func(ctx context.Context) (err error) { return u.updatePurevpn(ctx) }
|
2022-04-16 19:30:26 +00:00
|
|
|
case providers.Surfshark:
|
2022-01-06 06:40:23 -05:00
|
|
|
return func(ctx context.Context) (err error) { return u.updateSurfshark(ctx) }
|
2022-04-16 19:30:26 +00:00
|
|
|
case providers.Torguard:
|
2022-01-06 06:40:23 -05:00
|
|
|
return func(ctx context.Context) (err error) { return u.updateTorguard(ctx) }
|
2022-04-16 19:30:26 +00:00
|
|
|
case providers.VPNUnlimited:
|
2022-01-06 06:40:23 -05:00
|
|
|
return func(ctx context.Context) (err error) { return u.updateVPNUnlimited(ctx) }
|
2022-04-16 19:30:26 +00:00
|
|
|
case providers.Vyprvpn:
|
2022-01-06 06:40:23 -05:00
|
|
|
return func(ctx context.Context) (err error) { return u.updateVyprvpn(ctx) }
|
2022-04-16 19:30:26 +00:00
|
|
|
case providers.Wevpn:
|
2022-01-06 06:40:23 -05:00
|
|
|
return func(ctx context.Context) (err error) { return u.updateWevpn(ctx) }
|
2022-04-16 19:30:26 +00:00
|
|
|
case providers.Windscribe:
|
2022-01-06 06:40:23 -05:00
|
|
|
return func(ctx context.Context) (err error) { return u.updateWindscribe(ctx) }
|
|
|
|
|
default:
|
|
|
|
|
panic("provider " + provider + " is unknown")
|
2020-08-28 08:17:04 -04:00
|
|
|
}
|
|
|
|
|
}
|