mirror of
https://github.com/qdm12/gluetun.git
synced 2026-07-22 18:36:27 +02:00
104 lines
3.6 KiB
Go
104 lines
3.6 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"net/http"
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/qdm12/gluetun/internal/configuration/settings"
|
|
"github.com/qdm12/gluetun/internal/constants/providers"
|
|
"github.com/qdm12/gluetun/internal/updater"
|
|
)
|
|
|
|
type UpdaterLogger interface {
|
|
Info(s string)
|
|
Infof(format string, args ...any)
|
|
Warn(s string)
|
|
Warnf(format string, args ...any)
|
|
Error(s string)
|
|
}
|
|
|
|
func (c *CLI) Update(ctx context.Context, args []string, logger UpdaterLogger) error {
|
|
options := settings.Updater{}
|
|
var endUserMode, updateAll bool
|
|
var dnsServer, csvProviders, ipToken, protonUsername, protonEmail, protonPassword string
|
|
flagSet := flag.NewFlagSet("update", flag.ExitOnError)
|
|
flagSet.BoolVar(&endUserMode, "enduser", false, // TODO v4: remove
|
|
"Write results to /gluetun/servers/ (for end users)")
|
|
flagSet.StringVar(&dnsServer, "dns", "", "no longer used, your DNS will use DoH with Cloudflare and Google")
|
|
const defaultMinRatio = 0.8
|
|
flagSet.Float64Var(&options.MinRatio, "minratio", defaultMinRatio,
|
|
"Minimum ratio of servers to find for the update to succeed")
|
|
flagSet.BoolVar(&updateAll, "all", false, "Update servers for all VPN providers")
|
|
flagSet.StringVar(&csvProviders, "providers", "", "CSV string of VPN providers to update server data for")
|
|
flagSet.StringVar(&ipToken, "ip-token", "", "IP data service token (e.g. ipinfo.io) to use")
|
|
flagSet.StringVar(&protonUsername, "proton-username", "",
|
|
"(Retro-compatibility) Username to use to authenticate with Proton. Use -proton-email instead.") // v4 remove this
|
|
flagSet.StringVar(&protonEmail, "proton-email", "", "Email to use to authenticate with Proton")
|
|
flagSet.StringVar(&protonPassword, "proton-password", "", "Password to use to authenticate with Proton")
|
|
if err := flagSet.Parse(args); err != nil {
|
|
return err
|
|
}
|
|
|
|
switch {
|
|
case dnsServer != "":
|
|
logger.Warn("The -dns flag is no longer used, your DNS will use DoH with Cloudflare and Google")
|
|
case endUserMode:
|
|
logger.Warn("The -enduser flag is no longer used and has no effect")
|
|
case ipToken != "":
|
|
logger.Warn("The -ip-token flag is no longer used and has no effect")
|
|
case protonUsername != "":
|
|
logger.Warn("The -proton-username flag is no longer used and has no effect. Use -proton-email instead.")
|
|
case protonEmail != "":
|
|
logger.Warn("The -proton-email flag is no longer used and has no effect")
|
|
case protonPassword != "":
|
|
logger.Warn("The -proton-password flag is no longer used and has no effect")
|
|
}
|
|
|
|
if updateAll {
|
|
options.Providers = providers.All()
|
|
} else {
|
|
if csvProviders == "" {
|
|
return errors.New("no provider was specified")
|
|
}
|
|
options.Providers = strings.Split(csvProviders, ",")
|
|
}
|
|
|
|
if slices.Contains(options.Providers, providers.Protonvpn) {
|
|
if protonEmail == "" && protonUsername != "" {
|
|
protonEmail = protonUsername + "@protonmail.com"
|
|
logger.Warn("use -proton-email instead of -proton-username in the future. " +
|
|
"This assumes the email is " + protonEmail + " and may not work.")
|
|
}
|
|
options.ProtonEmail = &protonEmail
|
|
options.ProtonPassword = &protonPassword
|
|
}
|
|
|
|
options.SetDefaults(options.Providers[0])
|
|
|
|
err := options.Validate()
|
|
if err != nil {
|
|
return fmt.Errorf("options validation failed: %w", err)
|
|
}
|
|
|
|
storage, err := setupStorage(logger)
|
|
if err != nil {
|
|
return fmt.Errorf("creating servers storage: %w", err)
|
|
}
|
|
|
|
const clientTimeout = 10 * time.Second
|
|
httpClient := &http.Client{Timeout: clientTimeout}
|
|
updater := updater.New(httpClient, storage, logger)
|
|
err = updater.UpdateServers(ctx, options.Providers, options.MinRatio)
|
|
if err != nil {
|
|
return fmt.Errorf("updating server information: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|