chore: rename routing final as fallback

This commit is contained in:
mzz2017 2023-02-18 03:01:51 +08:00
parent 914aec31a2
commit de08c7c861
11 changed files with 64 additions and 31 deletions

View File

@ -53,7 +53,7 @@ const (
MatchType_IpVersion
MatchType_Mac
MatchType_ProcessName
MatchType_Final
MatchType_Fallback
)
type OutboundIndex uint8

View File

@ -21,5 +21,5 @@ const (
Function_Mac = "mac"
Function_ProcessName = "pname"
Declaration_Final = "final"
Declaration_Fallback = "fallback"
)

View File

@ -29,7 +29,7 @@ type MatcherBuilder interface {
AddIpVersion(f *config_parser.Function, values consts.IpVersionType, outbound string)
AddSourceMac(f *config_parser.Function, values [][6]byte, outbound string)
AddProcessName(f *config_parser.Function, values [][consts.TaskCommLen]byte, outbound string)
AddFinal(outbound string)
AddFallback(outbound string)
AddAnyBefore(f *config_parser.Function, key string, values []string, outbound string)
AddAnyAfter(f *config_parser.Function, key string, values []string, outbound string)
Build() (err error)
@ -67,7 +67,7 @@ func ToProcessName(processName string) (procName [consts.TaskCommLen]byte) {
return procName
}
func ApplyMatcherBuilder(log *logrus.Logger, builder MatcherBuilder, rules []*config_parser.RoutingRule, finalOutbound string) (err error) {
func ApplyMatcherBuilder(log *logrus.Logger, builder MatcherBuilder, rules []*config_parser.RoutingRule, fallbackOutbound string) (err error) {
for _, rule := range rules {
log.Debugln("[rule]", rule.String(true))
@ -172,12 +172,12 @@ func ApplyMatcherBuilder(log *logrus.Logger, builder MatcherBuilder, rules []*co
}
}
builder.AddAnyBefore(&config_parser.Function{
Name: "final",
}, "", nil, finalOutbound)
builder.AddFinal(finalOutbound)
Name: "fallback",
}, "", nil, fallbackOutbound)
builder.AddFallback(fallbackOutbound)
builder.AddAnyAfter(&config_parser.Function{
Name: "final",
}, "", nil, finalOutbound)
Name: "fallback",
}, "", nil, fallbackOutbound)
return nil
}
@ -200,7 +200,7 @@ func (d *DefaultMatcherBuilder) AddIpVersion(f *config_parser.Function, values c
}
func (d *DefaultMatcherBuilder) AddSourceMac(f *config_parser.Function, values [][6]byte, outbound string) {
}
func (d *DefaultMatcherBuilder) AddFinal(outbound string) {}
func (d *DefaultMatcherBuilder) AddFallback(outbound string) {}
func (d *DefaultMatcherBuilder) AddAnyBefore(f *config_parser.Function, key string, values []string, outbound string) {
}
func (d *DefaultMatcherBuilder) AddProcessName(f *config_parser.Function, values [][consts.TaskCommLen]byte, outbound string) {

View File

@ -41,8 +41,9 @@ type GroupParam struct {
}
type Routing struct {
Rules []*config_parser.RoutingRule `mapstructure:"_"`
Final string `mapstructure:"final" required:""`
Rules []*config_parser.RoutingRule `mapstructure:"_"`
Fallback string `mapstructure:"fallback"`
Final string `mapstructure:"final"`
}
type Params struct {
@ -110,5 +111,12 @@ func New(sections []*config_parser.Section) (params *Params, err error) {
return nil, fmt.Errorf("unknown section: %v", name)
}
}
// Apply config patches.
for _, patch := range patches {
if err = patch(params); err != nil {
return nil, err
}
}
return params, nil
}

26
config/patch.go Normal file
View File

@ -0,0 +1,26 @@
/*
* SPDX-License-Identifier: AGPL-3.0-only
* Copyright (c) since 2023, v2rayA Organization <team@v2raya.org>
*/
package config
import "fmt"
type patch func(params *Params) error
var patches = []patch{
patchRoutingFallback,
}
func patchRoutingFallback(params *Params) error {
// We renamed final as fallback. So we apply this patch for compatibility with older users.
if params.Routing.Fallback == "" && params.Routing.Final != "" {
params.Routing.Fallback = params.Routing.Final
}
// Fallback is required.
if params.Routing.Fallback == "" {
return fmt.Errorf("fallback is required in routing")
}
return nil
}

