forked from danbai225/gpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
301 lines (291 loc) · 7.2 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
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"
"github.com/shirou/gopsutil/v3/net"
"github.com/wailsapp/wails/v2/pkg/runtime"
"io"
"net/http"
"os"
"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) })
}
func (a *App) testPing() {
a.PingAll()
tick := time.Tick(time.Second * 5)
for range tick {
a.PingAll()
}
}
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 {
a.gamePeer = a.conf.PeerList[0]
a.httpPeer = a.conf.PeerList[0]
}
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://mirror.ghproxy.com/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://mirror.ghproxy.com/https://github.com/SagerNet/sing-geoip/releases/latest/download/geoip.db")
if err2 == nil {
_ = os.WriteFile(geoPath, rdata, 0o644)
}
}
}
} else {
rdata, err2 := httpGet("https://mirror.ghproxy.com/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://mirror.ghproxy.com/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://mirror.ghproxy.com/https://github.com/SagerNet/sing-geosite/releases/latest/download/geosite.db")
if err2 == nil {
_ = os.WriteFile(geoPath, rdata, 0o644)
}
}
}
} else {
rdata, err2 := httpGet("https://mirror.ghproxy.com/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, _ := net.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 {
return a.conf.PeerList
}
func (a *App) Add(token string) string {
if a.conf.PeerList == nil {
a.conf.PeerList = make([]*config.Peer, 0)
}
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
break
}
}
for _, peer := range a.conf.PeerList {
if peer.Name == http {
a.httpPeer = peer
break
}
}
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)
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 resp.Body.Close()
return io.ReadAll(resp.Body)
}