-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtest.go
More file actions
47 lines (42 loc) · 1.02 KB
/
Copy pathtest.go
File metadata and controls
47 lines (42 loc) · 1.02 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
package main
import (
"encoding/json"
"fmt"
)
type RawConfigs map[string]interface{}
// GetConfigs 获取 Clash 配置, 返回原始 JSON 数据(方便只解析后续部分内容)
func GetConfigs(jsonData []byte) (RawConfigs, error) {
raw := RawConfigs{}
err := json.Unmarshal(jsonData, &raw)
return raw, err
}
func IsTunEnabled(raw RawConfigs) (bool, error) {
tunValue, ok := raw["tun"].(map[string]interface{})
if !ok {
return false, fmt.Errorf("raw has not tun field")
}
enable, ok := tunValue["enable"].(bool)
if !ok {
return false, fmt.Errorf("tun field has not enable field")
}
return enable, nil
}
func main() {
// 原始 JSON 数据
jsonData := []byte(`{
"port": 8080,
"socks-port": 8081,
"mode": "debug",
"unknownField1": "unknown value 1",
"unknownField2": "unknown value 2",
"tun": {
"enable": false,
"stack": "system"
}
}`)
raw, _ := GetConfigs(jsonData)
fmt.Printf("%+v\n", raw)
fmt.Printf("%+v\n", raw["tun"])
enabled, _ := IsTunEnabled(raw)
fmt.Printf("%+v\n", enabled)
}