-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
129 lines (112 loc) · 2.92 KB
/
router.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package router
import (
"v2ray.com/core/app"
"v2ray.com/core/app/dns"
"v2ray.com/core/common/errors"
"v2ray.com/core/common/loader"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy"
)
const (
APP_ID = app.ID(3)
)
var (
ErrInvalidRule = errors.New("Invalid Rule")
ErrNoRuleApplicable = errors.New("No rule applicable")
)
type Router struct {
domainStrategy Config_DomainStrategy
rules []Rule
// cache *RoutingTable
dnsServer dns.Server
}
func NewRouter(config *Config, space app.Space) *Router {
r := &Router{
domainStrategy: config.DomainStrategy,
//cache: NewRoutingTable(),
rules: make([]Rule, len(config.Rule)),
}
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) {
return errors.New("Router: DNS is not found in the space.")
}
r.dnsServer = space.GetApp(dns.APP_ID).(dns.Server)
return nil
})
return r
}
func (v *Router) Release() {
}
// Private: Visible for testing.
func (v *Router) ResolveIP(dest v2net.Destination) []v2net.Destination {
ips := v.dnsServer.Get(dest.Address.Domain())
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
}
func (v *Router) takeDetourWithoutCache(session *proxy.SessionInfo) (string, error) {
for _, rule := range v.rules {
if rule.Apply(session) {
return rule.Tag, nil
}
}
dest := session.Destination
if v.domainStrategy == Config_IpIfNonMatch && dest.Address.Family().IsDomain() {
log.Info("Router: Looking up IP for ", dest)
ipDests := v.ResolveIP(dest)
if ipDests != nil {
for _, ipDest := range ipDests {
log.Info("Router: Trying IP ", ipDest)
for _, rule := range v.rules {
if rule.Apply(&proxy.SessionInfo{
Source: session.Source,
Destination: ipDest,
User: session.User,
}) {
return rule.Tag, nil
}
}
}
}
}
return "", ErrNoRuleApplicable
}
func (v *Router) TakeDetour(session *proxy.SessionInfo) (string, error) {
//destStr := dest.String()
//found, tag, err := v.cache.Get(destStr)
//if !found {
tag, err := v.takeDetourWithoutCache(session)
//v.cache.Set(destStr, tag, err)
return tag, err
//}
//return tag, err
}
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() {
app.RegisterApplicationFactory(loader.GetType(new(Config)), RouterFactory{})
}