From def407d610a6e80b7e08da06808a9047b3437ec0 Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Sun, 28 May 2023 10:33:36 +0000 Subject: [PATCH] chore(settings): use qdm12/gosettings functions - use: FileExists, ObfuscateKey, BoolToYesNo - remove local functions moved to gosettings --- internal/configuration/settings/dns.go | 3 +- .../configuration/settings/dnsblacklist.go | 7 ++--- internal/configuration/settings/dot.go | 3 +- internal/configuration/settings/firewall.go | 3 +- .../configuration/settings/helpers/files.go | 31 ------------------- .../settings/helpers/obfuscate.go | 25 --------------- .../configuration/settings/helpers/string.go | 7 ----- internal/configuration/settings/httpproxy.go | 9 +++--- internal/configuration/settings/openvpn.go | 13 ++++---- .../settings/openvpnselection.go | 2 +- internal/configuration/settings/server.go | 3 +- .../configuration/settings/shadowsocks.go | 7 ++--- internal/configuration/settings/unbound.go | 5 ++- internal/configuration/settings/version.go | 3 +- internal/configuration/settings/wireguard.go | 4 +-- 15 files changed, 26 insertions(+), 99 deletions(-) delete mode 100644 internal/configuration/settings/helpers/files.go delete mode 100644 internal/configuration/settings/helpers/obfuscate.go diff --git a/internal/configuration/settings/dns.go b/internal/configuration/settings/dns.go index 532b35f0..c48b951c 100644 --- a/internal/configuration/settings/dns.go +++ b/internal/configuration/settings/dns.go @@ -4,7 +4,6 @@ import ( "fmt" "net/netip" - "github.com/qdm12/gluetun/internal/configuration/settings/helpers" "github.com/qdm12/gosettings" "github.com/qdm12/gotree" ) @@ -77,7 +76,7 @@ func (d DNS) String() string { func (d DNS) toLinesNode() (node *gotree.Node) { node = gotree.New("DNS settings:") node.Appendf("DNS server address to use: %s", d.ServerAddress) - node.Appendf("Keep existing nameserver(s): %s", helpers.BoolPtrToYesNo(d.KeepNameserver)) + node.Appendf("Keep existing nameserver(s): %s", gosettings.BoolToYesNo(d.KeepNameserver)) node.AppendNode(d.DoT.toLinesNode()) return node } diff --git a/internal/configuration/settings/dnsblacklist.go b/internal/configuration/settings/dnsblacklist.go index eef7d924..08996909 100644 --- a/internal/configuration/settings/dnsblacklist.go +++ b/internal/configuration/settings/dnsblacklist.go @@ -7,7 +7,6 @@ import ( "regexp" "github.com/qdm12/dns/pkg/blacklist" - "github.com/qdm12/gluetun/internal/configuration/settings/helpers" "github.com/qdm12/gosettings" "github.com/qdm12/gotree" ) @@ -103,9 +102,9 @@ func (b DNSBlacklist) String() string { func (b DNSBlacklist) toLinesNode() (node *gotree.Node) { node = gotree.New("DNS filtering settings:") - node.Appendf("Block malicious: %s", helpers.BoolPtrToYesNo(b.BlockMalicious)) - node.Appendf("Block ads: %s", helpers.BoolPtrToYesNo(b.BlockAds)) - node.Appendf("Block surveillance: %s", helpers.BoolPtrToYesNo(b.BlockSurveillance)) + node.Appendf("Block malicious: %s", gosettings.BoolToYesNo(b.BlockMalicious)) + node.Appendf("Block ads: %s", gosettings.BoolToYesNo(b.BlockAds)) + node.Appendf("Block surveillance: %s", gosettings.BoolToYesNo(b.BlockSurveillance)) if len(b.AllowedHosts) > 0 { allowedHostsNode := node.Appendf("Allowed hosts:") diff --git a/internal/configuration/settings/dot.go b/internal/configuration/settings/dot.go index c6755d35..0e326453 100644 --- a/internal/configuration/settings/dot.go +++ b/internal/configuration/settings/dot.go @@ -5,7 +5,6 @@ import ( "fmt" "time" - "github.com/qdm12/gluetun/internal/configuration/settings/helpers" "github.com/qdm12/gosettings" "github.com/qdm12/gotree" ) @@ -96,7 +95,7 @@ func (d DoT) String() string { func (d DoT) toLinesNode() (node *gotree.Node) { node = gotree.New("DNS over TLS settings:") - node.Appendf("Enabled: %s", helpers.BoolPtrToYesNo(d.Enabled)) + node.Appendf("Enabled: %s", gosettings.BoolToYesNo(d.Enabled)) if !*d.Enabled { return node } diff --git a/internal/configuration/settings/firewall.go b/internal/configuration/settings/firewall.go index 9fe3d14a..466072d2 100644 --- a/internal/configuration/settings/firewall.go +++ b/internal/configuration/settings/firewall.go @@ -4,7 +4,6 @@ import ( "fmt" "net/netip" - "github.com/qdm12/gluetun/internal/configuration/settings/helpers" "github.com/qdm12/gosettings" "github.com/qdm12/gotree" ) @@ -84,7 +83,7 @@ func (f Firewall) String() string { func (f Firewall) toLinesNode() (node *gotree.Node) { node = gotree.New("Firewall settings:") - node.Appendf("Enabled: %s", helpers.BoolPtrToYesNo(f.Enabled)) + node.Appendf("Enabled: %s", gosettings.BoolToYesNo(f.Enabled)) if !*f.Enabled { return node } diff --git a/internal/configuration/settings/helpers/files.go b/internal/configuration/settings/helpers/files.go deleted file mode 100644 index 998e2ebc..00000000 --- a/internal/configuration/settings/helpers/files.go +++ /dev/null @@ -1,31 +0,0 @@ -package helpers - -import ( - "errors" - "fmt" - "os" - "path/filepath" -) - -var ( - ErrFileDoesNotExist = errors.New("file does not exist") - ErrFileRead = errors.New("cannot read file") - ErrFileClose = errors.New("cannot close file") -) - -func FileExists(path string) (err error) { - path = filepath.Clean(path) - - f, err := os.Open(path) - if errors.Is(err, os.ErrNotExist) { - return fmt.Errorf("%w: %s", ErrFileDoesNotExist, path) - } else if err != nil { - return fmt.Errorf("%w: %s", ErrFileRead, err) - } - - if err := f.Close(); err != nil { - return fmt.Errorf("%w: %s", ErrFileClose, err) - } - - return nil -} diff --git a/internal/configuration/settings/helpers/obfuscate.go b/internal/configuration/settings/helpers/obfuscate.go deleted file mode 100644 index 7835b618..00000000 --- a/internal/configuration/settings/helpers/obfuscate.go +++ /dev/null @@ -1,25 +0,0 @@ -package helpers - -func ObfuscateWireguardKey(fullKey string) (obfuscatedKey string) { - const minKeyLength = 10 - if len(fullKey) < minKeyLength { - return "(too short)" - } - - lastIndex := len(fullKey) - 1 - return fullKey[0:2] + "..." + fullKey[lastIndex-2:] -} - -func ObfuscatePassword(password string) (obfuscatedPassword string) { - if password != "" { - return "[set]" - } - return "[not set]" -} - -func ObfuscateData(data string) (obfuscated string) { - if data != "" { - return "[set]" - } - return "[not set]" -} diff --git a/internal/configuration/settings/helpers/string.go b/internal/configuration/settings/helpers/string.go index 594036f9..1103d4f4 100644 --- a/internal/configuration/settings/helpers/string.go +++ b/internal/configuration/settings/helpers/string.go @@ -1,12 +1,5 @@ package helpers -func BoolPtrToYesNo(b *bool) string { - if *b { - return "yes" - } - return "no" -} - func TCPPtrToString(tcp *bool) string { if *tcp { return "TCP" diff --git a/internal/configuration/settings/httpproxy.go b/internal/configuration/settings/httpproxy.go index 3fc56b9d..d107a54e 100644 --- a/internal/configuration/settings/httpproxy.go +++ b/internal/configuration/settings/httpproxy.go @@ -5,7 +5,6 @@ import ( "os" "time" - "github.com/qdm12/gluetun/internal/configuration/settings/helpers" "github.com/qdm12/gosettings" "github.com/qdm12/gotree" "github.com/qdm12/govalid/address" @@ -114,16 +113,16 @@ func (h HTTPProxy) String() string { func (h HTTPProxy) toLinesNode() (node *gotree.Node) { node = gotree.New("HTTP proxy settings:") - node.Appendf("Enabled: %s", helpers.BoolPtrToYesNo(h.Enabled)) + node.Appendf("Enabled: %s", gosettings.BoolToYesNo(h.Enabled)) if !*h.Enabled { return node } node.Appendf("Listening address: %s", h.ListeningAddress) node.Appendf("User: %s", *h.User) - node.Appendf("Password: %s", helpers.ObfuscatePassword(*h.Password)) - node.Appendf("Stealth mode: %s", helpers.BoolPtrToYesNo(h.Stealth)) - node.Appendf("Log: %s", helpers.BoolPtrToYesNo(h.Log)) + node.Appendf("Password: %s", gosettings.ObfuscateKey(*h.Password)) + node.Appendf("Stealth mode: %s", gosettings.BoolToYesNo(h.Stealth)) + node.Appendf("Log: %s", gosettings.BoolToYesNo(h.Log)) node.Appendf("Read header timeout: %s", h.ReadHeaderTimeout) node.Appendf("Read timeout: %s", h.ReadTimeout) diff --git a/internal/configuration/settings/openvpn.go b/internal/configuration/settings/openvpn.go index b1afb538..6c2f0b04 100644 --- a/internal/configuration/settings/openvpn.go +++ b/internal/configuration/settings/openvpn.go @@ -5,7 +5,6 @@ import ( "fmt" "regexp" - "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" @@ -163,7 +162,7 @@ func validateOpenVPNConfigFilepath(isCustom bool, return fmt.Errorf("%w", ErrFilepathMissing) } - err = helpers.FileExists(confFile) + err = validate.FileExists(confFile) if err != nil { return err } @@ -339,8 +338,8 @@ func (o OpenVPN) String() string { func (o OpenVPN) toLinesNode() (node *gotree.Node) { node = gotree.New("OpenVPN settings:") node.Appendf("OpenVPN version: %s", o.Version) - node.Appendf("User: %s", helpers.ObfuscatePassword(*o.User)) - node.Appendf("Password: %s", helpers.ObfuscatePassword(*o.Password)) + node.Appendf("User: %s", gosettings.ObfuscateKey(*o.User)) + node.Appendf("Password: %s", gosettings.ObfuscateKey(*o.Password)) if *o.ConfFile != "" { node.Appendf("Custom configuration file: %s", *o.ConfFile) @@ -355,16 +354,16 @@ func (o OpenVPN) toLinesNode() (node *gotree.Node) { } if *o.Cert != "" { - node.Appendf("Client crt: %s", helpers.ObfuscateData(*o.Cert)) + node.Appendf("Client crt: %s", gosettings.ObfuscateKey(*o.Cert)) } if *o.Key != "" { - node.Appendf("Client key: %s", helpers.ObfuscateData(*o.Key)) + node.Appendf("Client key: %s", gosettings.ObfuscateKey(*o.Key)) } if *o.EncryptedKey != "" { node.Appendf("Encrypted key: %s (key passhrapse %s)", - helpers.ObfuscateData(*o.EncryptedKey), helpers.ObfuscatePassword(*o.KeyPassphrase)) + gosettings.ObfuscateKey(*o.EncryptedKey), gosettings.ObfuscateKey(*o.KeyPassphrase)) } if *o.PIAEncPreset != "" { diff --git a/internal/configuration/settings/openvpnselection.go b/internal/configuration/settings/openvpnselection.go index 6e8e9dfc..470e22eb 100644 --- a/internal/configuration/settings/openvpnselection.go +++ b/internal/configuration/settings/openvpnselection.go @@ -34,7 +34,7 @@ type OpenVPNSelection struct { func (o OpenVPNSelection) validate(vpnProvider string) (err error) { // Validate ConfFile if confFile := *o.ConfFile; confFile != "" { - err := helpers.FileExists(confFile) + err := validate.FileExists(confFile) if err != nil { return fmt.Errorf("configuration file: %w", err) } diff --git a/internal/configuration/settings/server.go b/internal/configuration/settings/server.go index ed3b9437..b86fb955 100644 --- a/internal/configuration/settings/server.go +++ b/internal/configuration/settings/server.go @@ -6,7 +6,6 @@ import ( "os" "strconv" - "github.com/qdm12/gluetun/internal/configuration/settings/helpers" "github.com/qdm12/gosettings" "github.com/qdm12/gotree" ) @@ -76,6 +75,6 @@ func (c ControlServer) String() string { func (c ControlServer) toLinesNode() (node *gotree.Node) { node = gotree.New("Control server settings:") node.Appendf("Listening address: %s", *c.Address) - node.Appendf("Logging: %s", helpers.BoolPtrToYesNo(c.Log)) + node.Appendf("Logging: %s", gosettings.BoolToYesNo(c.Log)) return node } diff --git a/internal/configuration/settings/shadowsocks.go b/internal/configuration/settings/shadowsocks.go index 98e5f3da..b7d1a020 100644 --- a/internal/configuration/settings/shadowsocks.go +++ b/internal/configuration/settings/shadowsocks.go @@ -1,7 +1,6 @@ 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" @@ -54,7 +53,7 @@ func (s Shadowsocks) String() string { func (s Shadowsocks) toLinesNode() (node *gotree.Node) { node = gotree.New("Shadowsocks server settings:") - node.Appendf("Enabled: %s", helpers.BoolPtrToYesNo(s.Enabled)) + node.Appendf("Enabled: %s", gosettings.BoolToYesNo(s.Enabled)) if !*s.Enabled { return node } @@ -62,8 +61,8 @@ func (s Shadowsocks) toLinesNode() (node *gotree.Node) { // TODO have ToLinesNode in qdm12/ss-server node.Appendf("Listening address: %s", s.Address) node.Appendf("Cipher: %s", s.CipherName) - node.Appendf("Password: %s", helpers.ObfuscatePassword(*s.Password)) - node.Appendf("Log addresses: %s", helpers.BoolPtrToYesNo(s.LogAddresses)) + node.Appendf("Password: %s", gosettings.ObfuscateKey(*s.Password)) + node.Appendf("Log addresses: %s", gosettings.BoolToYesNo(s.LogAddresses)) return node } diff --git a/internal/configuration/settings/unbound.go b/internal/configuration/settings/unbound.go index 951a33af..50e59abd 100644 --- a/internal/configuration/settings/unbound.go +++ b/internal/configuration/settings/unbound.go @@ -7,7 +7,6 @@ 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" ) @@ -187,8 +186,8 @@ func (u Unbound) toLinesNode() (node *gotree.Node) { authServers.Appendf(provider) } - node.Appendf("Caching: %s", helpers.BoolPtrToYesNo(u.Caching)) - node.Appendf("IPv6: %s", helpers.BoolPtrToYesNo(u.IPv6)) + node.Appendf("Caching: %s", gosettings.BoolToYesNo(u.Caching)) + node.Appendf("IPv6: %s", gosettings.BoolToYesNo(u.IPv6)) node.Appendf("Verbosity level: %d", *u.VerbosityLevel) node.Appendf("Verbosity details level: %d", *u.VerbosityDetailsLevel) node.Appendf("Validation log level: %d", *u.ValidationLogLevel) diff --git a/internal/configuration/settings/version.go b/internal/configuration/settings/version.go index 8ed892e0..47f11310 100644 --- a/internal/configuration/settings/version.go +++ b/internal/configuration/settings/version.go @@ -1,7 +1,6 @@ package settings import ( - "github.com/qdm12/gluetun/internal/configuration/settings/helpers" "github.com/qdm12/gosettings" "github.com/qdm12/gotree" ) @@ -48,7 +47,7 @@ func (v Version) String() string { func (v Version) toLinesNode() (node *gotree.Node) { node = gotree.New("Version settings:") - node.Appendf("Enabled: %s", helpers.BoolPtrToYesNo(v.Enabled)) + node.Appendf("Enabled: %s", gosettings.BoolToYesNo(v.Enabled)) return node } diff --git a/internal/configuration/settings/wireguard.go b/internal/configuration/settings/wireguard.go index 6f179630..cfa58f0f 100644 --- a/internal/configuration/settings/wireguard.go +++ b/internal/configuration/settings/wireguard.go @@ -156,12 +156,12 @@ func (w Wireguard) toLinesNode() (node *gotree.Node) { node = gotree.New("Wireguard settings:") if *w.PrivateKey != "" { - s := helpers.ObfuscateWireguardKey(*w.PrivateKey) + s := gosettings.ObfuscateKey(*w.PrivateKey) node.Appendf("Private key: %s", s) } if *w.PreSharedKey != "" { - s := helpers.ObfuscateWireguardKey(*w.PreSharedKey) + s := gosettings.ObfuscateKey(*w.PreSharedKey) node.Appendf("Pre-shared key: %s", s) }