all: unify code to handle static DNS file path

This commit is contained in:
Cuong Manh Le
2025-05-05 17:36:02 +07:00
committed by Cuong Manh Le
parent 5ce92abf1f
commit 6286a71f2a
8 changed files with 27 additions and 46 deletions
+13 -2
View File
@@ -54,13 +54,18 @@ func userHomeDir() (string, error) {
// SavedStaticDnsSettingsFilePath returns the file path where the static DNS settings
// for the provided interface are saved.
//
// The caller must ensure iface is non-nil.
func SavedStaticDnsSettingsFilePath(iface *net.Interface) string {
// The file is stored in the user home directory under a hidden file.
return absHomeDir(".dns_" + iface.Name)
}
// SavedStaticNameservers returns the stored static nameservers for the given interface.
func SavedStaticNameservers(iface *net.Interface) ([]string, string) {
// SavedStaticNameserversAndPath returns the stored static nameservers for the given interface,
// and the absolute path to file that stored the settings.
//
// The caller must ensure iface is non-nil.
func SavedStaticNameserversAndPath(iface *net.Interface) ([]string, string) {
file := SavedStaticDnsSettingsFilePath(iface)
data, err := os.ReadFile(file)
if err != nil || len(data) == 0 {
@@ -77,3 +82,9 @@ func SavedStaticNameservers(iface *net.Interface) ([]string, string) {
}
return ns, file
}
// SavedStaticNameservers is like SavedStaticNameserversAndPath, but only returns the static nameservers.
func SavedStaticNameservers(iface *net.Interface) []string {
nss, _ := SavedStaticNameserversAndPath(iface)
return nss
}