1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-30 07:46:41 -04:00
v2fly/app/router/router.go

127 lines
2.8 KiB
Go
Raw Normal View History

2015-10-28 18:18:07 -04:00
package router
2017-12-02 19:04:57 -05:00
//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg router -path App,Router
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"
"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"
2016-10-18 17:01:39 -04:00
"v2ray.com/core/proxy"
2015-10-28 18:18:07 -04:00
)
2018-04-04 15:32:40 -04:00
// Router is an implementation of core.Router.
2016-10-12 10:11:13 -04:00
type Router struct {
domainStrategy Config_DomainStrategy
rules []Rule
dns core.DNSClient
2015-10-28 18:18:07 -04:00
}
2018-04-04 15:32:40 -04:00
// NewRouter creates a new Router based on the given config.
2017-01-13 07:41:40 -05:00
func NewRouter(ctx context.Context, config *Config) (*Router, error) {
2018-02-21 11:05:29 -05:00
v := core.MustFromContext(ctx)
2016-10-12 10:11:13 -04:00
r := &Router{
domainStrategy: config.DomainStrategy,
2017-01-27 16:49:54 -05:00
rules: make([]Rule, len(config.Rule)),
dns: v.DNSClient(),
2016-10-12 10:11:13 -04:00
}
for idx, rule := range config.Rule {
r.rules[idx].Tag = rule.Tag
cond, err := rule.BuildCondition()
if err != nil {
return nil, err
2016-10-12 10:11:13 -04:00
}
r.rules[idx].Condition = cond
}
if err := v.RegisterFeature((*core.Router)(nil), r); err != nil {
return nil, newError("unable to register Router").Base(err)
}
2017-01-13 07:41:40 -05:00
return r, nil
2015-10-28 18:18:07 -04:00
}
2017-11-15 06:55:47 -05:00
type ipResolver struct {
dns core.DNSClient
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
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-04-04 15:32:40 -04:00
// PickRoute implements core.Router.
func (r *Router) PickRoute(ctx context.Context) (string, error) {
resolver := &ipResolver{
dns: r.dns,
}
2017-11-15 06:55:47 -05:00
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)
}
}
2017-04-23 13:16:56 -04:00
for _, rule := range r.rules {
if rule.Apply(ctx) {
2016-10-12 10:11:13 -04:00
return rule.Tag, nil
}
2016-08-18 02:34:21 -04:00
}
2017-02-09 16:49:38 -05:00
dest, ok := proxy.TargetFromContext(ctx)
if !ok {
return "", core.ErrNoClue
2017-02-09 16:49:38 -05:00
}
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 {
ctx = proxy.ContextWithResolveIPs(ctx, resolver)
2017-04-23 13:16:56 -04:00
for _, rule := range r.rules {
if rule.Apply(ctx) {
return rule.Tag, nil
2016-10-12 10:11:13 -04:00
}
}
}
}
return "", core.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
2016-10-16 10:04:30 -04:00
func init() {
2017-01-13 07:41:40 -05:00
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
return NewRouter(ctx, config.(*Config))
}))
2016-10-16 10:04:30 -04:00
}