support include other config file

This commit is contained in:
yuzuki999 2023-09-13 01:21:20 +08:00
parent dc29da8c3c
commit 5286dfa244
3 changed files with 44 additions and 9 deletions

View File

@ -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
}

View File

@ -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"
}
/*,
{

13
example/config_node1.json Normal file
View File

@ -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"
}