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