You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/app/router/router.go

137 lines
3.0 KiB

9 years ago
package router
6 years ago
//go:generate errorgen
9 years ago
import (
8 years ago
"context"
"v2ray.com/core"
"v2ray.com/core/common"
8 years ago
"v2ray.com/core/common/net"
"v2ray.com/core/common/session"
"v2ray.com/core/features"
"v2ray.com/core/features/dns"
"v2ray.com/core/features/routing"
"v2ray.com/core/proxy"
9 years ago
)
// Router is an implementation of routing.Router.
8 years ago
type Router struct {
domainStrategy Config_DomainStrategy
rules []Rule
dns dns.Client
9 years ago
}
7 years ago
// NewRouter creates a new Router based on the given config.
8 years ago
func NewRouter(ctx context.Context, config *Config) (*Router, error) {
8 years ago
r := &Router{
domainStrategy: config.DomainStrategy,
rules: make([]Rule, len(config.Rule)),
8 years ago
}
for idx, rule := range config.Rule {
r.rules[idx].Tag = rule.Tag
cond, err := rule.BuildCondition()
if err != nil {
return nil, err
8 years ago
}
r.rules[idx].Condition = cond
}
v := core.MustFromContext(ctx)
v.RequireFeatures([]interface{}{dns.ClientType()}, func(fs []features.Feature) {
r.dns = fs[0].(dns.Client)
})
8 years ago
return r, nil
9 years ago
}
type ipResolver struct {
dns dns.Client
7 years ago
ip []net.Address
domain string
resolved bool
}
func (r *ipResolver) Resolve() []net.Address {
if r.resolved {
return r.ip
}
newError("looking for IP for domain: ", r.domain).WriteToLog()
r.resolved = true
ips, err := r.dns.LookupIP(r.domain)
7 years ago
if err != nil {
newError("failed to get IP address").Base(err).WriteToLog()
}
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
}
// PickRoute implements routing.Router.
func (r *Router) PickRoute(ctx context.Context) (string, error) {
resolver := &ipResolver{
dns: r.dns,
}
outbound := session.OutboundFromContext(ctx)
if r.domainStrategy == Config_IpOnDemand {
if outbound != nil && outbound.Target.IsValid() && outbound.Target.Address.Family().IsDomain() {
resolver.domain = outbound.Target.Address.Domain()
ctx = proxy.ContextWithResolveIPs(ctx, resolver)
}
}
8 years ago
for _, rule := range r.rules {
if rule.Apply(ctx) {
8 years ago
return rule.Tag, nil
}
}
if outbound == nil || !outbound.Target.IsValid() {
return "", common.ErrNoClue
}
dest := outbound.Target
8 years ago
if r.domainStrategy == Config_IpIfNonMatch && dest.Address.Family().IsDomain() {
resolver.domain = dest.Address.Domain()
ips := resolver.Resolve()
if len(ips) > 0 {
ctx = proxy.ContextWithResolveIPs(ctx, resolver)
8 years ago
for _, rule := range r.rules {
if rule.Apply(ctx) {
return rule.Tag, nil
8 years ago
}
}
}
}
return "", common.ErrNoClue
}
7 years ago
// Start implements common.Runnable.
8 years ago
func (*Router) Start() error {
return nil
}
7 years ago
// Close implements common.Closable.
func (*Router) Close() error {
return nil
}
// Type implement common.HasType.
func (*Router) Type() interface{} {
return routing.RouterType()
}
func init() {
8 years ago
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
return NewRouter(ctx, config.(*Config))
}))
}