-
Notifications
You must be signed in to change notification settings - Fork 24
/
vpnagent.go
58 lines (52 loc) · 1019 Bytes
/
vpnagent.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
//go:build linux || darwin || windows
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/kardianos/service"
"sslcon/base"
"sslcon/rpc"
"sslcon/svc"
)
func main() {
// fmt.Println("os.Args: ", len(os.Args))
if len(os.Args) < 2 {
if service.Interactive() {
base.Setup()
rpc.Setup()
watchSignal() // 主协程退出则应用退出
} else {
svc.RunSvc()
}
} else {
cmd := os.Args[1]
switch cmd {
case "install":
svc.InstallSvc()
case "uninstall":
svc.UninstallSvc()
// todo uninstall wintun driver
default:
fmt.Println("invalid command: ", cmd)
}
}
}
func watchSignal() {
base.Info("Server pid: ", os.Getpid())
sigs := make(chan os.Signal, 1)
// https://pkg.go.dev/os/signal
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
for {
// 没有信号就阻塞,从而避免主协程退出
sig := <-sigs
base.Info("Get signal:", sig)
switch sig {
default:
base.Info("Stop")
rpc.DisConnect()
return
}
}
}