-
Notifications
You must be signed in to change notification settings - Fork 15
/
app.go
384 lines (375 loc) · 9.31 KB
/
app.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
package main
import (
"context"
"crypto/sha256"
"fmt"
"github.com/cloverstd/tcping/ping"
"github.com/danbai225/gpp/backend/client"
"github.com/danbai225/gpp/backend/config"
"github.com/danbai225/gpp/backend/data"
"github.com/danbai225/gpp/systray"
box "github.com/sagernet/sing-box"
netutils "github.com/shirou/gopsutil/v3/net"
"github.com/wailsapp/wails/v2/pkg/runtime"
"io"
"net"
"net/http"
"os"
"sort"
"strings"
"sync"
"time"
)
// App struct
type App struct {
ctx context.Context
conf *config.Config
gamePeer *config.Peer
httpPeer *config.Peer
box *box.Box
lock sync.Mutex
}
// NewApp creates a new App application struct
func NewApp() *App {
conf := config.Config{}
app := App{
conf: &conf,
}
return &app
}
func (a *App) systemTray() {
systray.SetIcon(logo) // read the icon from a file
show := systray.AddMenuItem("显示窗口", "显示窗口")
systray.AddSeparator()
exit := systray.AddMenuItem("退出加速器", "退出加速器")
show.Click(func() { runtime.WindowShow(a.ctx) })
exit.Click(func() {
a.Stop()
runtime.Quit(a.ctx)
systray.Quit()
time.Sleep(time.Second)
os.Exit(0)
})
systray.SetOnClick(func(menu systray.IMenu) { runtime.WindowShow(a.ctx) })
go func() {
listener, err := net.Listen("tcp", "127.0.0.1:54713")
if err != nil {
_, _ = runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "监听错误",
Message: fmt.Sprintln("Error listening0:", err),
})
}
var conn net.Conn
for {
conn, err = listener.Accept()
if err != nil {
_, _ = runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "监听错误",
Message: fmt.Sprintln("Error listening1:", err),
})
continue
}
// 读取指令
buffer := make([]byte, 1024)
n, err := conn.Read(buffer)
if err != nil {
_, _ = runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "监听错误",
Message: fmt.Sprintln("Error read:", err),
})
continue
}
command := string(buffer[:n])
// 如果收到显示窗口的命令,则显示窗口
if command == "SHOW_WINDOW" {
// 展示窗口的代码
runtime.WindowShow(a.ctx)
}
_ = conn.Close()
}
}()
}
func (a *App) testPing() {
for {
a.PingAll()
time.Sleep(time.Second * 5)
}
}
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
go systray.Run(a.systemTray, func() {})
loadConfig, err := config.LoadConfig()
if err != nil {
_, _ = runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.WarningDialog,
Title: "配置加载错误",
Message: err.Error(),
})
} else {
a.conf = loadConfig
}
if len(a.conf.PeerList) > 0 {
if a.conf.GamePeer == "" {
a.conf.GamePeer = a.conf.PeerList[0].Name
} else {
for _, peer := range a.conf.PeerList {
if peer.Name == a.conf.GamePeer {
a.gamePeer = peer
}
}
}
if a.conf.HTTPPeer == "" {
a.conf.HTTPPeer = a.conf.PeerList[0].Name
} else {
for _, peer := range a.conf.PeerList {
if peer.Name == a.conf.HTTPPeer {
a.httpPeer = peer
}
}
}
}
go a.testPing()
home, _ := os.UserHomeDir()
geoPath := fmt.Sprintf("%s%c%s%c%s", home, os.PathSeparator, ".gpp", os.PathSeparator, "geoip.db")
file, err := os.ReadFile(geoPath)
if err == nil {
rdata, err2 := httpGet("https://ghp.ci/https://github.com/SagerNet/sing-geoip/releases/latest/download/geoip.db.sha256sum")
if err2 == nil {
sum256 := sha256.Sum256(file)
if fmt.Sprintf("%x", sum256) != string(rdata) {
rdata, err2 = httpGet("https://ghp.ci/https://github.com/SagerNet/sing-geoip/releases/latest/download/geoip.db")
if err2 == nil {
_ = os.WriteFile(geoPath, rdata, 0o644)
}
}
}
} else {
rdata, err2 := httpGet("https://ghp.ci/https://github.com/SagerNet/sing-geoip/releases/latest/download/geoip.db")
if err2 == nil {
_ = os.WriteFile(geoPath, rdata, 0o644)
}
}
geoPath = fmt.Sprintf("%s%c%s%c%s", home, os.PathSeparator, ".gpp", os.PathSeparator, "geosite.db")
file, err = os.ReadFile(geoPath)
if err == nil {
rdata, err2 := httpGet("https://ghp.ci/https://github.com/SagerNet/sing-geosite/releases/latest/download/geosite.db.sha256sum")
if err2 == nil {
sum256 := sha256.Sum256(file)
if fmt.Sprintf("%x", sum256) != string(rdata) {
rdata, err2 = httpGet("https://ghp.ci/https://github.com/SagerNet/sing-geosite/releases/latest/download/geosite.db")
if err2 == nil {
_ = os.WriteFile(geoPath, rdata, 0o644)
}
}
}
} else {
rdata, err2 := httpGet("https://ghp.ci/https://github.com/SagerNet/sing-geosite/releases/latest/download/geosite.db")
if err2 == nil {
_ = os.WriteFile(geoPath, rdata, 0o644)
}
}
}
func (a *App) PingAll() {
a.lock.Lock()
if a.box != nil {
a.lock.Unlock()
return
}
a.lock.Unlock()
group := sync.WaitGroup{}
for i := range a.conf.PeerList {
if a.conf.PeerList[i].Protocol == "direct" {
continue
}
group.Add(1)
peer := a.conf.PeerList[i]
go func() {
defer group.Done()
peer.Ping = pingPort(peer.Addr, peer.Port)
}()
}
group.Wait()
}
func (a *App) Status() *data.Status {
a.lock.Lock()
defer a.lock.Unlock()
status := data.Status{
Running: a.box != nil,
GamePeer: a.gamePeer,
HttpPeer: a.httpPeer,
}
counters, _ := netutils.IOCounters(true)
for _, counter := range counters {
if counter.Name == "utun225" {
status.Up = counter.BytesSent
status.Down = counter.BytesRecv
}
}
return &status
}
func (a *App) List() []*config.Peer {
list := a.conf.PeerList
sort.Slice(list, func(i, j int) bool { return list[i].Ping < list[j].Ping })
return list
}
func (a *App) Add(token string) string {
if a.conf.PeerList == nil {
a.conf.PeerList = make([]*config.Peer, 0)
}
if strings.HasPrefix(token, "http") {
_, err := http.Get(token)
if err != nil {
_, _ = runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "订阅错误",
Message: err.Error(),
})
return err.Error()
}
a.conf.SubAddr = token
} else {
err, peer := config.ParsePeer(token)
if err != nil {
_, _ = runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "导入错误",
Message: err.Error(),
})
return err.Error()
}
for _, p := range a.conf.PeerList {
if p.Name == peer.Name {
_, _ = runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "导入错误",
Message: fmt.Sprintf("节点 %s 已存在", peer.Name),
})
return fmt.Sprintf("peer %s already exists", peer.Name)
}
}
a.conf.PeerList = append(a.conf.PeerList, peer)
}
err := config.SaveConfig(a.conf)
if err != nil {
_, _ = runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "导入错误",
Message: err.Error(),
})
return err.Error()
}
return "ok"
}
func (a *App) Del(Name string) string {
for i, peer := range a.conf.PeerList {
if peer.Name == Name {
a.conf.PeerList = append(a.conf.PeerList[:i], a.conf.PeerList[i+1:]...)
break
}
}
err := config.SaveConfig(a.conf)
if err != nil {
return err.Error()
}
return "ok"
}
func (a *App) SetPeer(game, http string) string {
for _, peer := range a.conf.PeerList {
if peer.Name == game {
a.gamePeer = peer
a.conf.GamePeer = peer.Name
break
}
}
for _, peer := range a.conf.PeerList {
if peer.Name == http {
a.httpPeer = peer
a.conf.HTTPPeer = peer.Name
break
}
}
err := config.SaveConfig(a.conf)
if err != nil {
_, _ = runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "保存错误",
Message: err.Error(),
})
return err.Error()
}
return "ok"
}
// Start 启动加速
func (a *App) Start() string {
a.lock.Lock()
defer a.lock.Unlock()
if a.box != nil {
return "running"
}
var err error
a.box, err = client.Client(a.gamePeer, a.httpPeer, a.conf.ProxyDNS, a.conf.LocalDNS, a.conf.Rules)
if err != nil {
_, _ = runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "加速失败",
Message: err.Error(),
})
a.box = nil
return err.Error()
}
err = a.box.Start()
if err != nil {
_, _ = runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "加速失败",
Message: err.Error(),
})
a.box = nil
return err.Error()
}
return "ok"
}
// Stop 停止加速
func (a *App) Stop() string {
a.lock.Lock()
defer a.lock.Unlock()
if a.box == nil {
return "not running"
}
err := a.box.Close()
if err != nil {
_, _ = runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "停止失败",
Message: err.Error(),
})
return err.Error()
}
a.box = nil
return "ok"
}
func pingPort(host string, port uint16) uint {
tcPing := ping.NewTCPing()
tcPing.SetTarget(&ping.Target{
Host: host,
Port: int(port),
Counter: 1,
Interval: time.Millisecond * 200,
Timeout: time.Second * 3,
})
start := tcPing.Start()
<-start
result := tcPing.Result()
return uint(result.Avg().Milliseconds())
}
func httpGet(url string) ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
return io.ReadAll(resp.Body)
}