api.go 1.5 KB

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