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 (
|
2015-11-22 11:41:52 -05:00
|
|
|
InvalidRule = errors.New("Invalid Rule")
|
|
|
|
NoRuleApplicable = errors.New("No rule applicable")
|
|
|
|
|
|
|
|
EmptyTag = pointconfig.DetourTag("")
|
2015-11-22 06:05:21 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type Router struct {
|
|
|
|
rules []config.Rule
|
|
|
|
}
|
|
|
|
|
2015-11-22 11:41:52 -05:00
|
|
|
func (this *Router) TakeDetour(dest v2net.Destination) (pointconfig.DetourTag, error) {
|
2015-11-22 06:05:21 -05:00
|
|
|
for _, rule := range this.rules {
|
|
|
|
if rule.Apply(dest) {
|
|
|
|
return rule.Tag(), nil
|
|
|
|
}
|
|
|
|
}
|
2015-11-22 11:41:52 -05:00
|
|
|
return EmptyTag, NoRuleApplicable
|
2015-11-22 06:05:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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{})
|
|
|
|
}
|