router.go 685 B

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