v2fly/app/router/router.go

158 lines
3.4 KiB
Go
Raw Normal View History

2019-02-01 19:08:21 +00:00
// +build !confonly
2015-10-28 22:18:07 +00:00
package router
2018-09-30 21:08:41 +00:00
//go:generate errorgen
2017-04-08 23:43:25 +00:00
2015-10-28 22:18:07 +00:00
import (
2017-01-13 12:41:40 +00:00
"context"
"v2ray.com/core"
2017-01-06 14:32:36 +00:00
"v2ray.com/core/common"
"v2ray.com/core/common/net"
2018-10-11 20:34:31 +00:00
"v2ray.com/core/features/dns"
2018-11-07 20:08:20 +00:00
"v2ray.com/core/features/outbound"
2018-10-11 20:34:31 +00:00
"v2ray.com/core/features/routing"
2015-10-28 22:18:07 +00:00
)
2018-10-22 13:58:52 +00:00
func init() {
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
r := new(Router)
2018-11-07 20:08:20 +00:00
if err := core.RequireFeatures(ctx, func(d dns.Client, ohm outbound.Manager) error {
return r.Init(config.(*Config), d, ohm)
2018-10-22 13:58:52 +00:00
}); err != nil {
return nil, err
}
return r, nil
}))
}
// Router is an implementation of routing.Router.
2016-10-12 14:11:13 +00:00
type Router struct {
domainStrategy Config_DomainStrategy
2018-11-07 20:08:20 +00:00
rules []*Rule
balancers map[string]*Balancer
2018-10-11 20:34:31 +00:00
dns dns.Client
2015-10-28 22:18:07 +00:00
}
2018-10-22 13:58:52 +00:00
// Init initializes the Router.
2018-11-07 20:08:20 +00:00
func (r *Router) Init(config *Config, d dns.Client, ohm outbound.Manager) error {
2018-10-22 13:58:52 +00:00
r.domainStrategy = config.DomainStrategy
r.dns = d
2016-10-12 14:11:13 +00:00
2018-11-07 20:08:20 +00:00
r.balancers = make(map[string]*Balancer, len(config.BalancingRule))
for _, rule := range config.BalancingRule {
balancer, err := rule.Build(ohm)
if err != nil {
return err
}
r.balancers[rule.Tag] = balancer
}
r.rules = make([]*Rule, 0, len(config.Rule))
for _, rule := range config.Rule {
cond, err := rule.BuildCondition()
if err != nil {
2018-10-22 13:58:52 +00:00
return err
2016-10-12 14:11:13 +00:00
}
2018-11-07 20:08:20 +00:00
rr := &Rule{
Condition: cond,
Tag: rule.GetTag(),
}
btag := rule.GetBalancingTag()
if len(btag) > 0 {
brule, found := r.balancers[btag]
if !found {
return newError("balancer ", btag, " not found")
}
rr.Balancer = brule
}
r.rules = append(r.rules, rr)
}
2018-10-22 13:58:52 +00:00
return nil
2015-10-28 22:18:07 +00:00
}
// PickRoute implements routing.Router.
func (r *Router) PickRoute(ctx routing.Context) (string, error) {
2018-11-07 20:08:20 +00:00
rule, err := r.pickRouteInternal(ctx)
if err != nil {
return "", err
}
return rule.GetTag()
}
func (r *Router) pickRouteInternal(ctx routing.Context) (*Rule, error) {
if r.domainStrategy == Config_IpOnDemand {
ctx = ContextWithDNSClient(ctx, r.dns)
2017-11-15 11:55:47 +00:00
}
2017-04-23 17:16:56 +00:00
for _, rule := range r.rules {
if rule.Apply(ctx) {
2018-11-07 20:08:20 +00:00
return rule, nil
2016-10-12 14:11:13 +00:00
}
2016-08-18 06:34:21 +00:00
}
if r.domainStrategy != Config_IpIfNonMatch || len(ctx.GetTargetDomain()) == 0 {
2018-11-07 20:08:20 +00:00
return nil, common.ErrNoClue
2017-02-09 21:49:38 +00:00
}
ctx = ContextWithDNSClient(ctx, r.dns)
2018-12-04 19:36:51 +00:00
// Try applying rules again if we have IPs.
for _, rule := range r.rules {
if rule.Apply(ctx) {
2018-12-04 19:36:51 +00:00
return rule, nil
2016-10-12 14:11:13 +00:00
}
}
2018-11-07 20:08:20 +00:00
return nil, common.ErrNoClue
2016-10-16 14:04:30 +00:00
}
2018-04-04 19:32:40 +00:00
// Start implements common.Runnable.
2017-04-23 17:16:56 +00:00
func (*Router) Start() error {
2017-02-01 20:35:40 +00:00
return nil
}
2018-04-04 19:32:40 +00:00
// Close implements common.Closable.
2018-02-08 14:39:46 +00:00
func (*Router) Close() error {
return nil
}
2017-02-01 20:35:40 +00:00
2018-10-12 21:57:56 +00:00
// Type implement common.HasType.
func (*Router) Type() interface{} {
return routing.RouterType()
}
// ContextWithDNSClient creates a new routing context with domain resolving capability. Resolved domain IPs can be retrieved by GetTargetIPs().
func ContextWithDNSClient(ctx routing.Context, client dns.Client) routing.Context {
return &resolvableContext{Context: ctx, dnsClient: client}
}
type resolvableContext struct {
routing.Context
dnsClient dns.Client
resolvedIPs []net.IP
}
func (ctx *resolvableContext) GetTargetIPs() []net.IP {
if ips := ctx.Context.GetTargetIPs(); len(ips) != 0 {
return ips
}
if len(ctx.resolvedIPs) > 0 {
return ctx.resolvedIPs
}
if domain := ctx.GetTargetDomain(); len(domain) != 0 {
ips, err := ctx.dnsClient.LookupIP(domain)
if err == nil {
ctx.resolvedIPs = ips
return ips
}
newError("resolve ip for ", domain).Base(err).WriteToLog()
}
return nil
}