1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 18:17:52 -05:00
v2fly/app/router/router.go

126 lines
2.9 KiB
Go
Raw Normal View History

2015-10-28 18:18:07 -04:00
package router
import (
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"
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"
v2net "v2ray.com/core/common/net"
2016-12-15 05:51:09 -05:00
"v2ray.com/core/common/serial"
2016-10-18 17:01:39 -04:00
"v2ray.com/core/proxy"
2015-10-28 18:18:07 -04:00
)
2016-05-18 02:05:52 -04:00
const (
APP_ID = app.ID(3)
)
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
}
2016-10-12 10:11:13 -04:00
func NewRouter(config *Config, space app.Space) *Router {
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
}
space.InitializeApplication(func() error {
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
}
if !space.HasApp(dns.APP_ID) {
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
}
r.dnsServer = space.GetApp(dns.APP_ID).(dns.Server)
return nil
})
return r
2015-10-28 18:18:07 -04:00
}
2016-10-12 10:11:13 -04:00
// Private: Visible for testing.
2016-11-27 15:39:09 -05:00
func (v *Router) ResolveIP(dest v2net.Destination) []v2net.Destination {
ips := v.dnsServer.Get(dest.Address.Domain())
2016-10-12 10:11:13 -04:00
if len(ips) == 0 {
return nil
}
dests := make([]v2net.Destination, len(ips))
for idx, ip := range ips {
if dest.Network == v2net.Network_TCP {
dests[idx] = v2net.TCPDestination(v2net.IPAddress(ip), dest.Port)
} else {
dests[idx] = v2net.UDPDestination(v2net.IPAddress(ip), dest.Port)
}
}
return dests
}
2016-11-27 15:39:09 -05:00
func (v *Router) takeDetourWithoutCache(session *proxy.SessionInfo) (string, error) {
for _, rule := range v.rules {
2016-10-18 17:01:39 -04:00
if rule.Apply(session) {
2016-10-12 10:11:13 -04:00
return rule.Tag, nil
}
2016-08-18 02:34:21 -04:00
}
2016-10-18 17:01:39 -04:00
dest := session.Destination
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 {
for _, ipDest := range ipDests {
log.Info("Router: Trying IP ", ipDest)
2016-11-27 15:39:09 -05:00
for _, rule := range v.rules {
2016-10-18 17:01:39 -04:00
if rule.Apply(&proxy.SessionInfo{
Source: session.Source,
Destination: ipDest,
User: session.User,
}) {
2016-10-12 10:11:13 -04:00
return rule.Tag, nil
}
}
}
}
}
return "", ErrNoRuleApplicable
2015-10-28 18:18:07 -04:00
}
2015-11-03 17:24:56 -05:00
2016-11-27 15:39:09 -05:00
func (v *Router) TakeDetour(session *proxy.SessionInfo) (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 {
2016-11-27 15:39:09 -05:00
tag, err := v.takeDetourWithoutCache(session)
//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
type RouterFactory struct{}
func (RouterFactory) Create(space app.Space, config interface{}) (app.Application, error) {
router := NewRouter(config.(*Config), space)
return router, nil
}
func (RouterFactory) AppId() app.ID {
return APP_ID
}
func init() {
2016-12-15 05:51:09 -05:00
app.RegisterApplicationFactory(serial.GetMessageType(new(Config)), RouterFactory{})
2016-10-16 10:04:30 -04:00
}