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 {
|
2023-09-02 05:34:55 -04:00
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2023-08-25 22:57:54 -04:00
|
|
|
func (n *NodeConfig) UnmarshalJSON(data []byte) (err error) {
|
2023-08-20 03:13:52 -04:00
|
|
|
r := rawNodeConfig{}
|
2023-08-25 22:57:54 -04:00
|
|
|
err = json.Unmarshal(data, &r)
|
2023-08-20 03:13:52 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-02 14:59:54 -04:00
|
|
|
n.ApiConfig = ApiConfig{
|
|
|
|
APIHost: "http://127.0.0.1",
|
|
|
|
Timeout: 30,
|
|
|
|
}
|
2023-09-02 05:34:55 -04:00
|
|
|
if len(r.ApiRaw) > 0 {
|
|
|
|
err = json.Unmarshal(r.ApiRaw, &n.ApiConfig)
|
2023-08-20 03:13:52 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
2023-08-25 22:57:54 -04:00
|
|
|
err = json.Unmarshal(data, &n.ApiConfig)
|
2023-08-20 03:13:52 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2023-09-02 14:59:54 -04:00
|
|
|
|
|
|
|
n.Options = Options{
|
|
|
|
ListenIP: "0.0.0.0",
|
|
|
|
SendIP: "0.0.0.0",
|
|
|
|
CertConfig: NewCertConfig(),
|
|
|
|
}
|
2023-09-02 05:34:55 -04:00
|
|
|
if len(r.OptRaw) > 0 {
|
|
|
|
err = json.Unmarshal(r.OptRaw, &n.Options)
|
2023-08-20 03:13:52 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
2023-08-25 22:57:54 -04:00
|
|
|
err = json.Unmarshal(data, &n.Options)
|
2023-08-20 03:13:52 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-08-25 22:57:54 -04:00
|
|
|
}
|
2023-09-02 05:30:28 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type Options struct {
|
|
|
|
Core string `json:"Core"`
|
|
|
|
ListenIP string `json:"ListenIP"`
|
|
|
|
SendIP string `json:"SendIP"`
|
|
|
|
LimitConfig LimitConfig `json:"LimitConfig"`
|
|
|
|
RawOptions json.RawMessage `json:"RawOptions"`
|
|
|
|
XrayOptions *XrayOptions `json:"XrayOptions"`
|
|
|
|
SingOptions *SingOptions `json:"SingOptions"`
|
|
|
|
CertConfig *CertConfig `json:"CertConfig"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Options) UnmarshalJSON(data []byte) error {
|
|
|
|
type opt Options
|
|
|
|
err := json.Unmarshal(data, (*opt)(o))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch o.Core {
|
2023-08-25 22:57:54 -04:00
|
|
|
case "xray":
|
2023-09-02 05:30:28 -04:00
|
|
|
o.XrayOptions = NewXrayOptions()
|
|
|
|
return json.Unmarshal(data, o.XrayOptions)
|
2023-08-25 22:57:54 -04:00
|
|
|
case "sing":
|
2023-09-02 05:30:28 -04:00
|
|
|
o.SingOptions = NewSingOptions()
|
|
|
|
return json.Unmarshal(data, o.SingOptions)
|
|
|
|
default:
|
2023-09-02 05:45:32 -04:00
|
|
|
o.Core = ""
|
2023-09-02 05:30:28 -04:00
|
|
|
o.RawOptions = data
|
2023-08-20 03:13:52 -04:00
|
|
|
}
|
2023-09-02 05:30:28 -04:00
|
|
|
return nil
|
2023-05-16 21:46:52 -04:00
|
|
|
}
|