summaryrefslogtreecommitdiff
path: root/pkg/config
diff options
context:
space:
mode:
authorMikhail Osipov <mike.osipov@gmail.com>2020-02-27 01:51:55 +0300
committerMikhail Osipov <mike.osipov@gmail.com>2020-02-27 02:11:12 +0300
commit27e13f14f4dba71b417ea530bfe035adbd8f0a93 (patch)
tree0ed374c02a7f1b5516a8e6e9ab1d37b42860e299 /pkg/config
parent085bdfb75eb1c4b90a25a792815f8b80ed06dccb (diff)
add config file support
Diffstat (limited to 'pkg/config')
-rw-r--r--pkg/config/config.go38
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 ""
+ }
}