1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-20 14:35:23 +00:00
v2fly/app/router/rules/router.go

106 lines
2.4 KiB
Go
Raw Normal View History

2015-11-14 13:24:56 +00:00
package rules
2015-11-22 11:05:21 +00:00
import (
"errors"
2016-08-20 18:55:45 +00:00
"v2ray.com/core/app"
"v2ray.com/core/app/dns"
"v2ray.com/core/app/router"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
2015-11-22 11:05:21 +00:00
)
var (
2016-06-27 06:53:35 +00:00
ErrInvalidRule = errors.New("Invalid Rule")
ErrNoRuleApplicable = errors.New("No rule applicable")
2015-11-22 11:05:21 +00:00
)
type Router struct {
2016-05-18 15:12:04 +00:00
config *RouterRuleConfig
2016-05-30 22:21:29 +00:00
cache *RoutingTable
2016-05-18 15:12:04 +00:00
dnsServer dns.Server
2015-12-07 21:47:47 +00:00
}
2016-05-16 07:25:34 +00:00
func NewRouter(config *RouterRuleConfig, space app.Space) *Router {
2016-05-18 15:12:04 +00:00
r := &Router{
2016-02-01 22:21:54 +00:00
config: config,
2016-05-30 22:21:29 +00:00
cache: NewRoutingTable(),
2015-12-07 21:47:47 +00:00
}
2016-05-18 15:12:04 +00:00
space.InitializeApplication(func() error {
if !space.HasApp(dns.APP_ID) {
log.Error("DNS: Router is not found in the space.")
2016-06-27 06:53:35 +00:00
return app.ErrMissingApplication
2016-05-18 15:12:04 +00:00
}
r.dnsServer = space.GetApp(dns.APP_ID).(dns.Server)
return nil
})
return r
}
func (this *Router) Release() {
2015-12-07 21:47:47 +00:00
}
2016-05-16 07:25:34 +00:00
// @Private
func (this *Router) ResolveIP(dest v2net.Destination) []v2net.Destination {
2016-05-18 15:12:04 +00:00
ips := this.dnsServer.Get(dest.Address().Domain())
2016-05-16 07:25:34 +00: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 13:40:13 +00:00
func (this *Router) takeDetourWithoutCache(dest v2net.Destination) (string, error) {
2016-02-01 22:21:54 +00:00
for _, rule := range this.config.Rules {
2015-11-22 11:05:21 +00:00
if rule.Apply(dest) {
2016-01-17 15:20:49 +00:00
return rule.Tag, nil
2015-11-22 11:05:21 +00:00
}
}
2016-08-14 16:14:12 +00:00
if this.config.DomainStrategy == UseIPIfNonMatch && dest.Address().Family().IsDomain() {
2016-05-16 18:53:18 +00:00
log.Info("Router: Looking up IP for ", dest)
2016-05-16 07:25:34 +00:00
ipDests := this.ResolveIP(dest)
if ipDests != nil {
for _, ipDest := range ipDests {
2016-05-16 18:53:18 +00:00
log.Info("Router: Trying IP ", ipDest)
2016-05-16 07:25:34 +00:00
for _, rule := range this.config.Rules {
if rule.Apply(ipDest) {
return rule.Tag, nil
}
}
}
}
}
2016-06-27 06:53:35 +00:00
return "", ErrNoRuleApplicable
2015-11-22 11:05:21 +00:00
}
2015-12-14 13:40:13 +00:00
func (this *Router) TakeDetour(dest v2net.Destination) (string, error) {
2016-05-24 19:55:46 +00:00
destStr := dest.String()
2016-05-30 22:21:29 +00:00
found, tag, err := this.cache.Get(destStr)
if !found {
2015-12-14 13:40:13 +00:00
tag, err := this.takeDetourWithoutCache(dest)
2016-05-30 22:21:29 +00:00
this.cache.Set(destStr, tag, err)
2015-12-14 13:40:13 +00:00
return tag, err
}
2016-05-30 22:21:29 +00:00
return tag, err
2015-12-14 13:40:13 +00:00
}
2015-11-22 11:05:21 +00:00
type RouterFactory struct {
}
2016-05-16 07:25:34 +00:00
func (this *RouterFactory) Create(rawConfig interface{}, space app.Space) (router.Router, error) {
return NewRouter(rawConfig.(*RouterRuleConfig), space), nil
2015-11-22 11:05:21 +00:00
}
func init() {
router.RegisterRouter("rules", &RouterFactory{})
}