mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-05-27 12:52:27 +02:00
all: unify handling user home directory logic
This commit is contained in:
committed by
Cuong Manh Le
parent
6286a71f2a
commit
47c04bf0f6
+1
-30
@@ -966,28 +966,11 @@ func userHomeDir() (string, error) {
|
|||||||
if dir != "" {
|
if dir != "" {
|
||||||
return dir, nil
|
return dir, nil
|
||||||
}
|
}
|
||||||
// viper will expand for us.
|
|
||||||
if runtime.GOOS == "windows" {
|
|
||||||
// If we're on windows, use the install path for this.
|
|
||||||
exePath, err := os.Executable()
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
return filepath.Dir(exePath), nil
|
|
||||||
}
|
|
||||||
// Mobile platform should provide a rw dir path for this.
|
// Mobile platform should provide a rw dir path for this.
|
||||||
if isMobile() {
|
if isMobile() {
|
||||||
return homedir, nil
|
return homedir, nil
|
||||||
}
|
}
|
||||||
dir = "/etc/controld"
|
return ctrld.UserHomeDir()
|
||||||
if err := os.MkdirAll(dir, 0750); err != nil {
|
|
||||||
return os.UserHomeDir() // fallback to user home directory
|
|
||||||
}
|
|
||||||
if ok, _ := dirWritable(dir); !ok {
|
|
||||||
return os.UserHomeDir()
|
|
||||||
}
|
|
||||||
return dir, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// socketDir returns directory that ctrld will create socket file for running controlServer.
|
// socketDir returns directory that ctrld will create socket file for running controlServer.
|
||||||
@@ -1754,18 +1737,6 @@ func exchangeContextWithTimeout(c *dns.Client, timeout time.Duration, msg *dns.M
|
|||||||
return c.ExchangeContext(ctx, msg, addr)
|
return c.ExchangeContext(ctx, msg, addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// absHomeDir returns the absolute path to given filename using home directory as root dir.
|
|
||||||
func absHomeDir(filename string) string {
|
|
||||||
if homedir != "" {
|
|
||||||
return filepath.Join(homedir, filename)
|
|
||||||
}
|
|
||||||
dir, err := userHomeDir()
|
|
||||||
if err != nil {
|
|
||||||
return filename
|
|
||||||
}
|
|
||||||
return filepath.Join(dir, filename)
|
|
||||||
}
|
|
||||||
|
|
||||||
// runInCdMode reports whether ctrld service is running in cd mode.
|
// runInCdMode reports whether ctrld service is running in cd mode.
|
||||||
func runInCdMode() bool {
|
func runInCdMode() bool {
|
||||||
return curCdUID() != ""
|
return curCdUID() != ""
|
||||||
|
|||||||
+1
-1
@@ -985,7 +985,7 @@ NOTE: Uninstalling will set DNS to values provided by DHCP.`,
|
|||||||
})
|
})
|
||||||
// Windows forwarders file.
|
// Windows forwarders file.
|
||||||
if hasLocalDnsServerRunning() {
|
if hasLocalDnsServerRunning() {
|
||||||
files = append(files, absHomeDir(windowsForwardersFilename))
|
files = append(files, ctrld.AbsHomeDir(windowsForwardersFilename))
|
||||||
}
|
}
|
||||||
// Binary itself.
|
// Binary itself.
|
||||||
bin, _ := os.Executable()
|
bin, _ := os.Executable()
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ func setDNS(iface *net.Interface, nameservers []string) error {
|
|||||||
if hasLocalDnsServerRunning() {
|
if hasLocalDnsServerRunning() {
|
||||||
mainLog.Load().Debug().Msg("Local DNS server detected, configuring forwarders")
|
mainLog.Load().Debug().Msg("Local DNS server detected, configuring forwarders")
|
||||||
|
|
||||||
file := absHomeDir(windowsForwardersFilename)
|
file := ctrld.AbsHomeDir(windowsForwardersFilename)
|
||||||
mainLog.Load().Debug().Msgf("Using forwarders file: %s", file)
|
mainLog.Load().Debug().Msgf("Using forwarders file: %s", file)
|
||||||
|
|
||||||
oldForwardersContent, err := os.ReadFile(file)
|
oldForwardersContent, err := os.ReadFile(file)
|
||||||
@@ -131,7 +131,7 @@ func resetDNS(iface *net.Interface) error {
|
|||||||
resetDNSOnce.Do(func() {
|
resetDNSOnce.Do(func() {
|
||||||
// See corresponding comment in setDNS.
|
// See corresponding comment in setDNS.
|
||||||
if hasLocalDnsServerRunning() {
|
if hasLocalDnsServerRunning() {
|
||||||
file := absHomeDir(windowsForwardersFilename)
|
file := ctrld.AbsHomeDir(windowsForwardersFilename)
|
||||||
content, err := os.ReadFile(file)
|
content, err := os.ReadFile(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
mainLog.Load().Error().Err(err).Msg("could not read forwarders settings")
|
mainLog.Load().Error().Err(err).Msg("could not read forwarders settings")
|
||||||
|
|||||||
+6
-10
@@ -8,14 +8,9 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var homedir string
|
// AbsHomeDir returns the absolute path to given filename using home directory as root dir.
|
||||||
|
func AbsHomeDir(filename string) string {
|
||||||
// absHomeDir returns the absolute path to given filename using home directory as root dir.
|
dir, err := UserHomeDir()
|
||||||
func absHomeDir(filename string) string {
|
|
||||||
if homedir != "" {
|
|
||||||
return filepath.Join(homedir, filename)
|
|
||||||
}
|
|
||||||
dir, err := userHomeDir()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return filename
|
return filename
|
||||||
}
|
}
|
||||||
@@ -31,7 +26,8 @@ func dirWritable(dir string) (bool, error) {
|
|||||||
return true, f.Close()
|
return true, f.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func userHomeDir() (string, error) {
|
// UserHomeDir returns the home directory for user who is running ctrld.
|
||||||
|
func UserHomeDir() (string, error) {
|
||||||
// viper will expand for us.
|
// viper will expand for us.
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
// If we're on windows, use the install path for this.
|
// If we're on windows, use the install path for this.
|
||||||
@@ -58,7 +54,7 @@ func userHomeDir() (string, error) {
|
|||||||
// The caller must ensure iface is non-nil.
|
// The caller must ensure iface is non-nil.
|
||||||
func SavedStaticDnsSettingsFilePath(iface *net.Interface) string {
|
func SavedStaticDnsSettingsFilePath(iface *net.Interface) string {
|
||||||
// The file is stored in the user home directory under a hidden file.
|
// The file is stored in the user home directory under a hidden file.
|
||||||
return absHomeDir(".dns_" + iface.Name)
|
return AbsHomeDir(".dns_" + iface.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SavedStaticNameserversAndPath returns the stored static nameservers for the given interface,
|
// SavedStaticNameserversAndPath returns the stored static nameservers for the given interface,
|
||||||
|
|||||||
Reference in New Issue
Block a user