github.com/xtls/xray-core@v1.8.12-0.20240518155711-3168d27b0bdb/infra/conf/api.go (about)

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