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