diff --git a/conf/node.go b/conf/node.go index aa00ddd..2ca8708 100644 --- a/conf/node.go +++ b/conf/node.go @@ -23,16 +23,6 @@ type ApiConfig struct { 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(data []byte) (err error) { r := rawNodeConfig{} err = json.Unmarshal(data, &r) @@ -46,6 +36,7 @@ func (n *NodeConfig) UnmarshalJSON(data []byte) (err error) { } } else { n.ApiConfig = ApiConfig{ + APIHost: "http://127.0.0.1", Timeout: 30, } err = json.Unmarshal(data, &n.ApiConfig) @@ -61,7 +52,6 @@ func (n *NodeConfig) UnmarshalJSON(data []byte) (err error) { } } else { n.Options = Options{ - Core: "xray", ListenIP: "0.0.0.0", SendIP: "0.0.0.0", } @@ -70,13 +60,35 @@ func (n *NodeConfig) UnmarshalJSON(data []byte) (err error) { return } } - switch n.Options.Core { - case "xray": - n.Options.XrayOptions = NewXrayOptions() - return json.Unmarshal(data, n.Options.XrayOptions) - case "sing": - n.Options.SingOptions = NewSingOptions() - return json.Unmarshal(data, n.Options.SingOptions) - } 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 { + case "xray": + o.XrayOptions = NewXrayOptions() + return json.Unmarshal(data, o.XrayOptions) + case "sing": + o.SingOptions = NewSingOptions() + return json.Unmarshal(data, o.SingOptions) + default: + o.RawOptions = data + } + return nil +} diff --git a/core/selector.go b/core/selector.go index ebc0bad..f72aded 100644 --- a/core/selector.go +++ b/core/selector.go @@ -2,6 +2,7 @@ package core import ( "errors" + "fmt" "sync" "github.com/InazumaV/V2bX/api/panel" @@ -40,11 +41,19 @@ func isSupported(protocol string, protocols []string) bool { } func (s *Selector) AddNode(tag string, info *panel.NodeInfo, option *conf.Options) error { - for i := range s.cores { - if option.Core != s.cores[i].Type() { + for i, c := range s.cores { + if len(option.Core) == 0 { + if isSupported(info.Type, c.Protocols()) { + option.Core = c.Type() + err := option.UnmarshalJSON(option.RawOptions) + if err != nil { + return fmt.Errorf("unmarshal option error: %s", err) + } + } + } else if option.Core != c.Type() { continue } - err := s.cores[i].AddNode(tag, info, option) + err := c.AddNode(tag, info, option) if err != nil { return err } diff --git a/go.mod b/go.mod index ae38f38..b9a48b0 100644 --- a/go.mod +++ b/go.mod @@ -2,8 +2,6 @@ module github.com/InazumaV/V2bX go 1.21 -toolchain go1.21.0 - require ( github.com/beevik/ntp v1.2.0 github.com/dustin/go-humanize v1.0.1