1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-27 22:36:12 -04:00
v2fly/app/router/rules/router.go

131 lines
2.9 KiB
Go
Raw Normal View History

2015-11-14 08:24:56 -05:00
package rules
2015-11-22 06:05:21 -05:00
import (
"errors"
2015-12-14 08:40:13 -05:00
"time"
2015-11-22 06:05:21 -05:00
2016-05-16 03:25:34 -04:00
"github.com/v2ray/v2ray-core/app"
"github.com/v2ray/v2ray-core/app/dns"
2015-11-22 06:05:21 -05:00
"github.com/v2ray/v2ray-core/app/router"
2015-12-14 08:40:13 -05:00
"github.com/v2ray/v2ray-core/common/collect"
2016-05-16 14:53:18 -04:00
"github.com/v2ray/v2ray-core/common/log"
2015-11-22 06:05:21 -05:00
v2net "github.com/v2ray/v2ray-core/common/net"
)
var (
2016-01-30 06:32:38 -05:00
ErrorInvalidRule = errors.New("Invalid Rule")
ErrorNoRuleApplicable = errors.New("No rule applicable")
2015-11-22 06:05:21 -05:00
)
2015-12-14 08:40:13 -05:00
type cacheEntry struct {
tag string
err error
validUntil time.Time
}
func newCacheEntry(tag string, err error) *cacheEntry {
this := &cacheEntry{
tag: tag,
err: err,
}
this.Extend()
return this
}
func (this *cacheEntry) IsValid() bool {
return this.validUntil.Before(time.Now())
}
func (this *cacheEntry) Extend() {
this.validUntil = time.Now().Add(time.Hour)
}
2015-11-22 06:05:21 -05:00
type Router struct {
2016-05-18 11:12:04 -04:00
config *RouterRuleConfig
cache *collect.ValidityMap
dnsServer dns.Server
2015-12-07 16:47:47 -05:00
}
2016-05-16 03:25:34 -04:00
func NewRouter(config *RouterRuleConfig, space app.Space) *Router {
2016-05-18 11:12:04 -04:00
r := &Router{
2016-02-01 17:21:54 -05:00
config: config,
cache: collect.NewValidityMap(3600),
2015-12-07 16:47:47 -05:00
}
2016-05-18 11:12:04 -04:00
space.InitializeApplication(func() error {
if !space.HasApp(dns.APP_ID) {
log.Error("DNS: Router is not found in the space.")
return app.ErrorMissingApplication
}
r.dnsServer = space.GetApp(dns.APP_ID).(dns.Server)
return nil
})
return r
}
func (this *Router) Release() {
2015-12-07 16:47:47 -05:00
}
2016-05-16 03:25:34 -04:00
// @Private
func (this *Router) ResolveIP(dest v2net.Destination) []v2net.Destination {
2016-05-18 11:12:04 -04:00
ips := this.dnsServer.Get(dest.Address().Domain())
2016-05-16 03:25:34 -04:00
if len(ips) == 0 {
return nil
}
dests := make([]v2net.Destination, len(ips))
for idx, ip := range ips {
if dest.IsTCP() {
dests[idx] = v2net.TCPDestination(v2net.IPAddress(ip), dest.Port())
} else {
dests[idx] = v2net.UDPDestination(v2net.IPAddress(ip), dest.Port())
}
}
return dests
}
2015-12-14 08:40:13 -05:00
func (this *Router) takeDetourWithoutCache(dest v2net.Destination) (string, error) {
2016-02-01 17:21:54 -05:00
for _, rule := range this.config.Rules {
2015-11-22 06:05:21 -05:00
if rule.Apply(dest) {
2016-01-17 10:20:49 -05:00
return rule.Tag, nil
2015-11-22 06:05:21 -05:00
}
}
2016-05-16 03:25:34 -04:00
if this.config.DomainStrategy == UseIPIfNonMatch && dest.Address().IsDomain() {
2016-05-16 14:53:18 -04:00
log.Info("Router: Looking up IP for ", dest)
2016-05-16 03:25:34 -04:00
ipDests := this.ResolveIP(dest)
if ipDests != nil {
for _, ipDest := range ipDests {
2016-05-16 14:53:18 -04:00
log.Info("Router: Trying IP ", ipDest)
2016-05-16 03:25:34 -04:00
for _, rule := range this.config.Rules {
if rule.Apply(ipDest) {
return rule.Tag, nil
}
}
}
}
}
2016-01-30 06:32:38 -05:00
return "", ErrorNoRuleApplicable
2015-11-22 06:05:21 -05:00
}
2015-12-14 08:40:13 -05:00
func (this *Router) TakeDetour(dest v2net.Destination) (string, error) {
rawEntry := this.cache.Get(dest)
if rawEntry == nil {
tag, err := this.takeDetourWithoutCache(dest)
this.cache.Set(dest, newCacheEntry(tag, err))
return tag, err
}
entry := rawEntry.(*cacheEntry)
return entry.tag, entry.err
}
2015-11-22 06:05:21 -05:00
type RouterFactory struct {
}
2016-05-16 03:25:34 -04:00
func (this *RouterFactory) Create(rawConfig interface{}, space app.Space) (router.Router, error) {
return NewRouter(rawConfig.(*RouterRuleConfig), space), nil
2015-11-22 06:05:21 -05:00
}
func init() {
router.RegisterRouter("rules", &RouterFactory{})
}