Skip to content

Commit

Permalink
feat(storage): STORAGE_FILEPATH option (#2416)
Browse files Browse the repository at this point in the history
- `STORAGE_FILEPATH=` disables storing to and reading from a local servers.json file
- `STORAGE_FILEPATH` defaults to `/gluetun/servers.json`
- Fix #2074
  • Loading branch information
jfroy authored Aug 19, 2024
1 parent 3bf937d commit 12c411e
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 14 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ ENV VPN_SERVICE_PROVIDER=pia \
PUBLICIP_PERIOD=12h \
PUBLICIP_API=ipinfo \
PUBLICIP_API_TOKEN= \
# Storage
STORAGE_FILEPATH=/gluetun/servers.json \
# Pprof
PPROF_ENABLED=no \
PPROF_BLOCK_PROFILE_RATE=0 \
Expand Down
2 changes: 1 addition & 1 deletion cmd/gluetun/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func _main(ctx context.Context, buildInfo models.BuildInformation,

// TODO run this in a loop or in openvpn to reload from file without restarting
storageLogger := logger.New(log.SetComponent("storage"))
storage, err := storage.New(storageLogger, constants.ServersData)
storage, err := storage.New(storageLogger, *allSettings.Storage.Filepath)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/configuration/settings/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Provider struct {
}

// TODO v4 remove pointer for receiver (because of Surfshark).
func (p *Provider) validate(vpnType string, storage Storage, warner Warner) (err error) {
func (p *Provider) validate(vpnType string, filterChoicesGetter FilterChoicesGetter, warner Warner) (err error) {
// Validate Name
var validNames []string
if vpnType == vpn.OpenVPN {
Expand All @@ -48,7 +48,7 @@ func (p *Provider) validate(vpnType string, storage Storage, warner Warner) (err
return fmt.Errorf("%w for Wireguard: %w", ErrVPNProviderNameNotValid, err)
}

err = p.ServerSelection.validate(p.Name, storage, warner)
err = p.ServerSelection.validate(p.Name, filterChoicesGetter, warner)
if err != nil {
return fmt.Errorf("server selection: %w", err)
}
Expand Down
8 changes: 4 additions & 4 deletions internal/configuration/settings/serverselection.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ var (
)

func (ss *ServerSelection) validate(vpnServiceProvider string,
storage Storage, warner Warner) (err error) {
filterChoicesGetter FilterChoicesGetter, warner Warner) (err error) {
switch ss.VPN {
case vpn.OpenVPN, vpn.Wireguard:
default:
return fmt.Errorf("%w: %s", ErrVPNTypeNotValid, ss.VPN)
}

filterChoices, err := getLocationFilterChoices(vpnServiceProvider, ss, storage, warner)
filterChoices, err := getLocationFilterChoices(vpnServiceProvider, ss, filterChoicesGetter, warner)
if err != nil {
return err // already wrapped error
}
Expand Down Expand Up @@ -142,9 +142,9 @@ func (ss *ServerSelection) validate(vpnServiceProvider string,
}

func getLocationFilterChoices(vpnServiceProvider string,
ss *ServerSelection, storage Storage, warner Warner) (
ss *ServerSelection, filterChoicesGetter FilterChoicesGetter, warner Warner) (
filterChoices models.FilterChoices, err error) {
filterChoices = storage.GetFilterChoices(vpnServiceProvider)
filterChoices = filterChoicesGetter.GetFilterChoices(vpnServiceProvider)

if vpnServiceProvider == providers.Surfshark {
// // Retro compatibility
Expand Down
17 changes: 12 additions & 5 deletions internal/configuration/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,22 @@ type Settings struct {
Log Log
PublicIP PublicIP
Shadowsocks Shadowsocks
Storage Storage
System System
Updater Updater
Version Version
VPN VPN
Pprof pprof.Settings
}

type Storage interface {
type FilterChoicesGetter interface {
GetFilterChoices(provider string) models.FilterChoices
}

// Validate validates all the settings and returns an error
// if one of them is not valid.
// TODO v4 remove pointer for receiver (because of Surfshark).
func (s *Settings) Validate(storage Storage, ipv6Supported bool,
func (s *Settings) Validate(filterChoicesGetter FilterChoicesGetter, ipv6Supported bool,
warner Warner) (err error) {
nameToValidation := map[string]func() error{
"control server": s.ControlServer.validate,
Expand All @@ -47,12 +48,13 @@ func (s *Settings) Validate(storage Storage, ipv6Supported bool,
"log": s.Log.validate,
"public ip check": s.PublicIP.validate,
"shadowsocks": s.Shadowsocks.validate,
"storage": s.Storage.validate,
"system": s.System.validate,
"updater": s.Updater.Validate,
"version": s.Version.validate,
// Pprof validation done in pprof constructor
"VPN": func() error {
return s.VPN.Validate(storage, ipv6Supported, warner)
return s.VPN.Validate(filterChoicesGetter, ipv6Supported, warner)
},
}

Expand All @@ -76,6 +78,7 @@ func (s *Settings) copy() (copied Settings) {
Log: s.Log.copy(),
PublicIP: s.PublicIP.copy(),
Shadowsocks: s.Shadowsocks.copy(),
Storage: s.Storage.copy(),
System: s.System.copy(),
Updater: s.Updater.copy(),
Version: s.Version.copy(),
Expand All @@ -85,7 +88,7 @@ func (s *Settings) copy() (copied Settings) {
}

func (s *Settings) OverrideWith(other Settings,
storage Storage, ipv6Supported bool, warner Warner) (err error) {
filterChoicesGetter FilterChoicesGetter, ipv6Supported bool, warner Warner) (err error) {
patchedSettings := s.copy()
patchedSettings.ControlServer.overrideWith(other.ControlServer)
patchedSettings.DNS.overrideWith(other.DNS)
Expand All @@ -95,12 +98,13 @@ func (s *Settings) OverrideWith(other Settings,
patchedSettings.Log.overrideWith(other.Log)
patchedSettings.PublicIP.overrideWith(other.PublicIP)
patchedSettings.Shadowsocks.overrideWith(other.Shadowsocks)
patchedSettings.Storage.overrideWith(other.Storage)
patchedSettings.System.overrideWith(other.System)
patchedSettings.Updater.overrideWith(other.Updater)
patchedSettings.Version.overrideWith(other.Version)
patchedSettings.VPN.OverrideWith(other.VPN)
patchedSettings.Pprof.OverrideWith(other.Pprof)
err = patchedSettings.Validate(storage, ipv6Supported, warner)
err = patchedSettings.Validate(filterChoicesGetter, ipv6Supported, warner)
if err != nil {
return err
}
Expand All @@ -117,6 +121,7 @@ func (s *Settings) SetDefaults() {
s.Log.setDefaults()
s.PublicIP.setDefaults()
s.Shadowsocks.setDefaults()
s.Storage.setDefaults()
s.System.setDefaults()
s.Version.setDefaults()
s.VPN.setDefaults()
Expand All @@ -139,6 +144,7 @@ func (s Settings) toLinesNode() (node *gotree.Node) {
node.AppendNode(s.Shadowsocks.toLinesNode())
node.AppendNode(s.HTTPProxy.toLinesNode())
node.AppendNode(s.ControlServer.toLinesNode())
node.AppendNode(s.Storage.toLinesNode())
node.AppendNode(s.System.toLinesNode())
node.AppendNode(s.PublicIP.toLinesNode())
node.AppendNode(s.Updater.toLinesNode())
Expand Down Expand Up @@ -188,6 +194,7 @@ func (s *Settings) Read(r *reader.Reader) (err error) {
"log": s.Log.read,
"public ip": s.PublicIP.read,
"shadowsocks": s.Shadowsocks.read,
"storage": s.Storage.read,
"system": s.System.read,
"updater": s.Updater.read,
"version": s.Version.read,
Expand Down
59 changes: 59 additions & 0 deletions internal/configuration/settings/storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package settings

import (
"fmt"
"path/filepath"

"github.com/qdm12/gosettings"
"github.com/qdm12/gosettings/reader"
"github.com/qdm12/gotree"
)

// Storage contains settings to configure the storage.
type Storage struct {
// Filepath is the path to the servers.json file. An empty string disables on-disk storage.
Filepath *string
}

func (s Storage) validate() (err error) {
if *s.Filepath != "" { // optional
_, err := filepath.Abs(*s.Filepath)
if err != nil {
return fmt.Errorf("filepath is not valid: %w", err)
}
}
return nil
}

func (s *Storage) copy() (copied Storage) {
return Storage{
Filepath: gosettings.CopyPointer(s.Filepath),
}
}

func (s *Storage) overrideWith(other Storage) {
s.Filepath = gosettings.OverrideWithPointer(s.Filepath, other.Filepath)
}

func (s *Storage) setDefaults() {
const defaultFilepath = "/gluetun/servers.json"
s.Filepath = gosettings.DefaultPointer(s.Filepath, defaultFilepath)
}

func (s Storage) String() string {
return s.toLinesNode().String()
}

func (s Storage) toLinesNode() (node *gotree.Node) {
if *s.Filepath == "" {
return gotree.New("Storage settings: disabled")
}
node = gotree.New("Storage settings:")
node.Appendf("Filepath: %s", *s.Filepath)
return node
}

func (s *Storage) read(r *reader.Reader) (err error) {
s.Filepath = r.Get("STORAGE_FILEPATH", reader.AcceptEmpty(true))
return nil
}
4 changes: 2 additions & 2 deletions internal/configuration/settings/vpn.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ type VPN struct {
}

// TODO v4 remove pointer for receiver (because of Surfshark).
func (v *VPN) Validate(storage Storage, ipv6Supported bool, warner Warner) (err error) {
func (v *VPN) Validate(filterChoicesGetter FilterChoicesGetter, ipv6Supported bool, warner Warner) (err error) {
// Validate Type
validVPNTypes := []string{vpn.OpenVPN, vpn.Wireguard}
if err = validate.IsOneOf(v.Type, validVPNTypes...); err != nil {
return fmt.Errorf("%w: %w", ErrVPNTypeNotValid, err)
}

err = v.Provider.validate(v.Type, storage, warner)
err = v.Provider.validate(v.Type, filterChoicesGetter, warner)
if err != nil {
return fmt.Errorf("provider settings: %w", err)
}
Expand Down

0 comments on commit 12c411e

Please sign in to comment.