forked from rekby/lets-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
platform_nix.go
192 lines (162 loc) · 4.26 KB
/
platform_nix.go
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// +build !windows
package main
import (
"bufio"
"bytes"
"errors"
"os"
"os/signal"
"path/filepath"
"strconv"
"syscall"
"github.com/Sirupsen/logrus"
"github.com/sevlyar/go-daemon"
)
type User struct {
Name string
HomeDir string
UserId uint32
DefaultGroupId uint32
}
var (
daemonContext *daemon.Context // need global var for prevent close (and unlock) pid-file
osSignals = make(chan os.Signal, 1)
)
// return true if it is child process
func daemonize() bool {
daemonContext = &daemon.Context{}
if *runAs != "" {
userName := *runAs
user, err := userLookup(userName)
if err != nil {
logrus.Fatalf("Can't lookup runas user '%v': %v", userName, err)
}
logrus.Infof("Parse runas '%v' as %v:%v", *runAs, user.UserId, user.DefaultGroupId)
daemonContext.Credential = &syscall.Credential{
Uid: user.UserId,
Gid: user.DefaultGroupId,
}
daemonContext.WorkDir = user.HomeDir
}
if *workingDir != "" {
daemonContext.WorkDir = *workingDir
}
logrus.Infof("Daemon working dir: %v", daemonContext.WorkDir)
if *pidFilePath != "" {
pidPath := *pidFilePath
if !filepath.IsAbs(pidPath) && daemonContext.WorkDir != "" {
pidPath = filepath.Join(daemonContext.WorkDir, pidPath)
}
daemonContext.PidFileName = pidPath
logrus.Infof("Pidfile: %v", daemonContext.PidFileName)
}
if *stdErrToFile != "" {
if filepath.IsAbs(*stdErrToFile) {
daemonContext.LogFileName = *stdErrToFile
} else {
daemonContext.LogFileName = filepath.Join(daemonContext.WorkDir, *stdErrToFile)
}
}
child, err := daemonContext.Reborn()
if err != nil {
logrus.Fatalf("Can't start daemon process: %v", err)
}
if child == nil {
logrus.Info("Start as daemon child")
if *runAs != "" && os.Getuid() == 0 {
logrus.Fatal("Start with uid 0 instead runas")
logFilePath := *logOutput
if logFilePath == "" {
logFilePath = "FATAL_ERROR.txt"
}
if !filepath.IsAbs(logFilePath) && daemonContext.WorkDir != "" {
logFilePath = filepath.Join(daemonContext.WorkDir, logFilePath)
}
logFile, _ := os.OpenFile(logFilePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, DEFAULT_FILE_MODE)
if logFile != nil {
//nolint:errcheck
logFile.WriteString("Start with uid 0 instead runas\n")
logFile.Close()
}
}
return true
} else {
logrus.Info("Start as daemons parent")
return false
}
}
// "os/user".Lookup need cgo and can't used when binary is cross-compiled.
// use own lookup
func userLookup(login string) (*User, error) {
var isId bool = false
_, err := parseUint32(login)
if err == nil {
isId = true
}
loginBytes := []byte(login)
passwdFile, err := os.Open("/etc/passwd")
if passwdFile != nil {
defer passwdFile.Close()
}
if err != nil {
return nil, err
}
splitBytes := []byte(":")
scanner := bufio.NewScanner(passwdFile)
var userLine []byte
var userLineParts [][]byte
for scanner.Scan() {
line := scanner.Bytes()
lineParts := bytes.SplitN(line, splitBytes, 7)
if len(lineParts) < 6 {
logrus.Warnf("Short passwd line '%s'", line)
continue
}
if isId && bytes.Equal(lineParts[2], loginBytes) ||
!isId && bytes.Equal(lineParts[0], loginBytes) {
userLine = line
userLineParts = lineParts
break
}
}
if scanner.Err() != nil {
return nil, err
}
if userLineParts == nil {
return nil, errors.New("User not found")
}
user := User{
Name: string(userLineParts[0]),
HomeDir: string(userLineParts[5]),
}
user.UserId, err = parseUint32(string(userLineParts[2]))
if err != nil {
logrus.Errorf("Can't parse user id '%s' from passwd line '%s': %v", userLineParts[2], userLine, err)
return nil, errors.New("Can't parse user id")
}
user.DefaultGroupId, err = parseUint32(string(userLineParts[3]))
if err != nil {
logrus.Errorf("Can't parse users group id '%s' from passwd line '%s': %v", userLineParts[3], userLine, err)
return nil, errors.New("Can't parse group id")
}
return &user, nil
}
func parseUint32(s string) (uint32, error) {
res, err := strconv.ParseUint(s, 10, 32)
if err == nil {
return uint32(res), nil
} else {
return 0, err
}
}
func signalWorker() {
signal.Notify(osSignals, syscall.SIGHUP)
for s := range osSignals {
switch s {
case syscall.SIGHUP:
logrus.Info("Flush cache by SIGHUP")
certificateCacheFlushMem()
skipDomainsFlush()
}
}
}