-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
65 lines (57 loc) · 1.72 KB
/
main.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
package main
import (
"flag"
"fmt"
"github.com/igoogolx/itun2socks/api"
"github.com/igoogolx/itun2socks/internal/configuration"
"github.com/igoogolx/itun2socks/internal/constants"
"github.com/igoogolx/itun2socks/internal/manager"
"github.com/igoogolx/itun2socks/pkg/log"
"os"
"os/signal"
"path/filepath"
"runtime/debug"
"syscall"
)
var (
homeDir string
version bool
port int
)
var packageName = "itun2socks"
//go:generate go run scripts/generate.go
func main() {
debug.SetMemoryLimit(32 * 1024 * 1024)
userConfigDir, _ := os.UserConfigDir()
defaultHomeDir := filepath.Join(userConfigDir, packageName)
flag.IntVar(&port, "port", constants.DefaultHubPort, "set running port")
flag.StringVar(&homeDir, "home_dir", defaultHomeDir, "set configuration directory")
flag.BoolVar(&version, "version", false, "print current version of itun2socks")
flag.Parse()
if version {
fmt.Printf("version: %v, build on: %v", constants.Version, constants.BuildTime)
os.Exit(0)
}
if !filepath.IsAbs(homeDir) {
currentDir, _ := os.Getwd()
homeDir = filepath.Join(currentDir, homeDir)
}
_ = os.MkdirAll(homeDir, os.ModePerm)
constants.Path.SetHomeDir(homeDir)
log.InitLog()
log.Infoln(log.FormatLog(log.InitPrefix, "using config: %v"), constants.Path.ConfigFilePath())
configuration.SetConfigFilePath(constants.Path.ConfigFilePath())
api.Start(port)
defer func() {
if p := recover(); p != nil {
log.Errorln(log.FormatLog(log.InitPrefix, "internal error: %v"), p)
}
err := manager.Close()
if err != nil {
log.Errorln(log.FormatLog(log.InitPrefix, "fail to close client:%v"), err)
}
}()
osSignals := make(chan os.Signal, 1)
signal.Notify(osSignals, os.Interrupt, os.Kill, syscall.SIGTERM, syscall.SIGHUP)
<-osSignals
}