2015-10-28 23:18:07 +01:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
2016-08-20 20:55:45 +02:00
|
|
|
"v2ray.com/core/app"
|
|
|
|
"v2ray.com/core/common"
|
|
|
|
"v2ray.com/core/common/log"
|
|
|
|
v2net "v2ray.com/core/common/net"
|
2015-10-28 23:18:07 +01:00
|
|
|
)
|
|
|
|
|
2016-05-17 23:05:52 -07:00
|
|
|
const (
|
|
|
|
APP_ID = app.ID(3)
|
|
|
|
)
|
|
|
|
|
2015-10-28 23:18:07 +01:00
|
|
|
type Router interface {
|
2016-05-18 08:12:04 -07:00
|
|
|
common.Releasable
|
2015-11-30 15:14:38 +00:00
|
|
|
TakeDetour(v2net.Destination) (string, error)
|
2015-10-28 23:18:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type RouterFactory interface {
|
2016-05-16 00:25:34 -07:00
|
|
|
Create(rawConfig interface{}, space app.Space) (Router, error)
|
2015-10-28 23:18:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
routerCache = make(map[string]RouterFactory)
|
|
|
|
)
|
|
|
|
|
|
|
|
func RegisterRouter(name string, factory RouterFactory) error {
|
2016-08-18 08:34:21 +02:00
|
|
|
if _, found := routerCache[name]; found {
|
|
|
|
return common.ErrDuplicatedName
|
|
|
|
}
|
2015-10-28 23:18:07 +01:00
|
|
|
routerCache[name] = factory
|
|
|
|
return nil
|
|
|
|
}
|
2015-11-03 23:24:56 +01:00
|
|
|
|
2016-05-16 00:25:34 -07:00
|
|
|
func CreateRouter(name string, rawConfig interface{}, space app.Space) (Router, error) {
|
2015-11-03 23:24:56 +01:00
|
|
|
if factory, found := routerCache[name]; found {
|
2016-05-16 00:25:34 -07:00
|
|
|
return factory.Create(rawConfig, space)
|
2015-11-03 23:24:56 +01:00
|
|
|
}
|
2016-08-18 08:34:21 +02:00
|
|
|
log.Error("Router: not found: ", name)
|
|
|
|
return nil, common.ErrObjectNotFound
|
2015-11-03 23:24:56 +01:00
|
|
|
}
|