refactor conf

This commit is contained in:
Yuzuki616
2023-08-20 15:13:52 +08:00
parent 42407d5c62
commit 214324496d
19 changed files with 346 additions and 287 deletions

View File

@@ -11,16 +11,19 @@ var (
cores = map[string]func(c *conf.CoreConfig) (Core, error){}
)
func NewCore(c *conf.CoreConfig) (Core, error) {
func NewCore(c []conf.CoreConfig) (Core, error) {
if len(c) < 0 {
return nil, errors.New("no have vail core")
}
// multi core
if types := strings.Split(c.Type, " "); len(types) > 1 {
if len(c) > 1 {
var cs []Core
for _, t := range types {
f, ok := cores[strings.ToLower(t)]
for _, t := range c {
f, ok := cores[strings.ToLower(t.Type)]
if !ok {
return nil, errors.New("unknown core type: " + t)
return nil, errors.New("unknown core type: " + t.Type)
}
core1, err := f(c)
core1, err := f(&t)
if err != nil {
return nil, err
}
@@ -31,8 +34,8 @@ func NewCore(c *conf.CoreConfig) (Core, error) {
}, nil
}
// one core
if f, ok := cores[strings.ToLower(c.Type)]; ok {
return f(c)
if f, ok := cores[c[0].Type]; ok {
return f(&c[0])
} else {
return nil, errors.New("unknown core type")
}

View File

@@ -20,4 +20,5 @@ type Core interface {
GetUserTraffic(tag, uuid string, reset bool) (up int64, down int64)
DelUsers(users []panel.UserInfo, tag string) error
Protocols() []string
Type() string
}

View File

@@ -39,12 +39,12 @@ func isSupported(protocol string, protocols []string) bool {
return false
}
func (s *Selector) AddNode(tag string, info *panel.NodeInfo, config *conf.Options) error {
func (s *Selector) AddNode(tag string, info *panel.NodeInfo, option *conf.Options) error {
for i := range s.cores {
if !isSupported(info.Type, s.cores[i].Protocols()) {
if option.Core != s.cores[i].Type() {
continue
}
err := s.cores[i].AddNode(tag, info, config)
err := s.cores[i].AddNode(tag, info, option)
if err != nil {
return err
}
@@ -97,3 +97,7 @@ func (s *Selector) Protocols() []string {
}
return protocols
}
func (s *Selector) Type() string {
return "selector"
}

View File

@@ -271,3 +271,7 @@ func (b *Box) Protocols() []string {
"hysteria",
}
}
func (b *Box) Type() string {
return "sing"
}

View File

@@ -194,3 +194,7 @@ func (c *Core) Protocols() []string {
"trojan",
}
}
func (c *Core) Type() string {
return "xray"
}