router.go 716 B

1234567891011121314151617181920212223242526272829303132333435
  1. package router
  2. import (
  3. "github.com/v2ray/v2ray-core/app"
  4. v2net "github.com/v2ray/v2ray-core/common/net"
  5. )
  6. const (
  7. APP_ID = app.ID(3)
  8. )
  9. type Router interface {
  10. TakeDetour(v2net.Destination) (string, error)
  11. }
  12. type RouterFactory interface {
  13. Create(rawConfig interface{}, space app.Space) (Router, error)
  14. }
  15. var (
  16. routerCache = make(map[string]RouterFactory)
  17. )
  18. func RegisterRouter(name string, factory RouterFactory) error {
  19. // TODO: check name
  20. routerCache[name] = factory
  21. return nil
  22. }
  23. func CreateRouter(name string, rawConfig interface{}, space app.Space) (Router, error) {
  24. if factory, found := routerCache[name]; found {
  25. return factory.Create(rawConfig, space)
  26. }
  27. return nil, ErrorRouterNotFound
  28. }