mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-25 10:01:30 +00:00
Add IP address support for rule-set match
match
This commit is contained in:
parent
f4f5a3c925
commit
dcb0141646
|
@ -14,6 +14,7 @@ import (
|
||||||
E "github.com/sagernet/sing/common/exceptions"
|
E "github.com/sagernet/sing/common/exceptions"
|
||||||
F "github.com/sagernet/sing/common/format"
|
F "github.com/sagernet/sing/common/format"
|
||||||
"github.com/sagernet/sing/common/json"
|
"github.com/sagernet/sing/common/json"
|
||||||
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
@ -21,8 +22,8 @@ import (
|
||||||
var flagRuleSetMatchFormat string
|
var flagRuleSetMatchFormat string
|
||||||
|
|
||||||
var commandRuleSetMatch = &cobra.Command{
|
var commandRuleSetMatch = &cobra.Command{
|
||||||
Use: "match <rule-set path> <domain>",
|
Use: "match <rule-set path> <IP address/domain>",
|
||||||
Short: "Check if a domain matches the rule set",
|
Short: "Check if an IP address or a domain matches the rule set",
|
||||||
Args: cobra.ExactArgs(2),
|
Args: cobra.ExactArgs(2),
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
err := ruleSetMatch(args[0], args[1])
|
err := ruleSetMatch(args[0], args[1])
|
||||||
|
@ -71,15 +72,20 @@ func ruleSetMatch(sourcePath string, domain string) error {
|
||||||
default:
|
default:
|
||||||
return E.New("unknown rule set format: ", flagRuleSetMatchFormat)
|
return E.New("unknown rule set format: ", flagRuleSetMatchFormat)
|
||||||
}
|
}
|
||||||
|
ipAddress := M.ParseAddr(domain)
|
||||||
|
var metadata adapter.InboundContext
|
||||||
|
if ipAddress.IsValid() {
|
||||||
|
metadata.Destination = M.SocksaddrFrom(ipAddress, 0)
|
||||||
|
} else {
|
||||||
|
metadata.Domain = domain
|
||||||
|
}
|
||||||
for i, ruleOptions := range plainRuleSet.Rules {
|
for i, ruleOptions := range plainRuleSet.Rules {
|
||||||
var currentRule adapter.HeadlessRule
|
var currentRule adapter.HeadlessRule
|
||||||
currentRule, err = route.NewHeadlessRule(nil, ruleOptions)
|
currentRule, err = route.NewHeadlessRule(nil, ruleOptions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return E.Cause(err, "parse rule_set.rules.[", i, "]")
|
return E.Cause(err, "parse rule_set.rules.[", i, "]")
|
||||||
}
|
}
|
||||||
if currentRule.Match(&adapter.InboundContext{
|
if currentRule.Match(&metadata) {
|
||||||
Domain: domain,
|
|
||||||
}) {
|
|
||||||
println(F.ToString("match rules.[", i, "]: ", currentRule))
|
println(F.ToString("match rules.[", i, "]: ", currentRule))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue