forked from snakem982/Pandora-Box
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_darwin.go
159 lines (137 loc) · 3.4 KB
/
main_darwin.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
//go:build darwin
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/menu"
"github.com/wailsapp/wails/v2/pkg/options"
"github.com/wailsapp/wails/v2/pkg/options/assetserver"
"github.com/wailsapp/wails/v2/pkg/options/mac"
"os"
"os/exec"
"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"
"strings"
"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() {
if !*devFlag && !IsAdmin.Check() {
status, pwd := GetAcStatus()
if status == "3" {
startMacInAdmin(pwd)
return
}
}
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: 780,
MinWidth: 925,
MinHeight: 675,
AssetServer: &assetserver.Options{
Assets: assets,
},
HideWindowOnClose: true,
OnStartup: app.startup,
OnShutdown: func(ctx context.Context) {
executor.Shutdown()
proxy.RemoveProxy()
_ = IsAdmin.KillProcessesByName("Pandora-Box")
},
Bind: []interface{}{
app,
},
}
AppMenu := menu.NewMenu()
AppMenu.Append(menu.AppMenu())
AppMenu.Append(menu.EditMenu())
option.Menu = AppMenu
option.Mac = &mac.Options{
TitleBar: mac.TitleBarHidden(),
About: &mac.AboutInfo{
Title: constant.PandoraVersion,
Message: "Copyright © 2024 snakem982",
Icon: icon,
},
}
option.CSSDragProperty = "widows"
option.CSSDragValue = "1"
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
}
func startMacInAdmin(pwd string) {
exePath, err := os.Executable()
if err != nil {
log.Errorln("get exe path error:%s", err.Error())
return
}
cmd := exec.Command("sudo", "-b", exePath, ">", "/dev/null")
cmd.Stdin = strings.NewReader(pwd)
err = cmd.Run()
if err != nil {
log.Errorln("cmd.Run() error:%s", err.Error())
}
}