1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-10 09:50:43 +00:00
v2fly/app/router/router.go

133 lines
2.7 KiB
Go
Raw Normal View History

2015-10-28 22:18:07 +00:00
package router
2017-12-03 00:04:57 +00:00
//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg router -path App,Router
2017-04-08 23:43:25 +00:00
2015-10-28 22:18:07 +00:00
import (
2017-01-13 12:41:40 +00:00
"context"
2016-08-20 18:55:45 +00:00
"v2ray.com/core/app"
2017-01-06 14:32:36 +00:00
"v2ray.com/core/common"
2017-01-13 23:27:45 +00:00
"v2ray.com/core/common/net"
2016-10-18 21:01:39 +00:00
"v2ray.com/core/proxy"
2015-10-28 22:18:07 +00:00
)
2016-10-12 14:11:13 +00:00
var (
2017-04-08 23:43:25 +00:00
ErrNoRuleApplicable = newError("No rule applicable")
2016-10-12 14:11:13 +00:00
)
type Router struct {
domainStrategy Config_DomainStrategy
rules []Rule
2015-10-28 22:18:07 +00:00
}
2017-01-13 12:41:40 +00:00
func NewRouter(ctx context.Context, config *Config) (*Router, error) {
space := app.SpaceFromContext(ctx)
if space == nil {
2017-04-09 13:04:04 +00:00
return nil, newError("no space in context")
2017-01-13 12:41:40 +00:00
}
2016-10-12 14:11:13 +00:00
r := &Router{
domainStrategy: config.DomainStrategy,
2017-01-27 21:49:54 +00:00
rules: make([]Rule, len(config.Rule)),
2016-10-12 14:11:13 +00:00
}
2017-11-28 22:41:20 +00:00
space.On(app.SpaceInitializing, func(interface{}) error {
2016-10-12 14:11:13 +00: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
}
return nil
})
2017-01-13 12:41:40 +00:00
return r, nil
2015-10-28 22:18:07 +00:00
}
2017-11-15 11:55:47 +00:00
type ipResolver struct {
2017-12-19 22:55:09 +00:00
ip []net.Address
domain string
resolved bool
2017-11-15 11:55:47 +00:00
}
func (r *ipResolver) Resolve() []net.Address {
if r.resolved {
return r.ip
}
2017-12-19 20:28:12 +00:00
newError("looking for IP for domain: ", r.domain).WriteToLog()
2017-11-15 11:55:47 +00:00
r.resolved = true
2017-12-19 22:55:09 +00:00
ips, err := net.LookupIP(r.domain)
if err != nil {
newError("failed to get IP address").Base(err).WriteToLog()
}
2017-11-15 11:55:47 +00:00
if len(ips) == 0 {
return nil
}
r.ip = make([]net.Address, len(ips))
for i, ip := range ips {
r.ip[i] = net.IPAddress(ip)
}
return r.ip
}
2017-04-23 17:16:56 +00:00
func (r *Router) TakeDetour(ctx context.Context) (string, error) {
2017-12-19 22:55:09 +00:00
resolver := &ipResolver{}
2017-11-15 11:55:47 +00:00
if r.domainStrategy == Config_IpOnDemand {
if dest, ok := proxy.TargetFromContext(ctx); ok && dest.Address.Family().IsDomain() {
resolver.domain = dest.Address.Domain()
ctx = proxy.ContextWithResolveIPs(ctx, resolver)
}
}
2017-04-23 17:16:56 +00:00
for _, rule := range r.rules {
if rule.Apply(ctx) {
2016-10-12 14:11:13 +00:00
return rule.Tag, nil
}
2016-08-18 06:34:21 +00:00
}
2017-02-09 21:49:38 +00:00
dest, ok := proxy.TargetFromContext(ctx)
if !ok {
return "", ErrNoRuleApplicable
}
2017-04-23 17:16:56 +00:00
if r.domainStrategy == Config_IpIfNonMatch && dest.Address.Family().IsDomain() {
2017-11-15 11:55:47 +00:00
resolver.domain = dest.Address.Domain()
ips := resolver.Resolve()
if len(ips) > 0 {
ctx = proxy.ContextWithResolveIPs(ctx, resolver)
2017-04-23 17:16:56 +00:00
for _, rule := range r.rules {
if rule.Apply(ctx) {
return rule.Tag, nil
2016-10-12 14:11:13 +00:00
}
}
}
}
return "", ErrNoRuleApplicable
2015-10-28 22:18:07 +00:00
}
2015-11-03 22:24:56 +00:00
2017-04-23 17:16:56 +00:00
func (*Router) Interface() interface{} {
2017-01-13 12:41:40 +00:00
return (*Router)(nil)
2016-10-16 14:04:30 +00:00
}
2017-04-23 17:16:56 +00:00
func (*Router) Start() error {
2017-02-01 20:35:40 +00:00
return nil
}
2017-04-23 17:16:56 +00:00
func (*Router) Close() {}
2017-02-01 20:35:40 +00:00
2017-01-06 14:32:36 +00:00
func FromSpace(space app.Space) *Router {
2017-01-13 12:41:40 +00:00
app := space.GetApplication((*Router)(nil))
2017-01-06 14:32:36 +00:00
if app == nil {
return nil
}
return app.(*Router)
2016-10-16 14:04:30 +00:00
}
func init() {
2017-01-13 12:41:40 +00:00
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
return NewRouter(ctx, config.(*Config))
}))
2016-10-16 14:04:30 +00:00
}