github.com/v2fly/v2ray-core/v4@v4.45.2/infra/conf/api.go (about)

     1  package conf
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/jhump/protoreflect/desc"
     7  	"github.com/jhump/protoreflect/dynamic"
     8  
     9  	"github.com/v2fly/v2ray-core/v4/app/commander"
    10  	loggerservice "github.com/v2fly/v2ray-core/v4/app/log/command"
    11  	observatoryservice "github.com/v2fly/v2ray-core/v4/app/observatory/command"
    12  	handlerservice "github.com/v2fly/v2ray-core/v4/app/proxyman/command"
    13  	statsservice "github.com/v2fly/v2ray-core/v4/app/stats/command"
    14  	"github.com/v2fly/v2ray-core/v4/common/serial"
    15  )
    16  
    17  type APIConfig struct {
    18  	Tag      string   `json:"tag"`
    19  	Services []string `json:"services"`
    20  }
    21  
    22  func (c *APIConfig) Build() (*commander.Config, error) {
    23  	if c.Tag == "" {
    24  		return nil, newError("API tag can't be empty.")
    25  	}
    26  
    27  	services := make([]*serial.TypedMessage, 0, 16)
    28  	for _, s := range c.Services {
    29  		switch strings.ToLower(s) {
    30  		case "reflectionservice":
    31  			services = append(services, serial.ToTypedMessage(&commander.ReflectionConfig{}))
    32  		case "handlerservice":
    33  			services = append(services, serial.ToTypedMessage(&handlerservice.Config{}))
    34  		case "loggerservice":
    35  			services = append(services, serial.ToTypedMessage(&loggerservice.Config{}))
    36  		case "statsservice":
    37  			services = append(services, serial.ToTypedMessage(&statsservice.Config{}))
    38  		case "observatoryservice":
    39  			services = append(services, serial.ToTypedMessage(&observatoryservice.Config{}))
    40  		default:
    41  			if !strings.HasPrefix(s, "#") {
    42  				continue
    43  			}
    44  			message, err := desc.LoadMessageDescriptor(s[1:])
    45  			if err != nil || message == nil {
    46  				return nil, newError("Cannot find API", s, "").Base(err)
    47  			}
    48  			serviceConfig := dynamic.NewMessage(message)
    49  			services = append(services, serial.ToTypedMessage(serviceConfig))
    50  		}
    51  	}
    52  
    53  	return &commander.Config{
    54  		Tag:     c.Tag,
    55  		Service: services,
    56  	}, nil
    57  }