summaryrefslogtreecommitdiff
path: root/pkg/config/config.go
blob: 2aa5bc2937d77d490460eb17657ea3499624bea0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package config

import (
	"fmt"
	"os"
	"time"
)

const TimeFormat = "2006-01-02/15:04:05"
const TimeMsFormat = "2006-01-02/15:04:05.000"

const IoTimeout = 5 * time.Second

const defaultName = "default"

func GetSysSocketPath(name string) string {
	const path = "/run/tunnel/"
	if name == "" {
		name = defaultName
	}
	return path + name
}

func getuid() int {
	uid := os.Getuid()
	if uid < 0 {
		panic("os.Getuid() returns negative uid")
	}
	return uid
}

func runAsRoot() bool {
	uid := os.Getuid()
	if uid < 0 {
		panic("os.Getuid() returns negative uid")
	}
	return uid == 0
}

func GetSocketPath(name string) string {
	if uid := getuid(); uid == 0 {
		return GetSysSocketPath(name)
	} else {
		if name == "" {
			name = defaultName
		}
		return fmt.Sprintf("/run/user/%d/tunnel/%s", uid, name)
	}
}

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 ""
	}
}