-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.go
214 lines (183 loc) · 5.69 KB
/
core.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
package main
import (
"fmt"
"log"
"net"
"os"
"path/filepath"
"strings"
"sync"
"gopkg.in/yaml.v3"
)
// CoreConfig core配置信息
type CoreConfig struct {
// 本程序需要的一些配置字段
Port int
MixedPort int
ExternalController string
Secret string
ExternalUi string
ExternalUiName string
// 额外自定义字段,不在yaml配置文件中
ExternalUiAddr string // 外部ui地址
OfficialUiAddr string // 官方ui地址
YACDUiAddr string // Yet Another Clash Dashboard ui地址
HttpProxyPort int // http代理端口
}
var (
coreDir string // core工作目录
coreName string // core程序名称
corePath string // core程序路径
coreConfigPath string // core配置文件路径
coreRunConfigPath string // core实际运行配置文件路径
coreConfig = &CoreConfig{
// 配置文件中不存在时需要赋予默认值的选项
ExternalController: "127.0.0.1:9090",
ExternalUi: "ui",
}
mutex sync.Mutex // 互斥锁
)
// 加载配置文件
func loadCoreConfig() error {
if coreConfigPath == "" {
coreConfigPath = filepath.Join(coreDir, "config.yaml")
}
if !isFileExist(coreConfigPath) {
return fmt.Errorf("config file not found: %s", coreConfigPath)
}
configBytes, err := os.ReadFile(coreConfigPath)
if err != nil {
return fmt.Errorf("failed to read config file: %v", err)
}
// 解析yaml至map以获取到所有配置
var configMap = map[string]any{}
if err = yaml.Unmarshal(configBytes, &configMap); err != nil {
return fmt.Errorf("failed to unmarshal config file: %v", err)
}
// 开始从map中获取配置项
if configMap["port"] != nil {
coreConfig.Port = configMap["port"].(int)
coreConfig.HttpProxyPort = coreConfig.Port
}
if configMap["mixed-port"] != nil {
coreConfig.MixedPort = configMap["mixed-port"].(int)
coreConfig.HttpProxyPort = coreConfig.MixedPort
}
if configMap["external-controller"] != nil && configMap["external-controller"] != "" {
coreConfig.ExternalController = configMap["external-controller"].(string)
} else {
// 赋默认值
configMap["external-controller"] = coreConfig.ExternalController
}
if configMap["secret"] != nil && configMap["secret"] != "" {
coreConfig.Secret = configMap["secret"].(string)
}
if configMap["external-ui"] != nil && configMap["external-ui"] != "" {
coreConfig.ExternalUi = configMap["external-ui"].(string)
} else {
// 赋默认值
configMap["external-ui"] = coreConfig.ExternalUi
}
if configMap["external-ui-name"] != nil && configMap["external-ui-name"] != "" {
coreConfig.ExternalUiName = configMap["external-ui-name"].(string)
}
if coreConfig.HttpProxyPort == 0 {
return fmt.Errorf("http proxy port not set")
}
if host, port, err := net.SplitHostPort(coreConfig.ExternalController); err == nil {
uiUrlPath := "/ui"
if coreConfig.ExternalUiName != "" {
// 去除开头/末尾的斜杠
uiUrlPath += "/" + strings.Trim(coreConfig.ExternalUiName, "/")
}
if host == "" || host == "0.0.0.0" {
// 形如 :9090 的格式,监听的是所有地址,管理面板就默认使用本地地址
host = "127.0.0.1"
}
// 本地面板地址
coreConfig.ExternalUiAddr = fmt.Sprintf("http://%s%s/?hostname=%s&port=%s&secret=%s",
net.JoinHostPort(host, port), uiUrlPath, host, port, coreConfig.Secret)
// 官方面板地址
coreConfig.OfficialUiAddr = fmt.Sprintf("https://metacubex.github.io/metacubexd/?http=true&hostname=%s&port=%s&secret=%s",
host, port, coreConfig.Secret)
// Yet Another Clash Dashboard
coreConfig.YACDUiAddr = fmt.Sprintf("https://yacd.metacubex.one/?hostname=%s&port=%s&secret=%s",
host, port, coreConfig.Secret)
}
var out []byte
if out, err = yaml.Marshal(&configMap); err != nil {
return fmt.Errorf("failed to marshal config file: %v", err)
}
if coreRunConfigPath == "" {
coreRunConfigPath = filepath.Join(coreDir, "config.auto-gen")
}
// 保存到运行配置文件
if err = os.WriteFile(coreRunConfigPath, out, 0644); err != nil {
return fmt.Errorf("failed to write run config file: %v", err)
}
log.Println("Core config loaded:", coreConfigPath)
return nil
}
// 启动core程序
func startCore() bool {
mutex.Lock()
defer mutex.Unlock()
if isCoreRunning() {
log.Println("Core is already running")
return true
}
// 启动core程序
cmd := execCommand(corePath, "-d", coreDir, "-f", coreRunConfigPath)
// 重定向输出到log
cmd.Stdout = log.Writer()
cmd.Stderr = log.Writer()
//cmd.Stdin = nil
if err := cmd.Start(); err != nil {
log.Println("Failed to start core:", err)
return false
}
log.Println("Core started")
return true
}
// 停止core程序
func stopCore() bool {
mutex.Lock()
defer mutex.Unlock()
if !isCoreRunning() {
log.Println("Core is not running")
return true
}
// 结束进程
if err := killProcessGracefully(coreName); err != nil {
// 优雅停止失败,直接强制结束进程
log.Println("Failed to stop core gracefully:", err)
if err = killProcess(coreName); err != nil {
log.Println("Failed to stop core:", err)
return false
}
}
log.Println("Core stopped")
return true
}
// 重启core程序
func restartCore() bool {
return stopCore() && startCore()
}
// 检查core程序是否正在运行
func isCoreRunning() bool {
return isProcessRunning(coreName)
}
// 设置系统代理为core配置的代理
func setCoreProxy() bool {
return setProxyWithDefaultBypass(true, fmt.Sprintf("127.0.0.1:%d", coreConfig.HttpProxyPort))
}
// 获取core版本号
func getCoreVersion() string {
if output, err := execCommand(corePath, "-v").CombinedOutput(); err == nil {
split := strings.Split(string(output), " ")
if len(split) > 2 && split[0] == CoreShowName {
return split[2]
}
}
return ""
}