forked from cyfdecyf/cow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
97 lines (82 loc) · 1.6 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
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
package main
import (
// "flag"
"os"
"os/exec"
"runtime"
// "runtime/pprof"
"sync"
"syscall"
)
// var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
var (
quit chan struct{}
relaunch bool
)
// This code is from goagain
func lookPath() (argv0 string, err error) {
argv0, err = exec.LookPath(os.Args[0])
if nil != err {
return
}
if _, err = os.Stat(argv0); nil != err {
return
}
return
}
func main() {
quit = make(chan struct{})
// Parse flags after load config to allow override options in config
cmdLineConfig := parseCmdLineConfig()
if cmdLineConfig.PrintVer {
printVersion()
os.Exit(0)
}
parseConfig(cmdLineConfig.RcFile, cmdLineConfig)
initSelfListenAddr()
initLog()
initAuth()
initSiteStat()
initPAC() // initPAC uses siteStat, so must init after site stat
initStat()
initParentPool()
/*
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
Fatal(err)
}
pprof.StartCPUProfile(f)
}
*/
if config.Core > 0 {
runtime.GOMAXPROCS(config.Core)
}
go sigHandler()
go runSSH()
if config.EstimateTimeout {
go runEstimateTimeout()
} else {
info.Println("timeout estimation disabled")
}
var wg sync.WaitGroup
wg.Add(len(listenProxy))
for _, proxy := range listenProxy {
go proxy.Serve(&wg, quit)
}
wg.Wait()
if relaunch {
info.Println("Relunching cow...")
// Need to fork me.
argv0, err := lookPath()
if nil != err {
errl.Println(err)
return
}
err = syscall.Exec(argv0, os.Args, os.Environ())
if err != nil {
errl.Println(err)
}
}
debug.Println("the main process is , exiting...")
}