api.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 c.Tag == "" {
  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 "reflectionservice":
  22. services = append(services, serial.ToTypedMessage(&commander.ReflectionConfig{}))
  23. case "handlerservice":
  24. services = append(services, serial.ToTypedMessage(&handlerservice.Config{}))
  25. case "loggerservice":
  26. services = append(services, serial.ToTypedMessage(&loggerservice.Config{}))
  27. case "statsservice":
  28. services = append(services, serial.ToTypedMessage(&statsservice.Config{}))
  29. }
  30. }
  31. return &commander.Config{
  32. Tag: c.Tag,
  33. Service: services,
  34. }, nil
  35. }