-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmain_windows.go
127 lines (107 loc) · 2.75 KB
/
main_windows.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
//go:build windows
package main
import (
"context"
"embed"
"flag"
"fmt"
"github.com/metacubex/mihomo/hub/executor"
"github.com/metacubex/mihomo/hub/route"
"github.com/metacubex/mihomo/log"
"github.com/wailsapp/wails/v2"
"github.com/wailsapp/wails/v2/pkg/options"
"github.com/wailsapp/wails/v2/pkg/options/assetserver"
"github.com/wailsapp/wails/v2/pkg/runtime"
"pandora-box/backend/api"
"pandora-box/backend/cache"
"pandora-box/backend/constant"
"pandora-box/backend/meta"
IsAdmin "pandora-box/backend/system/admin"
"pandora-box/backend/system/proxy"
"pandora-box/backend/tools"
goruntime "runtime"
"time"
)
var devFlag = flag.Bool("dev", false, "布尔类型参数")
func init() {
flag.Parse()
}
//go:embed all:frontend/dist
var assets embed.FS
//go:embed build/540x540.png
var icon []byte
func main() {
meta.Init()
log.Infoln("Pandora-Box %s %s %s with %s",
constant.PandoraVersion, goruntime.GOOS, goruntime.GOARCH, goruntime.Version())
route.Register(api.Hello)
route.Register(api.Version)
route.Register(api.Profile)
route.Register(api.Getter)
route.Register(api.System)
route.Register(api.Ignore)
route.Register(api.MyRules)
route.Register(api.Filter)
addr := startHttpApi()
meta.SwitchProfile(false)
app := NewApp(addr)
option := &options.App{
Title: "Pandora-Box",
Width: 1200,
Height: 785,
MinWidth: 925,
MinHeight: 675,
AssetServer: &assetserver.Options{
Assets: assets,
},
OnBeforeClose: func(ctx context.Context) (prevent bool) {
value := cache.Get(constant.QuitSignal)
if value != nil && string(value) == "1" {
_ = cache.Put(constant.QuitSignal, []byte("0"))
return false
}
runtime.WindowMinimise(ctx)
return true
},
OnStartup: app.startup,
OnShutdown: func(ctx context.Context) {
executor.Shutdown()
proxy.RemoveProxy()
_ = IsAdmin.KillProcessesByName("Pandora-Box")
},
Bind: []interface{}{
app,
},
}
err := wails.Run(option)
if err != nil {
log.Errorln("wails.Run Error:", err)
}
}
func startHttpApi() (addr string) {
var secret string
value := cache.Get(constant.SecretKey)
if value != nil {
secret = string(value)
} else {
secret = tools.String(32)
_ = cache.Put(constant.SecretKey, []byte(secret))
}
addr = route.StartByPandora(*devFlag, secret)
headers := map[string]string{
"Authorization": fmt.Sprintf("Bearer %s", secret),
}
timeOut := 500 * time.Millisecond
for i := 0; i < 3; i++ {
okUrl := fmt.Sprintf("http://%s/ok", addr)
body, _, err := tools.HttpGetWithTimeout(okUrl, timeOut, false, headers)
if err == nil && string(body) == "ok" {
log.Infoln("Start Http Serve Success.Addr is %s", addr)
break
} else {
log.Errorln("Start Http Serve Error: %v.Addr is %s", err, addr)
}
time.Sleep(timeOut)
}
return
}