Move predefined DNS server to rule action

This commit is contained in:
世界 2025-02-26 08:59:21 +08:00
parent 7c5ed07589
commit 17ffee13d1
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
15 changed files with 289 additions and 282 deletions

View file

@ -20,6 +20,8 @@ import (
"github.com/sagernet/sing/common/logger"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
"github.com/miekg/dns"
)
func NewRuleAction(ctx context.Context, logger logger.ContextLogger, action option.RuleAction) (adapter.RuleAction, error) {
@ -126,6 +128,13 @@ func NewDNSRuleAction(logger logger.ContextLogger, action option.DNSRuleAction)
NoDrop: action.RejectOptions.NoDrop,
logger: logger,
}
case C.RuleActionTypePredefined:
return &RuleActionPredefined{
Rcode: action.PredefinedOptions.Rcode.Build(),
Answer: common.Map(action.PredefinedOptions.Answer, option.DNSRecordOptions.Build),
Ns: common.Map(action.PredefinedOptions.Ns, option.DNSRecordOptions.Build),
Extra: common.Map(action.PredefinedOptions.Extra, option.DNSRecordOptions.Build),
}
default:
panic(F.ToString("unknown rule action: ", action.Action))
}
@ -413,3 +422,23 @@ func (r *RuleActionResolve) String() string {
return F.ToString("resolve(", strings.Join(options, ","), ")")
}
}
type RuleActionPredefined struct {
Rcode int
Answer []dns.RR
Ns []dns.RR
Extra []dns.RR
}
func (r *RuleActionPredefined) Type() string {
return C.RuleActionTypePredefined
}
func (r *RuleActionPredefined) String() string {
var options []string
options = append(options, dns.RcodeToString[r.Rcode])
options = append(options, common.Map(r.Answer, dns.RR.String)...)
options = append(options, common.Map(r.Ns, dns.RR.String)...)
options = append(options, common.Map(r.Extra, dns.RR.String)...)
return F.ToString("predefined(", strings.Join(options, ","), ")")
}