github.com/xraypb/Xray-core@v1.8.1/infra/conf/api.go (about)

     1  package conf
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/xraypb/Xray-core/app/commander"
     7  	loggerservice "github.com/xraypb/Xray-core/app/log/command"
     8  	observatoryservice "github.com/xraypb/Xray-core/app/observatory/command"
     9  	handlerservice "github.com/xraypb/Xray-core/app/proxyman/command"
    10  	statsservice "github.com/xraypb/Xray-core/app/stats/command"
    11  	"github.com/xraypb/Xray-core/common/serial"
    12  )
    13  
    14  type APIConfig struct {
    15  	Tag      string   `json:"tag"`
    16  	Services []string `json:"services"`
    17  }
    18  
    19  func (c *APIConfig) Build() (*commander.Config, error) {
    20  	if c.Tag == "" {
    21  		return nil, newError("API tag can't be empty.")
    22  	}
    23  
    24  	services := make([]*serial.TypedMessage, 0, 16)
    25  	for _, s := range c.Services {
    26  		switch strings.ToLower(s) {
    27  		case "reflectionservice":
    28  			services = append(services, serial.ToTypedMessage(&commander.ReflectionConfig{}))
    29  		case "handlerservice":
    30  			services = append(services, serial.ToTypedMessage(&handlerservice.Config{}))
    31  		case "loggerservice":
    32  			services = append(services, serial.ToTypedMessage(&loggerservice.Config{}))
    33  		case "statsservice":
    34  			services = append(services, serial.ToTypedMessage(&statsservice.Config{}))
    35  		case "observatoryservice":
    36  			services = append(services, serial.ToTypedMessage(&observatoryservice.Config{}))
    37  		}
    38  	}
    39  
    40  	return &commander.Config{
    41  		Tag:     c.Tag,
    42  		Service: services,
    43  	}, nil
    44  }