github.com/xmplusdev/xray-core@v1.8.10/infra/conf/api.go (about)

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