1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-16 09:26:21 -05:00
v2fly/app/router/router.go

265 lines
6.9 KiB
Go
Raw Normal View History

2015-10-28 18:18:07 -04:00
package router
//go:generate go run github.com/v2fly/v2ray-core/v5/common/errors/errorgen
2017-04-08 19:43:25 -04:00
2015-10-28 18:18:07 -04:00
import (
2017-01-13 07:41:40 -05:00
"context"
2021-10-28 06:34:19 -04:00
core "github.com/v2fly/v2ray-core/v5"
"github.com/v2fly/v2ray-core/v5/common"
"github.com/v2fly/v2ray-core/v5/common/platform"
"github.com/v2fly/v2ray-core/v5/features/dns"
"github.com/v2fly/v2ray-core/v5/features/outbound"
"github.com/v2fly/v2ray-core/v5/features/routing"
routing_dns "github.com/v2fly/v2ray-core/v5/features/routing/dns"
"github.com/v2fly/v2ray-core/v5/infra/conf/cfgcommon"
"github.com/v2fly/v2ray-core/v5/infra/conf/geodata"
2015-10-28 18:18:07 -04:00
)
// Router is an implementation of routing.Router.
2016-10-12 10:11:13 -04:00
type Router struct {
domainStrategy DomainStrategy
2018-11-07 15:08:20 -05:00
rules []*Rule
balancers map[string]*Balancer
2018-10-11 16:34:31 -04:00
dns dns.Client
2015-10-28 18:18:07 -04:00
}
// Route is an implementation of routing.Route.
type Route struct {
routing.Context
outboundGroupTags []string
outboundTag string
}
2018-10-22 09:58:52 -04:00
// Init initializes the Router.
func (r *Router) Init(ctx context.Context, config *Config, d dns.Client, ohm outbound.Manager, dispatcher routing.Dispatcher) error {
2018-10-22 09:58:52 -04:00
r.domainStrategy = config.DomainStrategy
r.dns = d
2016-10-12 10:11:13 -04:00
2018-11-07 15:08:20 -05:00
r.balancers = make(map[string]*Balancer, len(config.BalancingRule))
for _, rule := range config.BalancingRule {
balancer, err := rule.Build(ohm, dispatcher)
2018-11-07 15:08:20 -05:00
if err != nil {
return err
}
2021-04-08 16:34:52 -04:00
balancer.InjectContext(ctx)
2018-11-07 15:08:20 -05:00
r.balancers[rule.Tag] = balancer
}
r.rules = make([]*Rule, 0, len(config.Rule))
for _, rule := range config.Rule {
cond, err := rule.BuildCondition()
if err != nil {
2018-10-22 09:58:52 -04:00
return err
2016-10-12 10:11:13 -04:00
}
2018-11-07 15:08:20 -05:00
rr := &Rule{
Condition: cond,
Tag: rule.GetTag(),
}
btag := rule.GetBalancingTag()
if len(btag) > 0 {
brule, found := r.balancers[btag]
if !found {
return newError("balancer ", btag, " not found")
}
rr.Balancer = brule
}
r.rules = append(r.rules, rr)
}
2018-10-22 09:58:52 -04:00
return nil
2015-10-28 18:18:07 -04:00
}
// PickRoute implements routing.Router.
func (r *Router) PickRoute(ctx routing.Context) (routing.Route, error) {
rule, ctx, err := r.pickRouteInternal(ctx)
if err != nil {
return nil, err
}
tag, err := rule.GetTag()
2018-11-07 15:08:20 -05:00
if err != nil {
return nil, err
2018-11-07 15:08:20 -05:00
}
return &Route{Context: ctx, outboundTag: tag}, nil
2018-11-07 15:08:20 -05:00
}
func (r *Router) pickRouteInternal(ctx routing.Context) (*Rule, routing.Context, error) {
2020-12-30 05:35:19 -05:00
// SkipDNSResolve is set from DNS module.
// the DOH remote server maybe a domain name,
// this prevents cycle resolving dead loop
skipDNSResolve := ctx.GetSkipDNSResolve()
if r.domainStrategy == DomainStrategy_IpOnDemand && !skipDNSResolve {
ctx = routing_dns.ContextWithDNSClient(ctx, r.dns)
2017-11-15 06:55:47 -05:00
}
2017-04-23 13:16:56 -04:00
for _, rule := range r.rules {
if rule.Apply(ctx) {
return rule, ctx, nil
2016-10-12 10:11:13 -04:00
}
2016-08-18 02:34:21 -04:00
}
if r.domainStrategy != DomainStrategy_IpIfNonMatch || len(ctx.GetTargetDomain()) == 0 || skipDNSResolve {
return nil, ctx, common.ErrNoClue
2017-02-09 16:49:38 -05:00
}
ctx = routing_dns.ContextWithDNSClient(ctx, r.dns)
2018-12-04 14:36:51 -05:00
// Try applying rules again if we have IPs.
for _, rule := range r.rules {
if rule.Apply(ctx) {
return rule, ctx, nil
2016-10-12 10:11:13 -04:00
}
}
return nil, ctx, common.ErrNoClue
2016-10-16 10:04:30 -04:00
}
2018-04-04 15:32:40 -04:00
// Start implements common.Runnable.
func (r *Router) Start() error {
2017-02-01 15:35:40 -05:00
return nil
}
2018-04-04 15:32:40 -04:00
// Close implements common.Closable.
func (r *Router) Close() error {
2018-02-08 09:39:46 -05:00
return nil
}
2017-02-01 15:35:40 -05:00
2022-01-02 04:16:12 -05:00
// Type implements common.HasType.
2018-10-12 17:57:56 -04:00
func (*Router) Type() interface{} {
return routing.RouterType()
}
// GetOutboundGroupTags implements routing.Route.
func (r *Route) GetOutboundGroupTags() []string {
return r.outboundGroupTags
}
// GetOutboundTag implements routing.Route.
func (r *Route) GetOutboundTag() string {
return r.outboundTag
}
func init() {
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
r := new(Router)
if err := core.RequireFeatures(ctx, func(d dns.Client, ohm outbound.Manager, dispatcher routing.Dispatcher) error {
return r.Init(ctx, config.(*Config), d, ohm, dispatcher)
}); err != nil {
return nil, err
}
return r, nil
}))
2021-09-07 03:42:34 -04:00
common.Must(common.RegisterConfig((*SimplifiedConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
2021-09-07 06:09:34 -04:00
ctx = cfgcommon.NewConfigureLoadingContext(ctx)
2021-09-07 03:42:34 -04:00
geoloadername := platform.NewEnvFlag("v2ray.conf.geoloader").GetValue(func() string {
return "standard"
})
if loader, err := geodata.GetGeoDataLoader(geoloadername); err == nil {
cfgcommon.SetGeoDataLoader(ctx, loader)
} else {
return nil, newError("unable to create geo data loader ").Base(err)
}
cfgEnv := cfgcommon.GetConfigureLoadingEnvironment(ctx)
geoLoader := cfgEnv.GetGeoLoader()
simplifiedConfig := config.(*SimplifiedConfig)
2021-09-09 09:45:19 -04:00
var routingRules []*RoutingRule
2021-09-07 03:42:34 -04:00
for _, v := range simplifiedConfig.Rule {
2021-09-09 09:45:19 -04:00
rule := new(RoutingRule)
2021-09-07 03:42:34 -04:00
for _, geo := range v.Geoip {
if geo.Code != "" {
filepath := "geoip.dat"
if geo.FilePath != "" {
filepath = geo.FilePath
2021-09-07 06:09:34 -04:00
} else {
geo.CountryCode = geo.Code
2021-09-07 03:42:34 -04:00
}
var err error
geo.Cidr, err = geoLoader.LoadIP(filepath, geo.Code)
if err != nil {
return nil, newError("unable to load geoip").Base(err)
}
}
}
2021-09-09 09:45:19 -04:00
rule.Geoip = v.Geoip
for _, geo := range v.SourceGeoip {
if geo.Code != "" {
filepath := "geoip.dat"
if geo.FilePath != "" {
filepath = geo.FilePath
} else {
geo.CountryCode = geo.Code
}
var err error
geo.Cidr, err = geoLoader.LoadIP(filepath, geo.Code)
if err != nil {
return nil, newError("unable to load geoip").Base(err)
}
}
}
rule.SourceGeoip = v.SourceGeoip
2021-09-07 03:42:34 -04:00
for _, geo := range v.GeoDomain {
if geo.Code != "" {
2021-09-07 06:09:34 -04:00
filepath := "geosite.dat"
2021-09-07 03:42:34 -04:00
if geo.FilePath != "" {
filepath = geo.FilePath
}
var err error
geo.Domain, err = geoLoader.LoadGeoSiteWithAttr(filepath, geo.Code)
if err != nil {
return nil, newError("unable to load geodomain").Base(err)
}
2021-09-09 09:45:19 -04:00
}
}
if v.PortList != "" {
2021-09-09 09:45:19 -04:00
portList := &cfgcommon.PortList{}
err := portList.UnmarshalText(v.PortList)
if err != nil {
return nil, err
}
rule.PortList = portList.Build()
}
if v.SourcePortList != "" {
2021-09-09 09:45:19 -04:00
portList := &cfgcommon.PortList{}
err := portList.UnmarshalText(v.SourcePortList)
if err != nil {
return nil, err
2021-09-07 03:42:34 -04:00
}
2021-09-09 09:45:19 -04:00
rule.SourcePortList = portList.Build()
2021-09-07 03:42:34 -04:00
}
2021-09-09 09:45:19 -04:00
rule.Domain = v.Domain
rule.GeoDomain = v.GeoDomain
rule.Networks = v.Networks.GetNetwork()
2021-09-09 09:45:19 -04:00
rule.Protocol = v.Protocol
rule.Attributes = v.Attributes
rule.UserEmail = v.UserEmail
rule.InboundTag = v.InboundTag
rule.DomainMatcher = v.DomainMatcher
switch s := v.TargetTag.(type) {
case *SimplifiedRoutingRule_Tag:
rule.TargetTag = &RoutingRule_Tag{s.Tag}
case *SimplifiedRoutingRule_BalancingTag:
rule.TargetTag = &RoutingRule_BalancingTag{s.BalancingTag}
}
routingRules = append(routingRules, rule)
2021-09-07 03:42:34 -04:00
}
2021-09-07 06:09:34 -04:00
fullConfig := &Config{
2021-09-07 03:42:34 -04:00
DomainStrategy: simplifiedConfig.DomainStrategy,
2021-09-09 09:45:19 -04:00
Rule: routingRules,
2021-09-07 03:42:34 -04:00
BalancingRule: simplifiedConfig.BalancingRule,
}
return common.CreateObject(ctx, fullConfig)
}))
}