View File

@ -45,7 +45,7 @@ type ControlPlane struct {
SimulatedLpmTries [][]netip.Prefix
SimulatedDomainSet []DomainSet
Final string
Fallback string
// mutex protects the dnsCache.
dnsCacheMu sync.Mutex
@ -289,9 +289,9 @@ func NewControlPlane(
for _, rule := range rules {
debugBuilder.WriteString(rule.String(true) + "\n")
}
log.Debugf("RoutingA:\n%vfinal: %v\n", debugBuilder.String(), routingA.Final)
log.Debugf("RoutingA:\n%vfallback: %v\n", debugBuilder.String(), routingA.Fallback)
}
if err = routing.ApplyMatcherBuilder(log, builder, rules, routingA.Final); err != nil {
if err = routing.ApplyMatcherBuilder(log, builder, rules, routingA.Fallback); err != nil {
return nil, fmt.Errorf("ApplyMatcherBuilder: %w", err)
}
if err = builder.Build(); err != nil {
@ -308,7 +308,7 @@ func NewControlPlane(
outbounds: outbounds,
SimulatedLpmTries: builder.SimulatedLpmTries,
SimulatedDomainSet: builder.SimulatedDomainSet,
Final: routingA.Final,
Fallback: routingA.Fallback,
dnsCacheMu: sync.Mutex{},
dnsCache: make(map[string]*dnsCache),
dnsUpstream: DnsUpstreamRaw{

View File

@ -272,7 +272,7 @@ enum __attribute__((packed)) MatchType {
MatchType_IpVersion,
MatchType_Mac,
MatchType_ProcessName,
MatchType_Final,
MatchType_Fallback,
};
enum L4ProtoType {
L4ProtoType_TCP = 1,
@ -1106,9 +1106,9 @@ routing(const __u32 flag[6], const void *l4hdr, const __be32 saddr[4],
if (_is_wan && equal16(match_set->pname, _pname)) {
good_subrule = true;
}
} else if (match_set->type == MatchType_Final) {
} else if (match_set->type == MatchType_Fallback) {
#ifdef __DEBUG_ROUTING
bpf_printk("CHECK: hit final");
bpf_printk("CHECK: hit fallback");
#endif
good_subrule = true;
} else {

View File

@ -30,7 +30,7 @@ type RoutingMatcherBuilder struct {
rules []bpfMatchSet
SimulatedLpmTries [][]netip.Prefix
SimulatedDomainSet []DomainSet
Final string
Fallback string
err error
}
@ -215,13 +215,13 @@ func (b *RoutingMatcherBuilder) AddProcessName(f *config_parser.Function, values
}
}
func (b *RoutingMatcherBuilder) AddFinal(outbound string) {
func (b *RoutingMatcherBuilder) AddFallback(outbound string) {
if b.err != nil {
return
}
b.Final = outbound
b.Fallback = outbound
b.rules = append(b.rules, bpfMatchSet{
Type: uint8(consts.MatchType_Final),
Type: uint8(consts.MatchType_Fallback),
Outbound: b.OutboundToId(outbound),
})
}
@ -250,9 +250,9 @@ func (b *RoutingMatcherBuilder) Build() (err error) {
m.Close()
}
// Write routings.
// Final rule MUST be the last.
if b.rules[len(b.rules)-1].Type != uint8(consts.MatchType_Final) {
b.err = fmt.Errorf("final rule MUST be the last")
// Fallback rule MUST be the last.
if b.rules[len(b.rules)-1].Type != uint8(consts.MatchType_Fallback) {
b.err = fmt.Errorf("fallback rule MUST be the last")
return b.err
}
routingsLen := uint32(len(b.rules))

View File

@ -118,6 +118,5 @@ routing {
ip(geoip:cn) -> direct
domain(geosite:cn) -> direct
# Define final as the fallback outbound.
final: my_group
fallback: my_group
}

View File

@ -81,7 +81,7 @@ routing {
domain(geosite:netflix) -> netflix
ip(geoip:cn) -> direct
domain(geosite:cn) -> direct
final: my_group
fallback: my_group
}
`)
if err != nil {

View File

@ -5,8 +5,8 @@
```shell
# Built-in outbounds: block, direct
# If no rule matches, traffic will go through the outbound defined by final.
final: my_group
# If no rule matches, traffic will go through the outbound defined by fallback.
fallback: my_group
# Domain rule
domain(suffix: v2raya.org) -> my_group