mirror of
https://github.com/SagerNet/sing-box.git
synced 2025-08-30 05:57:36 +08:00
Compare commits
109 Commits
ccceb8b30a
...
ea149f2401
Author | SHA1 | Date | |
---|---|---|---|
![]() |
ea149f2401 | ||
![]() |
fe732ec51a | ||
![]() |
e650a8fdb0 | ||
![]() |
95f242086d | ||
![]() |
ed7fc2f366 | ||
![]() |
aabc089d26 | ||
![]() |
6db3c448b2 | ||
![]() |
d4789529e4 | ||
![]() |
6da963bb4f | ||
![]() |
a721c50745 | ||
![]() |
de96ccdd64 | ||
![]() |
dd80c94b07 | ||
![]() |
48f0e37dde | ||
![]() |
cff3c9e538 | ||
![]() |
fc04e6e3c7 | ||
![]() |
9375b542c5 | ||
![]() |
696b59373b | ||
![]() |
0135405735 | ||
![]() |
a7b269514e | ||
![]() |
b11311af1e | ||
![]() |
628cd5f993 | ||
![]() |
252894192d | ||
![]() |
177feafd2b | ||
![]() |
26d86e3831 | ||
![]() |
49ed32ec8e | ||
![]() |
ff264ad692 | ||
![]() |
9489ced466 | ||
![]() |
b43901001c | ||
![]() |
3687120b79 | ||
![]() |
f847aad67e | ||
![]() |
f7c72286dd | ||
![]() |
fa6d18b202 | ||
![]() |
b8f0dc16fb | ||
![]() |
c4eb22e954 | ||
![]() |
8acfc8be59 | ||
![]() |
355410f78d | ||
![]() |
0ca65510e5 | ||
![]() |
6444016923 | ||
![]() |
45fc68705c | ||
![]() |
0891c0b972 | ||
![]() |
e6854baeb0 | ||
![]() |
00c2fe7085 | ||
![]() |
a426679f64 | ||
![]() |
eb05fa9240 | ||
![]() |
ccee51c9ea | ||
![]() |
92dc32c851 | ||
![]() |
788b6a3512 | ||
![]() |
0a235149d6 | ||
![]() |
adc9b6a919 | ||
![]() |
ce264ed690 | ||
![]() |
fe963060f1 | ||
![]() |
07babae9bc | ||
![]() |
876f4999d5 | ||
![]() |
641f65df00 | ||
![]() |
7dbd4c5a7c | ||
![]() |
d72ae3f17f | ||
![]() |
4af5d6c276 | ||
![]() |
d527c441be | ||
![]() |
af6ee6b245 | ||
![]() |
873364c8fb | ||
![]() |
0980b2db93 | ||
![]() |
6f9b3b93be | ||
![]() |
236c0d9f6e | ||
![]() |
30115b6b68 | ||
![]() |
7c69371e49 | ||
![]() |
3decd70c59 | ||
![]() |
cb18b7aac2 | ||
![]() |
52a7bb75f5 | ||
![]() |
da0e88f960 | ||
![]() |
82418e4978 | ||
![]() |
4f60f9b7da | ||
![]() |
4324aa3e91 | ||
![]() |
736a0d3a73 | ||
![]() |
5c3c5b2493 | ||
![]() |
60fe58ff2b | ||
![]() |
3748e63d03 | ||
![]() |
6d5f8958f7 | ||
![]() |
e3c29ac877 | ||
![]() |
cc27586995 | ||
![]() |
e093fe3307 | ||
![]() |
e8a2cb638f | ||
![]() |
e42302a1a9 | ||
![]() |
c02ab0db8b | ||
![]() |
2f6b7e5b25 | ||
![]() |
81c5656dc6 | ||
![]() |
293ada693f | ||
![]() |
6df1994608 | ||
![]() |
d84e1ba87c | ||
![]() |
3c6c44ec42 | ||
![]() |
2b64f43f1f | ||
![]() |
9b13090ac4 | ||
![]() |
89a582b609 | ||
![]() |
4d6a1cc296 | ||
![]() |
d13b00a118 | ||
![]() |
e69b173464 | ||
![]() |
1e02700f6e | ||
![]() |
bc49dd9d3e | ||
![]() |
7278d2a71f | ||
![]() |
4a7a3e42b8 | ||
![]() |
8ab8d32b9b | ||
![]() |
df916b55bb | ||
![]() |
d4ef59f3aa | ||
![]() |
2936207436 | ||
![]() |
9e8f33c4f2 | ||
![]() |
9e6967186b | ||
![]() |
c1cebdda2e | ||
![]() |
9806a34411 | ||
![]() |
5dc74f93f4 | ||
![]() |
80f02962f9 |
@ -54,7 +54,7 @@ func convertRuleSet(sourcePath string) error {
|
||||
var rules []option.HeadlessRule
|
||||
switch flagRuleSetConvertType {
|
||||
case "adguard":
|
||||
rules, err = adguard.ToOptions(reader, log.StdLogger())
|
||||
rules, err = adguard.Convert(reader, log.StdLogger())
|
||||
case "":
|
||||
return E.New("source type is required")
|
||||
default:
|
||||
|
@ -6,10 +6,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/sagernet/sing-box/common/srs"
|
||||
C "github.com/sagernet/sing-box/constant"
|
||||
"github.com/sagernet/sing-box/log"
|
||||
"github.com/sagernet/sing-box/option"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
"github.com/sagernet/sing/common/json"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
@ -53,11 +50,6 @@ func decompileRuleSet(sourcePath string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if hasRule(ruleSet.Options.Rules, func(rule option.DefaultHeadlessRule) bool {
|
||||
return len(rule.AdGuardDomain) > 0
|
||||
}) {
|
||||
return E.New("unable to decompile binary AdGuard rules to rule-set.")
|
||||
}
|
||||
var outputPath string
|
||||
if flagRuleSetDecompileOutput == flagRuleSetDecompileDefaultOutput {
|
||||
if strings.HasSuffix(sourcePath, ".srs") {
|
||||
@ -83,19 +75,3 @@ func decompileRuleSet(sourcePath string) error {
|
||||
outputFile.Close()
|
||||
return nil
|
||||
}
|
||||
|
||||
func hasRule(rules []option.HeadlessRule, cond func(rule option.DefaultHeadlessRule) bool) bool {
|
||||
for _, rule := range rules {
|
||||
switch rule.Type {
|
||||
case C.RuleTypeDefault:
|
||||
if cond(rule.DefaultOptions) {
|
||||
return true
|
||||
}
|
||||
case C.RuleTypeLogical:
|
||||
if hasRule(rule.LogicalOptions.Rules, cond) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
@ -2,7 +2,6 @@ package adguard
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"io"
|
||||
"net/netip"
|
||||
"os"
|
||||
@ -28,7 +27,7 @@ type agdguardRuleLine struct {
|
||||
isImportant bool
|
||||
}
|
||||
|
||||
func ToOptions(reader io.Reader, logger logger.Logger) ([]option.HeadlessRule, error) {
|
||||
func Convert(reader io.Reader, logger logger.Logger) ([]option.HeadlessRule, error) {
|
||||
scanner := bufio.NewScanner(reader)
|
||||
var (
|
||||
ruleLines []agdguardRuleLine
|
||||
@ -37,17 +36,45 @@ func ToOptions(reader io.Reader, logger logger.Logger) ([]option.HeadlessRule, e
|
||||
parseLine:
|
||||
for scanner.Scan() {
|
||||
ruleLine := scanner.Text()
|
||||
|
||||
// Empty line
|
||||
if ruleLine == "" {
|
||||
continue
|
||||
}
|
||||
// Comment (both line comment and in-line comment)
|
||||
if strings.Contains(ruleLine, "!") {
|
||||
continue
|
||||
}
|
||||
// Either comment or cosmetic filter
|
||||
if strings.Contains(ruleLine, "#") {
|
||||
ignoredLines++
|
||||
logger.Debug("ignored unsupported cosmetic filter: ", ruleLine)
|
||||
continue
|
||||
}
|
||||
// We don't support URL query anyway
|
||||
if strings.Contains(ruleLine, "?") || strings.Contains(ruleLine, "&") {
|
||||
ignoredLines++
|
||||
logger.Debug("ignored unsupported rule with query: ", ruleLine)
|
||||
continue
|
||||
}
|
||||
// Commonly seen in CSS selectors of cosmetic filters
|
||||
if strings.Contains(ruleLine, "[") || strings.Contains(ruleLine, "]") {
|
||||
ignoredLines++
|
||||
logger.Debug("ignored unsupported cosmetic filter: ", ruleLine)
|
||||
continue
|
||||
}
|
||||
if strings.Contains(ruleLine, "(") || strings.Contains(ruleLine, ")") {
|
||||
ignoredLines++
|
||||
logger.Debug("ignored unsupported cosmetic filter: ", ruleLine)
|
||||
continue
|
||||
}
|
||||
// We don't support $domain modifier
|
||||
if strings.Contains(ruleLine, "~") {
|
||||
ignoredLines++
|
||||
logger.Debug("ignored unsupported rule modifier: ", ruleLine)
|
||||
continue
|
||||
}
|
||||
|
||||
originRuleLine := ruleLine
|
||||
if M.IsDomainName(ruleLine) {
|
||||
ruleLines = append(ruleLines, agdguardRuleLine{
|
||||
@ -142,26 +169,6 @@ parseLine:
|
||||
logger.Debug("ignored unsupported rule with path: ", ruleLine)
|
||||
continue
|
||||
}
|
||||
if strings.Contains(ruleLine, "?") || strings.Contains(ruleLine, "&") {
|
||||
ignoredLines++
|
||||
logger.Debug("ignored unsupported rule with query: ", ruleLine)
|
||||
continue
|
||||
}
|
||||
if strings.Contains(ruleLine, "[") || strings.Contains(ruleLine, "]") {
|
||||
ignoredLines++
|
||||
logger.Debug("ignored unsupported cosmetic filter: ", ruleLine)
|
||||
continue
|
||||
}
|
||||
if strings.Contains(ruleLine, "(") || strings.Contains(ruleLine, ")") {
|
||||
ignoredLines++
|
||||
logger.Debug("ignored unsupported cosmetic filter: ", ruleLine)
|
||||
continue
|
||||
}
|
||||
if strings.Contains(ruleLine, "~") {
|
||||
ignoredLines++
|
||||
logger.Debug("ignored unsupported rule modifier: ", ruleLine)
|
||||
continue
|
||||
}
|
||||
var domainCheck string
|
||||
if strings.HasPrefix(ruleLine, ".") || strings.HasPrefix(ruleLine, "-") {
|
||||
domainCheck = "r" + ruleLine
|
||||
@ -306,106 +313,6 @@ parseLine:
|
||||
return []option.HeadlessRule{currentRule}, nil
|
||||
}
|
||||
|
||||
var ErrInvalid = E.New("invalid binary AdGuard rule-set")
|
||||
|
||||
func FromOptions(rules []option.HeadlessRule) ([]byte, error) {
|
||||
if len(rules) != 1 {
|
||||
return nil, ErrInvalid
|
||||
}
|
||||
rule := rules[0]
|
||||
var (
|
||||
importantDomain []string
|
||||
importantDomainRegex []string
|
||||
importantExcludeDomain []string
|
||||
importantExcludeDomainRegex []string
|
||||
domain []string
|
||||
domainRegex []string
|
||||
excludeDomain []string
|
||||
excludeDomainRegex []string
|
||||
)
|
||||
parse:
|
||||
for {
|
||||
switch rule.Type {
|
||||
case C.RuleTypeLogical:
|
||||
if !(len(rule.LogicalOptions.Rules) == 2 && rule.LogicalOptions.Rules[0].Type == C.RuleTypeDefault) {
|
||||
return nil, ErrInvalid
|
||||
}
|
||||
if rule.LogicalOptions.Mode == C.LogicalTypeAnd && rule.LogicalOptions.Rules[0].DefaultOptions.Invert {
|
||||
if len(importantExcludeDomain) == 0 && len(importantExcludeDomainRegex) == 0 {
|
||||
importantExcludeDomain = rule.LogicalOptions.Rules[0].DefaultOptions.AdGuardDomain
|
||||
importantExcludeDomainRegex = rule.LogicalOptions.Rules[0].DefaultOptions.DomainRegex
|
||||
if len(importantExcludeDomain)+len(importantExcludeDomainRegex) == 0 {
|
||||
return nil, ErrInvalid
|
||||
}
|
||||
} else {
|
||||
excludeDomain = rule.LogicalOptions.Rules[0].DefaultOptions.AdGuardDomain
|
||||
excludeDomainRegex = rule.LogicalOptions.Rules[0].DefaultOptions.DomainRegex
|
||||
if len(excludeDomain)+len(excludeDomainRegex) == 0 {
|
||||
return nil, ErrInvalid
|
||||
}
|
||||
}
|
||||
} else if rule.LogicalOptions.Mode == C.LogicalTypeOr && !rule.LogicalOptions.Rules[0].DefaultOptions.Invert {
|
||||
importantDomain = rule.LogicalOptions.Rules[0].DefaultOptions.AdGuardDomain
|
||||
importantDomainRegex = rule.LogicalOptions.Rules[0].DefaultOptions.DomainRegex
|
||||
if len(importantDomain)+len(importantDomainRegex) == 0 {
|
||||
return nil, ErrInvalid
|
||||
}
|
||||
} else {
|
||||
return nil, ErrInvalid
|
||||
}
|
||||
rule = rule.LogicalOptions.Rules[1]
|
||||
case C.RuleTypeDefault:
|
||||
domain = rule.DefaultOptions.AdGuardDomain
|
||||
domainRegex = rule.DefaultOptions.DomainRegex
|
||||
if len(domain)+len(domainRegex) == 0 {
|
||||
return nil, ErrInvalid
|
||||
}
|
||||
break parse
|
||||
}
|
||||
}
|
||||
var output bytes.Buffer
|
||||
for _, ruleLine := range importantDomain {
|
||||
output.WriteString(ruleLine)
|
||||
output.WriteString("$important\n")
|
||||
}
|
||||
for _, ruleLine := range importantDomainRegex {
|
||||
output.WriteString("/")
|
||||
output.WriteString(ruleLine)
|
||||
output.WriteString("/$important\n")
|
||||
|
||||
}
|
||||
for _, ruleLine := range importantExcludeDomain {
|
||||
output.WriteString("@@")
|
||||
output.WriteString(ruleLine)
|
||||
output.WriteString("$important\n")
|
||||
}
|
||||
for _, ruleLine := range importantExcludeDomainRegex {
|
||||
output.WriteString("@@/")
|
||||
output.WriteString(ruleLine)
|
||||
output.WriteString("/$important\n")
|
||||
}
|
||||
for _, ruleLine := range domain {
|
||||
output.WriteString(ruleLine)
|
||||
output.WriteString("\n")
|
||||
}
|
||||
for _, ruleLine := range domainRegex {
|
||||
output.WriteString("/")
|
||||
output.WriteString(ruleLine)
|
||||
output.WriteString("/\n")
|
||||
}
|
||||
for _, ruleLine := range excludeDomain {
|
||||
output.WriteString("@@")
|
||||
output.WriteString(ruleLine)
|
||||
output.WriteString("\n")
|
||||
}
|
||||
for _, ruleLine := range excludeDomainRegex {
|
||||
output.WriteString("@@/")
|
||||
output.WriteString(ruleLine)
|
||||
output.WriteString("/\n")
|
||||
}
|
||||
return output.Bytes(), nil
|
||||
}
|
||||
|
||||
func ignoreIPCIDRRegexp(ruleLine string) bool {
|
||||
if strings.HasPrefix(ruleLine, "(http?:\\/\\/)") {
|
||||
ruleLine = ruleLine[12:]
|
||||
|
@ -14,8 +14,7 @@ import (
|
||||
|
||||
func TestConverter(t *testing.T) {
|
||||
t.Parallel()
|
||||
ruleString := `||sagernet.org^$important
|
||||
@@|sing-box.sagernet.org^$important
|
||||
rules, err := Convert(strings.NewReader(`
|
||||
||example.org^
|
||||
|example.com^
|
||||
example.net^
|
||||
@ -23,9 +22,10 @@ example.net^
|
||||
||example.edu.tw^
|
||||
|example.gov
|
||||
example.arpa
|
||||
@@|sagernet.example.org^
|
||||
`
|
||||
rules, err := ToOptions(strings.NewReader(ruleString), logger.NOP())
|
||||
@@|sagernet.example.org|
|
||||
||sagernet.org^$important
|
||||
@@|sing-box.sagernet.org^$important
|
||||
`), logger.NOP())
|
||||
require.NoError(t, err)
|
||||
require.Len(t, rules, 1)
|
||||
rule, err := rule.NewHeadlessRule(context.Background(), rules[0])
|
||||
@ -76,14 +76,11 @@ example.arpa
|
||||
Domain: domain,
|
||||
}), domain)
|
||||
}
|
||||
ruleFromOptions, err := FromOptions(rules)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, ruleString, string(ruleFromOptions))
|
||||
}
|
||||
|
||||
func TestHosts(t *testing.T) {
|
||||
t.Parallel()
|
||||
rules, err := ToOptions(strings.NewReader(`
|
||||
rules, err := Convert(strings.NewReader(`
|
||||
127.0.0.1 localhost
|
||||
::1 localhost #[IPv6]
|
||||
0.0.0.0 google.com
|
||||
@ -114,7 +111,7 @@ func TestHosts(t *testing.T) {
|
||||
|
||||
func TestSimpleHosts(t *testing.T) {
|
||||
t.Parallel()
|
||||
rules, err := ToOptions(strings.NewReader(`
|
||||
rules, err := Convert(strings.NewReader(`
|
||||
example.com
|
||||
www.example.org
|
||||
`), logger.NOP())
|
||||
|
@ -215,15 +215,16 @@ func readDefaultRule(reader varbin.Reader, recover bool) (rule option.DefaultHea
|
||||
case ruleItemWIFIBSSID:
|
||||
rule.WIFIBSSID, err = readRuleItemString(reader)
|
||||
case ruleItemAdGuardDomain:
|
||||
if recover {
|
||||
err = E.New("unable to decompile binary AdGuard rules to rule-set")
|
||||
return
|
||||
}
|
||||
var matcher *domain.AdGuardMatcher
|
||||
matcher, err = domain.ReadAdGuardMatcher(reader)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
rule.AdGuardDomainMatcher = matcher
|
||||
if recover {
|
||||
rule.AdGuardDomain = matcher.Dump()
|
||||
}
|
||||
case ruleItemNetworkType:
|
||||
rule.NetworkType, err = readRuleItemUint8[option.InterfaceType](reader)
|
||||
case ruleItemNetworkIsExpensive:
|
||||
|
@ -2,7 +2,7 @@
|
||||
icon: material/alert-decagram
|
||||
---
|
||||
|
||||
#### 1.12.0-beta.26
|
||||
#### 1.12.0-beta.25
|
||||
|
||||
* Fixes and improvements
|
||||
|
||||
|
2
go.mod
2
go.mod
@ -34,7 +34,7 @@ require (
|
||||
github.com/sagernet/sing-shadowsocks v0.2.8
|
||||
github.com/sagernet/sing-shadowsocks2 v0.2.1
|
||||
github.com/sagernet/sing-shadowtls v0.2.1-0.20250503051639-fcd445d33c11
|
||||
github.com/sagernet/sing-tun v0.6.9-0.20250617062442-df4458520f26
|
||||
github.com/sagernet/sing-tun v0.6.8-0.20250615093440-d1f6001b58c2
|
||||
github.com/sagernet/sing-vmess v0.2.4-0.20250605032146-38cc72672c88
|
||||
github.com/sagernet/smux v1.5.34-mod.2
|
||||
github.com/sagernet/tailscale v1.80.3-mod.5
|
||||
|
4
go.sum
4
go.sum
@ -180,8 +180,8 @@ github.com/sagernet/sing-shadowsocks2 v0.2.1 h1:dWV9OXCeFPuYGHb6IRqlSptVnSzOelnq
|
||||
github.com/sagernet/sing-shadowsocks2 v0.2.1/go.mod h1:RnXS0lExcDAovvDeniJ4IKa2IuChrdipolPYWBv9hWQ=
|
||||
github.com/sagernet/sing-shadowtls v0.2.1-0.20250503051639-fcd445d33c11 h1:tK+75l64tm9WvEFrYRE1t0YxoFdWQqw/h7Uhzj0vJ+w=
|
||||
github.com/sagernet/sing-shadowtls v0.2.1-0.20250503051639-fcd445d33c11/go.mod h1:sWqKnGlMipCHaGsw1sTTlimyUpgzP4WP3pjhCsYt9oA=
|
||||
github.com/sagernet/sing-tun v0.6.9-0.20250617062442-df4458520f26 h1:N9yAoqtQlawlJzLONjdQvIO3GJLEg9tZBfyKToSi0cM=
|
||||
github.com/sagernet/sing-tun v0.6.9-0.20250617062442-df4458520f26/go.mod h1:fisFCbC4Vfb6HqQNcwPJi2CDK2bf0Xapyz3j3t4cnHE=
|
||||
github.com/sagernet/sing-tun v0.6.8-0.20250615093440-d1f6001b58c2 h1:leio5dGtYCKHQam+SyLszq4bbXGaxF4ElK5Aif/lUb8=
|
||||
github.com/sagernet/sing-tun v0.6.8-0.20250615093440-d1f6001b58c2/go.mod h1:fisFCbC4Vfb6HqQNcwPJi2CDK2bf0Xapyz3j3t4cnHE=
|
||||
github.com/sagernet/sing-vmess v0.2.4-0.20250605032146-38cc72672c88 h1:0pVm8sPOel+BoiCddW3pV3cKDKEaSioVTYDdTSKjyFI=
|
||||
github.com/sagernet/sing-vmess v0.2.4-0.20250605032146-38cc72672c88/go.mod h1:IL8Rr+EGwuqijszZkNrEFTQDKhilEpkqFqOlvdpS6/w=
|
||||
github.com/sagernet/smux v1.5.34-mod.2 h1:gkmBjIjlJ2zQKpLigOkFur5kBKdV6bNRoFu2WkltRQ4=
|
||||
|
Loading…
x
Reference in New Issue
Block a user