v2ray-core/app/router/router.go

118 lines
2.4 KiB
Go
Raw Normal View History

2015-10-28 22:18:07 +00:00
package router
2017-04-08 23:43:25 +00:00
//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg router -path App,Router
2015-10-28 22:18:07 +00:00
import (
2017-01-13 12:41:40 +00:00
"context"
2016-08-20 18:55:45 +00:00
"v2ray.com/core/app"
2016-10-12 14:11:13 +00:00
"v2ray.com/core/app/dns"
2017-02-01 20:35:40 +00:00
"v2ray.com/core/app/log"
2017-01-06 14:32:36 +00:00
"v2ray.com/core/common"
2017-01-13 23:27:45 +00:00
"v2ray.com/core/common/net"
2016-10-18 21:01:39 +00:00
"v2ray.com/core/proxy"
2015-10-28 22:18:07 +00:00
)
2016-10-12 14:11:13 +00:00
var (
2017-04-08 23:43:25 +00:00
ErrNoRuleApplicable = newError("No rule applicable")
2016-10-12 14:11:13 +00:00
)
type Router struct {
domainStrategy Config_DomainStrategy
rules []Rule
2017-01-27 21:49:54 +00:00
dnsServer dns.Server
2015-10-28 22:18:07 +00:00
}
2017-01-13 12:41:40 +00:00
func NewRouter(ctx context.Context, config *Config) (*Router, error) {
space := app.SpaceFromContext(ctx)
if space == nil {
2017-04-09 13:04:04 +00:00
return nil, newError("no space in context")
2017-01-13 12:41:40 +00:00
}
2016-10-12 14:11:13 +00:00
r := &Router{
domainStrategy: config.DomainStrategy,
2017-01-27 21:49:54 +00:00
rules: make([]Rule, len(config.Rule)),
2016-10-12 14:11:13 +00:00
}
2017-01-06 14:32:36 +00:00
space.OnInitialize(func() error {
2016-10-12 14:11:13 +00:00
for idx, rule := range config.Rule {
r.rules[idx].Tag = rule.Tag
cond, err := rule.BuildCondition()
if err != nil {
return err
}
r.rules[idx].Condition = cond
}
2017-01-06 14:32:36 +00:00
r.dnsServer = dns.FromSpace(space)
if r.dnsServer == nil {
2017-04-09 13:04:04 +00:00
return newError("DNS is not found in the space")
2016-10-12 14:11:13 +00:00
}
return nil
})
2017-01-13 12:41:40 +00:00
return r, nil
2015-10-28 22:18:07 +00:00
}
2017-01-27 21:49:54 +00:00
func (v *Router) resolveIP(dest net.Destination) []net.Address {
2016-11-27 20:39:09 +00:00
ips := v.dnsServer.Get(dest.Address.Domain())
2016-10-12 14:11:13 +00:00
if len(ips) == 0 {
return nil
}
dests := make([]net.Address, len(ips))
2016-10-12 14:11:13 +00:00
for idx, ip := range ips {
dests[idx] = net.IPAddress(ip)
2016-10-12 14:11:13 +00:00
}
return dests
}
2017-01-27 21:49:54 +00:00
func (v *Router) TakeDetour(ctx context.Context) (string, error) {
2016-11-27 20:39:09 +00:00
for _, rule := range v.rules {
if rule.Apply(ctx) {
2016-10-12 14:11:13 +00:00
return rule.Tag, nil
}
2016-08-18 06:34:21 +00:00
}
2017-02-09 21:49:38 +00:00
dest, ok := proxy.TargetFromContext(ctx)
if !ok {
return "", ErrNoRuleApplicable
}
2016-11-27 20:39:09 +00:00
if v.domainStrategy == Config_IpIfNonMatch && dest.Address.Family().IsDomain() {
2017-04-08 23:43:25 +00:00
log.Trace(newError("looking up IP for ", dest))
2017-01-27 21:49:54 +00:00
ipDests := v.resolveIP(dest)
2016-10-12 14:11:13 +00:00
if ipDests != nil {
ctx = proxy.ContextWithResolveIPs(ctx, ipDests)
for _, rule := range v.rules {
if rule.Apply(ctx) {
return rule.Tag, nil
2016-10-12 14:11:13 +00:00
}
}
}
}
return "", ErrNoRuleApplicable
2015-10-28 22:18:07 +00:00
}
2015-11-03 22:24:56 +00:00
2017-01-13 12:41:40 +00:00
func (Router) Interface() interface{} {
return (*Router)(nil)
2016-10-16 14:04:30 +00:00
}
2017-02-01 20:35:40 +00:00
func (Router) Start() error {
return nil
}
func (Router) Close() {}
2017-01-06 14:32:36 +00:00
func FromSpace(space app.Space) *Router {
2017-01-13 12:41:40 +00:00
app := space.GetApplication((*Router)(nil))
2017-01-06 14:32:36 +00:00
if app == nil {
return nil
}
return app.(*Router)
2016-10-16 14:04:30 +00:00
}
func init() {
2017-01-13 12:41:40 +00:00
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
return NewRouter(ctx, config.(*Config))
}))
2016-10-16 14:04:30 +00:00
}