Merge route options to route actions

This commit is contained in:
世界 2024-11-11 16:32:34 +08:00
parent 9988144868
commit ef2a2fdd52
No known key found for this signature in database
GPG Key ID: CD109927C34A63C4
4 changed files with 51 additions and 56 deletions

View File

@ -100,15 +100,6 @@ var OptionInboundOptions = Note{
MigrationLink: "https://sing-box.sagernet.org/migration/#migrate-legacy-special-outbounds-to-rule-actions", MigrationLink: "https://sing-box.sagernet.org/migration/#migrate-legacy-special-outbounds-to-rule-actions",
} }
var OptionLegacyDNSRouteOptions = Note{
Name: "legacy-dns-route-options",
Description: "legacy dns route options",
DeprecatedVersion: "1.11.0",
ScheduledVersion: "1.12.0",
EnvName: "LEGACY_DNS_ROUTE_OPTIONS",
MigrationLink: "https://sing-box.sagernet.org/migration/#migrate-legacy-dns-route-options-to-rule-actions",
}
var Options = []Note{ var Options = []Note{
OptionBadMatchSource, OptionBadMatchSource,
OptionGEOIP, OptionGEOIP,
@ -116,5 +107,4 @@ var Options = []Note{
OptionTUNAddressX, OptionTUNAddressX,
OptionSpecialOutbounds, OptionSpecialOutbounds,
OptionInboundOptions, OptionInboundOptions,
OptionLegacyDNSRouteOptions,
} }

View File

@ -7,8 +7,7 @@ import (
"time" "time"
C "github.com/sagernet/sing-box/constant" C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/experimental/deprecated" "github.com/sagernet/sing-dns"
dns "github.com/sagernet/sing-dns"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/json" "github.com/sagernet/sing/common/json"
"github.com/sagernet/sing/common/json/badjson" "github.com/sagernet/sing/common/json/badjson"
@ -137,18 +136,10 @@ func (r *DNSRuleAction) UnmarshalJSONContext(ctx context.Context, data []byte) e
return badjson.UnmarshallExcludedContext(ctx, data, (*_DNSRuleAction)(r), v) return badjson.UnmarshallExcludedContext(ctx, data, (*_DNSRuleAction)(r), v)
} }
type _RouteActionOptions struct { type RouteActionOptions struct {
Outbound string `json:"outbound,omitempty"` Outbound string `json:"outbound,omitempty"`
} UDPDisableDomainUnmapping bool `json:"udp_disable_domain_unmapping,omitempty"`
UDPConnect bool `json:"udp_connect,omitempty"`
type RouteActionOptions _RouteActionOptions
func (r *RouteActionOptions) UnmarshalJSON(data []byte) error {
err := json.Unmarshal(data, (*_RouteActionOptions)(r))
if err != nil {
return err
}
return nil
} }
type _RouteOptionsActionOptions struct { type _RouteOptionsActionOptions struct {
@ -169,29 +160,13 @@ func (r *RouteOptionsActionOptions) UnmarshalJSON(data []byte) error {
return nil return nil
} }
type _DNSRouteActionOptions struct { type DNSRouteActionOptions struct {
Server string `json:"server,omitempty"` Server string `json:"server,omitempty"`
// Deprecated: Use DNSRouteOptionsActionOptions instead.
DisableCache bool `json:"disable_cache,omitempty"` DisableCache bool `json:"disable_cache,omitempty"`
// Deprecated: Use DNSRouteOptionsActionOptions instead.
RewriteTTL *uint32 `json:"rewrite_ttl,omitempty"` RewriteTTL *uint32 `json:"rewrite_ttl,omitempty"`
// Deprecated: Use DNSRouteOptionsActionOptions instead.
ClientSubnet *badoption.Prefixable `json:"client_subnet,omitempty"` ClientSubnet *badoption.Prefixable `json:"client_subnet,omitempty"`
} }
type DNSRouteActionOptions _DNSRouteActionOptions
func (r *DNSRouteActionOptions) UnmarshalJSONContext(ctx context.Context, data []byte) error {
err := json.Unmarshal(data, (*_DNSRouteActionOptions)(r))
if err != nil {
return err
}
if r.DisableCache || r.RewriteTTL != nil || r.ClientSubnet != nil {
deprecated.Report(ctx, deprecated.OptionLegacyDNSRouteOptions)
}
return nil
}
type _DNSRouteOptionsActionOptions struct { type _DNSRouteOptionsActionOptions struct {
DisableCache bool `json:"disable_cache,omitempty"` DisableCache bool `json:"disable_cache,omitempty"`
RewriteTTL *uint32 `json:"rewrite_ttl,omitempty"` RewriteTTL *uint32 `json:"rewrite_ttl,omitempty"`

View File

@ -437,6 +437,12 @@ match:
} }
} }
switch action := currentRule.Action().(type) { switch action := currentRule.Action().(type) {
case *rule.RuleActionRoute:
metadata.UDPDisableDomainUnmapping = action.UDPDisableDomainUnmapping
metadata.UDPConnect = action.UDPConnect
selectedRule = currentRule
selectedRuleIndex = currentRuleIndex
break match
case *rule.RuleActionRouteOptions: case *rule.RuleActionRouteOptions:
metadata.UDPDisableDomainUnmapping = action.UDPDisableDomainUnmapping metadata.UDPDisableDomainUnmapping = action.UDPDisableDomainUnmapping
metadata.UDPConnect = action.UDPConnect metadata.UDPConnect = action.UDPConnect

View File

@ -29,6 +29,10 @@ func NewRuleAction(ctx context.Context, logger logger.ContextLogger, action opti
case C.RuleActionTypeRoute: case C.RuleActionTypeRoute:
return &RuleActionRoute{ return &RuleActionRoute{
Outbound: action.RouteOptions.Outbound, Outbound: action.RouteOptions.Outbound,
RuleActionRouteOptions: RuleActionRouteOptions{
UDPDisableDomainUnmapping: action.RouteOptions.UDPDisableDomainUnmapping,
UDPConnect: action.RouteOptions.UDPConnect,
},
}, nil }, nil
case C.RuleActionTypeRouteOptions: case C.RuleActionTypeRouteOptions:
return &RuleActionRouteOptions{ return &RuleActionRouteOptions{
@ -86,9 +90,11 @@ func NewDNSRuleAction(logger logger.ContextLogger, action option.DNSRuleAction)
case C.RuleActionTypeRoute: case C.RuleActionTypeRoute:
return &RuleActionDNSRoute{ return &RuleActionDNSRoute{
Server: action.RouteOptions.Server, Server: action.RouteOptions.Server,
RuleActionDNSRouteOptions: RuleActionDNSRouteOptions{
DisableCache: action.RouteOptions.DisableCache, DisableCache: action.RouteOptions.DisableCache,
RewriteTTL: action.RouteOptions.RewriteTTL, RewriteTTL: action.RouteOptions.RewriteTTL,
ClientSubnet: netip.Prefix(common.PtrValueOrDefault(action.RouteOptions.ClientSubnet)), ClientSubnet: netip.Prefix(common.PtrValueOrDefault(action.RouteOptions.ClientSubnet)),
},
} }
case C.RuleActionTypeRouteOptions: case C.RuleActionTypeRouteOptions:
return &RuleActionDNSRouteOptions{ return &RuleActionDNSRouteOptions{
@ -109,6 +115,7 @@ func NewDNSRuleAction(logger logger.ContextLogger, action option.DNSRuleAction)
type RuleActionRoute struct { type RuleActionRoute struct {
Outbound string Outbound string
RuleActionRouteOptions
} }
func (r *RuleActionRoute) Type() string { func (r *RuleActionRoute) Type() string {
@ -116,7 +123,15 @@ func (r *RuleActionRoute) Type() string {
} }
func (r *RuleActionRoute) String() string { func (r *RuleActionRoute) String() string {
return F.ToString("route(", r.Outbound, ")") var descriptions []string
descriptions = append(descriptions, r.Outbound)
if r.UDPDisableDomainUnmapping {
descriptions = append(descriptions, "udp-disable-domain-unmapping")
}
if r.UDPConnect {
descriptions = append(descriptions, "udp-connect")
}
return F.ToString("route(", strings.Join(descriptions, ","), ")")
} }
type RuleActionRouteOptions struct { type RuleActionRouteOptions struct {
@ -141,9 +156,7 @@ func (r *RuleActionRouteOptions) String() string {
type RuleActionDNSRoute struct { type RuleActionDNSRoute struct {
Server string Server string
DisableCache bool RuleActionDNSRouteOptions
RewriteTTL *uint32
ClientSubnet netip.Prefix
} }
func (r *RuleActionDNSRoute) Type() string { func (r *RuleActionDNSRoute) Type() string {
@ -151,7 +164,18 @@ func (r *RuleActionDNSRoute) Type() string {
} }
func (r *RuleActionDNSRoute) String() string { func (r *RuleActionDNSRoute) String() string {
return F.ToString("route(", r.Server, ")") var descriptions []string
descriptions = append(descriptions, r.Server)
if r.DisableCache {
descriptions = append(descriptions, "disable-cache")
}
if r.RewriteTTL != nil {
descriptions = append(descriptions, F.ToString("rewrite-ttl=", *r.RewriteTTL))
}
if r.ClientSubnet.IsValid() {
descriptions = append(descriptions, F.ToString("client-subnet=", r.ClientSubnet))
}
return F.ToString("route(", strings.Join(descriptions, ","), ")")
} }
type RuleActionDNSRouteOptions struct { type RuleActionDNSRouteOptions struct {
@ -170,10 +194,10 @@ func (r *RuleActionDNSRouteOptions) String() string {
descriptions = append(descriptions, "disable-cache") descriptions = append(descriptions, "disable-cache")
} }
if r.RewriteTTL != nil { if r.RewriteTTL != nil {
descriptions = append(descriptions, F.ToString("rewrite-ttl(", *r.RewriteTTL, ")")) descriptions = append(descriptions, F.ToString("rewrite-ttl=", *r.RewriteTTL))
} }
if r.ClientSubnet.IsValid() { if r.ClientSubnet.IsValid() {
descriptions = append(descriptions, F.ToString("client-subnet(", r.ClientSubnet, ")")) descriptions = append(descriptions, F.ToString("client-subnet=", r.ClientSubnet))
} }
return F.ToString("route-options(", strings.Join(descriptions, ","), ")") return F.ToString("route-options(", strings.Join(descriptions, ","), ")")
} }