diff options
Diffstat (limited to 'pkg/config/config.go')
| -rw-r--r-- | pkg/config/config.go | 38 |
1 files changed, 28 insertions, 10 deletions
diff --git a/pkg/config/config.go b/pkg/config/config.go index 5483192..906c4b9 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -1,7 +1,6 @@ package config import ( - "errors" "fmt" "os" "time" @@ -13,21 +12,40 @@ const BufSize = 1024 const IoTimeout = 5 * time.Second -var errNegativeUid = errors.New("negative uid") +func GetSystemSocketPath() string { + return "/run/tunnel/socket" +} -func GetSystemSocketPath() (string, error) { - return "/run/tunnel/socket", nil +func getuid() int { + uid := os.Getuid() + if uid < 0 { + panic("os.Getuid() returns negative uid") + } + return uid } -func GetSocketPath() (string, error) { +func runAsRoot() bool { uid := os.Getuid() + if uid < 0 { + panic("os.Getuid() returns negative uid") + } + return uid == 0 +} - switch uid { - case -1: - return "", errNegativeUid - case 0: +func GetSocketPath() string { + if uid := getuid(); uid == 0 { return GetSystemSocketPath() + } else { + return fmt.Sprintf("/run/user/%d/tunnel/socket", uid) } +} - return fmt.Sprintf("/run/user/%d/tunnel/socket", uid), nil +func GetConfigPath() string { + if uid := getuid(); uid == 0 { + return "/etc/tunnel.conf" + } else if s, err := os.UserConfigDir(); err == nil { + return s + "/tunnel/config" + } else { + return "" + } } |
