1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-23 02:26:49 -05:00
v2fly/app/router/router.go

122 lines
2.7 KiB
Go
Raw Normal View History

2015-10-28 18:18:07 -04:00
package router
import (
2017-01-13 07:41:40 -05:00
"context"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/app"
2016-10-12 10:11:13 -04:00
"v2ray.com/core/app/dns"
2017-01-06 09:32:36 -05:00
"v2ray.com/core/common"
2016-12-04 03:10:47 -05:00
"v2ray.com/core/common/errors"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/common/log"
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
)
2016-10-12 10:11:13 -04:00
var (
ErrInvalidRule = errors.New("Invalid Rule")
ErrNoRuleApplicable = errors.New("No rule applicable")
)
type Router struct {
domainStrategy Config_DomainStrategy
rules []Rule
2016-10-18 17:01:39 -04:00
// cache *RoutingTable
dnsServer dns.Server
2015-10-28 18:18:07 -04:00
}
2017-01-13 07:41:40 -05:00
func NewRouter(ctx context.Context, config *Config) (*Router, error) {
space := app.SpaceFromContext(ctx)
if space == nil {
return nil, errors.New("Router: No space in context.")
}
2016-10-12 10:11:13 -04:00
r := &Router{
domainStrategy: config.DomainStrategy,
2016-10-18 17:01:39 -04:00
//cache: NewRoutingTable(),
rules: make([]Rule, len(config.Rule)),
2016-10-12 10:11:13 -04:00
}
2017-01-06 09:32:36 -05:00
space.OnInitialize(func() error {
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 err
}
r.rules[idx].Condition = cond
}
2017-01-06 09:32:36 -05:00
r.dnsServer = dns.FromSpace(space)
if r.dnsServer == nil {
2016-11-21 15:13:01 -05:00
return errors.New("Router: DNS is not found in the space.")
2016-10-12 10:11:13 -04:00
}
return nil
})
2017-01-13 07:41:40 -05:00
return r, nil
2015-10-28 18:18:07 -04:00
}
2016-10-12 10:11:13 -04:00
// Private: Visible for testing.
func (v *Router) ResolveIP(dest net.Destination) []net.Address {
2016-11-27 15:39:09 -05:00
ips := v.dnsServer.Get(dest.Address.Domain())
2016-10-12 10:11:13 -04:00
if len(ips) == 0 {
return nil
}
dests := make([]net.Address, len(ips))
2016-10-12 10:11:13 -04:00
for idx, ip := range ips {
dests[idx] = net.IPAddress(ip)
2016-10-12 10:11:13 -04:00
}
return dests
}
func (v *Router) takeDetourWithoutCache(ctx context.Context) (string, error) {
2016-11-27 15:39:09 -05:00
for _, rule := range v.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
}
dest := proxy.DestinationFromContext(ctx)
2016-11-27 15:39:09 -05:00
if v.domainStrategy == Config_IpIfNonMatch && dest.Address.Family().IsDomain() {
2016-10-12 10:11:13 -04:00
log.Info("Router: Looking up IP for ", dest)
2016-11-27 15:39:09 -05:00
ipDests := v.ResolveIP(dest)
2016-10-12 10:11:13 -04:00
if ipDests != nil {
ctx = proxy.ContextWithResolveIPs(ctx, ipDests)
for _, rule := range v.rules {
if rule.Apply(ctx) {
return rule.Tag, nil
2016-10-12 10:11:13 -04:00
}
}
}
}
return "", ErrNoRuleApplicable
2015-10-28 18:18:07 -04:00
}
2015-11-03 17:24:56 -05:00
func (v *Router) TakeDetour(ctx context.Context) (string, error) {
2016-10-18 17:01:39 -04:00
//destStr := dest.String()
2016-11-27 15:39:09 -05:00
//found, tag, err := v.cache.Get(destStr)
2016-10-18 17:01:39 -04:00
//if !found {
tag, err := v.takeDetourWithoutCache(ctx)
2016-11-27 15:39:09 -05:00
//v.cache.Set(destStr, tag, err)
2016-10-12 10:11:13 -04:00
return tag, err
2016-10-18 17:01:39 -04:00
//}
//return tag, err
2015-11-03 17:24:56 -05:00
}
2016-10-16 10:04:30 -04:00
2017-01-13 07:41:40 -05:00
func (Router) Interface() interface{} {
return (*Router)(nil)
2016-10-16 10:04:30 -04:00
}
2017-01-06 09:32:36 -05:00
func FromSpace(space app.Space) *Router {
2017-01-13 07:41:40 -05:00
app := space.GetApplication((*Router)(nil))
2017-01-06 09:32:36 -05:00
if app == nil {
return nil
}
return app.(*Router)
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
}