Files
gluetun/internal/updater/updater.go
Quentin McGaw 0549326dfb chore(updater): tiny code changes
- Remove unneeded ctx error check in cyberghost updating code
- Move global scope caser to function local scope
- Return error if updating a single provider in `UpdateServers`
- Add comments on different error paths in `UpdateServers`
2022-06-04 13:50:29 +00:00

79 lines
1.9 KiB
Go

// Package updater implements update mechanisms for each VPN provider servers.
package updater
import (
"context"
"net/http"
"time"
"github.com/qdm12/gluetun/internal/configuration/settings"
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/updater/resolver"
"github.com/qdm12/gluetun/internal/updater/unzip"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
type Updater struct {
// configuration
options settings.Updater
// state
servers models.AllServers
// Functions for tests
logger Logger
timeNow func() time.Time
presolver resolver.Parallel
client *http.Client
unzipper unzip.Unzipper
}
type Logger interface {
Info(s string)
Warn(s string)
Error(s string)
}
func New(settings settings.Updater, httpClient *http.Client,
currentServers models.AllServers, logger Logger) *Updater {
unzipper := unzip.New(httpClient)
return &Updater{
logger: logger,
timeNow: time.Now,
presolver: resolver.NewParallelResolver(settings.DNSAddress.String()),
client: httpClient,
unzipper: unzipper,
options: settings,
servers: currentServers,
}
}
func (u *Updater) UpdateServers(ctx context.Context) (allServers models.AllServers, err error) {
caser := cases.Title(language.English)
for _, provider := range u.options.Providers {
u.logger.Info("updating " + caser.String(provider) + " servers...")
// TODO support servers offering only TCP or only UDP
// for NordVPN and PureVPN
err := u.updateProvider(ctx, provider)
if err == nil {
continue
}
// return the only error for the single provider.
if len(u.options.Providers) == 1 {
return allServers, err
}
// stop updating the next providers if context is canceled.
if ctxErr := ctx.Err(); ctxErr != nil {
return allServers, ctxErr
}
// Log the error and continue updating the next provider.
u.logger.Error(err.Error())
}
return u.servers, nil
}