1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-28 23:06:14 -04:00
v2fly/app/router/rules/router.go

50 lines
1.0 KiB
Go
Raw Normal View History

2015-11-14 08:24:56 -05:00
package rules
2015-11-22 06:05:21 -05:00
import (
"errors"
pointconfig "github.com/v2ray/v2ray-core/app/point/config"
"github.com/v2ray/v2ray-core/app/router"
"github.com/v2ray/v2ray-core/app/router/rules/config"
"github.com/v2ray/v2ray-core/app/router/rules/config/json"
v2net "github.com/v2ray/v2ray-core/common/net"
)
var (
InvalidRule = errors.New("Invalid Rule")
EmptyTag = pointconfig.DetourTag("")
)
type Router struct {
rules []config.Rule
}
func (this *Router) TakeDetour(dest v2net.Destination) (*pointconfig.DetourTag, error) {
for _, rule := range this.rules {
if rule.Apply(dest) {
return rule.Tag(), nil
}
}
return &EmptyTag, nil
}
type RouterFactory struct {
}
func (this *RouterFactory) Create(rawConfig interface{}) (router.Router, error) {
config := rawConfig.(*json.RouterRuleConfig)
rules := config.Rules()
for _, rule := range rules {
if rule == nil {
return nil, InvalidRule
}
}
return &Router{
rules: rules,
}, nil
}
func init() {
router.RegisterRouter("rules", &RouterFactory{})
}