-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproxy_utils.go
More file actions
368 lines (311 loc) · 8.95 KB
/
proxy_utils.go
File metadata and controls
368 lines (311 loc) · 8.95 KB
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
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os/exec"
"strings"
"time"
)
// 切换到新的代理的实现函数
func switchProxy(groupName, proxyName string) error {
url := fmt.Sprintf("http://127.0.0.1:9090/proxies/%s", groupName)
// 准备请求数据
requestData := fmt.Sprintf(`{"name":"%s"}`, proxyName)
// 创建请求
req, err := http.NewRequest("PUT", url, strings.NewReader(requestData))
if err != nil {
return err
}
// 设置请求头
req.Header.Set("Content-Type", "application/json")
// 发送请求
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
// 检查响应
if resp.StatusCode != http.StatusNoContent && resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("切换代理失败,状态码: %d, 响应: %s", resp.StatusCode, string(body))
}
return nil
}
// 检查Clash服务是否正在运行的实现函数
func isClashRunning() bool {
cmd := exec.Command("systemctl", "is-active", "--quiet", "clash")
err := cmd.Run()
return err == nil
}
// 获取当前选中的代理
func getSelectedProxy() (*SelectedProxyInfo, error) {
// 发送请求获取代理组信息
resp, err := http.Get("http://127.0.0.1:9090/proxies")
if err != nil {
return nil, err
}
defer resp.Body.Close()
// 读取响应内容
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
// 解析JSON
var result map[string]interface{}
if err := json.Unmarshal(body, &result); err != nil {
return nil, err
}
// 获取所有代理信息
proxies, ok := result["proxies"].(map[string]interface{})
if !ok {
return nil, fmt.Errorf("无法获取代理信息")
}
// 查找选择器类型的代理组
for groupName, groupInfo := range proxies {
groupInfoMap, ok := groupInfo.(map[string]interface{})
if !ok {
continue
}
// 只处理类型为Selector的代理组
if groupType, ok := groupInfoMap["type"].(string); ok && groupType == "Selector" {
// 尝试获取当前选中的代理
if selected, ok := groupInfoMap["now"].(string); ok {
// 跳过包含DIRECT的组
if selected == "DIRECT" {
continue
}
// 找到第一个有效的代理组即返回
return &SelectedProxyInfo{
GroupName: groupName,
SelectedProxy: selected,
}, nil
}
}
}
// 如果没有找到任何代理组或选中的代理,返回错误
return nil, fmt.Errorf("未找到选中的代理")
}
// 获取所有代理列表
func getProxies() ([]string, error) {
// 发送请求获取代理组信息
resp, err := http.Get("http://127.0.0.1:9090/proxies")
if err != nil {
return nil, err
}
defer resp.Body.Close()
// 读取响应内容
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
// 解析JSON
var result map[string]interface{}
if err := json.Unmarshal(body, &result); err != nil {
return nil, err
}
// 获取代理组信息
proxies, ok := result["proxies"].(map[string]interface{})
if !ok {
return nil, fmt.Errorf("无法获取代理信息")
}
// 查找GLOBAL组或第一个Selector类型的组
var groupInfo map[string]interface{}
// 首先检查是否有GLOBAL组
if globalGroup, ok := proxies["GLOBAL"].(map[string]interface{}); ok {
if globalType, ok := globalGroup["type"].(string); ok && globalType == "Selector" {
groupInfo = globalGroup
}
}
// 如果没有找到GLOBAL组,查找第一个Selector类型的组
if groupInfo == nil {
for _, value := range proxies {
if group, ok := value.(map[string]interface{}); ok {
if groupType, ok := group["type"].(string); ok && groupType == "Selector" {
groupInfo = group
break
}
}
}
}
if groupInfo == nil {
return nil, fmt.Errorf("未找到可用的代理组")
}
// 获取所有代理
all, ok := groupInfo["all"].([]interface{})
if !ok {
return nil, fmt.Errorf("无法获取代理列表")
}
// 转换为字符串数组
var proxyList []string
for _, proxy := range all {
if proxyName, ok := proxy.(string); ok {
// 排除特殊代理
if proxyName != "DIRECT" && proxyName != "REJECT" && proxyName != "GLOBAL" {
proxyList = append(proxyList, proxyName)
}
}
}
return proxyList, nil
}
// 更新代理组的实现函数
func updateProxyGroup(config map[string]interface{}, proxyName string) {
// 获取代理组配置
proxyGroups, ok := config["proxy-groups"].([]interface{})
if !ok {
return
}
// 遍历所有代理组,添加新代理到每个组中
for i, groupInterface := range proxyGroups {
group, ok := groupInterface.(map[string]interface{})
if !ok {
continue
}
// 获取代理列表
proxies, ok := group["proxies"].([]interface{})
if !ok {
continue
}
// 检查代理是否已存在
found := false
for _, p := range proxies {
if pName, ok := p.(string); ok && pName == proxyName {
found = true
break
}
}
// 如果不存在,添加新代理
if !found {
proxies = append(proxies, proxyName)
group["proxies"] = proxies
proxyGroups[i] = group
}
}
// 更新配置
config["proxy-groups"] = proxyGroups
}
// 从代理组中移除代理的实现函数
func removeFromProxyGroups(config map[string]interface{}, proxyName string) {
// 获取代理组配置
proxyGroups, ok := config["proxy-groups"].([]interface{})
if !ok {
return
}
// 遍历所有代理组,移除指定代理
for i, groupInterface := range proxyGroups {
group, ok := groupInterface.(map[string]interface{})
if !ok {
continue
}
// 获取代理列表
proxiesInterface, ok := group["proxies"].([]interface{})
if !ok {
continue
}
// 创建新的代理列表,排除要删除的代理
var newProxies []interface{}
for _, p := range proxiesInterface {
if pName, ok := p.(string); ok && pName != proxyName {
newProxies = append(newProxies, p)
}
}
// 更新代理列表
group["proxies"] = newProxies
proxyGroups[i] = group
// 如果被删除的代理是当前选中的代理,更新selected字段
if selected, ok := group["selected"].(string); ok && selected == proxyName {
if len(newProxies) > 0 {
if firstProxy, ok := newProxies[0].(string); ok {
group["selected"] = firstProxy
}
} else {
delete(group, "selected")
}
proxyGroups[i] = group
}
}
// 更新配置
config["proxy-groups"] = proxyGroups
}
// 检查配置文件是否正确启用了API
func checkAPIConfig() error {
config, err := readClashConfig()
if err != nil {
return fmt.Errorf("读取配置失败: %v", err)
}
// 检查外部控制设置
externalController, ok := config["external-controller"].(string)
if !ok || externalController == "" {
return fmt.Errorf("配置文件中未找到 external-controller 设置或设置为空")
}
// 检查端口是否为9090
if !strings.Contains(externalController, "9090") {
return fmt.Errorf("external-controller 端口不是9090,当前设置为: %s", externalController)
}
// 检查是否允许外部访问
if strings.HasPrefix(externalController, "127.0.0.1") || strings.HasPrefix(externalController, "localhost") {
fmt.Println("提示: API仅允许本地访问")
} else if strings.HasPrefix(externalController, "0.0.0.0") {
fmt.Println("提示: API允许所有网络接口访问")
}
// 检查API密钥
if secret, ok := config["secret"].(string); ok && secret != "" {
fmt.Println("提示: API已设置访问密钥")
} else {
fmt.Println("提示: API未设置访问密钥,可能存在安全风险")
}
// 检查UI设置
if externalUI, ok := config["external-ui"].(string); ok && externalUI != "" {
fmt.Printf("提示: 已配置Web UI,路径为: %s\n", externalUI)
} else {
fmt.Println("提示: 未配置Web UI")
}
return nil
}
// 添加一个诊断函数,检查Clash API可用性
func diagnosisClashAPI() error {
client := &http.Client{
Timeout: 5 * time.Second,
}
endpoints := []string{
"http://127.0.0.1:9090/version",
"http://127.0.0.1:9090/configs",
"http://127.0.0.1:9090/proxies",
"http://127.0.0.1:9090/rules",
}
fmt.Println("正在检查Clash API端点:")
for _, endpoint := range endpoints {
fmt.Printf("测试端点: %s...", endpoint)
resp, err := client.Get(endpoint)
if err != nil {
fmt.Printf("失败: %v\n", err)
return fmt.Errorf("无法连接到API端点 %s: %v", endpoint, err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
fmt.Printf("失败: 状态码 %d\n", resp.StatusCode)
fmt.Printf("响应内容: %s\n", string(body))
return fmt.Errorf("API端点 %s 返回非正常状态码: %d", endpoint, resp.StatusCode)
}
fmt.Println("成功")
}
fmt.Println("API检查完成,所有端点都可以访问")
return nil
}
// 辅助函数:状态字符串
func statusString(isRunning bool) string {
if isRunning {
return "运行中"
}
return "已停止"
}
// 辅助函数:API状态字符串
func apiStatusString(isAvailable bool) string {
if isAvailable {
return "可用"
}
return "不可用"
}