-
Notifications
You must be signed in to change notification settings - Fork 311
/
main.go
161 lines (138 loc) · 4.41 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
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
// Copyright (C) 2020-2021, IrineSistiana
//
// This file is part of mosdns.
//
// mosdns is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// mosdns is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"flag"
"fmt"
"github.com/IrineSistiana/mosdns/dispatcher/coremain"
"github.com/IrineSistiana/mosdns/dispatcher/mlog"
"github.com/IrineSistiana/mosdns/tools"
"os"
"path/filepath"
"runtime"
//DEBUG ONLY
"net/http"
_ "net/http/pprof"
)
var (
version = "dev/unknown"
configPath = flag.String("c", "config.yaml", "[path] load config from file")
genConfigTo = flag.String("gen", "", "[path] generate a config template here")
dir = flag.String("dir", "", "[path] change working directory to here")
dirFollowExecutable = flag.Bool("dir2exe", false, "change working directory to the executable that started the current process")
showVersion = flag.Bool("v", false, "show version info")
probeServerTimeout = flag.String("probe-server-timeout", "", "[protocol://ip:port] probe server's idle timeout, protocol can be tcp or dot")
benchIPMatcherFile = flag.String("bench-ip-matcher", "", "[path] benchmark ip search using this file")
benchDomainMatcherFile = flag.String("bench-domain-matcher", "", "[path] benchmark domain search using this file")
convV2IPDat = flag.String("conv-v2ray-ip-dat", "", "[path] convert v2ray ip data file to text")
convV2DomainDat = flag.String("conv-v2ray-domain-dat", "", "[path] convert v2ray domain data file to text")
//DEBUG ONLY
cpu = flag.Int("cpu", runtime.NumCPU(), "the maximum number of CPUs that can be executing simultaneously")
pprofAddr = flag.String("pprof", "", "[ip:port] DEBUG ONLY, hook http/pprof at this address")
)
func main() {
flag.Parse()
// DEBUG ONLY
runtime.GOMAXPROCS(*cpu)
if len(*pprofAddr) != 0 {
go func() {
mlog.S().Infof("pprof backend is starting at: %v", *pprofAddr)
if err := http.ListenAndServe(*pprofAddr, nil); err != nil {
mlog.S().Fatalf("pprof backend is exited: %v", err)
}
}()
}
// helper function
// show version
if *showVersion {
fmt.Printf("%s\n", version)
os.Exit(0)
}
// idle timeout test
if len(*probeServerTimeout) != 0 {
err := tools.ProbServerTimeout(*probeServerTimeout)
if err != nil {
mlog.S().Error(err)
}
os.Exit(0)
}
// bench
if len(*benchIPMatcherFile) != 0 {
err := tools.BenchIPMatcher(*benchIPMatcherFile)
if err != nil {
mlog.S().Errorf("bench ip list failed, %v", err)
}
os.Exit(0)
}
if len(*benchDomainMatcherFile) != 0 {
err := tools.BenchDomainMatcher(*benchDomainMatcherFile)
if err != nil {
mlog.S().Errorf("bench domain list failed, %v", err)
}
os.Exit(0)
}
// convert
if len(*convV2IPDat) != 0 {
err := tools.ConvertIPDat(*convV2IPDat)
if err != nil {
mlog.S().Error(err)
}
os.Exit(0)
}
if len(*convV2DomainDat) != 0 {
err := tools.ConvertDomainDat(*convV2DomainDat)
if err != nil {
mlog.S().Error(err)
}
os.Exit(0)
}
// generate config
if len(*genConfigTo) != 0 {
err := coremain.GenConfig(*genConfigTo)
if err != nil {
mlog.S().Errorf("can not generate config template, %v", err)
} else {
mlog.S().Info("config template generated")
}
os.Exit(0)
}
// main program starts here
// show summary
mlog.S().Infof("mosdns ver: %s", version)
mlog.S().Infof("arch: %s, os: %s, go: %s", runtime.GOARCH, runtime.GOOS, runtime.Version())
// try to change working dir to os.Executable() or *dir
var wd string
if *dirFollowExecutable {
ex, err := os.Executable()
if err != nil {
mlog.S().Fatalf("failed to get executable path: %v", err)
}
wd = filepath.Dir(ex)
} else {
if len(*dir) != 0 {
wd = *dir
}
}
if len(wd) != 0 {
err := os.Chdir(wd)
if err != nil {
mlog.S().Fatalf("failed to change the current working directory: %v", err)
}
mlog.S().Infof("current working directory: %s", wd)
}
coremain.Run(*configPath)
}