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

140 lines
2.9 KiB

9 years ago
package router
//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg router -path App,Router
9 years ago
import (
8 years ago
"context"
"v2ray.com/core/app"
8 years ago
"v2ray.com/core/app/dns"
"v2ray.com/core/common"
8 years ago
"v2ray.com/core/common/net"
"v2ray.com/core/proxy"
9 years ago
)
8 years ago
var (
ErrNoRuleApplicable = newError("No rule applicable")
8 years ago
)
type Router struct {
domainStrategy Config_DomainStrategy
rules []Rule
dnsServer dns.Server
9 years ago
}
8 years ago
func NewRouter(ctx context.Context, config *Config) (*Router, error) {
space := app.SpaceFromContext(ctx)
if space == nil {
return nil, newError("no space in context")
8 years ago
}
8 years ago
r := &Router{
domainStrategy: config.DomainStrategy,
rules: make([]Rule, len(config.Rule)),
8 years ago
}
space.On(app.SpaceInitializing, func(interface{}) error {
8 years ago
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
}
r.dnsServer = dns.FromSpace(space)
if r.dnsServer == nil {
return newError("DNS is not found in the space")
8 years ago
}
return nil
})
8 years ago
return r, nil
9 years ago
}
type ipResolver struct {
ip []net.Address
domain string
resolved bool
dnsServer dns.Server
}
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 := r.dnsServer.Get(r.domain)
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
}
8 years ago
func (r *Router) TakeDetour(ctx context.Context) (string, error) {
resolver := &ipResolver{
dnsServer: r.dnsServer,
}
if r.domainStrategy == Config_IpOnDemand {
if dest, ok := proxy.TargetFromContext(ctx); ok && dest.Address.Family().IsDomain() {
resolver.domain = dest.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
}
}
dest, ok := proxy.TargetFromContext(ctx)
if !ok {
return "", ErrNoRuleApplicable
}
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 "", ErrNoRuleApplicable
9 years ago
}
8 years ago
func (*Router) Interface() interface{} {
8 years ago
return (*Router)(nil)
}
8 years ago
func (*Router) Start() error {
return nil
}
8 years ago
func (*Router) Close() {}
func FromSpace(space app.Space) *Router {
8 years ago
app := space.GetApplication((*Router)(nil))
if app == nil {
return nil
}
return app.(*Router)
}
func init() {
8 years ago
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
return NewRouter(ctx, config.(*Config))
}))
}