router.go 867 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package router
  2. import (
  3. "v2ray.com/core/app"
  4. "v2ray.com/core/common"
  5. "v2ray.com/core/common/log"
  6. v2net "v2ray.com/core/common/net"
  7. )
  8. const (
  9. APP_ID = app.ID(3)
  10. )
  11. type Router interface {
  12. common.Releasable
  13. TakeDetour(v2net.Destination) (string, error)
  14. }
  15. type RouterFactory interface {
  16. Create(rawConfig interface{}, space app.Space) (Router, error)
  17. }
  18. var (
  19. routerCache = make(map[string]RouterFactory)
  20. )
  21. func RegisterRouter(name string, factory RouterFactory) error {
  22. if _, found := routerCache[name]; found {
  23. return common.ErrDuplicatedName
  24. }
  25. routerCache[name] = factory
  26. return nil
  27. }
  28. func CreateRouter(name string, rawConfig interface{}, space app.Space) (Router, error) {
  29. if factory, found := routerCache[name]; found {
  30. return factory.Create(rawConfig, space)
  31. }
  32. log.Error("Router: not found: ", name)
  33. return nil, common.ErrObjectNotFound
  34. }