-
Notifications
You must be signed in to change notification settings - Fork 188
/
Copy pathmain.go
executable file
·318 lines (292 loc) · 10.3 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
_ "net/http/pprof"
"os"
"path/filepath"
"runtime"
"runtime/debug"
"strings"
"time"
"github.com/yinqiwen/gotoolkit/ots"
"github.com/yinqiwen/gsnova/common/channel"
_ "github.com/yinqiwen/gsnova/common/channel/common"
"github.com/yinqiwen/gsnova/common/helper"
"github.com/yinqiwen/gsnova/common/logger"
"github.com/yinqiwen/gsnova/local"
"github.com/yinqiwen/gsnova/remote"
)
func printASCIILogo() {
logo := `
___ ___ ___ ___ ___ ___
/\ \ /\ \ /\__\ /\ \ /\__\ /\ \
/::\ \ /::\ \ /::| | /::\ \ /:/ / /::\ \
/:/\:\ \ /:/\ \ \ /:|:| | /:/\:\ \ /:/ / /:/\:\ \
/:/ \:\ \ _\:\~\ \ \ /:/|:| |__ /:/ \:\ \ /:/__/ ___ /::\~\:\ \
/:/__/_\:\__\/\ \:\ \ \__\/:/ |:| /\__\/:/__/ \:\__\|:| | /\__\/:/\:\ \:\__\
\:\ /\ \/__/\:\ \:\ \/__/\/__|:|/:/ /\:\ \ /:/ /|:| |/:/ /\/__\:\/:/ /
\:\ \:\__\ \:\ \:\__\ |:/:/ / \:\ /:/ / |:|__/:/ / \::/ /
\:\/:/ / \:\/:/ / |::/ / \:\/:/ / \::::/__/ /:/ /
\::/ / \::/ / /:/ / \::/ / ~~~~ /:/ /
\/__/ \/__/ \/__/ \/__/ \/__/
`
fmt.Println(logo)
}
func main() {
// if err := agent.Listen(agent.Options{}); err != nil {
// log.Fatal(err)
// }
path, err := filepath.Abs(os.Args[0])
if nil != err {
fmt.Println(err)
return
}
//common options
otsListen := flag.String("ots", "", "Online trouble shooting listen address")
pprofAddr := flag.String("pprof", "", "PProf trouble shooting listen address")
version := flag.Bool("version", false, "Print version.")
cmd := flag.Bool("cmd", false, "Launch gsnova by command line without config file.")
isClient := flag.Bool("client", false, "Launch gsnova as client.")
isServer := flag.Bool("server", false, "Launch gsnova as server.")
pid := flag.String("pid", ".gsnova.pid", "PID file")
conf := flag.String("conf", "", "Config file of gsnova.")
key := flag.String("key", "809240d3a021449f6e67aa73221d42df942a308a", "Cipher key for transmission between local&remote.")
log := flag.String("log", "color,gsnova.log", "Log file setting")
window := flag.String("window", "", "Max mux stream window size, default 512K")
windowRefresh := flag.String("window_refresh", "", "Mux stream window refresh size, default 32K")
pingInterval := flag.Int("ping_interval", 30, "Channel ping interval seconds.")
streamIdle := flag.Int("stream_idle", 10, "Mux stream idle timout seconds.")
user := flag.String("user", "gsnova", "Username for remote server to authorize.")
var whilteList, blackList channel.HopServers
flag.Var(&whilteList, "whitelist", "Proxy whitelist item config")
flag.Var(&blackList, "blackList", "Proxy blacklist item config")
gcInterval := flag.Int("gc_interval", -1, "Manual GC every interval secs.")
//client options
admin := flag.String("admin", "", "Client Admin listen address")
cnip := flag.String("cnip", "./cnipset.txt", "China IP list.")
mitm := flag.Bool("mitm", false, "Launch gsnova as a MITM Proxy")
httpDumpDest := flag.String("httpdump.dst", "", "HTTP Dump destination file or http url")
var httpDumpFilters channel.HopServers
flag.Var(&httpDumpFilters, "httpdump.filter", "HTTP Dump Domain Filter, eg:*.google.com")
var hops, forwards channel.HopServers
home, _ := filepath.Split(path)
hosts := flag.String("hosts", "./hosts.json", "Hosts file of gsnova client.")
flag.Var(&hops, "remote", "Next remote proxy hop server to connect for client, eg:wss://xxx.paas.com")
flag.Var(&forwards, "forward", "Forward connection to specified address")
p2p := flag.String("p2p", "", "P2P Token.")
servable := flag.Bool("servable", false, "Client as a proxy server for peer p2p client")
proxy := flag.String("proxy", "", "Proxy setting to connect remote server.")
upnpPort := flag.Int("upnp", 0, "UPNP port to expose for p2p.")
p2s2p := flag.Bool("p2s2p", false, "Connect two peers by P2S2P mode.")
//client or server listen
var listens channel.HopServers
flag.Var(&listens, "listen", "Listen on address.")
//server options
tlsKey := flag.String("tls.key", "", "TLS Key file")
tlsCert := flag.String("tls.cert", "", "TLS Cert file")
flag.Parse()
if *version {
fmt.Printf("GSnova version:%s\n", channel.Version)
return
}
printASCIILogo()
confile := *conf
runAsClient := false
if !(*isServer) && !(*isClient) {
runAsClient = true
} else if *isClient != *isServer {
runAsClient = *isClient
} else {
logger.Error("GSnova can not run both as client & server.")
return
}
if len(*otsListen) > 0 {
err := ots.StartTroubleShootingServer(*otsListen)
if nil != err {
logger.Error("Failed to start admin server with reason:%v", err)
}
}
if len(*pprofAddr) > 0 {
go func() {
http.ListenAndServe(*pprofAddr, nil)
}()
}
if *gcInterval > 0 {
go func() {
for {
runtime.GC()
debug.FreeOSMemory()
time.Sleep(time.Duration(*gcInterval) * time.Second)
}
}()
}
if runAsClient {
options := local.ProxyOptions{
Home: home,
Hosts: *hosts,
CNIP: *cnip,
}
local.GConf.UPNPExposePort = *upnpPort
if *cmd {
if len(hops) == 0 {
logger.Error("At least one -hop argument required.", err)
flag.PrintDefaults()
return
}
if len(listens) == 0 {
if len(*p2p) == 0 || !(*servable) {
logger.Error("At least one -listen argument required.", err)
flag.PrintDefaults()
return
}
}
local.GConf.Admin.Listen = *admin
channelName := "default"
if strings.EqualFold(hops[0], channel.DirectChannelName) {
channelName = channel.DirectChannelName
}
local.GConf.Mux.MaxStreamWindow = *window
local.GConf.Mux.StreamMinRefresh = *windowRefresh
local.GConf.Mux.StreamIdleTimeout = *streamIdle
local.GConf.Cipher.Key = *key
local.GConf.Cipher.Method = "auto"
local.GConf.Cipher.User = *user
local.GConf.Log = strings.Split(*log, ",")
for _, lis := range listens {
proxyConf := local.ProxyConfig{}
proxyConf.MITM = *mitm
proxyConf.Local = lis
proxyConf.HTTPDump.Dump = *httpDumpDest
proxyConf.HTTPDump.Domain = httpDumpFilters
proxyConf.PAC = []local.PACConfig{{Remote: channelName}}
local.GConf.Proxy = append(local.GConf.Proxy, proxyConf)
}
for i, forward := range forwards {
if len(local.GConf.Proxy) > i {
local.GConf.Proxy[i].Forward = forward
}
}
if !strings.EqualFold(hops[0], channel.DirectChannelName) {
ch := channel.ProxyChannelConfig{}
ch.Enable = true
ch.Name = "default"
ch.ConnsPerServer = 3
ch.HeartBeatPeriod = *pingInterval
ch.ServerList = []string{hops[0]}
ch.Hops = hops[1:]
ch.P2PToken = *p2p
ch.P2S2PEnable = *p2s2p
ch.Proxy = *proxy
local.GConf.Channel = []channel.ProxyChannelConfig{ch}
}
local.GConf.ProxyLimit.WhiteList = whilteList
local.GConf.ProxyLimit.BlackList = blackList
if len(whilteList) == 0 && len(blackList) == 0 && *servable {
local.GConf.ProxyLimit.WhiteList = []string{"*"}
}
remote.InitDefaultConf()
remote.ServerConf.Cipher = local.GConf.Cipher
remote.ServerConf.Mux = local.GConf.Mux
remote.ServerConf.Cipher.AllowUsers(remote.ServerConf.Cipher.User)
channel.DefaultServerCipher = remote.ServerConf.Cipher
options.WatchConf = false
err = local.Start(options)
} else {
if len(confile) == 0 {
confile = "./client.json"
}
options.WatchConf = true
options.Config = confile
err = local.Start(options)
}
if nil != err {
logger.Error("Start gsnova error:%v", err)
}
} else {
//run as server
remote.InitDefaultConf()
if !(*cmd) {
if len(confile) == 0 {
confile = "./server.json"
}
if _, err := os.Stat(confile); nil == err {
logger.Info("Load server conf from file:%s", confile)
data, err := helper.ReadWithoutComment(confile, "//")
//data, err := ioutil.ReadFile(file)
if nil == err {
err = json.Unmarshal(data, &remote.ServerConf)
}
if nil != err {
logger.Error("Failed to load server config:%s for reason:%v", confile, err)
return
}
}
}
if *cmd {
if len(listens) == 0 {
logger.Error("At least one -listen argument required.", err)
flag.PrintDefaults()
return
}
for _, lis := range listens {
var lisCfg remote.ServerListenConfig
lisCfg.Listen = lis
if len(*tlsCert) > 0 && len(*tlsKey) > 0 {
lisCfg.Cert = *tlsCert
lisCfg.Key = *tlsKey
}
lisCfg.KCParams.InitDefaultConf()
config := &lisCfg.KCParams
switch config.Mode {
case "normal":
config.NoDelay, config.Interval, config.Resend, config.NoCongestion = 0, 40, 2, 1
case "fast":
config.NoDelay, config.Interval, config.Resend, config.NoCongestion = 0, 30, 2, 1
case "fast2":
config.NoDelay, config.Interval, config.Resend, config.NoCongestion = 1, 20, 2, 1
case "fast3":
config.NoDelay, config.Interval, config.Resend, config.NoCongestion = 1, 10, 2, 1
}
remote.ServerConf.Server = append(remote.ServerConf.Server, lisCfg)
}
if len(*key) > 0 {
remote.ServerConf.Cipher.Key = *key
}
if len(*user) > 0 {
remote.ServerConf.Cipher.User = *user
}
if len(*log) > 0 {
remote.ServerConf.Log = strings.Split(*log, ",")
}
if len(*window) > 0 {
remote.ServerConf.Mux.MaxStreamWindow = *window
}
if len(*windowRefresh) > 0 {
remote.ServerConf.Mux.StreamMinRefresh = *windowRefresh
}
}
cipherKey := os.Getenv("GSNOVA_CIPHER_KEY")
if len(cipherKey) > 0 {
remote.ServerConf.Cipher.Key = cipherKey
logger.Notice("Server cipher key overide by env:GSNOVA_CIPHER_KEY")
}
channel.DefaultServerRateLimit = remote.ServerConf.RateLimit
channel.SetDefaultMuxConfig(remote.ServerConf.Mux)
remote.ServerConf.Cipher.AllowUsers(remote.ServerConf.Cipher.User)
channel.DefaultServerCipher = remote.ServerConf.Cipher
logger.InitLogger(remote.ServerConf.Log)
logger.Info("Load server conf success.")
confdata, _ := json.MarshalIndent(&remote.ServerConf, "", " ")
logger.Info("GSnova server:%s start with config:\n%s", channel.Version, string(confdata))
remote.StartRemoteProxy()
}
if len(*pid) > 0 {
ioutil.WriteFile(*pid, []byte(fmt.Sprintf("%d", os.Getpid())), os.ModePerm)
}
ch := make(chan int)
<-ch
}