summaryrefslogtreecommitdiff
path: root/pkg/config/config.go
blob: 5184b7e4ad541bf04dcdab67c5176f71da2bc308 (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
package config

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

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

const BufSize = 64 * 1024

const IoTimeout = 5 * time.Second

func GetSystemSocketPath() string {
	return "/run/tunnel/socket"
}

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() string {
	if uid := getuid(); uid == 0 {
		return GetSystemSocketPath()
	} else {
		return fmt.Sprintf("/run/user/%d/tunnel/socket", uid)
	}
}

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