2015-10-28 18:18:07 -04:00
|
|
|
package router
|
|
|
|
|
2018-09-30 17:08:41 -04:00
|
|
|
//go:generate errorgen
|
2017-04-08 19:43:25 -04:00
|
|
|
|
2015-10-28 18:18:07 -04:00
|
|
|
import (
|
2017-01-13 07:41:40 -05:00
|
|
|
"context"
|
|
|
|
|
2018-01-10 06:22:37 -05:00
|
|
|
"v2ray.com/core"
|
2017-01-06 09:32:36 -05:00
|
|
|
"v2ray.com/core/common"
|
2017-01-13 18:27:45 -05:00
|
|
|
"v2ray.com/core/common/net"
|
2018-10-11 16:34:31 -04:00
|
|
|
"v2ray.com/core/common/session"
|
|
|
|
"v2ray.com/core/features/dns"
|
2018-11-07 15:08:20 -05:00
|
|
|
"v2ray.com/core/features/outbound"
|
2018-10-11 16:34:31 -04:00
|
|
|
"v2ray.com/core/features/routing"
|
2015-10-28 18:18:07 -04:00
|
|
|
)
|
|
|
|
|
2018-10-22 16:40:53 -04:00
|
|
|
type key uint32
|
|
|
|
|
|
|
|
const (
|
|
|
|
resolvedIPsKey key = iota
|
|
|
|
)
|
|
|
|
|
|
|
|
type IPResolver interface {
|
|
|
|
Resolve() []net.Address
|
|
|
|
}
|
|
|
|
|
|
|
|
func ContextWithResolveIPs(ctx context.Context, f IPResolver) context.Context {
|
|
|
|
return context.WithValue(ctx, resolvedIPsKey, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ResolvedIPsFromContext(ctx context.Context) (IPResolver, bool) {
|
|
|
|
ips, ok := ctx.Value(resolvedIPsKey).(IPResolver)
|
|
|
|
return ips, ok
|
|
|
|
}
|
|
|
|
|
2018-10-22 09:58:52 -04:00
|
|
|
func init() {
|
|
|
|
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
|
|
|
r := new(Router)
|
2018-11-07 15:08:20 -05:00
|
|
|
if err := core.RequireFeatures(ctx, func(d dns.Client, ohm outbound.Manager) error {
|
|
|
|
return r.Init(config.(*Config), d, ohm)
|
2018-10-22 09:58:52 -04:00
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return r, nil
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2018-10-11 14:43:37 -04:00
|
|
|
// Router is an implementation of routing.Router.
|
2016-10-12 10:11:13 -04:00
|
|
|
type Router struct {
|
|
|
|
domainStrategy Config_DomainStrategy
|
2018-11-07 15:08:20 -05:00
|
|
|
rules []*Rule
|
|
|
|
balancers map[string]*Balancer
|
2018-10-11 16:34:31 -04:00
|
|
|
dns dns.Client
|
2015-10-28 18:18:07 -04:00
|
|
|
}
|
|
|
|
|
2018-10-22 09:58:52 -04:00
|
|
|
// Init initializes the Router.
|
2018-11-07 15:08:20 -05:00
|
|
|
func (r *Router) Init(config *Config, d dns.Client, ohm outbound.Manager) error {
|
2018-10-22 09:58:52 -04:00
|
|
|
r.domainStrategy = config.DomainStrategy
|
|
|
|
r.dns = d
|
2016-10-12 10:11:13 -04:00
|
|
|
|
2018-11-07 15:08:20 -05: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 {
|
2018-01-10 06:22:37 -05:00
|
|
|
cond, err := rule.BuildCondition()
|
|
|
|
if err != nil {
|
2018-10-22 09:58:52 -04:00
|
|
|
return err
|
2016-10-12 10:11:13 -04:00
|
|
|
}
|
2018-11-07 15:08:20 -05: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-01-10 06:22:37 -05:00
|
|
|
}
|
|
|
|
|
2018-10-22 09:58:52 -04:00
|
|
|
return nil
|
2015-10-28 18:18:07 -04:00
|
|
|
}
|
|
|
|
|
2017-11-15 06:55:47 -05:00
|
|
|
type ipResolver struct {
|
2018-10-11 16:34:31 -04:00
|
|
|
dns dns.Client
|
2017-12-19 17:55:09 -05:00
|
|
|
ip []net.Address
|
|
|
|
domain string
|
|
|
|
resolved bool
|
2017-11-15 06:55:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *ipResolver) Resolve() []net.Address {
|
|
|
|
if r.resolved {
|
|
|
|
return r.ip
|
|
|
|
}
|
|
|
|
|
2017-12-19 15:28:12 -05:00
|
|
|
newError("looking for IP for domain: ", r.domain).WriteToLog()
|
2017-11-15 06:55:47 -05:00
|
|
|
r.resolved = true
|
2018-01-10 06:22:37 -05:00
|
|
|
ips, err := r.dns.LookupIP(r.domain)
|
2017-12-19 17:55:09 -05:00
|
|
|
if err != nil {
|
|
|
|
newError("failed to get IP address").Base(err).WriteToLog()
|
|
|
|
}
|
2017-11-15 06:55:47 -05:00
|
|
|
if len(ips) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
r.ip = make([]net.Address, len(ips))
|
|
|
|
for i, ip := range ips {
|
|
|
|
r.ip[i] = net.IPAddress(ip)
|
|
|
|
}
|
|
|
|
return r.ip
|
|
|
|
}
|
|
|
|
|
2018-01-10 06:22:37 -05:00
|
|
|
func (r *Router) PickRoute(ctx context.Context) (string, error) {
|
2018-11-07 15:08:20 -05:00
|
|
|
rule, err := r.pickRouteInternal(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return rule.GetTag()
|
|
|
|
}
|
|
|
|
|
|
|
|
// PickRoute implements routing.Router.
|
|
|
|
func (r *Router) pickRouteInternal(ctx context.Context) (*Rule, error) {
|
2018-01-10 06:22:37 -05:00
|
|
|
resolver := &ipResolver{
|
|
|
|
dns: r.dns,
|
|
|
|
}
|
2018-09-18 17:09:54 -04:00
|
|
|
|
|
|
|
outbound := session.OutboundFromContext(ctx)
|
2017-11-15 06:55:47 -05:00
|
|
|
if r.domainStrategy == Config_IpOnDemand {
|
2018-09-18 17:09:54 -04:00
|
|
|
if outbound != nil && outbound.Target.IsValid() && outbound.Target.Address.Family().IsDomain() {
|
|
|
|
resolver.domain = outbound.Target.Address.Domain()
|
2018-10-22 16:40:53 -04:00
|
|
|
ctx = ContextWithResolveIPs(ctx, resolver)
|
2017-11-15 06:55:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-23 13:16:56 -04:00
|
|
|
for _, rule := range r.rules {
|
2017-01-26 14:46:44 -05:00
|
|
|
if rule.Apply(ctx) {
|
2018-11-07 15:08:20 -05:00
|
|
|
return rule, nil
|
2016-10-12 10:11:13 -04:00
|
|
|
}
|
2016-08-18 02:34:21 -04:00
|
|
|
}
|
2017-01-26 14:46:44 -05:00
|
|
|
|
2018-09-18 17:09:54 -04:00
|
|
|
if outbound == nil || !outbound.Target.IsValid() {
|
2018-11-07 15:08:20 -05:00
|
|
|
return nil, common.ErrNoClue
|
2017-02-09 16:49:38 -05:00
|
|
|
}
|
|
|
|
|
2018-09-18 17:09:54 -04:00
|
|
|
dest := outbound.Target
|
2017-04-23 13:16:56 -04:00
|
|
|
if r.domainStrategy == Config_IpIfNonMatch && dest.Address.Family().IsDomain() {
|
2017-11-15 06:55:47 -05:00
|
|
|
resolver.domain = dest.Address.Domain()
|
|
|
|
ips := resolver.Resolve()
|
|
|
|
if len(ips) > 0 {
|
2018-10-22 16:40:53 -04:00
|
|
|
ctx = ContextWithResolveIPs(ctx, resolver)
|
2017-04-23 13:16:56 -04:00
|
|
|
for _, rule := range r.rules {
|
2017-01-26 14:46:44 -05:00
|
|
|
if rule.Apply(ctx) {
|
2018-11-07 15:08:20 -05:00
|
|
|
return rule, nil
|
2016-10-12 10:11:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-07 15:08:20 -05:00
|
|
|
return nil, common.ErrNoClue
|
2016-10-16 10:04:30 -04:00
|
|
|
}
|
|
|
|
|
2018-04-04 15:32:40 -04:00
|
|
|
// Start implements common.Runnable.
|
2017-04-23 13:16:56 -04:00
|
|
|
func (*Router) Start() error {
|
2017-02-01 15:35:40 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-04 15:32:40 -04:00
|
|
|
// Close implements common.Closable.
|
2018-02-08 09:39:46 -05:00
|
|
|
func (*Router) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
2017-02-01 15:35:40 -05:00
|
|
|
|
2018-10-12 17:57:56 -04:00
|
|
|
// Type implement common.HasType.
|
|
|
|
func (*Router) Type() interface{} {
|
|
|
|
return routing.RouterType()
|
|
|
|
}
|