/
controller.go
500 lines (465 loc) · 11.2 KB
/
controller.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
package local
import (
"fmt"
"net/http"
"net/url"
"os"
"path"
"runtime"
"strconv"
"time"
"github.com/missdeer/avege/common"
"github.com/missdeer/avege/common/cache"
"github.com/missdeer/avege/config"
"github.com/gin-gonic/gin"
"github.com/kardianos/osext"
"github.com/missdeer/avege/inbound"
"github.com/missdeer/avege/rule"
)
var (
p *os.Process
startAt = time.Now()
)
func pong(c *gin.Context) {
c.String(http.StatusOK, "pong")
}
func clearDNSCache(c *gin.Context) {
domain := c.PostForm("domain")
if len(domain) == 0 {
c.JSON(http.StatusOK, gin.H{
"Result": "Error",
"Error": "Domain name is required.",
})
return
}
if err := cache.Instance.Delete(fmt.Sprintf("dns:%s.:cachekey", domain)); err != nil {
c.JSON(http.StatusOK, gin.H{
"Result": "Error",
"Error": err,
})
} else {
c.JSON(http.StatusOK, gin.H{
"Result": "OK",
})
}
}
func updateIptablesRulesHandler(c *gin.Context) {
if runtime.GOOS == "linux" {
if inbound.IsModeEnabled("redir") {
go rule.UpdateRedirFirewallRules()
c.JSON(http.StatusOK, gin.H{
"Result": "OK",
})
} else {
c.JSON(http.StatusOK, gin.H{
"Result": "not redir mode, can't generate iptables rules",
})
}
} else {
c.JSON(http.StatusOK, gin.H{
"Result": "host is not Linux, can't generate iptables rules",
})
}
}
func getTokenHandler(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"Result": "OK",
"Token": config.Configurations.Generals.Token,
})
}
func getSelectServerHandler(c *gin.Context) {
id := c.Param("id")
next, _ := url.QueryUnescape(c.Query("next"))
defer c.Redirect(http.StatusFound, "/h5/?"+next)
if len(id) == 0 {
return
}
if smartLastUsedBackendInfo != nil && smartLastUsedBackendInfo.id == id {
return
}
statistics.RLock()
defer statistics.RUnlock()
for server := range statistics.StatisticMap {
if server.id == id {
smartLastUsedBackendInfo = server
return
}
}
}
func postSelectServerHandler(c *gin.Context) {
id := c.PostForm("id")
if len(id) == 0 {
c.JSON(http.StatusOK, gin.H{
"Result": "id is required",
})
return
}
if smartLastUsedBackendInfo != nil && smartLastUsedBackendInfo.id == id {
c.JSON(http.StatusOK, gin.H{
"Result": fmt.Sprintf("server with id %s does be used now, no need to change", id),
})
return
}
statistics.RLock()
defer statistics.RUnlock()
for server := range statistics.StatisticMap {
if server.id == id {
smartLastUsedBackendInfo = server
c.JSON(http.StatusOK, gin.H{
"Result": "OK",
"Msg": fmt.Sprintf("change current using server to %s", server.address),
})
return
}
}
c.JSON(http.StatusOK, gin.H{
"Result": fmt.Sprintf("can't find server info with id %s", id),
})
return
}
func forceUpdateSmartUsedServerInfoHandler(c *gin.Context) {
forceUpdateSmartLastUsedBackendInfo = true
c.JSON(http.StatusOK, gin.H{
"Result": "OK",
})
}
func getMethodHandler(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"Result": "OK",
"Msg": config.DefaultMethod,
})
}
func setMethodHandler(c *gin.Context) {
methodStr := c.PostForm("method")
if len(methodStr) == 0 {
c.JSON(http.StatusOK, gin.H{
"Result": "missing method field",
})
return
}
methodMap := map[string]bool{
"aes-128-cfb": true,
"aes-256-cfb": true,
"aes-192-cfb": true,
"aes-128-ctr": true,
"aes-256-ctr": true,
"aes-192-ctr": true,
"aes-128-ofb": true,
"aes-256-ofb": true,
"aes-192-ofb": true,
"des-cfb": true,
"bf-cfb": true,
"cast5-cfb": true,
"rc4-md5": true,
"chacha20": true,
"chacha20-ietf": true,
"salsa20": true,
"camellia-128-cfb": true,
"camellia-192-cfb": true,
"camellia-256-cfb": true,
"idea-cfb": true,
"rc2-cfb": true,
"seed-cfb": true,
}
_, ok := methodMap[methodStr]
if !ok {
c.JSON(http.StatusOK, gin.H{
"Result": fmt.Sprintf("unsupported method %s", methodStr),
})
return
}
config.DefaultMethod = methodStr
c.JSON(http.StatusOK, gin.H{
"Result": "OK",
})
}
func getKeyHandler(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"Result": "OK",
"Msg": config.DefaultKey,
})
}
func setKeyHandler(c *gin.Context) {
keyStr := c.PostForm("key")
if len(keyStr) == 0 {
c.JSON(http.StatusOK, gin.H{
"Result": "missing key field",
})
return
}
config.DefaultKey = keyStr
c.JSON(http.StatusOK, gin.H{
"Result": "OK",
})
}
func getPortHandler(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"Result": "OK",
"Msg": config.DefaultPort,
})
}
func setPortHandler(c *gin.Context) {
portStr := c.PostForm("port")
if len(portStr) == 0 {
c.JSON(http.StatusOK, gin.H{
"Result": "missing port field",
})
return
}
p, e := strconv.Atoi(portStr)
if e != nil {
c.JSON(http.StatusOK, gin.H{
"Result": fmt.Sprintln("invalid port value", e),
})
return
}
if p < 1024 || p > 65535 {
c.JSON(http.StatusOK, gin.H{
"Result": fmt.Sprintf("port value %d out of range[1024, 65535]", p),
})
return
}
config.DefaultPort = portStr
c.JSON(http.StatusOK, gin.H{
"Result": "OK",
})
}
func addServerFullHandler(c *gin.Context) {
address := c.PostForm("address")
if len(address) == 0 {
c.JSON(http.StatusOK, gin.H{
"Result": "missing server address",
})
return
}
portStr := c.PostForm("port")
if len(portStr) == 0 {
c.JSON(http.StatusOK, gin.H{
"Result": "missing port field",
})
return
}
keyStr := c.PostForm("key")
if len(keyStr) == 0 {
c.JSON(http.StatusOK, gin.H{
"Result": "missing key field",
})
return
}
methodStr := c.PostForm("method")
if len(methodStr) == 0 {
c.JSON(http.StatusOK, gin.H{
"Result": "missing method field",
})
return
}
methodMap := map[string]bool{
"aes-128-cfb": true,
"aes-256-cfb": true,
"aes-192-cfb": true,
"aes-128-ctr": true,
"aes-256-ctr": true,
"aes-192-ctr": true,
"aes-128-ofb": true,
"aes-256-ofb": true,
"aes-192-ofb": true,
"des-cfb": true,
"bf-cfb": true,
"cast5-cfb": true,
"rc4-md5": true,
"chacha20": true,
"chacha20-ietf": true,
"salsa20": true,
"camellia-128-cfb": true,
"camellia-192-cfb": true,
"camellia-256-cfb": true,
"idea-cfb": true,
"rc2-cfb": true,
"seed-cfb": true,
}
_, ok := methodMap[methodStr]
if !ok {
c.JSON(http.StatusOK, gin.H{
"Result": fmt.Sprintf("unsupported method %s", methodStr),
})
return
}
config.DefaultPort = portStr
config.DefaultKey = keyStr
config.DefaultMethod = methodStr
addServer(address)
c.JSON(http.StatusOK, gin.H{
"Result": "OK",
})
}
func addServerHandler(c *gin.Context) {
address := c.PostForm("address")
if len(address) > 0 {
addServer(address)
c.JSON(http.StatusOK, gin.H{
"Result": "OK",
})
} else {
c.JSON(http.StatusOK, gin.H{
"Result": "missing server address",
})
}
}
func removeServerHandler(c *gin.Context) {
address := c.PostForm("address")
if len(address) > 0 {
removeServer(address)
c.JSON(http.StatusOK, gin.H{
"Result": "OK",
})
} else {
c.JSON(http.StatusOK, gin.H{
"Result": "missing server address",
})
}
}
func startSSHReverseHandler(c *gin.Context) {
executable, _ := osext.Executable()
binDir := path.Dir(executable)
exe := path.Join(binDir, "ngrok")
args := []string{
exe,
fmt.Sprintf("-config=%s", path.Join(binDir, "ngrok.cfg")),
"-log=stdout",
"start",
"ssh",
}
var procAttr os.ProcAttr
procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
var err error
p, err = os.StartProcess(exe, args, &procAttr)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"Result": fmt.Sprintln("starting ssh reverse connection failed", err),
})
} else {
c.JSON(http.StatusOK, gin.H{
"Result": "OK",
})
go p.Wait()
}
}
func stopSSHReverseHandler(c *gin.Context) {
if p != nil {
err := p.Kill()
if err != nil {
c.JSON(http.StatusOK, gin.H{
"Result": fmt.Sprintln("stopping ssh reverse connection failed", err),
})
} else {
c.JSON(http.StatusOK, gin.H{
"Result": "OK",
})
}
p = nil
} else {
c.JSON(http.StatusOK, gin.H{
"Result": "ssh reverse connection not started yet",
})
}
}
// Report type for statistics report
type Report struct {
Quote int64
TotalDownload uint64
TotalUpload uint64
CurrentUsing string
StartAt time.Time
Uptime string
Stats
}
func createStats(order string, orderBy string) Stats {
stats := make(Stats, 0)
statistics.RLock()
for backendInfo, stat := range statistics.StatisticMap {
s := new(Stat)
s.Id = backendInfo.id
s.Address = backendInfo.address
s.ProtocolType = backendInfo.protocolType
s.FailedCount = stat.GetFailedCount()
s.Latency = stat.GetLatency()
s.TotalDownload = stat.GetTotalDownload()
s.TotalUpload = stat.GetTotalUploaded()
s.HighestLastHourBps = stat.GetHighestLastHourBps()
s.HighestLastTenMinutesBps = stat.GetHighestLastTenMinutesBps()
s.HighestLastMinuteBps = stat.GetHighestLastMinuteBps()
s.HighestLastSecondBps = stat.GetHighestLastSecondBps()
s.LastHourBps = stat.GetLastHourBps()
s.LastTenMinutesBps = stat.GetLastTenMinutesBps()
s.LastMinuteBps = stat.GetLastMinuteBps()
s.LastSecondBps = stat.GetLastSecondBps()
stats = append(stats, s)
}
statistics.RUnlock()
orderStats(order, orderBy, stats)
return stats
}
func createReport(order string, orderBy string) *Report {
stats := createStats(order, orderBy)
currentUsing := "nil"
if smartLastUsedBackendInfo != nil {
currentUsing = smartLastUsedBackendInfo.address
}
return &Report{
Stats: stats,
TotalDownload: common.TotalStat.GetDownload(),
TotalUpload: common.TotalStat.GetUpload(),
Quote: config.LeftQuote,
CurrentUsing: currentUsing,
StartAt: startAt,
Uptime: time.Now().Sub(startAt).String(),
}
}
func createGinH(order string, orderBy string) *gin.H {
stats := createStats(order, orderBy)
currentUsing := "nil"
if smartLastUsedBackendInfo != nil {
currentUsing = smartLastUsedBackendInfo.address
}
newOrders := map[string]string{
"asc": "desc",
"desc": "asc",
}
return &gin.H{
"title": "avege, a powerful anti-GFW toolset",
"stats": stats,
"currentUsing": currentUsing,
"totalDownload": common.TotalStat.GetDownload(),
"totalUpload": common.TotalStat.GetUpload(),
"quote": config.LeftQuote,
"startAt": startAt,
"uptime": time.Now().Sub(startAt).String(),
"order": newOrders[order],
"next": url.QueryEscape(fmt.Sprintf("orderby=%s&order=%s", orderBy, order)),
}
}
func statisticsXMLHandler(c *gin.Context) {
order := c.DefaultQuery("order", "asc")
orderBy := c.DefaultQuery("orderby", "address")
r := createReport(order, orderBy)
c.XML(http.StatusOK, *r)
}
func statisticsJSONHandler(c *gin.Context) {
order := c.DefaultQuery("order", "asc")
orderBy := c.DefaultQuery("orderby", "address")
r := createReport(order, orderBy)
c.JSON(http.StatusOK, *r)
}
func statisticsHTMLHandler(c *gin.Context) {
order := c.DefaultQuery("order", "asc")
orderBy := c.DefaultQuery("orderby", "address")
h := createGinH(order, orderBy)
c.HTML(http.StatusOK, "index.tmpl", *h)
}
func orderStats(order string, orderBy string, stats Stats) {
if sorter, ok := statsSortMap[orderBy]; ok {
sorter(order, stats)
} else {
orderByAddress(order, stats)
}
}