From a43973c093662c2b5521eb2e9e3aea053584e96b Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Thu, 25 May 2023 12:08:43 +0000 Subject: [PATCH] chore(settings): use github.com/qdm12/gosettings --- go.mod | 3 +- go.sum | 4 + internal/configuration/settings/dns.go | 15 +- .../configuration/settings/dnsblacklist.go | 49 +++---- internal/configuration/settings/dot.go | 17 +-- internal/configuration/settings/firewall.go | 35 ++--- internal/configuration/settings/health.go | 32 ++--- .../configuration/settings/healthywait.go | 18 +-- .../configuration/settings/helpers/belong.go | 44 +----- .../configuration/settings/helpers/default.go | 2 +- .../configuration/settings/helpers/merge.go | 4 +- .../settings/helpers/override.go | 2 +- internal/configuration/settings/httpproxy.go | 59 ++++---- internal/configuration/settings/log.go | 10 +- internal/configuration/settings/openvpn.go | 128 ++++++++--------- .../settings/openvpnselection.go | 56 ++++---- .../configuration/settings/portforward.go | 25 ++-- internal/configuration/settings/provider.go | 16 +-- internal/configuration/settings/publicip.go | 18 +-- internal/configuration/settings/server.go | 17 +-- .../configuration/settings/serverselection.go | 129 +++++++++--------- .../configuration/settings/shadowsocks.go | 9 +- internal/configuration/settings/system.go | 22 +-- internal/configuration/settings/unbound.go | 59 ++++---- internal/configuration/settings/updater.go | 40 +++--- internal/configuration/settings/version.go | 9 +- internal/configuration/settings/vpn.go | 15 +- internal/configuration/settings/wireguard.go | 47 +++---- .../settings/wireguardselection.go | 21 +-- internal/httpserver/settings.go | 30 ++-- internal/pprof/settings.go | 22 +-- 31 files changed, 461 insertions(+), 496 deletions(-) diff --git a/go.mod b/go.mod index 1f5f5a39..1104270b 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/golang/mock v1.6.0 github.com/qdm12/dns v1.11.0 github.com/qdm12/golibs v0.0.0-20210822203818-5c568b0777b6 + github.com/qdm12/gosettings v0.2.0 github.com/qdm12/goshutdown v0.3.0 github.com/qdm12/gosplash v0.1.0 github.com/qdm12/gotree v0.2.0 @@ -18,7 +19,7 @@ require ( github.com/stretchr/testify v1.8.2 github.com/vishvananda/netlink v1.2.1-beta.2 github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a - golang.org/x/exp v0.0.0-20230519143937-03e91628a987 + golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 golang.org/x/net v0.10.0 golang.org/x/sys v0.8.0 golang.org/x/text v0.9.0 diff --git a/go.sum b/go.sum index 37daf5ba..48ce8853 100644 --- a/go.sum +++ b/go.sum @@ -91,6 +91,8 @@ github.com/qdm12/golibs v0.0.0-20210603202746-e5494e9c2ebb/go.mod h1:15RBzkun0i8 github.com/qdm12/golibs v0.0.0-20210723175634-a75ca7fd74c2/go.mod h1:6aRbg4Z/bTbm9JfxsGXfWKHi7zsOvPfUTK1S5HuAFKg= github.com/qdm12/golibs v0.0.0-20210822203818-5c568b0777b6 h1:bge5AL7cjHJMPz+5IOz5yF01q/l8No6+lIEBieA8gMg= github.com/qdm12/golibs v0.0.0-20210822203818-5c568b0777b6/go.mod h1:6aRbg4Z/bTbm9JfxsGXfWKHi7zsOvPfUTK1S5HuAFKg= +github.com/qdm12/gosettings v0.2.0 h1:Q/4s0t8k9e3MSKMM5oMUIE3UGrdd+U2+qR8rTgiKRvA= +github.com/qdm12/gosettings v0.2.0/go.mod h1:GptXlJVLQxfWznSlgc+pNo3JGnp+f2AKEv0tq/U7igU= github.com/qdm12/goshutdown v0.3.0 h1:pqBpJkdwlZlfTEx4QHtS8u8CXx6pG0fVo6S1N0MpSEM= github.com/qdm12/goshutdown v0.3.0/go.mod h1:EqZ46No00kCTZ5qzdd3qIzY6ayhMt24QI8Mh8LVQYmM= github.com/qdm12/gosplash v0.1.0 h1:Sfl+zIjFZFP7b0iqf2l5UkmEY97XBnaKkH3FNY6Gf7g= @@ -152,6 +154,8 @@ golang.org/x/crypto v0.6.0 h1:qfktjS5LUO+fFKeJXZ+ikTRijMmljikvG68fpMMruSc= golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58= golang.org/x/exp v0.0.0-20230519143937-03e91628a987 h1:3xJIFvzUFbu4ls0BTBYcgbCGhA63eAOEMxIHugyXJqA= golang.org/x/exp v0.0.0-20230519143937-03e91628a987/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w= +golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 h1:k/i9J1pBpvlfR+9QsetwPyERsqu1GIbi967PQMq3Ivc= +golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w= golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= diff --git a/internal/configuration/settings/dns.go b/internal/configuration/settings/dns.go index 47ddcc3f..532b35f0 100644 --- a/internal/configuration/settings/dns.go +++ b/internal/configuration/settings/dns.go @@ -5,6 +5,7 @@ import ( "net/netip" "github.com/qdm12/gluetun/internal/configuration/settings/helpers" + "github.com/qdm12/gosettings" "github.com/qdm12/gotree" ) @@ -40,7 +41,7 @@ func (d DNS) validate() (err error) { func (d *DNS) Copy() (copied DNS) { return DNS{ ServerAddress: d.ServerAddress, - KeepNameserver: helpers.CopyPointer(d.KeepNameserver), + KeepNameserver: gosettings.CopyPointer(d.KeepNameserver), DoT: d.DoT.copy(), } } @@ -48,8 +49,8 @@ func (d *DNS) Copy() (copied DNS) { // mergeWith merges the other settings into any // unset field of the receiver settings object. func (d *DNS) mergeWith(other DNS) { - d.ServerAddress = helpers.MergeWithIP(d.ServerAddress, other.ServerAddress) - d.KeepNameserver = helpers.MergeWithPointer(d.KeepNameserver, other.KeepNameserver) + d.ServerAddress = gosettings.MergeWithValidator(d.ServerAddress, other.ServerAddress) + d.KeepNameserver = gosettings.MergeWithPointer(d.KeepNameserver, other.KeepNameserver) d.DoT.mergeWith(other.DoT) } @@ -57,15 +58,15 @@ func (d *DNS) mergeWith(other DNS) { // settings object with any field set in the other // settings. func (d *DNS) overrideWith(other DNS) { - d.ServerAddress = helpers.OverrideWithIP(d.ServerAddress, other.ServerAddress) - d.KeepNameserver = helpers.OverrideWithPointer(d.KeepNameserver, other.KeepNameserver) + d.ServerAddress = gosettings.OverrideWithValidator(d.ServerAddress, other.ServerAddress) + d.KeepNameserver = gosettings.OverrideWithPointer(d.KeepNameserver, other.KeepNameserver) d.DoT.overrideWith(other.DoT) } func (d *DNS) setDefaults() { localhost := netip.AddrFrom4([4]byte{127, 0, 0, 1}) - d.ServerAddress = helpers.DefaultIP(d.ServerAddress, localhost) - d.KeepNameserver = helpers.DefaultPointer(d.KeepNameserver, false) + d.ServerAddress = gosettings.DefaultValidator(d.ServerAddress, localhost) + d.KeepNameserver = gosettings.DefaultPointer(d.KeepNameserver, false) d.DoT.setDefaults() } diff --git a/internal/configuration/settings/dnsblacklist.go b/internal/configuration/settings/dnsblacklist.go index 1618de0d..f3f87d1e 100644 --- a/internal/configuration/settings/dnsblacklist.go +++ b/internal/configuration/settings/dnsblacklist.go @@ -8,6 +8,7 @@ import ( "github.com/qdm12/dns/pkg/blacklist" "github.com/qdm12/gluetun/internal/configuration/settings/helpers" + "github.com/qdm12/gosettings" "github.com/qdm12/gotree" ) @@ -23,9 +24,9 @@ type DNSBlacklist struct { } func (b *DNSBlacklist) setDefaults() { - b.BlockMalicious = helpers.DefaultPointer(b.BlockMalicious, true) - b.BlockAds = helpers.DefaultPointer(b.BlockAds, false) - b.BlockSurveillance = helpers.DefaultPointer(b.BlockSurveillance, true) + b.BlockMalicious = gosettings.DefaultPointer(b.BlockMalicious, true) + b.BlockAds = gosettings.DefaultPointer(b.BlockAds, false) + b.BlockSurveillance = gosettings.DefaultPointer(b.BlockSurveillance, true) } var hostRegex = regexp.MustCompile(`^([a-zA-Z0-9]|[a-zA-Z0-9_][a-zA-Z0-9\-_]{0,61}[a-zA-Z0-9_])(\.([a-zA-Z0-9]|[a-zA-Z0-9_][a-zA-Z0-9\-_]{0,61}[a-zA-Z0-9]))*$`) //nolint:lll @@ -53,34 +54,34 @@ func (b DNSBlacklist) validate() (err error) { func (b DNSBlacklist) copy() (copied DNSBlacklist) { return DNSBlacklist{ - BlockMalicious: helpers.CopyPointer(b.BlockMalicious), - BlockAds: helpers.CopyPointer(b.BlockAds), - BlockSurveillance: helpers.CopyPointer(b.BlockSurveillance), - AllowedHosts: helpers.CopySlice(b.AllowedHosts), - AddBlockedHosts: helpers.CopySlice(b.AddBlockedHosts), - AddBlockedIPs: helpers.CopySlice(b.AddBlockedIPs), - AddBlockedIPPrefixes: helpers.CopySlice(b.AddBlockedIPPrefixes), + BlockMalicious: gosettings.CopyPointer(b.BlockMalicious), + BlockAds: gosettings.CopyPointer(b.BlockAds), + BlockSurveillance: gosettings.CopyPointer(b.BlockSurveillance), + AllowedHosts: gosettings.CopySlice(b.AllowedHosts), + AddBlockedHosts: gosettings.CopySlice(b.AddBlockedHosts), + AddBlockedIPs: gosettings.CopySlice(b.AddBlockedIPs), + AddBlockedIPPrefixes: gosettings.CopySlice(b.AddBlockedIPPrefixes), } } func (b *DNSBlacklist) mergeWith(other DNSBlacklist) { - b.BlockMalicious = helpers.MergeWithPointer(b.BlockMalicious, other.BlockMalicious) - b.BlockAds = helpers.MergeWithPointer(b.BlockAds, other.BlockAds) - b.BlockSurveillance = helpers.MergeWithPointer(b.BlockSurveillance, other.BlockSurveillance) - b.AllowedHosts = helpers.MergeSlices(b.AllowedHosts, other.AllowedHosts) - b.AddBlockedHosts = helpers.MergeSlices(b.AddBlockedHosts, other.AddBlockedHosts) - b.AddBlockedIPs = helpers.MergeSlices(b.AddBlockedIPs, other.AddBlockedIPs) - b.AddBlockedIPPrefixes = helpers.MergeSlices(b.AddBlockedIPPrefixes, other.AddBlockedIPPrefixes) + b.BlockMalicious = gosettings.MergeWithPointer(b.BlockMalicious, other.BlockMalicious) + b.BlockAds = gosettings.MergeWithPointer(b.BlockAds, other.BlockAds) + b.BlockSurveillance = gosettings.MergeWithPointer(b.BlockSurveillance, other.BlockSurveillance) + b.AllowedHosts = helpers.MergeWithSlice(b.AllowedHosts, other.AllowedHosts) + b.AddBlockedHosts = gosettings.MergeWithSlice(b.AddBlockedHosts, other.AddBlockedHosts) + b.AddBlockedIPs = gosettings.MergeWithSlice(b.AddBlockedIPs, other.AddBlockedIPs) + b.AddBlockedIPPrefixes = gosettings.MergeWithSlice(b.AddBlockedIPPrefixes, other.AddBlockedIPPrefixes) } func (b *DNSBlacklist) overrideWith(other DNSBlacklist) { - b.BlockMalicious = helpers.OverrideWithPointer(b.BlockMalicious, other.BlockMalicious) - b.BlockAds = helpers.OverrideWithPointer(b.BlockAds, other.BlockAds) - b.BlockSurveillance = helpers.OverrideWithPointer(b.BlockSurveillance, other.BlockSurveillance) - b.AllowedHosts = helpers.OverrideWithSlice(b.AllowedHosts, other.AllowedHosts) - b.AddBlockedHosts = helpers.OverrideWithSlice(b.AddBlockedHosts, other.AddBlockedHosts) - b.AddBlockedIPs = helpers.OverrideWithSlice(b.AddBlockedIPs, other.AddBlockedIPs) - b.AddBlockedIPPrefixes = helpers.OverrideWithSlice(b.AddBlockedIPPrefixes, other.AddBlockedIPPrefixes) + b.BlockMalicious = gosettings.OverrideWithPointer(b.BlockMalicious, other.BlockMalicious) + b.BlockAds = gosettings.OverrideWithPointer(b.BlockAds, other.BlockAds) + b.BlockSurveillance = gosettings.OverrideWithPointer(b.BlockSurveillance, other.BlockSurveillance) + b.AllowedHosts = gosettings.OverrideWithSlice(b.AllowedHosts, other.AllowedHosts) + b.AddBlockedHosts = gosettings.OverrideWithSlice(b.AddBlockedHosts, other.AddBlockedHosts) + b.AddBlockedIPs = gosettings.OverrideWithSlice(b.AddBlockedIPs, other.AddBlockedIPs) + b.AddBlockedIPPrefixes = gosettings.OverrideWithSlice(b.AddBlockedIPPrefixes, other.AddBlockedIPPrefixes) } func (b DNSBlacklist) ToBlacklistFormat() (settings blacklist.BuilderSettings, err error) { diff --git a/internal/configuration/settings/dot.go b/internal/configuration/settings/dot.go index b85355c1..c6755d35 100644 --- a/internal/configuration/settings/dot.go +++ b/internal/configuration/settings/dot.go @@ -6,6 +6,7 @@ import ( "time" "github.com/qdm12/gluetun/internal/configuration/settings/helpers" + "github.com/qdm12/gosettings" "github.com/qdm12/gotree" ) @@ -54,8 +55,8 @@ func (d DoT) validate() (err error) { func (d *DoT) copy() (copied DoT) { return DoT{ - Enabled: helpers.CopyPointer(d.Enabled), - UpdatePeriod: helpers.CopyPointer(d.UpdatePeriod), + Enabled: gosettings.CopyPointer(d.Enabled), + UpdatePeriod: gosettings.CopyPointer(d.UpdatePeriod), Unbound: d.Unbound.copy(), Blacklist: d.Blacklist.copy(), } @@ -64,8 +65,8 @@ func (d *DoT) copy() (copied DoT) { // mergeWith merges the other settings into any // unset field of the receiver settings object. func (d *DoT) mergeWith(other DoT) { - d.Enabled = helpers.MergeWithPointer(d.Enabled, other.Enabled) - d.UpdatePeriod = helpers.MergeWithPointer(d.UpdatePeriod, other.UpdatePeriod) + d.Enabled = gosettings.MergeWithPointer(d.Enabled, other.Enabled) + d.UpdatePeriod = gosettings.MergeWithPointer(d.UpdatePeriod, other.UpdatePeriod) d.Unbound.mergeWith(other.Unbound) d.Blacklist.mergeWith(other.Blacklist) } @@ -74,16 +75,16 @@ func (d *DoT) mergeWith(other DoT) { // settings object with any field set in the other // settings. func (d *DoT) overrideWith(other DoT) { - d.Enabled = helpers.OverrideWithPointer(d.Enabled, other.Enabled) - d.UpdatePeriod = helpers.OverrideWithPointer(d.UpdatePeriod, other.UpdatePeriod) + d.Enabled = gosettings.OverrideWithPointer(d.Enabled, other.Enabled) + d.UpdatePeriod = gosettings.OverrideWithPointer(d.UpdatePeriod, other.UpdatePeriod) d.Unbound.overrideWith(other.Unbound) d.Blacklist.overrideWith(other.Blacklist) } func (d *DoT) setDefaults() { - d.Enabled = helpers.DefaultPointer(d.Enabled, true) + d.Enabled = gosettings.DefaultPointer(d.Enabled, true) const defaultUpdatePeriod = 24 * time.Hour - d.UpdatePeriod = helpers.DefaultPointer(d.UpdatePeriod, defaultUpdatePeriod) + d.UpdatePeriod = gosettings.DefaultPointer(d.UpdatePeriod, defaultUpdatePeriod) d.Unbound.setDefaults() d.Blacklist.setDefaults() } diff --git a/internal/configuration/settings/firewall.go b/internal/configuration/settings/firewall.go index 6aa6c988..9fe3d14a 100644 --- a/internal/configuration/settings/firewall.go +++ b/internal/configuration/settings/firewall.go @@ -5,6 +5,7 @@ import ( "net/netip" "github.com/qdm12/gluetun/internal/configuration/settings/helpers" + "github.com/qdm12/gosettings" "github.com/qdm12/gotree" ) @@ -40,11 +41,11 @@ func hasZeroPort(ports []uint16) (has bool) { func (f *Firewall) copy() (copied Firewall) { return Firewall{ - VPNInputPorts: helpers.CopySlice(f.VPNInputPorts), - InputPorts: helpers.CopySlice(f.InputPorts), - OutboundSubnets: helpers.CopySlice(f.OutboundSubnets), - Enabled: helpers.CopyPointer(f.Enabled), - Debug: helpers.CopyPointer(f.Debug), + VPNInputPorts: gosettings.CopySlice(f.VPNInputPorts), + InputPorts: gosettings.CopySlice(f.InputPorts), + OutboundSubnets: gosettings.CopySlice(f.OutboundSubnets), + Enabled: gosettings.CopyPointer(f.Enabled), + Debug: gosettings.CopyPointer(f.Debug), } } @@ -53,27 +54,27 @@ func (f *Firewall) copy() (copied Firewall) { // It merges values of slices together, even if they // are set in the receiver settings. func (f *Firewall) mergeWith(other Firewall) { - f.VPNInputPorts = helpers.MergeSlices(f.VPNInputPorts, other.VPNInputPorts) - f.InputPorts = helpers.MergeSlices(f.InputPorts, other.InputPorts) - f.OutboundSubnets = helpers.MergeSlices(f.OutboundSubnets, other.OutboundSubnets) - f.Enabled = helpers.MergeWithPointer(f.Enabled, other.Enabled) - f.Debug = helpers.MergeWithPointer(f.Debug, other.Debug) + f.VPNInputPorts = gosettings.MergeWithSlice(f.VPNInputPorts, other.VPNInputPorts) + f.InputPorts = gosettings.MergeWithSlice(f.InputPorts, other.InputPorts) + f.OutboundSubnets = gosettings.MergeWithSlice(f.OutboundSubnets, other.OutboundSubnets) + f.Enabled = gosettings.MergeWithPointer(f.Enabled, other.Enabled) + f.Debug = gosettings.MergeWithPointer(f.Debug, other.Debug) } // overrideWith overrides fields of the receiver // settings object with any field set in the other // settings. func (f *Firewall) overrideWith(other Firewall) { - f.VPNInputPorts = helpers.OverrideWithSlice(f.VPNInputPorts, other.VPNInputPorts) - f.InputPorts = helpers.OverrideWithSlice(f.InputPorts, other.InputPorts) - f.OutboundSubnets = helpers.OverrideWithSlice(f.OutboundSubnets, other.OutboundSubnets) - f.Enabled = helpers.OverrideWithPointer(f.Enabled, other.Enabled) - f.Debug = helpers.OverrideWithPointer(f.Debug, other.Debug) + f.VPNInputPorts = gosettings.OverrideWithSlice(f.VPNInputPorts, other.VPNInputPorts) + f.InputPorts = gosettings.OverrideWithSlice(f.InputPorts, other.InputPorts) + f.OutboundSubnets = gosettings.OverrideWithSlice(f.OutboundSubnets, other.OutboundSubnets) + f.Enabled = gosettings.OverrideWithPointer(f.Enabled, other.Enabled) + f.Debug = gosettings.OverrideWithPointer(f.Debug, other.Debug) } func (f *Firewall) setDefaults() { - f.Enabled = helpers.DefaultPointer(f.Enabled, true) - f.Debug = helpers.DefaultPointer(f.Debug, false) + f.Enabled = gosettings.DefaultPointer(f.Enabled, true) + f.Debug = gosettings.DefaultPointer(f.Debug, false) } func (f Firewall) String() string { diff --git a/internal/configuration/settings/health.go b/internal/configuration/settings/health.go index 0c279637..6cdc3106 100644 --- a/internal/configuration/settings/health.go +++ b/internal/configuration/settings/health.go @@ -5,7 +5,7 @@ import ( "os" "time" - "github.com/qdm12/gluetun/internal/configuration/settings/helpers" + "github.com/qdm12/gosettings" "github.com/qdm12/gotree" "github.com/qdm12/govalid/address" ) @@ -65,11 +65,11 @@ func (h *Health) copy() (copied Health) { // MergeWith merges the other settings into any // unset field of the receiver settings object. func (h *Health) MergeWith(other Health) { - h.ServerAddress = helpers.MergeWithString(h.ServerAddress, other.ServerAddress) - h.ReadHeaderTimeout = helpers.MergeWithNumber(h.ReadHeaderTimeout, other.ReadHeaderTimeout) - h.ReadTimeout = helpers.MergeWithNumber(h.ReadTimeout, other.ReadTimeout) - h.TargetAddress = helpers.MergeWithString(h.TargetAddress, other.TargetAddress) - h.SuccessWait = helpers.MergeWithNumber(h.SuccessWait, other.SuccessWait) + h.ServerAddress = gosettings.MergeWithString(h.ServerAddress, other.ServerAddress) + h.ReadHeaderTimeout = gosettings.MergeWithNumber(h.ReadHeaderTimeout, other.ReadHeaderTimeout) + h.ReadTimeout = gosettings.MergeWithNumber(h.ReadTimeout, other.ReadTimeout) + h.TargetAddress = gosettings.MergeWithString(h.TargetAddress, other.TargetAddress) + h.SuccessWait = gosettings.MergeWithNumber(h.SuccessWait, other.SuccessWait) h.VPN.mergeWith(other.VPN) } @@ -77,23 +77,23 @@ func (h *Health) MergeWith(other Health) { // settings object with any field set in the other // settings. func (h *Health) OverrideWith(other Health) { - h.ServerAddress = helpers.OverrideWithString(h.ServerAddress, other.ServerAddress) - h.ReadHeaderTimeout = helpers.OverrideWithNumber(h.ReadHeaderTimeout, other.ReadHeaderTimeout) - h.ReadTimeout = helpers.OverrideWithNumber(h.ReadTimeout, other.ReadTimeout) - h.TargetAddress = helpers.OverrideWithString(h.TargetAddress, other.TargetAddress) - h.SuccessWait = helpers.OverrideWithNumber(h.SuccessWait, other.SuccessWait) + h.ServerAddress = gosettings.OverrideWithString(h.ServerAddress, other.ServerAddress) + h.ReadHeaderTimeout = gosettings.OverrideWithNumber(h.ReadHeaderTimeout, other.ReadHeaderTimeout) + h.ReadTimeout = gosettings.OverrideWithNumber(h.ReadTimeout, other.ReadTimeout) + h.TargetAddress = gosettings.OverrideWithString(h.TargetAddress, other.TargetAddress) + h.SuccessWait = gosettings.OverrideWithNumber(h.SuccessWait, other.SuccessWait) h.VPN.overrideWith(other.VPN) } func (h *Health) SetDefaults() { - h.ServerAddress = helpers.DefaultString(h.ServerAddress, "127.0.0.1:9999") + h.ServerAddress = gosettings.DefaultString(h.ServerAddress, "127.0.0.1:9999") const defaultReadHeaderTimeout = 100 * time.Millisecond - h.ReadHeaderTimeout = helpers.DefaultNumber(h.ReadHeaderTimeout, defaultReadHeaderTimeout) + h.ReadHeaderTimeout = gosettings.DefaultNumber(h.ReadHeaderTimeout, defaultReadHeaderTimeout) const defaultReadTimeout = 500 * time.Millisecond - h.ReadTimeout = helpers.DefaultNumber(h.ReadTimeout, defaultReadTimeout) - h.TargetAddress = helpers.DefaultString(h.TargetAddress, "cloudflare.com:443") + h.ReadTimeout = gosettings.DefaultNumber(h.ReadTimeout, defaultReadTimeout) + h.TargetAddress = gosettings.DefaultString(h.TargetAddress, "cloudflare.com:443") const defaultSuccessWait = 5 * time.Second - h.SuccessWait = helpers.DefaultNumber(h.SuccessWait, defaultSuccessWait) + h.SuccessWait = gosettings.DefaultNumber(h.SuccessWait, defaultSuccessWait) h.VPN.setDefaults() } diff --git a/internal/configuration/settings/healthywait.go b/internal/configuration/settings/healthywait.go index 28bc9888..2a23dc88 100644 --- a/internal/configuration/settings/healthywait.go +++ b/internal/configuration/settings/healthywait.go @@ -3,7 +3,7 @@ package settings import ( "time" - "github.com/qdm12/gluetun/internal/configuration/settings/helpers" + "github.com/qdm12/gosettings" "github.com/qdm12/gotree" ) @@ -27,31 +27,31 @@ func (h HealthyWait) validate() (err error) { // unset field of the receiver settings object. func (h *HealthyWait) copy() (copied HealthyWait) { return HealthyWait{ - Initial: helpers.CopyPointer(h.Initial), - Addition: helpers.CopyPointer(h.Addition), + Initial: gosettings.CopyPointer(h.Initial), + Addition: gosettings.CopyPointer(h.Addition), } } // mergeWith merges the other settings into any // unset field of the receiver settings object. func (h *HealthyWait) mergeWith(other HealthyWait) { - h.Initial = helpers.MergeWithPointer(h.Initial, other.Initial) - h.Addition = helpers.MergeWithPointer(h.Addition, other.Addition) + h.Initial = gosettings.MergeWithPointer(h.Initial, other.Initial) + h.Addition = gosettings.MergeWithPointer(h.Addition, other.Addition) } // overrideWith overrides fields of the receiver // settings object with any field set in the other // settings. func (h *HealthyWait) overrideWith(other HealthyWait) { - h.Initial = helpers.OverrideWithPointer(h.Initial, other.Initial) - h.Addition = helpers.OverrideWithPointer(h.Addition, other.Addition) + h.Initial = gosettings.OverrideWithPointer(h.Initial, other.Initial) + h.Addition = gosettings.OverrideWithPointer(h.Addition, other.Addition) } func (h *HealthyWait) setDefaults() { const initialDurationDefault = 6 * time.Second const additionDurationDefault = 5 * time.Second - h.Initial = helpers.DefaultPointer(h.Initial, initialDurationDefault) - h.Addition = helpers.DefaultPointer(h.Addition, additionDurationDefault) + h.Initial = gosettings.DefaultPointer(h.Initial, initialDurationDefault) + h.Addition = gosettings.DefaultPointer(h.Addition, additionDurationDefault) } func (h HealthyWait) String() string { diff --git a/internal/configuration/settings/helpers/belong.go b/internal/configuration/settings/helpers/belong.go index 899c1dc8..4978ae31 100644 --- a/internal/configuration/settings/helpers/belong.go +++ b/internal/configuration/settings/helpers/belong.go @@ -1,12 +1,6 @@ package helpers -import ( - "errors" - "fmt" - "strings" -) - -func IsOneOf(value string, choices ...string) (ok bool) { +func IsOneOf[T comparable](value T, choices ...T) (ok bool) { for _, choice := range choices { if value == choice { return true @@ -14,39 +8,3 @@ func IsOneOf(value string, choices ...string) (ok bool) { } return false } - -var ( - ErrNoChoice = errors.New("one or more values is set but there is no possible value available") - ErrValueNotOneOf = errors.New("value is not one of the possible choices") -) - -func AreAllOneOf(values, choices []string) (err error) { - if len(values) > 0 && len(choices) == 0 { - return fmt.Errorf("%w", ErrNoChoice) - } - - set := make(map[string]struct{}, len(choices)) - for _, choice := range choices { - choice = strings.ToLower(choice) - set[choice] = struct{}{} - } - - for _, value := range values { - _, ok := set[value] - if !ok { - return fmt.Errorf("%w: value %q, choices available are %s", - ErrValueNotOneOf, value, strings.Join(choices, ", ")) - } - } - - return nil -} - -func Uint16IsOneOf(port uint16, choices []uint16) (ok bool) { - for _, choice := range choices { - if port == choice { - return true - } - } - return false -} diff --git a/internal/configuration/settings/helpers/default.go b/internal/configuration/settings/helpers/default.go index 91b94e3c..bfa27d0b 100644 --- a/internal/configuration/settings/helpers/default.go +++ b/internal/configuration/settings/helpers/default.go @@ -30,7 +30,7 @@ func DefaultNumber[T Number](existing T, defaultValue T) ( //nolint:ireturn return defaultValue } -func DefaultIP(existing netip.Addr, defaultValue netip.Addr) ( +func DefaultValidator(existing netip.Addr, defaultValue netip.Addr) ( result netip.Addr) { if existing.IsValid() { return existing diff --git a/internal/configuration/settings/helpers/merge.go b/internal/configuration/settings/helpers/merge.go index 17d096fe..58ac8e14 100644 --- a/internal/configuration/settings/helpers/merge.go +++ b/internal/configuration/settings/helpers/merge.go @@ -30,7 +30,7 @@ func MergeWithNumber[T Number](existing, other T) (result T) { //nolint:ireturn return other } -func MergeWithIP(existing, other netip.Addr) (result netip.Addr) { +func MergeWithValidator(existing, other netip.Addr) (result netip.Addr) { if existing.IsValid() { return existing } @@ -44,7 +44,7 @@ func MergeWithHTTPHandler(existing, other http.Handler) (result http.Handler) { return other } -func MergeSlices[T comparable](a, b []T) (result []T) { +func MergeWithSlice[T comparable](a, b []T) (result []T) { if a == nil && b == nil { return nil } diff --git a/internal/configuration/settings/helpers/override.go b/internal/configuration/settings/helpers/override.go index 8bc45446..81630d58 100644 --- a/internal/configuration/settings/helpers/override.go +++ b/internal/configuration/settings/helpers/override.go @@ -28,7 +28,7 @@ func OverrideWithNumber[T Number](existing, other T) (result T) { //nolint:iretu return other } -func OverrideWithIP(existing, other netip.Addr) (result netip.Addr) { +func OverrideWithValidator(existing, other netip.Addr) (result netip.Addr) { if !other.IsValid() { return existing } diff --git a/internal/configuration/settings/httpproxy.go b/internal/configuration/settings/httpproxy.go index 15954091..50053eae 100644 --- a/internal/configuration/settings/httpproxy.go +++ b/internal/configuration/settings/httpproxy.go @@ -6,6 +6,7 @@ import ( "time" "github.com/qdm12/gluetun/internal/configuration/settings/helpers" + "github.com/qdm12/gosettings" "github.com/qdm12/gotree" "github.com/qdm12/govalid/address" ) @@ -56,12 +57,12 @@ func (h HTTPProxy) validate() (err error) { func (h *HTTPProxy) copy() (copied HTTPProxy) { return HTTPProxy{ - User: helpers.CopyPointer(h.User), - Password: helpers.CopyPointer(h.Password), + User: gosettings.CopyPointer(h.User), + Password: gosettings.CopyPointer(h.Password), ListeningAddress: h.ListeningAddress, - Enabled: helpers.CopyPointer(h.Enabled), - Stealth: helpers.CopyPointer(h.Stealth), - Log: helpers.CopyPointer(h.Log), + Enabled: gosettings.CopyPointer(h.Enabled), + Stealth: gosettings.CopyPointer(h.Stealth), + Log: gosettings.CopyPointer(h.Log), ReadHeaderTimeout: h.ReadHeaderTimeout, ReadTimeout: h.ReadTimeout, } @@ -70,41 +71,41 @@ func (h *HTTPProxy) copy() (copied HTTPProxy) { // mergeWith merges the other settings into any // unset field of the receiver settings object. func (h *HTTPProxy) mergeWith(other HTTPProxy) { - h.User = helpers.MergeWithPointer(h.User, other.User) - h.Password = helpers.MergeWithPointer(h.Password, other.Password) - h.ListeningAddress = helpers.MergeWithString(h.ListeningAddress, other.ListeningAddress) - h.Enabled = helpers.MergeWithPointer(h.Enabled, other.Enabled) - h.Stealth = helpers.MergeWithPointer(h.Stealth, other.Stealth) - h.Log = helpers.MergeWithPointer(h.Log, other.Log) - h.ReadHeaderTimeout = helpers.MergeWithNumber(h.ReadHeaderTimeout, other.ReadHeaderTimeout) - h.ReadTimeout = helpers.MergeWithNumber(h.ReadTimeout, other.ReadTimeout) + h.User = gosettings.MergeWithPointer(h.User, other.User) + h.Password = gosettings.MergeWithPointer(h.Password, other.Password) + h.ListeningAddress = gosettings.MergeWithString(h.ListeningAddress, other.ListeningAddress) + h.Enabled = gosettings.MergeWithPointer(h.Enabled, other.Enabled) + h.Stealth = gosettings.MergeWithPointer(h.Stealth, other.Stealth) + h.Log = gosettings.MergeWithPointer(h.Log, other.Log) + h.ReadHeaderTimeout = gosettings.MergeWithNumber(h.ReadHeaderTimeout, other.ReadHeaderTimeout) + h.ReadTimeout = gosettings.MergeWithNumber(h.ReadTimeout, other.ReadTimeout) } // overrideWith overrides fields of the receiver // settings object with any field set in the other // settings. func (h *HTTPProxy) overrideWith(other HTTPProxy) { - h.User = helpers.OverrideWithPointer(h.User, other.User) - h.Password = helpers.OverrideWithPointer(h.Password, other.Password) - h.ListeningAddress = helpers.OverrideWithString(h.ListeningAddress, other.ListeningAddress) - h.Enabled = helpers.OverrideWithPointer(h.Enabled, other.Enabled) - h.Stealth = helpers.OverrideWithPointer(h.Stealth, other.Stealth) - h.Log = helpers.OverrideWithPointer(h.Log, other.Log) - h.ReadHeaderTimeout = helpers.OverrideWithNumber(h.ReadHeaderTimeout, other.ReadHeaderTimeout) - h.ReadTimeout = helpers.OverrideWithNumber(h.ReadTimeout, other.ReadTimeout) + h.User = gosettings.OverrideWithPointer(h.User, other.User) + h.Password = gosettings.OverrideWithPointer(h.Password, other.Password) + h.ListeningAddress = gosettings.OverrideWithString(h.ListeningAddress, other.ListeningAddress) + h.Enabled = gosettings.OverrideWithPointer(h.Enabled, other.Enabled) + h.Stealth = gosettings.OverrideWithPointer(h.Stealth, other.Stealth) + h.Log = gosettings.OverrideWithPointer(h.Log, other.Log) + h.ReadHeaderTimeout = gosettings.OverrideWithNumber(h.ReadHeaderTimeout, other.ReadHeaderTimeout) + h.ReadTimeout = gosettings.OverrideWithNumber(h.ReadTimeout, other.ReadTimeout) } func (h *HTTPProxy) setDefaults() { - h.User = helpers.DefaultPointer(h.User, "") - h.Password = helpers.DefaultPointer(h.Password, "") - h.ListeningAddress = helpers.DefaultString(h.ListeningAddress, ":8888") - h.Enabled = helpers.DefaultPointer(h.Enabled, false) - h.Stealth = helpers.DefaultPointer(h.Stealth, false) - h.Log = helpers.DefaultPointer(h.Log, false) + h.User = gosettings.DefaultPointer(h.User, "") + h.Password = gosettings.DefaultPointer(h.Password, "") + h.ListeningAddress = gosettings.DefaultString(h.ListeningAddress, ":8888") + h.Enabled = gosettings.DefaultPointer(h.Enabled, false) + h.Stealth = gosettings.DefaultPointer(h.Stealth, false) + h.Log = gosettings.DefaultPointer(h.Log, false) const defaultReadHeaderTimeout = time.Second - h.ReadHeaderTimeout = helpers.DefaultNumber(h.ReadHeaderTimeout, defaultReadHeaderTimeout) + h.ReadHeaderTimeout = gosettings.DefaultNumber(h.ReadHeaderTimeout, defaultReadHeaderTimeout) const defaultReadTimeout = 3 * time.Second - h.ReadTimeout = helpers.DefaultNumber(h.ReadTimeout, defaultReadTimeout) + h.ReadTimeout = gosettings.DefaultNumber(h.ReadTimeout, defaultReadTimeout) } func (h HTTPProxy) String() string { diff --git a/internal/configuration/settings/log.go b/internal/configuration/settings/log.go index 0f0269eb..68135576 100644 --- a/internal/configuration/settings/log.go +++ b/internal/configuration/settings/log.go @@ -1,7 +1,7 @@ package settings import ( - "github.com/qdm12/gluetun/internal/configuration/settings/helpers" + "github.com/qdm12/gosettings" "github.com/qdm12/gotree" "github.com/qdm12/log" ) @@ -19,25 +19,25 @@ func (l Log) validate() (err error) { func (l *Log) copy() (copied Log) { return Log{ - Level: helpers.CopyPointer(l.Level), + Level: gosettings.CopyPointer(l.Level), } } // mergeWith merges the other settings into any // unset field of the receiver settings object. func (l *Log) mergeWith(other Log) { - l.Level = helpers.MergeWithPointer(l.Level, other.Level) + l.Level = gosettings.MergeWithPointer(l.Level, other.Level) } // overrideWith overrides fields of the receiver // settings object with any field set in the other // settings. func (l *Log) overrideWith(other Log) { - l.Level = helpers.OverrideWithPointer(l.Level, other.Level) + l.Level = gosettings.OverrideWithPointer(l.Level, other.Level) } func (l *Log) setDefaults() { - l.Level = helpers.DefaultPointer(l.Level, log.LevelInfo) + l.Level = gosettings.DefaultPointer(l.Level, log.LevelInfo) } func (l Log) String() string { diff --git a/internal/configuration/settings/openvpn.go b/internal/configuration/settings/openvpn.go index 8fe6435a..b1afb538 100644 --- a/internal/configuration/settings/openvpn.go +++ b/internal/configuration/settings/openvpn.go @@ -4,13 +4,14 @@ import ( "encoding/base64" "fmt" "regexp" - "strings" "github.com/qdm12/gluetun/internal/configuration/settings/helpers" "github.com/qdm12/gluetun/internal/constants/openvpn" "github.com/qdm12/gluetun/internal/constants/providers" "github.com/qdm12/gluetun/internal/openvpn/extract" "github.com/qdm12/gluetun/internal/provider/privateinternetaccess/presets" + "github.com/qdm12/gosettings" + "github.com/qdm12/gosettings/validate" "github.com/qdm12/gotree" ) @@ -89,9 +90,8 @@ var ivpnAccountID = regexp.MustCompile(`^(i|ivpn)\-[a-zA-Z0-9]{4}\-[a-zA-Z0-9]{4 func (o OpenVPN) validate(vpnProvider string) (err error) { // Validate version validVersions := []string{openvpn.Openvpn25, openvpn.Openvpn26} - if !helpers.IsOneOf(o.Version, validVersions...) { - return fmt.Errorf("%w: %q can only be one of %s", - ErrOpenVPNVersionIsNotValid, o.Version, strings.Join(validVersions, ", ")) + if err = validate.IsOneOf(o.Version, validVersions...); err != nil { + return fmt.Errorf("%w: %w", ErrOpenVPNVersionIsNotValid, err) } isCustom := vpnProvider == providers.Custom @@ -244,92 +244,92 @@ func validateOpenVPNEncryptedKey(vpnProvider, func (o *OpenVPN) copy() (copied OpenVPN) { return OpenVPN{ Version: o.Version, - User: helpers.CopyPointer(o.User), - Password: helpers.CopyPointer(o.Password), - ConfFile: helpers.CopyPointer(o.ConfFile), - Ciphers: helpers.CopySlice(o.Ciphers), - Auth: helpers.CopyPointer(o.Auth), - Cert: helpers.CopyPointer(o.Cert), - Key: helpers.CopyPointer(o.Key), - EncryptedKey: helpers.CopyPointer(o.EncryptedKey), - KeyPassphrase: helpers.CopyPointer(o.KeyPassphrase), - PIAEncPreset: helpers.CopyPointer(o.PIAEncPreset), - MSSFix: helpers.CopyPointer(o.MSSFix), + User: gosettings.CopyPointer(o.User), + Password: gosettings.CopyPointer(o.Password), + ConfFile: gosettings.CopyPointer(o.ConfFile), + Ciphers: gosettings.CopySlice(o.Ciphers), + Auth: gosettings.CopyPointer(o.Auth), + Cert: gosettings.CopyPointer(o.Cert), + Key: gosettings.CopyPointer(o.Key), + EncryptedKey: gosettings.CopyPointer(o.EncryptedKey), + KeyPassphrase: gosettings.CopyPointer(o.KeyPassphrase), + PIAEncPreset: gosettings.CopyPointer(o.PIAEncPreset), + MSSFix: gosettings.CopyPointer(o.MSSFix), Interface: o.Interface, ProcessUser: o.ProcessUser, - Verbosity: helpers.CopyPointer(o.Verbosity), - Flags: helpers.CopySlice(o.Flags), + Verbosity: gosettings.CopyPointer(o.Verbosity), + Flags: gosettings.CopySlice(o.Flags), } } // mergeWith merges the other settings into any // unset field of the receiver settings object. func (o *OpenVPN) mergeWith(other OpenVPN) { - o.Version = helpers.MergeWithString(o.Version, other.Version) - o.User = helpers.MergeWithPointer(o.User, other.User) - o.Password = helpers.MergeWithPointer(o.Password, other.Password) - o.ConfFile = helpers.MergeWithPointer(o.ConfFile, other.ConfFile) - o.Ciphers = helpers.MergeSlices(o.Ciphers, other.Ciphers) - o.Auth = helpers.MergeWithPointer(o.Auth, other.Auth) - o.Cert = helpers.MergeWithPointer(o.Cert, other.Cert) - o.Key = helpers.MergeWithPointer(o.Key, other.Key) - o.EncryptedKey = helpers.MergeWithPointer(o.EncryptedKey, other.EncryptedKey) - o.KeyPassphrase = helpers.MergeWithPointer(o.KeyPassphrase, other.KeyPassphrase) - o.PIAEncPreset = helpers.MergeWithPointer(o.PIAEncPreset, other.PIAEncPreset) - o.MSSFix = helpers.MergeWithPointer(o.MSSFix, other.MSSFix) - o.Interface = helpers.MergeWithString(o.Interface, other.Interface) - o.ProcessUser = helpers.MergeWithString(o.ProcessUser, other.ProcessUser) - o.Verbosity = helpers.MergeWithPointer(o.Verbosity, other.Verbosity) - o.Flags = helpers.MergeSlices(o.Flags, other.Flags) + o.Version = gosettings.MergeWithString(o.Version, other.Version) + o.User = gosettings.MergeWithPointer(o.User, other.User) + o.Password = gosettings.MergeWithPointer(o.Password, other.Password) + o.ConfFile = gosettings.MergeWithPointer(o.ConfFile, other.ConfFile) + o.Ciphers = gosettings.MergeWithSlice(o.Ciphers, other.Ciphers) + o.Auth = gosettings.MergeWithPointer(o.Auth, other.Auth) + o.Cert = gosettings.MergeWithPointer(o.Cert, other.Cert) + o.Key = gosettings.MergeWithPointer(o.Key, other.Key) + o.EncryptedKey = gosettings.MergeWithPointer(o.EncryptedKey, other.EncryptedKey) + o.KeyPassphrase = gosettings.MergeWithPointer(o.KeyPassphrase, other.KeyPassphrase) + o.PIAEncPreset = gosettings.MergeWithPointer(o.PIAEncPreset, other.PIAEncPreset) + o.MSSFix = gosettings.MergeWithPointer(o.MSSFix, other.MSSFix) + o.Interface = gosettings.MergeWithString(o.Interface, other.Interface) + o.ProcessUser = gosettings.MergeWithString(o.ProcessUser, other.ProcessUser) + o.Verbosity = gosettings.MergeWithPointer(o.Verbosity, other.Verbosity) + o.Flags = gosettings.MergeWithSlice(o.Flags, other.Flags) } // overrideWith overrides fields of the receiver // settings object with any field set in the other // settings. func (o *OpenVPN) overrideWith(other OpenVPN) { - o.Version = helpers.OverrideWithString(o.Version, other.Version) - o.User = helpers.OverrideWithPointer(o.User, other.User) - o.Password = helpers.OverrideWithPointer(o.Password, other.Password) - o.ConfFile = helpers.OverrideWithPointer(o.ConfFile, other.ConfFile) - o.Ciphers = helpers.OverrideWithSlice(o.Ciphers, other.Ciphers) - o.Auth = helpers.OverrideWithPointer(o.Auth, other.Auth) - o.Cert = helpers.OverrideWithPointer(o.Cert, other.Cert) - o.Key = helpers.OverrideWithPointer(o.Key, other.Key) - o.EncryptedKey = helpers.OverrideWithPointer(o.EncryptedKey, other.EncryptedKey) - o.KeyPassphrase = helpers.OverrideWithPointer(o.KeyPassphrase, other.KeyPassphrase) - o.PIAEncPreset = helpers.OverrideWithPointer(o.PIAEncPreset, other.PIAEncPreset) - o.MSSFix = helpers.OverrideWithPointer(o.MSSFix, other.MSSFix) - o.Interface = helpers.OverrideWithString(o.Interface, other.Interface) - o.ProcessUser = helpers.OverrideWithString(o.ProcessUser, other.ProcessUser) - o.Verbosity = helpers.OverrideWithPointer(o.Verbosity, other.Verbosity) - o.Flags = helpers.OverrideWithSlice(o.Flags, other.Flags) + o.Version = gosettings.OverrideWithString(o.Version, other.Version) + o.User = gosettings.OverrideWithPointer(o.User, other.User) + o.Password = gosettings.OverrideWithPointer(o.Password, other.Password) + o.ConfFile = gosettings.OverrideWithPointer(o.ConfFile, other.ConfFile) + o.Ciphers = gosettings.OverrideWithSlice(o.Ciphers, other.Ciphers) + o.Auth = gosettings.OverrideWithPointer(o.Auth, other.Auth) + o.Cert = gosettings.OverrideWithPointer(o.Cert, other.Cert) + o.Key = gosettings.OverrideWithPointer(o.Key, other.Key) + o.EncryptedKey = gosettings.OverrideWithPointer(o.EncryptedKey, other.EncryptedKey) + o.KeyPassphrase = gosettings.OverrideWithPointer(o.KeyPassphrase, other.KeyPassphrase) + o.PIAEncPreset = gosettings.OverrideWithPointer(o.PIAEncPreset, other.PIAEncPreset) + o.MSSFix = gosettings.OverrideWithPointer(o.MSSFix, other.MSSFix) + o.Interface = gosettings.OverrideWithString(o.Interface, other.Interface) + o.ProcessUser = gosettings.OverrideWithString(o.ProcessUser, other.ProcessUser) + o.Verbosity = gosettings.OverrideWithPointer(o.Verbosity, other.Verbosity) + o.Flags = gosettings.OverrideWithSlice(o.Flags, other.Flags) } func (o *OpenVPN) setDefaults(vpnProvider string) { - o.Version = helpers.DefaultString(o.Version, openvpn.Openvpn25) - o.User = helpers.DefaultPointer(o.User, "") + o.Version = gosettings.DefaultString(o.Version, openvpn.Openvpn25) + o.User = gosettings.DefaultPointer(o.User, "") if vpnProvider == providers.Mullvad { - o.Password = helpers.DefaultPointer(o.Password, "m") + o.Password = gosettings.DefaultPointer(o.Password, "m") } else { - o.Password = helpers.DefaultPointer(o.Password, "") + o.Password = gosettings.DefaultPointer(o.Password, "") } - o.ConfFile = helpers.DefaultPointer(o.ConfFile, "") - o.Auth = helpers.DefaultPointer(o.Auth, "") - o.Cert = helpers.DefaultPointer(o.Cert, "") - o.Key = helpers.DefaultPointer(o.Key, "") - o.EncryptedKey = helpers.DefaultPointer(o.EncryptedKey, "") - o.KeyPassphrase = helpers.DefaultPointer(o.KeyPassphrase, "") + o.ConfFile = gosettings.DefaultPointer(o.ConfFile, "") + o.Auth = gosettings.DefaultPointer(o.Auth, "") + o.Cert = gosettings.DefaultPointer(o.Cert, "") + o.Key = gosettings.DefaultPointer(o.Key, "") + o.EncryptedKey = gosettings.DefaultPointer(o.EncryptedKey, "") + o.KeyPassphrase = gosettings.DefaultPointer(o.KeyPassphrase, "") var defaultEncPreset string if vpnProvider == providers.PrivateInternetAccess { defaultEncPreset = presets.Strong } - o.PIAEncPreset = helpers.DefaultPointer(o.PIAEncPreset, defaultEncPreset) - o.MSSFix = helpers.DefaultPointer(o.MSSFix, 0) - o.Interface = helpers.DefaultString(o.Interface, "tun0") - o.ProcessUser = helpers.DefaultString(o.ProcessUser, "root") - o.Verbosity = helpers.DefaultPointer(o.Verbosity, 1) + o.PIAEncPreset = gosettings.DefaultPointer(o.PIAEncPreset, defaultEncPreset) + o.MSSFix = gosettings.DefaultPointer(o.MSSFix, 0) + o.Interface = gosettings.DefaultString(o.Interface, "tun0") + o.ProcessUser = gosettings.DefaultString(o.ProcessUser, "root") + o.Verbosity = gosettings.DefaultPointer(o.Verbosity, 1) } func (o OpenVPN) String() string { diff --git a/internal/configuration/settings/openvpnselection.go b/internal/configuration/settings/openvpnselection.go index 5b4b89bb..6e8e9dfc 100644 --- a/internal/configuration/settings/openvpnselection.go +++ b/internal/configuration/settings/openvpnselection.go @@ -6,6 +6,8 @@ import ( "github.com/qdm12/gluetun/internal/configuration/settings/helpers" "github.com/qdm12/gluetun/internal/constants/providers" "github.com/qdm12/gluetun/internal/provider/privateinternetaccess/presets" + "github.com/qdm12/gosettings" + "github.com/qdm12/gosettings/validate" "github.com/qdm12/gotree" ) @@ -99,14 +101,14 @@ func (o OpenVPNSelection) validate(vpnProvider string) (err error) { allowedUDP = []uint16{53, 80, 123, 443, 1194, 54783} } - if *o.TCP && !helpers.Uint16IsOneOf(*o.CustomPort, allowedTCP) { - return fmt.Errorf("%w: %d for VPN service provider %s; %s", - ErrOpenVPNCustomPortNotAllowed, o.CustomPort, vpnProvider, - helpers.PortChoicesOrString(allowedTCP)) - } else if !*o.TCP && !helpers.Uint16IsOneOf(*o.CustomPort, allowedUDP) { - return fmt.Errorf("%w: %d for VPN service provider %s; %s", - ErrOpenVPNCustomPortNotAllowed, o.CustomPort, vpnProvider, - helpers.PortChoicesOrString(allowedUDP)) + allowedPorts := allowedUDP + if *o.TCP { + allowedPorts = allowedTCP + } + err = validate.IsOneOf(*o.CustomPort, allowedPorts...) + if err != nil { + return fmt.Errorf("%w: for VPN service provider %s: %w", + ErrOpenVPNCustomPortNotAllowed, vpnProvider, err) } } } @@ -118,10 +120,8 @@ func (o OpenVPNSelection) validate(vpnProvider string) (err error) { presets.Normal, presets.Strong, } - if !helpers.IsOneOf(*o.PIAEncPreset, validEncryptionPresets...) { - return fmt.Errorf("%w: %s; valid presets are %s", - ErrOpenVPNEncryptionPresetNotValid, *o.PIAEncPreset, - helpers.ChoicesOrString(validEncryptionPresets)) + if err = validate.IsOneOf(*o.PIAEncPreset, validEncryptionPresets...); err != nil { + return fmt.Errorf("%w: %w", ErrOpenVPNEncryptionPresetNotValid, err) } } @@ -130,37 +130,37 @@ func (o OpenVPNSelection) validate(vpnProvider string) (err error) { func (o *OpenVPNSelection) copy() (copied OpenVPNSelection) { return OpenVPNSelection{ - ConfFile: helpers.CopyPointer(o.ConfFile), - TCP: helpers.CopyPointer(o.TCP), - CustomPort: helpers.CopyPointer(o.CustomPort), - PIAEncPreset: helpers.CopyPointer(o.PIAEncPreset), + ConfFile: gosettings.CopyPointer(o.ConfFile), + TCP: gosettings.CopyPointer(o.TCP), + CustomPort: gosettings.CopyPointer(o.CustomPort), + PIAEncPreset: gosettings.CopyPointer(o.PIAEncPreset), } } func (o *OpenVPNSelection) mergeWith(other OpenVPNSelection) { - o.ConfFile = helpers.MergeWithPointer(o.ConfFile, other.ConfFile) - o.TCP = helpers.MergeWithPointer(o.TCP, other.TCP) - o.CustomPort = helpers.MergeWithPointer(o.CustomPort, other.CustomPort) - o.PIAEncPreset = helpers.MergeWithPointer(o.PIAEncPreset, other.PIAEncPreset) + o.ConfFile = gosettings.MergeWithPointer(o.ConfFile, other.ConfFile) + o.TCP = gosettings.MergeWithPointer(o.TCP, other.TCP) + o.CustomPort = gosettings.MergeWithPointer(o.CustomPort, other.CustomPort) + o.PIAEncPreset = gosettings.MergeWithPointer(o.PIAEncPreset, other.PIAEncPreset) } func (o *OpenVPNSelection) overrideWith(other OpenVPNSelection) { - o.ConfFile = helpers.OverrideWithPointer(o.ConfFile, other.ConfFile) - o.TCP = helpers.OverrideWithPointer(o.TCP, other.TCP) - o.CustomPort = helpers.OverrideWithPointer(o.CustomPort, other.CustomPort) - o.PIAEncPreset = helpers.OverrideWithPointer(o.PIAEncPreset, other.PIAEncPreset) + o.ConfFile = gosettings.OverrideWithPointer(o.ConfFile, other.ConfFile) + o.TCP = gosettings.OverrideWithPointer(o.TCP, other.TCP) + o.CustomPort = gosettings.OverrideWithPointer(o.CustomPort, other.CustomPort) + o.PIAEncPreset = gosettings.OverrideWithPointer(o.PIAEncPreset, other.PIAEncPreset) } func (o *OpenVPNSelection) setDefaults(vpnProvider string) { - o.ConfFile = helpers.DefaultPointer(o.ConfFile, "") - o.TCP = helpers.DefaultPointer(o.TCP, false) - o.CustomPort = helpers.DefaultPointer(o.CustomPort, 0) + o.ConfFile = gosettings.DefaultPointer(o.ConfFile, "") + o.TCP = gosettings.DefaultPointer(o.TCP, false) + o.CustomPort = gosettings.DefaultPointer(o.CustomPort, 0) var defaultEncPreset string if vpnProvider == providers.PrivateInternetAccess { defaultEncPreset = presets.Strong } - o.PIAEncPreset = helpers.DefaultPointer(o.PIAEncPreset, defaultEncPreset) + o.PIAEncPreset = gosettings.DefaultPointer(o.PIAEncPreset, defaultEncPreset) } func (o OpenVPNSelection) String() string { diff --git a/internal/configuration/settings/portforward.go b/internal/configuration/settings/portforward.go index 8de12c20..58e4a46e 100644 --- a/internal/configuration/settings/portforward.go +++ b/internal/configuration/settings/portforward.go @@ -3,10 +3,10 @@ package settings import ( "fmt" "path/filepath" - "strings" - "github.com/qdm12/gluetun/internal/configuration/settings/helpers" "github.com/qdm12/gluetun/internal/constants/providers" + "github.com/qdm12/gosettings" + "github.com/qdm12/gosettings/validate" "github.com/qdm12/gotree" ) @@ -29,9 +29,8 @@ func (p PortForwarding) validate(vpnProvider string) (err error) { // Validate Enabled validProviders := []string{providers.PrivateInternetAccess} - if !helpers.IsOneOf(vpnProvider, validProviders...) { - return fmt.Errorf("%w: for provider %s, it is only available for %s", - ErrPortForwardingEnabled, vpnProvider, strings.Join(validProviders, ", ")) + if err = validate.IsOneOf(vpnProvider, validProviders...); err != nil { + return fmt.Errorf("%w: %w", ErrPortForwardingEnabled, err) } // Validate Filepath @@ -47,24 +46,24 @@ func (p PortForwarding) validate(vpnProvider string) (err error) { func (p *PortForwarding) copy() (copied PortForwarding) { return PortForwarding{ - Enabled: helpers.CopyPointer(p.Enabled), - Filepath: helpers.CopyPointer(p.Filepath), + Enabled: gosettings.CopyPointer(p.Enabled), + Filepath: gosettings.CopyPointer(p.Filepath), } } func (p *PortForwarding) mergeWith(other PortForwarding) { - p.Enabled = helpers.MergeWithPointer(p.Enabled, other.Enabled) - p.Filepath = helpers.MergeWithPointer(p.Filepath, other.Filepath) + p.Enabled = gosettings.MergeWithPointer(p.Enabled, other.Enabled) + p.Filepath = gosettings.MergeWithPointer(p.Filepath, other.Filepath) } func (p *PortForwarding) overrideWith(other PortForwarding) { - p.Enabled = helpers.OverrideWithPointer(p.Enabled, other.Enabled) - p.Filepath = helpers.OverrideWithPointer(p.Filepath, other.Filepath) + p.Enabled = gosettings.OverrideWithPointer(p.Enabled, other.Enabled) + p.Filepath = gosettings.OverrideWithPointer(p.Filepath, other.Filepath) } func (p *PortForwarding) setDefaults() { - p.Enabled = helpers.DefaultPointer(p.Enabled, false) - p.Filepath = helpers.DefaultPointer(p.Filepath, "/tmp/gluetun/forwarded_port") + p.Enabled = gosettings.DefaultPointer(p.Enabled, false) + p.Filepath = gosettings.DefaultPointer(p.Filepath, "/tmp/gluetun/forwarded_port") } func (p PortForwarding) String() string { diff --git a/internal/configuration/settings/provider.go b/internal/configuration/settings/provider.go index 5d5be6f4..81002071 100644 --- a/internal/configuration/settings/provider.go +++ b/internal/configuration/settings/provider.go @@ -3,9 +3,10 @@ package settings import ( "fmt" - "github.com/qdm12/gluetun/internal/configuration/settings/helpers" "github.com/qdm12/gluetun/internal/constants/providers" "github.com/qdm12/gluetun/internal/constants/vpn" + "github.com/qdm12/gosettings" + "github.com/qdm12/gosettings/validate" "github.com/qdm12/gotree" ) @@ -38,9 +39,8 @@ func (p *Provider) validate(vpnType string, storage Storage) (err error) { providers.Windscribe, } } - if !helpers.IsOneOf(*p.Name, validNames...) { - return fmt.Errorf("%w for Wireguard: %q can only be one of %s", - ErrVPNProviderNameNotValid, *p.Name, helpers.ChoicesOrString(validNames)) + if err = validate.IsOneOf(*p.Name, validNames...); err != nil { + return fmt.Errorf("%w for Wireguard: %w", ErrVPNProviderNameNotValid, err) } err = p.ServerSelection.validate(*p.Name, storage) @@ -58,26 +58,26 @@ func (p *Provider) validate(vpnType string, storage Storage) (err error) { func (p *Provider) copy() (copied Provider) { return Provider{ - Name: helpers.CopyPointer(p.Name), + Name: gosettings.CopyPointer(p.Name), ServerSelection: p.ServerSelection.copy(), PortForwarding: p.PortForwarding.copy(), } } func (p *Provider) mergeWith(other Provider) { - p.Name = helpers.MergeWithPointer(p.Name, other.Name) + p.Name = gosettings.MergeWithPointer(p.Name, other.Name) p.ServerSelection.mergeWith(other.ServerSelection) p.PortForwarding.mergeWith(other.PortForwarding) } func (p *Provider) overrideWith(other Provider) { - p.Name = helpers.OverrideWithPointer(p.Name, other.Name) + p.Name = gosettings.OverrideWithPointer(p.Name, other.Name) p.ServerSelection.overrideWith(other.ServerSelection) p.PortForwarding.overrideWith(other.PortForwarding) } func (p *Provider) setDefaults() { - p.Name = helpers.DefaultPointer(p.Name, providers.PrivateInternetAccess) + p.Name = gosettings.DefaultPointer(p.Name, providers.PrivateInternetAccess) p.ServerSelection.setDefaults(*p.Name) p.PortForwarding.setDefaults() } diff --git a/internal/configuration/settings/publicip.go b/internal/configuration/settings/publicip.go index 6ec8ece6..55f6a8f1 100644 --- a/internal/configuration/settings/publicip.go +++ b/internal/configuration/settings/publicip.go @@ -5,7 +5,7 @@ import ( "path/filepath" "time" - "github.com/qdm12/gluetun/internal/configuration/settings/helpers" + "github.com/qdm12/gosettings" "github.com/qdm12/gotree" ) @@ -42,25 +42,25 @@ func (p PublicIP) validate() (err error) { func (p *PublicIP) copy() (copied PublicIP) { return PublicIP{ - Period: helpers.CopyPointer(p.Period), - IPFilepath: helpers.CopyPointer(p.IPFilepath), + Period: gosettings.CopyPointer(p.Period), + IPFilepath: gosettings.CopyPointer(p.IPFilepath), } } func (p *PublicIP) mergeWith(other PublicIP) { - p.Period = helpers.MergeWithPointer(p.Period, other.Period) - p.IPFilepath = helpers.MergeWithPointer(p.IPFilepath, other.IPFilepath) + p.Period = gosettings.MergeWithPointer(p.Period, other.Period) + p.IPFilepath = gosettings.MergeWithPointer(p.IPFilepath, other.IPFilepath) } func (p *PublicIP) overrideWith(other PublicIP) { - p.Period = helpers.OverrideWithPointer(p.Period, other.Period) - p.IPFilepath = helpers.OverrideWithPointer(p.IPFilepath, other.IPFilepath) + p.Period = gosettings.OverrideWithPointer(p.Period, other.Period) + p.IPFilepath = gosettings.OverrideWithPointer(p.IPFilepath, other.IPFilepath) } func (p *PublicIP) setDefaults() { const defaultPeriod = 12 * time.Hour - p.Period = helpers.DefaultPointer(p.Period, defaultPeriod) - p.IPFilepath = helpers.DefaultPointer(p.IPFilepath, "/tmp/gluetun/ip") + p.Period = gosettings.DefaultPointer(p.Period, defaultPeriod) + p.IPFilepath = gosettings.DefaultPointer(p.IPFilepath, "/tmp/gluetun/ip") } func (p PublicIP) String() string { diff --git a/internal/configuration/settings/server.go b/internal/configuration/settings/server.go index 1696e2fa..ed3b9437 100644 --- a/internal/configuration/settings/server.go +++ b/internal/configuration/settings/server.go @@ -7,6 +7,7 @@ import ( "strconv" "github.com/qdm12/gluetun/internal/configuration/settings/helpers" + "github.com/qdm12/gosettings" "github.com/qdm12/gotree" ) @@ -43,29 +44,29 @@ func (c ControlServer) validate() (err error) { func (c *ControlServer) copy() (copied ControlServer) { return ControlServer{ - Address: helpers.CopyPointer(c.Address), - Log: helpers.CopyPointer(c.Log), + Address: gosettings.CopyPointer(c.Address), + Log: gosettings.CopyPointer(c.Log), } } // mergeWith merges the other settings into any // unset field of the receiver settings object. func (c *ControlServer) mergeWith(other ControlServer) { - c.Address = helpers.MergeWithPointer(c.Address, other.Address) - c.Log = helpers.MergeWithPointer(c.Log, other.Log) + c.Address = gosettings.MergeWithPointer(c.Address, other.Address) + c.Log = gosettings.MergeWithPointer(c.Log, other.Log) } // overrideWith overrides fields of the receiver // settings object with any field set in the other // settings. func (c *ControlServer) overrideWith(other ControlServer) { - c.Address = helpers.OverrideWithPointer(c.Address, other.Address) - c.Log = helpers.OverrideWithPointer(c.Log, other.Log) + c.Address = gosettings.OverrideWithPointer(c.Address, other.Address) + c.Log = gosettings.OverrideWithPointer(c.Log, other.Log) } func (c *ControlServer) setDefaults() { - c.Address = helpers.DefaultPointer(c.Address, ":8000") - c.Log = helpers.DefaultPointer(c.Log, true) + c.Address = gosettings.DefaultPointer(c.Address, ":8000") + c.Log = gosettings.DefaultPointer(c.Log, true) } func (c ControlServer) String() string { diff --git a/internal/configuration/settings/serverselection.go b/internal/configuration/settings/serverselection.go index 1d7d5852..62e6b2e4 100644 --- a/internal/configuration/settings/serverselection.go +++ b/internal/configuration/settings/serverselection.go @@ -11,6 +11,8 @@ import ( "github.com/qdm12/gluetun/internal/constants/providers" "github.com/qdm12/gluetun/internal/constants/vpn" "github.com/qdm12/gluetun/internal/models" + "github.com/qdm12/gosettings" + "github.com/qdm12/gosettings/validate" "github.com/qdm12/gotree" ) @@ -88,10 +90,7 @@ func (ss *ServerSelection) validate(vpnServiceProvider string, err = validateServerFilters(*ss, filterChoices) if err != nil { - if errors.Is(err, helpers.ErrNoChoice) { - return fmt.Errorf("for VPN service provider %s: %w", vpnServiceProvider, err) - } - return err // already wrapped error + return fmt.Errorf("for VPN service provider %s: %w", vpnServiceProvider, err) } if *ss.OwnedOnly && @@ -160,8 +159,8 @@ func getLocationFilterChoices(vpnServiceProvider string, // // Retro compatibility // TODO v4 remove filterChoices.Regions = append(filterChoices.Regions, validation.SurfsharkRetroLocChoices()...) - if err := helpers.AreAllOneOf(ss.Regions, filterChoices.Regions); err != nil { - return models.FilterChoices{}, fmt.Errorf("%w: %s", ErrRegionNotValid, err) + if err := validate.AreAllOneOf(ss.Regions, filterChoices.Regions); err != nil { + return models.FilterChoices{}, fmt.Errorf("%w: %w", ErrRegionNotValid, err) } *ss = surfsharkRetroRegion(*ss) } @@ -172,28 +171,28 @@ func getLocationFilterChoices(vpnServiceProvider string, // validateServerFilters validates filters against the choices given as arguments. // Set an argument to nil to pass the check for a particular filter. func validateServerFilters(settings ServerSelection, filterChoices models.FilterChoices) (err error) { - if err := helpers.AreAllOneOf(settings.Countries, filterChoices.Countries); err != nil { - return fmt.Errorf("%w: %s", ErrCountryNotValid, err) + if err := validate.AreAllOneOf(settings.Countries, filterChoices.Countries); err != nil { + return fmt.Errorf("%w: %w", ErrCountryNotValid, err) } - if err := helpers.AreAllOneOf(settings.Regions, filterChoices.Regions); err != nil { - return fmt.Errorf("%w: %s", ErrRegionNotValid, err) + if err := validate.AreAllOneOf(settings.Regions, filterChoices.Regions); err != nil { + return fmt.Errorf("%w: %w", ErrRegionNotValid, err) } - if err := helpers.AreAllOneOf(settings.Cities, filterChoices.Cities); err != nil { - return fmt.Errorf("%w: %s", ErrCityNotValid, err) + if err := validate.AreAllOneOf(settings.Cities, filterChoices.Cities); err != nil { + return fmt.Errorf("%w: %w", ErrCityNotValid, err) } - if err := helpers.AreAllOneOf(settings.ISPs, filterChoices.ISPs); err != nil { - return fmt.Errorf("%w: %s", ErrISPNotValid, err) + if err := validate.AreAllOneOf(settings.ISPs, filterChoices.ISPs); err != nil { + return fmt.Errorf("%w: %w", ErrISPNotValid, err) } - if err := helpers.AreAllOneOf(settings.Hostnames, filterChoices.Hostnames); err != nil { - return fmt.Errorf("%w: %s", ErrHostnameNotValid, err) + if err := validate.AreAllOneOf(settings.Hostnames, filterChoices.Hostnames); err != nil { + return fmt.Errorf("%w: %w", ErrHostnameNotValid, err) } - if err := helpers.AreAllOneOf(settings.Names, filterChoices.Names); err != nil { - return fmt.Errorf("%w: %s", ErrNameNotValid, err) + if err := validate.AreAllOneOf(settings.Names, filterChoices.Names); err != nil { + return fmt.Errorf("%w: %w", ErrNameNotValid, err) } return nil @@ -203,70 +202,70 @@ func (ss *ServerSelection) copy() (copied ServerSelection) { return ServerSelection{ VPN: ss.VPN, TargetIP: ss.TargetIP, - Countries: helpers.CopySlice(ss.Countries), - Regions: helpers.CopySlice(ss.Regions), - Cities: helpers.CopySlice(ss.Cities), - ISPs: helpers.CopySlice(ss.ISPs), - Hostnames: helpers.CopySlice(ss.Hostnames), - Names: helpers.CopySlice(ss.Names), - Numbers: helpers.CopySlice(ss.Numbers), - OwnedOnly: helpers.CopyPointer(ss.OwnedOnly), - FreeOnly: helpers.CopyPointer(ss.FreeOnly), - PremiumOnly: helpers.CopyPointer(ss.PremiumOnly), - StreamOnly: helpers.CopyPointer(ss.StreamOnly), - MultiHopOnly: helpers.CopyPointer(ss.MultiHopOnly), + Countries: gosettings.CopySlice(ss.Countries), + Regions: gosettings.CopySlice(ss.Regions), + Cities: gosettings.CopySlice(ss.Cities), + ISPs: gosettings.CopySlice(ss.ISPs), + Hostnames: gosettings.CopySlice(ss.Hostnames), + Names: gosettings.CopySlice(ss.Names), + Numbers: gosettings.CopySlice(ss.Numbers), + OwnedOnly: gosettings.CopyPointer(ss.OwnedOnly), + FreeOnly: gosettings.CopyPointer(ss.FreeOnly), + PremiumOnly: gosettings.CopyPointer(ss.PremiumOnly), + StreamOnly: gosettings.CopyPointer(ss.StreamOnly), + MultiHopOnly: gosettings.CopyPointer(ss.MultiHopOnly), OpenVPN: ss.OpenVPN.copy(), Wireguard: ss.Wireguard.copy(), } } func (ss *ServerSelection) mergeWith(other ServerSelection) { - ss.VPN = helpers.MergeWithString(ss.VPN, other.VPN) - ss.TargetIP = helpers.MergeWithIP(ss.TargetIP, other.TargetIP) - ss.Countries = helpers.MergeSlices(ss.Countries, other.Countries) - ss.Regions = helpers.MergeSlices(ss.Regions, other.Regions) - ss.Cities = helpers.MergeSlices(ss.Cities, other.Cities) - ss.ISPs = helpers.MergeSlices(ss.ISPs, other.ISPs) - ss.Hostnames = helpers.MergeSlices(ss.Hostnames, other.Hostnames) - ss.Names = helpers.MergeSlices(ss.Names, other.Names) - ss.Numbers = helpers.MergeSlices(ss.Numbers, other.Numbers) - ss.OwnedOnly = helpers.MergeWithPointer(ss.OwnedOnly, other.OwnedOnly) - ss.FreeOnly = helpers.MergeWithPointer(ss.FreeOnly, other.FreeOnly) - ss.PremiumOnly = helpers.MergeWithPointer(ss.PremiumOnly, other.PremiumOnly) - ss.StreamOnly = helpers.MergeWithPointer(ss.StreamOnly, other.StreamOnly) - ss.MultiHopOnly = helpers.MergeWithPointer(ss.MultiHopOnly, other.MultiHopOnly) + ss.VPN = gosettings.MergeWithString(ss.VPN, other.VPN) + ss.TargetIP = gosettings.MergeWithValidator(ss.TargetIP, other.TargetIP) + ss.Countries = gosettings.MergeWithSlice(ss.Countries, other.Countries) + ss.Regions = gosettings.MergeWithSlice(ss.Regions, other.Regions) + ss.Cities = gosettings.MergeWithSlice(ss.Cities, other.Cities) + ss.ISPs = gosettings.MergeWithSlice(ss.ISPs, other.ISPs) + ss.Hostnames = gosettings.MergeWithSlice(ss.Hostnames, other.Hostnames) + ss.Names = gosettings.MergeWithSlice(ss.Names, other.Names) + ss.Numbers = gosettings.MergeWithSlice(ss.Numbers, other.Numbers) + ss.OwnedOnly = gosettings.MergeWithPointer(ss.OwnedOnly, other.OwnedOnly) + ss.FreeOnly = gosettings.MergeWithPointer(ss.FreeOnly, other.FreeOnly) + ss.PremiumOnly = gosettings.MergeWithPointer(ss.PremiumOnly, other.PremiumOnly) + ss.StreamOnly = gosettings.MergeWithPointer(ss.StreamOnly, other.StreamOnly) + ss.MultiHopOnly = gosettings.MergeWithPointer(ss.MultiHopOnly, other.MultiHopOnly) ss.OpenVPN.mergeWith(other.OpenVPN) ss.Wireguard.mergeWith(other.Wireguard) } func (ss *ServerSelection) overrideWith(other ServerSelection) { - ss.VPN = helpers.OverrideWithString(ss.VPN, other.VPN) - ss.TargetIP = helpers.OverrideWithIP(ss.TargetIP, other.TargetIP) - ss.Countries = helpers.OverrideWithSlice(ss.Countries, other.Countries) - ss.Regions = helpers.OverrideWithSlice(ss.Regions, other.Regions) - ss.Cities = helpers.OverrideWithSlice(ss.Cities, other.Cities) - ss.ISPs = helpers.OverrideWithSlice(ss.ISPs, other.ISPs) - ss.Hostnames = helpers.OverrideWithSlice(ss.Hostnames, other.Hostnames) - ss.Names = helpers.OverrideWithSlice(ss.Names, other.Names) - ss.Numbers = helpers.OverrideWithSlice(ss.Numbers, other.Numbers) - ss.OwnedOnly = helpers.OverrideWithPointer(ss.OwnedOnly, other.OwnedOnly) - ss.FreeOnly = helpers.OverrideWithPointer(ss.FreeOnly, other.FreeOnly) - ss.PremiumOnly = helpers.OverrideWithPointer(ss.PremiumOnly, other.PremiumOnly) - ss.StreamOnly = helpers.OverrideWithPointer(ss.StreamOnly, other.StreamOnly) - ss.MultiHopOnly = helpers.OverrideWithPointer(ss.MultiHopOnly, other.MultiHopOnly) + ss.VPN = gosettings.OverrideWithString(ss.VPN, other.VPN) + ss.TargetIP = gosettings.OverrideWithValidator(ss.TargetIP, other.TargetIP) + ss.Countries = gosettings.OverrideWithSlice(ss.Countries, other.Countries) + ss.Regions = gosettings.OverrideWithSlice(ss.Regions, other.Regions) + ss.Cities = gosettings.OverrideWithSlice(ss.Cities, other.Cities) + ss.ISPs = gosettings.OverrideWithSlice(ss.ISPs, other.ISPs) + ss.Hostnames = gosettings.OverrideWithSlice(ss.Hostnames, other.Hostnames) + ss.Names = gosettings.OverrideWithSlice(ss.Names, other.Names) + ss.Numbers = gosettings.OverrideWithSlice(ss.Numbers, other.Numbers) + ss.OwnedOnly = gosettings.OverrideWithPointer(ss.OwnedOnly, other.OwnedOnly) + ss.FreeOnly = gosettings.OverrideWithPointer(ss.FreeOnly, other.FreeOnly) + ss.PremiumOnly = gosettings.OverrideWithPointer(ss.PremiumOnly, other.PremiumOnly) + ss.StreamOnly = gosettings.OverrideWithPointer(ss.StreamOnly, other.StreamOnly) + ss.MultiHopOnly = gosettings.OverrideWithPointer(ss.MultiHopOnly, other.MultiHopOnly) ss.OpenVPN.overrideWith(other.OpenVPN) ss.Wireguard.overrideWith(other.Wireguard) } func (ss *ServerSelection) setDefaults(vpnProvider string) { - ss.VPN = helpers.DefaultString(ss.VPN, vpn.OpenVPN) - ss.TargetIP = helpers.DefaultIP(ss.TargetIP, netip.IPv4Unspecified()) - ss.OwnedOnly = helpers.DefaultPointer(ss.OwnedOnly, false) - ss.FreeOnly = helpers.DefaultPointer(ss.FreeOnly, false) - ss.PremiumOnly = helpers.DefaultPointer(ss.PremiumOnly, false) - ss.StreamOnly = helpers.DefaultPointer(ss.StreamOnly, false) - ss.MultiHopOnly = helpers.DefaultPointer(ss.MultiHopOnly, false) + ss.VPN = gosettings.DefaultString(ss.VPN, vpn.OpenVPN) + ss.TargetIP = gosettings.DefaultValidator(ss.TargetIP, netip.IPv4Unspecified()) + ss.OwnedOnly = gosettings.DefaultPointer(ss.OwnedOnly, false) + ss.FreeOnly = gosettings.DefaultPointer(ss.FreeOnly, false) + ss.PremiumOnly = gosettings.DefaultPointer(ss.PremiumOnly, false) + ss.StreamOnly = gosettings.DefaultPointer(ss.StreamOnly, false) + ss.MultiHopOnly = gosettings.DefaultPointer(ss.MultiHopOnly, false) ss.OpenVPN.setDefaults(vpnProvider) ss.Wireguard.setDefaults() } diff --git a/internal/configuration/settings/shadowsocks.go b/internal/configuration/settings/shadowsocks.go index d967579e..98e5f3da 100644 --- a/internal/configuration/settings/shadowsocks.go +++ b/internal/configuration/settings/shadowsocks.go @@ -2,6 +2,7 @@ package settings import ( "github.com/qdm12/gluetun/internal/configuration/settings/helpers" + "github.com/qdm12/gosettings" "github.com/qdm12/gotree" "github.com/qdm12/ss-server/pkg/tcpudp" ) @@ -21,7 +22,7 @@ func (s Shadowsocks) validate() (err error) { func (s *Shadowsocks) copy() (copied Shadowsocks) { return Shadowsocks{ - Enabled: helpers.CopyPointer(s.Enabled), + Enabled: gosettings.CopyPointer(s.Enabled), Settings: s.Settings.Copy(), } } @@ -29,7 +30,7 @@ func (s *Shadowsocks) copy() (copied Shadowsocks) { // mergeWith merges the other settings into any // unset field of the receiver settings object. func (s *Shadowsocks) mergeWith(other Shadowsocks) { - s.Enabled = helpers.MergeWithPointer(s.Enabled, other.Enabled) + s.Enabled = gosettings.MergeWithPointer(s.Enabled, other.Enabled) s.Settings.MergeWith(other.Settings) } @@ -37,12 +38,12 @@ func (s *Shadowsocks) mergeWith(other Shadowsocks) { // settings object with any field set in the other // settings. func (s *Shadowsocks) overrideWith(other Shadowsocks) { - s.Enabled = helpers.OverrideWithPointer(s.Enabled, other.Enabled) + s.Enabled = gosettings.OverrideWithPointer(s.Enabled, other.Enabled) s.Settings.OverrideWith(other.Settings) } func (s *Shadowsocks) setDefaults() { - s.Enabled = helpers.DefaultPointer(s.Enabled, false) + s.Enabled = gosettings.DefaultPointer(s.Enabled, false) s.Settings.SetDefaults() } diff --git a/internal/configuration/settings/system.go b/internal/configuration/settings/system.go index 52bb87db..43e16b85 100644 --- a/internal/configuration/settings/system.go +++ b/internal/configuration/settings/system.go @@ -1,7 +1,7 @@ package settings import ( - "github.com/qdm12/gluetun/internal/configuration/settings/helpers" + "github.com/qdm12/gosettings" "github.com/qdm12/gotree" ) @@ -19,28 +19,28 @@ func (s System) validate() (err error) { func (s *System) copy() (copied System) { return System{ - PUID: helpers.CopyPointer(s.PUID), - PGID: helpers.CopyPointer(s.PGID), + PUID: gosettings.CopyPointer(s.PUID), + PGID: gosettings.CopyPointer(s.PGID), Timezone: s.Timezone, } } func (s *System) mergeWith(other System) { - s.PUID = helpers.MergeWithPointer(s.PUID, other.PUID) - s.PGID = helpers.MergeWithPointer(s.PGID, other.PGID) - s.Timezone = helpers.MergeWithString(s.Timezone, other.Timezone) + s.PUID = gosettings.MergeWithPointer(s.PUID, other.PUID) + s.PGID = gosettings.MergeWithPointer(s.PGID, other.PGID) + s.Timezone = gosettings.MergeWithString(s.Timezone, other.Timezone) } func (s *System) overrideWith(other System) { - s.PUID = helpers.OverrideWithPointer(s.PUID, other.PUID) - s.PGID = helpers.OverrideWithPointer(s.PGID, other.PGID) - s.Timezone = helpers.OverrideWithString(s.Timezone, other.Timezone) + s.PUID = gosettings.OverrideWithPointer(s.PUID, other.PUID) + s.PGID = gosettings.OverrideWithPointer(s.PGID, other.PGID) + s.Timezone = gosettings.OverrideWithString(s.Timezone, other.Timezone) } func (s *System) setDefaults() { const defaultID = 1000 - s.PUID = helpers.DefaultPointer(s.PUID, defaultID) - s.PGID = helpers.DefaultPointer(s.PGID, defaultID) + s.PUID = gosettings.DefaultPointer(s.PUID, defaultID) + s.PGID = gosettings.DefaultPointer(s.PGID, defaultID) } func (s System) String() string { diff --git a/internal/configuration/settings/unbound.go b/internal/configuration/settings/unbound.go index ff3b0fd8..951a33af 100644 --- a/internal/configuration/settings/unbound.go +++ b/internal/configuration/settings/unbound.go @@ -8,6 +8,7 @@ import ( "github.com/qdm12/dns/pkg/provider" "github.com/qdm12/dns/pkg/unbound" "github.com/qdm12/gluetun/internal/configuration/settings/helpers" + "github.com/qdm12/gosettings" "github.com/qdm12/gotree" ) @@ -30,17 +31,17 @@ func (u *Unbound) setDefaults() { } } - u.Caching = helpers.DefaultPointer(u.Caching, true) - u.IPv6 = helpers.DefaultPointer(u.IPv6, false) + u.Caching = gosettings.DefaultPointer(u.Caching, true) + u.IPv6 = gosettings.DefaultPointer(u.IPv6, false) const defaultVerbosityLevel = 1 - u.VerbosityLevel = helpers.DefaultPointer(u.VerbosityLevel, defaultVerbosityLevel) + u.VerbosityLevel = gosettings.DefaultPointer(u.VerbosityLevel, defaultVerbosityLevel) const defaultVerbosityDetailsLevel = 0 - u.VerbosityDetailsLevel = helpers.DefaultPointer(u.VerbosityDetailsLevel, defaultVerbosityDetailsLevel) + u.VerbosityDetailsLevel = gosettings.DefaultPointer(u.VerbosityDetailsLevel, defaultVerbosityDetailsLevel) const defaultValidationLogLevel = 0 - u.ValidationLogLevel = helpers.DefaultPointer(u.ValidationLogLevel, defaultValidationLogLevel) + u.ValidationLogLevel = gosettings.DefaultPointer(u.ValidationLogLevel, defaultValidationLogLevel) if u.Allowed == nil { u.Allowed = []netip.Prefix{ @@ -49,7 +50,7 @@ func (u *Unbound) setDefaults() { } } - u.Username = helpers.DefaultString(u.Username, "root") + u.Username = gosettings.DefaultString(u.Username, "root") } var ( @@ -94,37 +95,37 @@ func (u Unbound) validate() (err error) { func (u Unbound) copy() (copied Unbound) { return Unbound{ - Providers: helpers.CopySlice(u.Providers), - Caching: helpers.CopyPointer(u.Caching), - IPv6: helpers.CopyPointer(u.IPv6), - VerbosityLevel: helpers.CopyPointer(u.VerbosityLevel), - VerbosityDetailsLevel: helpers.CopyPointer(u.VerbosityDetailsLevel), - ValidationLogLevel: helpers.CopyPointer(u.ValidationLogLevel), + Providers: gosettings.CopySlice(u.Providers), + Caching: gosettings.CopyPointer(u.Caching), + IPv6: gosettings.CopyPointer(u.IPv6), + VerbosityLevel: gosettings.CopyPointer(u.VerbosityLevel), + VerbosityDetailsLevel: gosettings.CopyPointer(u.VerbosityDetailsLevel), + ValidationLogLevel: gosettings.CopyPointer(u.ValidationLogLevel), Username: u.Username, - Allowed: helpers.CopySlice(u.Allowed), + Allowed: gosettings.CopySlice(u.Allowed), } } func (u *Unbound) mergeWith(other Unbound) { - u.Providers = helpers.MergeSlices(u.Providers, other.Providers) - u.Caching = helpers.MergeWithPointer(u.Caching, other.Caching) - u.IPv6 = helpers.MergeWithPointer(u.IPv6, other.IPv6) - u.VerbosityLevel = helpers.MergeWithPointer(u.VerbosityLevel, other.VerbosityLevel) - u.VerbosityDetailsLevel = helpers.MergeWithPointer(u.VerbosityDetailsLevel, other.VerbosityDetailsLevel) - u.ValidationLogLevel = helpers.MergeWithPointer(u.ValidationLogLevel, other.ValidationLogLevel) - u.Username = helpers.MergeWithString(u.Username, other.Username) - u.Allowed = helpers.MergeSlices(u.Allowed, other.Allowed) + u.Providers = gosettings.MergeWithSlice(u.Providers, other.Providers) + u.Caching = gosettings.MergeWithPointer(u.Caching, other.Caching) + u.IPv6 = gosettings.MergeWithPointer(u.IPv6, other.IPv6) + u.VerbosityLevel = gosettings.MergeWithPointer(u.VerbosityLevel, other.VerbosityLevel) + u.VerbosityDetailsLevel = gosettings.MergeWithPointer(u.VerbosityDetailsLevel, other.VerbosityDetailsLevel) + u.ValidationLogLevel = gosettings.MergeWithPointer(u.ValidationLogLevel, other.ValidationLogLevel) + u.Username = gosettings.MergeWithString(u.Username, other.Username) + u.Allowed = gosettings.MergeWithSlice(u.Allowed, other.Allowed) } func (u *Unbound) overrideWith(other Unbound) { - u.Providers = helpers.OverrideWithSlice(u.Providers, other.Providers) - u.Caching = helpers.OverrideWithPointer(u.Caching, other.Caching) - u.IPv6 = helpers.OverrideWithPointer(u.IPv6, other.IPv6) - u.VerbosityLevel = helpers.OverrideWithPointer(u.VerbosityLevel, other.VerbosityLevel) - u.VerbosityDetailsLevel = helpers.OverrideWithPointer(u.VerbosityDetailsLevel, other.VerbosityDetailsLevel) - u.ValidationLogLevel = helpers.OverrideWithPointer(u.ValidationLogLevel, other.ValidationLogLevel) - u.Username = helpers.OverrideWithString(u.Username, other.Username) - u.Allowed = helpers.OverrideWithSlice(u.Allowed, other.Allowed) + u.Providers = gosettings.OverrideWithSlice(u.Providers, other.Providers) + u.Caching = gosettings.OverrideWithPointer(u.Caching, other.Caching) + u.IPv6 = gosettings.OverrideWithPointer(u.IPv6, other.IPv6) + u.VerbosityLevel = gosettings.OverrideWithPointer(u.VerbosityLevel, other.VerbosityLevel) + u.VerbosityDetailsLevel = gosettings.OverrideWithPointer(u.VerbosityDetailsLevel, other.VerbosityDetailsLevel) + u.ValidationLogLevel = gosettings.OverrideWithPointer(u.ValidationLogLevel, other.ValidationLogLevel) + u.Username = gosettings.OverrideWithString(u.Username, other.Username) + u.Allowed = gosettings.OverrideWithSlice(u.Allowed, other.Allowed) } func (u Unbound) ToUnboundFormat() (settings unbound.Settings, err error) { diff --git a/internal/configuration/settings/updater.go b/internal/configuration/settings/updater.go index 1d3c7561..8c6de540 100644 --- a/internal/configuration/settings/updater.go +++ b/internal/configuration/settings/updater.go @@ -5,8 +5,9 @@ import ( "strings" "time" - "github.com/qdm12/gluetun/internal/configuration/settings/helpers" "github.com/qdm12/gluetun/internal/constants/providers" + "github.com/qdm12/gosettings" + "github.com/qdm12/gosettings/validate" "github.com/qdm12/gotree" ) @@ -45,16 +46,9 @@ func (u Updater) Validate() (err error) { validProviders := providers.All() for _, provider := range u.Providers { - valid := false - for _, validProvider := range validProviders { - if provider == validProvider { - valid = true - break - } - } - if !valid { - return fmt.Errorf("%w: %q can only be one of %s", - ErrVPNProviderNameNotValid, provider, helpers.ChoicesOrString(validProviders)) + err = validate.IsOneOf(provider, validProviders...) + if err != nil { + return fmt.Errorf("%w: %w", ErrVPNProviderNameNotValid, err) } } @@ -63,35 +57,35 @@ func (u Updater) Validate() (err error) { func (u *Updater) copy() (copied Updater) { return Updater{ - Period: helpers.CopyPointer(u.Period), + Period: gosettings.CopyPointer(u.Period), DNSAddress: u.DNSAddress, MinRatio: u.MinRatio, - Providers: helpers.CopySlice(u.Providers), + Providers: gosettings.CopySlice(u.Providers), } } // mergeWith merges the other settings into any // unset field of the receiver settings object. func (u *Updater) mergeWith(other Updater) { - u.Period = helpers.MergeWithPointer(u.Period, other.Period) - u.DNSAddress = helpers.MergeWithString(u.DNSAddress, other.DNSAddress) - u.MinRatio = helpers.MergeWithNumber(u.MinRatio, other.MinRatio) - u.Providers = helpers.MergeSlices(u.Providers, other.Providers) + u.Period = gosettings.MergeWithPointer(u.Period, other.Period) + u.DNSAddress = gosettings.MergeWithString(u.DNSAddress, other.DNSAddress) + u.MinRatio = gosettings.MergeWithNumber(u.MinRatio, other.MinRatio) + u.Providers = gosettings.MergeWithSlice(u.Providers, other.Providers) } // overrideWith overrides fields of the receiver // settings object with any field set in the other // settings. func (u *Updater) overrideWith(other Updater) { - u.Period = helpers.OverrideWithPointer(u.Period, other.Period) - u.DNSAddress = helpers.OverrideWithString(u.DNSAddress, other.DNSAddress) - u.MinRatio = helpers.OverrideWithNumber(u.MinRatio, other.MinRatio) - u.Providers = helpers.OverrideWithSlice(u.Providers, other.Providers) + u.Period = gosettings.OverrideWithPointer(u.Period, other.Period) + u.DNSAddress = gosettings.OverrideWithString(u.DNSAddress, other.DNSAddress) + u.MinRatio = gosettings.OverrideWithNumber(u.MinRatio, other.MinRatio) + u.Providers = gosettings.OverrideWithSlice(u.Providers, other.Providers) } func (u *Updater) SetDefaults(vpnProvider string) { - u.Period = helpers.DefaultPointer(u.Period, 0) - u.DNSAddress = helpers.DefaultString(u.DNSAddress, "1.1.1.1:53") + u.Period = gosettings.DefaultPointer(u.Period, 0) + u.DNSAddress = gosettings.DefaultString(u.DNSAddress, "1.1.1.1:53") if u.MinRatio == 0 { const defaultMinRatio = 0.8 diff --git a/internal/configuration/settings/version.go b/internal/configuration/settings/version.go index 2cba3ecb..8ed892e0 100644 --- a/internal/configuration/settings/version.go +++ b/internal/configuration/settings/version.go @@ -2,6 +2,7 @@ package settings import ( "github.com/qdm12/gluetun/internal/configuration/settings/helpers" + "github.com/qdm12/gosettings" "github.com/qdm12/gotree" ) @@ -19,25 +20,25 @@ func (v Version) validate() (err error) { func (v *Version) copy() (copied Version) { return Version{ - Enabled: helpers.CopyPointer(v.Enabled), + Enabled: gosettings.CopyPointer(v.Enabled), } } // mergeWith merges the other settings into any // unset field of the receiver settings object. func (v *Version) mergeWith(other Version) { - v.Enabled = helpers.MergeWithPointer(v.Enabled, other.Enabled) + v.Enabled = gosettings.MergeWithPointer(v.Enabled, other.Enabled) } // overrideWith overrides fields of the receiver // settings object with any field set in the other // settings. func (v *Version) overrideWith(other Version) { - v.Enabled = helpers.OverrideWithPointer(v.Enabled, other.Enabled) + v.Enabled = gosettings.OverrideWithPointer(v.Enabled, other.Enabled) } func (v *Version) setDefaults() { - v.Enabled = helpers.DefaultPointer(v.Enabled, true) + v.Enabled = gosettings.DefaultPointer(v.Enabled, true) } func (v Version) String() string { diff --git a/internal/configuration/settings/vpn.go b/internal/configuration/settings/vpn.go index d84db03c..bc4906aa 100644 --- a/internal/configuration/settings/vpn.go +++ b/internal/configuration/settings/vpn.go @@ -2,10 +2,10 @@ package settings import ( "fmt" - "strings" - "github.com/qdm12/gluetun/internal/configuration/settings/helpers" "github.com/qdm12/gluetun/internal/constants/vpn" + "github.com/qdm12/gosettings" + "github.com/qdm12/gosettings/validate" "github.com/qdm12/gotree" ) @@ -23,9 +23,8 @@ type VPN struct { func (v *VPN) Validate(storage Storage, ipv6Supported bool) (err error) { // Validate Type validVPNTypes := []string{vpn.OpenVPN, vpn.Wireguard} - if !helpers.IsOneOf(v.Type, validVPNTypes...) { - return fmt.Errorf("%w: %q and can only be one of %s", - ErrVPNTypeNotValid, v.Type, strings.Join(validVPNTypes, ", ")) + if err = validate.IsOneOf(v.Type, validVPNTypes...); err != nil { + return fmt.Errorf("%w: %w", ErrVPNTypeNotValid, err) } err = v.Provider.validate(v.Type, storage) @@ -58,21 +57,21 @@ func (v *VPN) Copy() (copied VPN) { } func (v *VPN) mergeWith(other VPN) { - v.Type = helpers.MergeWithString(v.Type, other.Type) + v.Type = gosettings.MergeWithString(v.Type, other.Type) v.Provider.mergeWith(other.Provider) v.OpenVPN.mergeWith(other.OpenVPN) v.Wireguard.mergeWith(other.Wireguard) } func (v *VPN) OverrideWith(other VPN) { - v.Type = helpers.OverrideWithString(v.Type, other.Type) + v.Type = gosettings.OverrideWithString(v.Type, other.Type) v.Provider.overrideWith(other.Provider) v.OpenVPN.overrideWith(other.OpenVPN) v.Wireguard.overrideWith(other.Wireguard) } func (v *VPN) setDefaults() { - v.Type = helpers.DefaultString(v.Type, vpn.OpenVPN) + v.Type = gosettings.DefaultString(v.Type, vpn.OpenVPN) v.Provider.setDefaults() v.OpenVPN.setDefaults(*v.Provider.Name) v.Wireguard.setDefaults() diff --git a/internal/configuration/settings/wireguard.go b/internal/configuration/settings/wireguard.go index 3a3a894f..6f179630 100644 --- a/internal/configuration/settings/wireguard.go +++ b/internal/configuration/settings/wireguard.go @@ -7,6 +7,8 @@ import ( "github.com/qdm12/gluetun/internal/configuration/settings/helpers" "github.com/qdm12/gluetun/internal/constants/providers" + "github.com/qdm12/gosettings" + "github.com/qdm12/gosettings/validate" "github.com/qdm12/gotree" wireguarddevice "golang.zx2c4.com/wireguard/device" "golang.zx2c4.com/wireguard/wgctrl/wgtypes" @@ -102,9 +104,8 @@ func (w Wireguard) validate(vpnProvider string, ipv6Supported bool) (err error) } validImplementations := []string{"auto", "userspace", "kernelspace"} - if !helpers.IsOneOf(w.Implementation, validImplementations...) { - return fmt.Errorf("%w: %s must be one of %s", ErrWireguardImplementationNotValid, - w.Implementation, helpers.ChoicesOrString(validImplementations)) + if err := validate.IsOneOf(w.Implementation, validImplementations...); err != nil { + return fmt.Errorf("%w: %w", ErrWireguardImplementationNotValid, err) } return nil @@ -112,9 +113,9 @@ func (w Wireguard) validate(vpnProvider string, ipv6Supported bool) (err error) func (w *Wireguard) copy() (copied Wireguard) { return Wireguard{ - PrivateKey: helpers.CopyPointer(w.PrivateKey), - PreSharedKey: helpers.CopyPointer(w.PreSharedKey), - Addresses: helpers.CopySlice(w.Addresses), + PrivateKey: gosettings.CopyPointer(w.PrivateKey), + PreSharedKey: gosettings.CopyPointer(w.PreSharedKey), + Addresses: gosettings.CopySlice(w.Addresses), Interface: w.Interface, MTU: w.MTU, Implementation: w.Implementation, @@ -122,29 +123,29 @@ func (w *Wireguard) copy() (copied Wireguard) { } func (w *Wireguard) mergeWith(other Wireguard) { - w.PrivateKey = helpers.MergeWithPointer(w.PrivateKey, other.PrivateKey) - w.PreSharedKey = helpers.MergeWithPointer(w.PreSharedKey, other.PreSharedKey) - w.Addresses = helpers.MergeSlices(w.Addresses, other.Addresses) - w.Interface = helpers.MergeWithString(w.Interface, other.Interface) - w.MTU = helpers.MergeWithNumber(w.MTU, other.MTU) - w.Implementation = helpers.MergeWithString(w.Implementation, other.Implementation) + w.PrivateKey = gosettings.MergeWithPointer(w.PrivateKey, other.PrivateKey) + w.PreSharedKey = gosettings.MergeWithPointer(w.PreSharedKey, other.PreSharedKey) + w.Addresses = gosettings.MergeWithSlice(w.Addresses, other.Addresses) + w.Interface = gosettings.MergeWithString(w.Interface, other.Interface) + w.MTU = gosettings.MergeWithNumber(w.MTU, other.MTU) + w.Implementation = gosettings.MergeWithString(w.Implementation, other.Implementation) } func (w *Wireguard) overrideWith(other Wireguard) { - w.PrivateKey = helpers.OverrideWithPointer(w.PrivateKey, other.PrivateKey) - w.PreSharedKey = helpers.OverrideWithPointer(w.PreSharedKey, other.PreSharedKey) - w.Addresses = helpers.OverrideWithSlice(w.Addresses, other.Addresses) - w.Interface = helpers.OverrideWithString(w.Interface, other.Interface) - w.MTU = helpers.OverrideWithNumber(w.MTU, other.MTU) - w.Implementation = helpers.OverrideWithString(w.Implementation, other.Implementation) + w.PrivateKey = gosettings.OverrideWithPointer(w.PrivateKey, other.PrivateKey) + w.PreSharedKey = gosettings.OverrideWithPointer(w.PreSharedKey, other.PreSharedKey) + w.Addresses = gosettings.OverrideWithSlice(w.Addresses, other.Addresses) + w.Interface = gosettings.OverrideWithString(w.Interface, other.Interface) + w.MTU = gosettings.OverrideWithNumber(w.MTU, other.MTU) + w.Implementation = gosettings.OverrideWithString(w.Implementation, other.Implementation) } func (w *Wireguard) setDefaults() { - w.PrivateKey = helpers.DefaultPointer(w.PrivateKey, "") - w.PreSharedKey = helpers.DefaultPointer(w.PreSharedKey, "") - w.Interface = helpers.DefaultString(w.Interface, "wg0") - w.MTU = helpers.DefaultNumber(w.MTU, wireguarddevice.DefaultMTU) - w.Implementation = helpers.DefaultString(w.Implementation, "auto") + w.PrivateKey = gosettings.DefaultPointer(w.PrivateKey, "") + w.PreSharedKey = gosettings.DefaultPointer(w.PreSharedKey, "") + w.Interface = gosettings.DefaultString(w.Interface, "wg0") + w.MTU = gosettings.DefaultNumber(w.MTU, wireguarddevice.DefaultMTU) + w.Implementation = gosettings.DefaultString(w.Implementation, "auto") } func (w Wireguard) String() string { diff --git a/internal/configuration/settings/wireguardselection.go b/internal/configuration/settings/wireguardselection.go index 9c138b12..b153bd73 100644 --- a/internal/configuration/settings/wireguardselection.go +++ b/internal/configuration/settings/wireguardselection.go @@ -6,6 +6,7 @@ import ( "github.com/qdm12/gluetun/internal/configuration/settings/helpers" "github.com/qdm12/gluetun/internal/constants/providers" + "github.com/qdm12/gosettings" "github.com/qdm12/gotree" "golang.zx2c4.com/wireguard/wgctrl/wgtypes" ) @@ -76,7 +77,7 @@ func (w WireguardSelection) validate(vpnProvider string) (err error) { allowed = []uint16{53, 80, 123, 443, 1194, 65142} } - if helpers.Uint16IsOneOf(*w.EndpointPort, allowed) { + if helpers.IsOneOf(*w.EndpointPort, allowed...) { break } return fmt.Errorf("%w: %d for VPN service provider %s; %s", @@ -110,26 +111,26 @@ func (w WireguardSelection) validate(vpnProvider string) (err error) { func (w *WireguardSelection) copy() (copied WireguardSelection) { return WireguardSelection{ EndpointIP: w.EndpointIP, - EndpointPort: helpers.CopyPointer(w.EndpointPort), + EndpointPort: gosettings.CopyPointer(w.EndpointPort), PublicKey: w.PublicKey, } } func (w *WireguardSelection) mergeWith(other WireguardSelection) { - w.EndpointIP = helpers.MergeWithIP(w.EndpointIP, other.EndpointIP) - w.EndpointPort = helpers.MergeWithPointer(w.EndpointPort, other.EndpointPort) - w.PublicKey = helpers.MergeWithString(w.PublicKey, other.PublicKey) + w.EndpointIP = gosettings.MergeWithValidator(w.EndpointIP, other.EndpointIP) + w.EndpointPort = gosettings.MergeWithPointer(w.EndpointPort, other.EndpointPort) + w.PublicKey = gosettings.MergeWithString(w.PublicKey, other.PublicKey) } func (w *WireguardSelection) overrideWith(other WireguardSelection) { - w.EndpointIP = helpers.OverrideWithIP(w.EndpointIP, other.EndpointIP) - w.EndpointPort = helpers.OverrideWithPointer(w.EndpointPort, other.EndpointPort) - w.PublicKey = helpers.OverrideWithString(w.PublicKey, other.PublicKey) + w.EndpointIP = gosettings.OverrideWithValidator(w.EndpointIP, other.EndpointIP) + w.EndpointPort = gosettings.OverrideWithPointer(w.EndpointPort, other.EndpointPort) + w.PublicKey = gosettings.OverrideWithString(w.PublicKey, other.PublicKey) } func (w *WireguardSelection) setDefaults() { - w.EndpointIP = helpers.DefaultIP(w.EndpointIP, netip.IPv4Unspecified()) - w.EndpointPort = helpers.DefaultPointer(w.EndpointPort, 0) + w.EndpointIP = gosettings.DefaultValidator(w.EndpointIP, netip.IPv4Unspecified()) + w.EndpointPort = gosettings.DefaultPointer(w.EndpointPort, 0) } func (w WireguardSelection) String() string { diff --git a/internal/httpserver/settings.go b/internal/httpserver/settings.go index 6d574f62..c28b444b 100644 --- a/internal/httpserver/settings.go +++ b/internal/httpserver/settings.go @@ -7,7 +7,7 @@ import ( "os" "time" - "github.com/qdm12/gluetun/internal/configuration/settings/helpers" + "github.com/qdm12/gosettings" "github.com/qdm12/gotree" "github.com/qdm12/govalid/address" ) @@ -34,12 +34,12 @@ type Settings struct { } func (s *Settings) SetDefaults() { - s.Address = helpers.DefaultString(s.Address, ":8000") + s.Address = gosettings.DefaultString(s.Address, ":8000") const defaultReadTimeout = 3 * time.Second - s.ReadHeaderTimeout = helpers.DefaultNumber(s.ReadHeaderTimeout, defaultReadTimeout) - s.ReadTimeout = helpers.DefaultNumber(s.ReadTimeout, defaultReadTimeout) + s.ReadHeaderTimeout = gosettings.DefaultNumber(s.ReadHeaderTimeout, defaultReadTimeout) + s.ReadTimeout = gosettings.DefaultNumber(s.ReadTimeout, defaultReadTimeout) const defaultShutdownTimeout = 3 * time.Second - s.ShutdownTimeout = helpers.DefaultNumber(s.ShutdownTimeout, defaultShutdownTimeout) + s.ShutdownTimeout = gosettings.DefaultNumber(s.ShutdownTimeout, defaultShutdownTimeout) } func (s Settings) Copy() Settings { @@ -54,25 +54,25 @@ func (s Settings) Copy() Settings { } func (s *Settings) MergeWith(other Settings) { - s.Address = helpers.MergeWithString(s.Address, other.Address) - s.Handler = helpers.MergeWithHTTPHandler(s.Handler, other.Handler) + s.Address = gosettings.MergeWithString(s.Address, other.Address) + s.Handler = gosettings.MergeWithInterface(s.Handler, other.Handler) if s.Logger == nil { s.Logger = other.Logger } - s.ReadHeaderTimeout = helpers.MergeWithNumber(s.ReadHeaderTimeout, other.ReadHeaderTimeout) - s.ReadTimeout = helpers.MergeWithNumber(s.ReadTimeout, other.ReadTimeout) - s.ShutdownTimeout = helpers.MergeWithNumber(s.ShutdownTimeout, other.ShutdownTimeout) + s.ReadHeaderTimeout = gosettings.MergeWithNumber(s.ReadHeaderTimeout, other.ReadHeaderTimeout) + s.ReadTimeout = gosettings.MergeWithNumber(s.ReadTimeout, other.ReadTimeout) + s.ShutdownTimeout = gosettings.MergeWithNumber(s.ShutdownTimeout, other.ShutdownTimeout) } func (s *Settings) OverrideWith(other Settings) { - s.Address = helpers.OverrideWithString(s.Address, other.Address) - s.Handler = helpers.OverrideWithHTTPHandler(s.Handler, other.Handler) + s.Address = gosettings.OverrideWithString(s.Address, other.Address) + s.Handler = gosettings.OverrideWithInterface(s.Handler, other.Handler) if other.Logger != nil { s.Logger = other.Logger } - s.ReadHeaderTimeout = helpers.OverrideWithNumber(s.ReadHeaderTimeout, other.ReadHeaderTimeout) - s.ReadTimeout = helpers.OverrideWithNumber(s.ReadTimeout, other.ReadTimeout) - s.ShutdownTimeout = helpers.OverrideWithNumber(s.ShutdownTimeout, other.ShutdownTimeout) + s.ReadHeaderTimeout = gosettings.OverrideWithNumber(s.ReadHeaderTimeout, other.ReadHeaderTimeout) + s.ReadTimeout = gosettings.OverrideWithNumber(s.ReadTimeout, other.ReadTimeout) + s.ShutdownTimeout = gosettings.OverrideWithNumber(s.ShutdownTimeout, other.ShutdownTimeout) } var ( diff --git a/internal/pprof/settings.go b/internal/pprof/settings.go index 914a0e7c..b8bca3fc 100644 --- a/internal/pprof/settings.go +++ b/internal/pprof/settings.go @@ -5,8 +5,8 @@ import ( "fmt" "time" - "github.com/qdm12/gluetun/internal/configuration/settings/helpers" "github.com/qdm12/gluetun/internal/httpserver" + "github.com/qdm12/gosettings" "github.com/qdm12/gotree" ) @@ -27,16 +27,16 @@ type Settings struct { } func (s *Settings) SetDefaults() { - s.Enabled = helpers.DefaultPointer(s.Enabled, false) - s.HTTPServer.Address = helpers.DefaultString(s.HTTPServer.Address, "localhost:6060") + s.Enabled = gosettings.DefaultPointer(s.Enabled, false) + s.HTTPServer.Address = gosettings.DefaultString(s.HTTPServer.Address, "localhost:6060") const defaultReadTimeout = 5 * time.Minute // for CPU profiling - s.HTTPServer.ReadTimeout = helpers.DefaultNumber(s.HTTPServer.ReadTimeout, defaultReadTimeout) + s.HTTPServer.ReadTimeout = gosettings.DefaultNumber(s.HTTPServer.ReadTimeout, defaultReadTimeout) s.HTTPServer.SetDefaults() } func (s Settings) Copy() (copied Settings) { return Settings{ - Enabled: helpers.CopyPointer(s.Enabled), + Enabled: gosettings.CopyPointer(s.Enabled), BlockProfileRate: s.BlockProfileRate, MutexProfileRate: s.MutexProfileRate, HTTPServer: s.HTTPServer.Copy(), @@ -44,16 +44,16 @@ func (s Settings) Copy() (copied Settings) { } func (s *Settings) MergeWith(other Settings) { - s.Enabled = helpers.MergeWithPointer(s.Enabled, other.Enabled) - s.BlockProfileRate = helpers.MergeWithPointer(s.BlockProfileRate, other.BlockProfileRate) - s.MutexProfileRate = helpers.MergeWithPointer(s.MutexProfileRate, other.MutexProfileRate) + s.Enabled = gosettings.MergeWithPointer(s.Enabled, other.Enabled) + s.BlockProfileRate = gosettings.MergeWithPointer(s.BlockProfileRate, other.BlockProfileRate) + s.MutexProfileRate = gosettings.MergeWithPointer(s.MutexProfileRate, other.MutexProfileRate) s.HTTPServer.MergeWith(other.HTTPServer) } func (s *Settings) OverrideWith(other Settings) { - s.Enabled = helpers.OverrideWithPointer(s.Enabled, other.Enabled) - s.BlockProfileRate = helpers.OverrideWithPointer(s.BlockProfileRate, other.BlockProfileRate) - s.MutexProfileRate = helpers.OverrideWithPointer(s.MutexProfileRate, other.MutexProfileRate) + s.Enabled = gosettings.OverrideWithPointer(s.Enabled, other.Enabled) + s.BlockProfileRate = gosettings.OverrideWithPointer(s.BlockProfileRate, other.BlockProfileRate) + s.MutexProfileRate = gosettings.OverrideWithPointer(s.MutexProfileRate, other.MutexProfileRate) s.HTTPServer.OverrideWith(other.HTTPServer) }