api.go 986 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package conf
  2. import (
  3. "strings"
  4. "v2ray.com/core/app/commander"
  5. loggerservice "v2ray.com/core/app/log/command"
  6. handlerservice "v2ray.com/core/app/proxyman/command"
  7. statsservice "v2ray.com/core/app/stats/command"
  8. "v2ray.com/core/common/serial"
  9. )
  10. type ApiConfig struct {
  11. Tag string `json:"tag"`
  12. Services []string `json:"services"`
  13. }
  14. func (c *ApiConfig) Build() (*commander.Config, error) {
  15. if len(c.Tag) == 0 {
  16. return nil, newError("Api tag can't be empty.")
  17. }
  18. services := make([]*serial.TypedMessage, 0, 16)
  19. for _, s := range c.Services {
  20. switch strings.ToLower(s) {
  21. case "handlerservice":
  22. services = append(services, serial.ToTypedMessage(&handlerservice.Config{}))
  23. case "loggerservice":
  24. services = append(services, serial.ToTypedMessage(&loggerservice.Config{}))
  25. case "statsservice":
  26. services = append(services, serial.ToTypedMessage(&statsservice.Config{}))
  27. }
  28. }
  29. return &commander.Config{
  30. Tag: c.Tag,
  31. Service: services,
  32. }, nil
  33. }