mirror of
https://github.com/wyx2685/V2bX.git
synced 2026-02-04 12:40:11 +00:00
support include other config file
This commit is contained in:
34
conf/node.go
34
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user