2022-07-28 11:00:05 -04:00
|
|
|
package conf
|
2022-06-01 13:35:41 -04:00
|
|
|
|
2023-08-20 03:13:52 -04:00
|
|
|
import (
|
|
|
|
"github.com/goccy/go-json"
|
|
|
|
)
|
|
|
|
|
2023-05-16 21:46:52 -04:00
|
|
|
type NodeConfig struct {
|
2023-08-20 03:13:52 -04:00
|
|
|
ApiConfig ApiConfig `json:"-"`
|
|
|
|
Options Options `json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type rawNodeConfig struct {
|
|
|
|
ApiRaw *json.RawMessage `json:"ApiConfig"`
|
|
|
|
OptRaw *json.RawMessage `json:"Options"`
|
2023-05-16 21:46:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type ApiConfig struct {
|
2023-08-20 03:13:52 -04:00
|
|
|
APIHost string `json:"ApiHost"`
|
|
|
|
NodeID int `json:"NodeID"`
|
|
|
|
Key string `json:"ApiKey"`
|
|
|
|
NodeType string `json:"NodeType"`
|
|
|
|
Timeout int `json:"Timeout"`
|
|
|
|
RuleListPath string `json:"RuleListPath"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Options struct {
|
|
|
|
Core string `json:"Core"`
|
|
|
|
ListenIP string `json:"ListenIP"`
|
|
|
|
SendIP string `json:"SendIP"`
|
|
|
|
LimitConfig LimitConfig `json:"LimitConfig"`
|
|
|
|
XrayOptions *XrayOptions `json:"XrayOptions"`
|
|
|
|
SingOptions *SingOptions `json:"SingOptions"`
|
|
|
|
CertConfig *CertConfig `json:"CertConfig"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NodeConfig) UnmarshalJSON(b []byte) (err error) {
|
|
|
|
r := rawNodeConfig{}
|
|
|
|
err = json.Unmarshal(b, &r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if r.ApiRaw != nil {
|
|
|
|
err = json.Unmarshal(*r.ApiRaw, &n.ApiConfig)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
n.ApiConfig = ApiConfig{
|
|
|
|
Timeout: 30,
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(b, &n.ApiConfig)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.OptRaw != nil {
|
|
|
|
err = json.Unmarshal(*r.OptRaw, &n.Options)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
n.Options = Options{
|
|
|
|
Core: "xray",
|
|
|
|
ListenIP: "0.0.0.0",
|
|
|
|
SendIP: "0.0.0.0",
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(b, &n.Options)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch n.Options.Core {
|
|
|
|
case "xray":
|
|
|
|
n.Options.XrayOptions = NewXrayOptions()
|
|
|
|
return json.Unmarshal(b, n.Options.XrayOptions)
|
|
|
|
case "sing":
|
|
|
|
n.Options.SingOptions = NewSingOptions()
|
|
|
|
return json.Unmarshal(b, n.Options.SingOptions)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
2023-05-16 21:46:52 -04:00
|
|
|
}
|