diff --git a/conf/node.go b/conf/node.go index e0cd704..ee79cd1 100644 --- a/conf/node.go +++ b/conf/node.go @@ -1,7 +1,11 @@ package conf import ( + "fmt" + "github.com/InazumaV/V2bX/common/json5" "github.com/goccy/go-json" + "io" + "os" ) type NodeConfig struct { @@ -10,8 +14,9 @@ type NodeConfig struct { } type rawNodeConfig struct { - ApiRaw json.RawMessage `json:"ApiConfig"` - OptRaw json.RawMessage `json:"Options"` + Include string `json:"Include"` + ApiRaw json.RawMessage `json:"ApiConfig"` + OptRaw json.RawMessage `json:"Options"` } type ApiConfig struct { @@ -24,17 +29,30 @@ type ApiConfig struct { } func (n *NodeConfig) UnmarshalJSON(data []byte) (err error) { - r := rawNodeConfig{} - err = json.Unmarshal(data, &r) + rn := rawNodeConfig{} + err = json.Unmarshal(data, &rn) if err != nil { return err } + if len(rn.Include) != 0 { + f, err := os.Open(rn.Include) + if err != nil { + return fmt.Errorf("open include file error: %s", err) + } + defer f.Close() + data, err = io.ReadAll(json5.NewTrimNodeReader(f)) + err = json.Unmarshal(data, &rn) + if err != nil { + return fmt.Errorf("unmarshal include file error: %s", err) + } + } + n.ApiConfig = ApiConfig{ APIHost: "http://127.0.0.1", Timeout: 30, } - if len(r.ApiRaw) > 0 { - err = json.Unmarshal(r.ApiRaw, &n.ApiConfig) + if len(rn.ApiRaw) > 0 { + err = json.Unmarshal(rn.ApiRaw, &n.ApiConfig) if err != nil { return } @@ -50,8 +68,8 @@ func (n *NodeConfig) UnmarshalJSON(data []byte) (err error) { SendIP: "0.0.0.0", CertConfig: NewCertConfig(), } - if len(r.OptRaw) > 0 { - err = json.Unmarshal(r.OptRaw, &n.Options) + if len(rn.OptRaw) > 0 { + err = json.Unmarshal(rn.OptRaw, &n.Options) if err != nil { return } diff --git a/example/config.json b/example/config.json index a918615..9594457 100644 --- a/example/config.json +++ b/example/config.json @@ -62,12 +62,16 @@ "EnableProxyProtocol": false, // 开启 TCP Fast Open "EnableTFO": true, - // 设置 Domain Strategy // 可选 prefer_ipv4 / prefer_ipv6 / ipv4_only / ipv6_only "DomainStrategy": "ipv4_only" // More + }, + { + // 引用其他配置文件 + + "Include": "../example/config_node1.json" } /*, { diff --git a/example/config_node1.json b/example/config_node1.json new file mode 100644 index 0000000..d422922 --- /dev/null +++ b/example/config_node1.json @@ -0,0 +1,13 @@ +{ + "Core": "xray", + "ApiHost": "https://127.0.0.1", + "ApiKey": "key", + "NodeID": 1, + "NodeType": "vmess", + "Timeout": 30, + "ListenIP": "0.0.0.0", + "SendIP": "0.0.0.0", + "EnableProxyProtocol": false, + "EnableTFO": true, + "DomainStrategy": "ipv4_only" +} \ No newline at end of file