@@ -143,8 +143,8 @@ type DNS struct {
143
143
UseSystemHosts bool
144
144
NameServer []dns.NameServer
145
145
Fallback []dns.NameServer
146
- FallbackIPFilter []C.Rule
147
- FallbackDomainFilter []C.Rule
146
+ FallbackIPFilter []C.IpMatcher
147
+ FallbackDomainFilter []C.DomainMatcher
148
148
Listen string
149
149
EnhancedMode C.DNSMode
150
150
DefaultNameserver []dns.NameServer
@@ -640,7 +640,7 @@ func ParseRawConfig(rawCfg *RawConfig) (*Config, error) {
640
640
}
641
641
config .Hosts = hosts
642
642
643
- dnsCfg , err := parseDNS (rawCfg , hosts , rules , ruleProviders )
643
+ dnsCfg , err := parseDNS (rawCfg , hosts , ruleProviders )
644
644
if err != nil {
645
645
return nil , err
646
646
}
@@ -1297,7 +1297,7 @@ func parsePureDNSServer(server string) string {
1297
1297
}
1298
1298
}
1299
1299
1300
- func parseNameServerPolicy (nsPolicy * orderedmap.OrderedMap [string , any ], rules []C. Rule , ruleProviders map [string ]providerTypes.RuleProvider , respectRules bool , preferH3 bool ) ([]dns.Policy , error ) {
1300
+ func parseNameServerPolicy (nsPolicy * orderedmap.OrderedMap [string , any ], ruleProviders map [string ]providerTypes.RuleProvider , respectRules bool , preferH3 bool ) ([]dns.Policy , error ) {
1301
1301
var policy []dns.Policy
1302
1302
re := regexp .MustCompile (`[a-zA-Z0-9\-]+\.[a-zA-Z]{2,}(\.[a-zA-Z]{2,})?` )
1303
1303
@@ -1350,18 +1350,18 @@ func parseNameServerPolicy(nsPolicy *orderedmap.OrderedMap[string, any], rules [
1350
1350
1351
1351
if strings .HasPrefix (domain , "rule-set:" ) {
1352
1352
domainSetName := domain [9 :]
1353
- rule , err := parseDomainRuleSet (domainSetName , "dns.nameserver-policy" , ruleProviders )
1353
+ matcher , err := parseDomainRuleSet (domainSetName , "dns.nameserver-policy" , ruleProviders )
1354
1354
if err != nil {
1355
1355
return nil , err
1356
1356
}
1357
- policy [idx ] = dns.Policy {Rule : rule , NameServers : nameservers }
1357
+ policy [idx ] = dns.Policy {Matcher : matcher , NameServers : nameservers }
1358
1358
} else if strings .HasPrefix (domain , "geosite:" ) {
1359
1359
country := domain [8 :]
1360
- rule , err := RC .NewGEOSITE (country , "dns.nameserver-policy" )
1360
+ matcher , err := RC .NewGEOSITE (country , "dns.nameserver-policy" )
1361
1361
if err != nil {
1362
1362
return nil , err
1363
1363
}
1364
- policy [idx ] = dns.Policy {Rule : rule , NameServers : nameservers }
1364
+ policy [idx ] = dns.Policy {Matcher : matcher , NameServers : nameservers }
1365
1365
} else {
1366
1366
if _ , valid := trie .ValidAndSplitDomain (domain ); ! valid {
1367
1367
return nil , fmt .Errorf ("DNS ResoverRule invalid domain: %s" , domain )
@@ -1372,7 +1372,7 @@ func parseNameServerPolicy(nsPolicy *orderedmap.OrderedMap[string, any], rules [
1372
1372
return policy , nil
1373
1373
}
1374
1374
1375
- func parseDNS (rawCfg * RawConfig , hosts * trie.DomainTrie [resolver.HostValue ], rules []C. Rule , ruleProviders map [string ]providerTypes.RuleProvider ) (* DNS , error ) {
1375
+ func parseDNS (rawCfg * RawConfig , hosts * trie.DomainTrie [resolver.HostValue ], ruleProviders map [string ]providerTypes.RuleProvider ) (* DNS , error ) {
1376
1376
cfg := rawCfg .DNS
1377
1377
if cfg .Enable && len (cfg .NameServer ) == 0 {
1378
1378
return nil , fmt .Errorf ("if DNS configuration is turned on, NameServer cannot be empty" )
@@ -1400,7 +1400,7 @@ func parseDNS(rawCfg *RawConfig, hosts *trie.DomainTrie[resolver.HostValue], rul
1400
1400
return nil , err
1401
1401
}
1402
1402
1403
- if dnsCfg .NameServerPolicy , err = parseNameServerPolicy (cfg .NameServerPolicy , rules , ruleProviders , cfg .RespectRules , cfg .PreferH3 ); err != nil {
1403
+ if dnsCfg .NameServerPolicy , err = parseNameServerPolicy (cfg .NameServerPolicy , ruleProviders , cfg .RespectRules , cfg .PreferH3 ); err != nil {
1404
1404
return nil , err
1405
1405
}
1406
1406
@@ -1467,14 +1467,13 @@ func parseDNS(rawCfg *RawConfig, hosts *trie.DomainTrie[resolver.HostValue], rul
1467
1467
dnsCfg .FakeIPRange = pool
1468
1468
}
1469
1469
1470
- var rule C.Rule
1471
1470
if len (cfg .Fallback ) != 0 {
1472
1471
if cfg .FallbackFilter .GeoIP {
1473
- rule , err = RC .NewGEOIP (cfg .FallbackFilter .GeoIPCode , "dns.fallback-filter.geoip" , false , true )
1472
+ matcher , err : = RC .NewGEOIP (cfg .FallbackFilter .GeoIPCode , "dns.fallback-filter.geoip" , false , true )
1474
1473
if err != nil {
1475
1474
return nil , fmt .Errorf ("load GeoIP dns fallback filter error, %w" , err )
1476
1475
}
1477
- dnsCfg .FallbackIPFilter = append (dnsCfg .FallbackIPFilter , rule )
1476
+ dnsCfg .FallbackIPFilter = append (dnsCfg .FallbackIPFilter , matcher )
1478
1477
}
1479
1478
if len (cfg .FallbackFilter .IPCIDR ) > 0 {
1480
1479
cidrSet := cidr .NewIpCidrSet ()
@@ -1488,8 +1487,8 @@ func parseDNS(rawCfg *RawConfig, hosts *trie.DomainTrie[resolver.HostValue], rul
1488
1487
if err != nil {
1489
1488
return nil , err
1490
1489
}
1491
- rule = RP . NewIpCidrSet ( cidrSet , " dns.fallback-filter.ipcidr" )
1492
- dnsCfg .FallbackIPFilter = append (dnsCfg .FallbackIPFilter , rule )
1490
+ matcher := cidrSet // dns.fallback-filter.ipcidr
1491
+ dnsCfg .FallbackIPFilter = append (dnsCfg .FallbackIPFilter , matcher )
1493
1492
}
1494
1493
if len (cfg .FallbackFilter .Domain ) > 0 {
1495
1494
domainTrie := trie .New [struct {}]()
@@ -1499,17 +1498,17 @@ func parseDNS(rawCfg *RawConfig, hosts *trie.DomainTrie[resolver.HostValue], rul
1499
1498
return nil , fmt .Errorf ("DNS FallbackDomain[%d] format error: %w" , idx , err )
1500
1499
}
1501
1500
}
1502
- rule = RP . NewDomainSet ( domainTrie .NewDomainSet (), " dns.fallback-filter.domain" )
1503
- dnsCfg .FallbackDomainFilter = append (dnsCfg .FallbackDomainFilter , rule )
1501
+ matcher := domainTrie .NewDomainSet () // dns.fallback-filter.domain
1502
+ dnsCfg .FallbackDomainFilter = append (dnsCfg .FallbackDomainFilter , matcher )
1504
1503
}
1505
1504
if len (cfg .FallbackFilter .GeoSite ) > 0 {
1506
1505
log .Warnln ("replace fallback-filter.geosite with nameserver-policy, it will be removed in the future" )
1507
1506
for idx , geoSite := range cfg .FallbackFilter .GeoSite {
1508
- rule , err = RC .NewGEOSITE (geoSite , "dns.fallback-filter.geosite" )
1507
+ matcher , err : = RC .NewGEOSITE (geoSite , "dns.fallback-filter.geosite" )
1509
1508
if err != nil {
1510
1509
return nil , fmt .Errorf ("DNS FallbackGeosite[%d] format error: %w" , idx , err )
1511
1510
}
1512
- dnsCfg .FallbackDomainFilter = append (dnsCfg .FallbackDomainFilter , rule )
1511
+ dnsCfg .FallbackDomainFilter = append (dnsCfg .FallbackDomainFilter , matcher )
1513
1512
}
1514
1513
}
1515
1514
}
@@ -1701,31 +1700,31 @@ func parseSniffer(snifferRaw RawSniffer, ruleProviders map[string]providerTypes.
1701
1700
return snifferConfig , nil
1702
1701
}
1703
1702
1704
- func parseIPCIDR (addresses []string , cidrSet * cidr.IpCidrSet , adapterName string , ruleProviders map [string ]providerTypes.RuleProvider ) (ipRules []C.Rule , err error ) {
1705
- var rule C.Rule
1703
+ func parseIPCIDR (addresses []string , cidrSet * cidr.IpCidrSet , adapterName string , ruleProviders map [string ]providerTypes.RuleProvider ) (matchers []C.IpMatcher , err error ) {
1704
+ var matcher C.IpMatcher
1706
1705
for _ , ipcidr := range addresses {
1707
1706
ipcidrLower := strings .ToLower (ipcidr )
1708
1707
if strings .Contains (ipcidrLower , "geoip:" ) {
1709
1708
subkeys := strings .Split (ipcidr , ":" )
1710
1709
subkeys = subkeys [1 :]
1711
1710
subkeys = strings .Split (subkeys [0 ], "," )
1712
1711
for _ , country := range subkeys {
1713
- rule , err = RC .NewGEOIP (country , adapterName , false , false )
1712
+ matcher , err = RC .NewGEOIP (country , adapterName , false , false )
1714
1713
if err != nil {
1715
1714
return nil , err
1716
1715
}
1717
- ipRules = append (ipRules , rule )
1716
+ matchers = append (matchers , matcher )
1718
1717
}
1719
1718
} else if strings .Contains (ipcidrLower , "rule-set:" ) {
1720
1719
subkeys := strings .Split (ipcidr , ":" )
1721
1720
subkeys = subkeys [1 :]
1722
1721
subkeys = strings .Split (subkeys [0 ], "," )
1723
1722
for _ , domainSetName := range subkeys {
1724
- rule , err = parseIPRuleSet (domainSetName , adapterName , ruleProviders )
1723
+ matcher , err = parseIPRuleSet (domainSetName , adapterName , ruleProviders )
1725
1724
if err != nil {
1726
1725
return nil , err
1727
1726
}
1728
- ipRules = append (ipRules , rule )
1727
+ matchers = append (matchers , matcher )
1729
1728
}
1730
1729
} else {
1731
1730
if cidrSet == nil {
@@ -1742,37 +1741,37 @@ func parseIPCIDR(addresses []string, cidrSet *cidr.IpCidrSet, adapterName string
1742
1741
if err != nil {
1743
1742
return nil , err
1744
1743
}
1745
- rule = RP . NewIpCidrSet ( cidrSet , adapterName )
1746
- ipRules = append (ipRules , rule )
1744
+ matcher = cidrSet
1745
+ matchers = append (matchers , matcher )
1747
1746
}
1748
1747
return
1749
1748
}
1750
1749
1751
- func parseDomain (domains []string , domainTrie * trie.DomainTrie [struct {}], adapterName string , ruleProviders map [string ]providerTypes.RuleProvider ) (domainRules []C.Rule , err error ) {
1752
- var rule C.Rule
1750
+ func parseDomain (domains []string , domainTrie * trie.DomainTrie [struct {}], adapterName string , ruleProviders map [string ]providerTypes.RuleProvider ) (matchers []C.DomainMatcher , err error ) {
1751
+ var matcher C.DomainMatcher
1753
1752
for _ , domain := range domains {
1754
1753
domainLower := strings .ToLower (domain )
1755
1754
if strings .Contains (domainLower , "geosite:" ) {
1756
1755
subkeys := strings .Split (domain , ":" )
1757
1756
subkeys = subkeys [1 :]
1758
1757
subkeys = strings .Split (subkeys [0 ], "," )
1759
1758
for _ , country := range subkeys {
1760
- rule , err = RC .NewGEOSITE (country , adapterName )
1759
+ matcher , err = RC .NewGEOSITE (country , adapterName )
1761
1760
if err != nil {
1762
1761
return nil , err
1763
1762
}
1764
- domainRules = append (domainRules , rule )
1763
+ matchers = append (matchers , matcher )
1765
1764
}
1766
1765
} else if strings .Contains (domainLower , "rule-set:" ) {
1767
1766
subkeys := strings .Split (domain , ":" )
1768
1767
subkeys = subkeys [1 :]
1769
1768
subkeys = strings .Split (subkeys [0 ], "," )
1770
1769
for _ , domainSetName := range subkeys {
1771
- rule , err = parseDomainRuleSet (domainSetName , adapterName , ruleProviders )
1770
+ matcher , err = parseDomainRuleSet (domainSetName , adapterName , ruleProviders )
1772
1771
if err != nil {
1773
1772
return nil , err
1774
1773
}
1775
- domainRules = append (domainRules , rule )
1774
+ matchers = append (matchers , matcher )
1776
1775
}
1777
1776
} else {
1778
1777
if domainTrie == nil {
@@ -1785,13 +1784,13 @@ func parseDomain(domains []string, domainTrie *trie.DomainTrie[struct{}], adapte
1785
1784
}
1786
1785
}
1787
1786
if ! domainTrie .IsEmpty () {
1788
- rule = RP . NewDomainSet ( domainTrie .NewDomainSet (), adapterName )
1789
- domainRules = append (domainRules , rule )
1787
+ matcher = domainTrie .NewDomainSet ()
1788
+ matchers = append (matchers , matcher )
1790
1789
}
1791
1790
return
1792
1791
}
1793
1792
1794
- func parseIPRuleSet (domainSetName string , adapterName string , ruleProviders map [string ]providerTypes.RuleProvider ) (C.Rule , error ) {
1793
+ func parseIPRuleSet (domainSetName string , adapterName string , ruleProviders map [string ]providerTypes.RuleProvider ) (C.IpMatcher , error ) {
1795
1794
if rp , ok := ruleProviders [domainSetName ]; ! ok {
1796
1795
return nil , fmt .Errorf ("not found rule-set: %s" , domainSetName )
1797
1796
} else {
@@ -1806,7 +1805,7 @@ func parseIPRuleSet(domainSetName string, adapterName string, ruleProviders map[
1806
1805
return RP .NewRuleSet (domainSetName , adapterName , true )
1807
1806
}
1808
1807
1809
- func parseDomainRuleSet (domainSetName string , adapterName string , ruleProviders map [string ]providerTypes.RuleProvider ) (C.Rule , error ) {
1808
+ func parseDomainRuleSet (domainSetName string , adapterName string , ruleProviders map [string ]providerTypes.RuleProvider ) (C.DomainMatcher , error ) {
1810
1809
if rp , ok := ruleProviders [domainSetName ]; ! ok {
1811
1810
return nil , fmt .Errorf ("not found rule-set: %s" , domainSetName )
1812
1811
} else {
0 commit comments