2024-10-21 23:38:34 +08:00
|
|
|
package rule
|
2022-07-07 21:47:21 +08:00
|
|
|
|
|
|
|
import (
|
2024-10-18 09:58:03 +08:00
|
|
|
"context"
|
2024-02-09 18:37:25 +08:00
|
|
|
|
2022-07-07 21:47:21 +08:00
|
|
|
"github.com/sagernet/sing-box/adapter"
|
|
|
|
C "github.com/sagernet/sing-box/constant"
|
2024-10-18 09:58:03 +08:00
|
|
|
"github.com/sagernet/sing-box/experimental/deprecated"
|
2022-07-07 21:47:21 +08:00
|
|
|
"github.com/sagernet/sing-box/log"
|
|
|
|
"github.com/sagernet/sing-box/option"
|
2024-02-03 17:45:27 +08:00
|
|
|
"github.com/sagernet/sing/common"
|
2022-07-08 23:03:57 +08:00
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
2022-07-07 21:47:21 +08:00
|
|
|
)
|
|
|
|
|
2024-10-18 09:58:03 +08:00
|
|
|
func NewDNSRule(ctx context.Context, router adapter.Router, logger log.ContextLogger, options option.DNSRule, checkServer bool) (adapter.DNSRule, error) {
|
2022-07-07 21:47:21 +08:00
|
|
|
switch options.Type {
|
|
|
|
case "", C.RuleTypeDefault:
|
|
|
|
if !options.DefaultOptions.IsValid() {
|
|
|
|
return nil, E.New("missing conditions")
|
|
|
|
}
|
2024-10-21 23:38:34 +08:00
|
|
|
switch options.DefaultOptions.Action {
|
|
|
|
case "", C.RuleActionTypeRoute:
|
|
|
|
if options.DefaultOptions.RouteOptions.Server == "" && checkServer {
|
|
|
|
return nil, E.New("missing server field")
|
|
|
|
}
|
2022-07-07 21:47:21 +08:00
|
|
|
}
|
2024-10-18 09:58:03 +08:00
|
|
|
return NewDefaultDNSRule(ctx, router, logger, options.DefaultOptions)
|
2022-07-07 21:47:21 +08:00
|
|
|
case C.RuleTypeLogical:
|
|
|
|
if !options.LogicalOptions.IsValid() {
|
|
|
|
return nil, E.New("missing conditions")
|
|
|
|
}
|
2024-10-21 23:38:34 +08:00
|
|
|
switch options.LogicalOptions.Action {
|
|
|
|
case "", C.RuleActionTypeRoute:
|
|
|
|
if options.LogicalOptions.RouteOptions.Server == "" && checkServer {
|
|
|
|
return nil, E.New("missing server field")
|
|
|
|
}
|
2022-07-07 21:47:21 +08:00
|
|
|
}
|
2024-10-18 09:58:03 +08:00
|
|
|
return NewLogicalDNSRule(ctx, router, logger, options.LogicalOptions)
|
2022-07-07 21:47:21 +08:00
|
|
|
default:
|
|
|
|
return nil, E.New("unknown rule type: ", options.Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-24 14:05:06 +08:00
|
|
|
var _ adapter.DNSRule = (*DefaultDNSRule)(nil)
|
2022-07-07 21:47:21 +08:00
|
|
|
|
|
|
|
type DefaultDNSRule struct {
|
2023-06-07 20:28:21 +08:00
|
|
|
abstractDefaultRule
|
2022-07-19 22:16:49 +08:00
|
|
|
}
|
|
|
|
|
2024-10-18 09:58:03 +08:00
|
|
|
func NewDefaultDNSRule(ctx context.Context, router adapter.Router, logger log.ContextLogger, options option.DefaultDNSRule) (*DefaultDNSRule, error) {
|
2022-07-07 21:47:21 +08:00
|
|
|
rule := &DefaultDNSRule{
|
2023-06-07 20:28:21 +08:00
|
|
|
abstractDefaultRule: abstractDefaultRule{
|
2024-10-21 23:38:34 +08:00
|
|
|
invert: options.Invert,
|
|
|
|
action: NewDNSRuleAction(options.DNSRuleAction),
|
2023-06-07 20:28:21 +08:00
|
|
|
},
|
2022-07-07 21:47:21 +08:00
|
|
|
}
|
|
|
|
if len(options.Inbound) > 0 {
|
|
|
|
item := NewInboundRule(options.Inbound)
|
|
|
|
rule.items = append(rule.items, item)
|
2022-07-10 14:22:28 +08:00
|
|
|
rule.allItems = append(rule.allItems, item)
|
2022-07-07 21:47:21 +08:00
|
|
|
}
|
2022-08-16 23:46:05 +08:00
|
|
|
if options.IPVersion > 0 {
|
|
|
|
switch options.IPVersion {
|
|
|
|
case 4, 6:
|
|
|
|
item := NewIPVersionItem(options.IPVersion == 6)
|
|
|
|
rule.items = append(rule.items, item)
|
|
|
|
rule.allItems = append(rule.allItems, item)
|
|
|
|
default:
|
|
|
|
return nil, E.New("invalid ip version: ", options.IPVersion)
|
|
|
|
}
|
|
|
|
}
|
2023-02-08 16:18:40 +08:00
|
|
|
if len(options.QueryType) > 0 {
|
|
|
|
item := NewQueryTypeItem(options.QueryType)
|
|
|
|
rule.items = append(rule.items, item)
|
|
|
|
rule.allItems = append(rule.allItems, item)
|
|
|
|
}
|
2023-06-07 20:28:21 +08:00
|
|
|
if len(options.Network) > 0 {
|
|
|
|
item := NewNetworkItem(options.Network)
|
|
|
|
rule.items = append(rule.items, item)
|
|
|
|
rule.allItems = append(rule.allItems, item)
|
2022-07-07 21:47:21 +08:00
|
|
|
}
|
2022-07-23 19:01:41 +08:00
|
|
|
if len(options.AuthUser) > 0 {
|
|
|
|
item := NewAuthUserItem(options.AuthUser)
|
2022-07-17 15:11:26 +08:00
|
|
|
rule.items = append(rule.items, item)
|
|
|
|
rule.allItems = append(rule.allItems, item)
|
|
|
|
}
|
2022-07-07 21:47:21 +08:00
|
|
|
if len(options.Protocol) > 0 {
|
2022-07-20 09:41:44 +08:00
|
|
|
item := NewProtocolItem(options.Protocol)
|
2022-07-07 21:47:21 +08:00
|
|
|
rule.items = append(rule.items, item)
|
2022-07-10 14:22:28 +08:00
|
|
|
rule.allItems = append(rule.allItems, item)
|
2022-07-07 21:47:21 +08:00
|
|
|
}
|
|
|
|
if len(options.Domain) > 0 || len(options.DomainSuffix) > 0 {
|
|
|
|
item := NewDomainItem(options.Domain, options.DomainSuffix)
|
2022-09-14 22:03:26 +08:00
|
|
|
rule.destinationAddressItems = append(rule.destinationAddressItems, item)
|
2022-07-10 14:22:28 +08:00
|
|
|
rule.allItems = append(rule.allItems, item)
|
2022-07-07 21:47:21 +08:00
|
|
|
}
|
|
|
|
if len(options.DomainKeyword) > 0 {
|
|
|
|
item := NewDomainKeywordItem(options.DomainKeyword)
|
2022-09-14 22:03:26 +08:00
|
|
|
rule.destinationAddressItems = append(rule.destinationAddressItems, item)
|
2022-07-10 14:22:28 +08:00
|
|
|
rule.allItems = append(rule.allItems, item)
|
2022-07-07 21:47:21 +08:00
|
|
|
}
|
|
|
|
if len(options.DomainRegex) > 0 {
|
|
|
|
item, err := NewDomainRegexItem(options.DomainRegex)
|
|
|
|
if err != nil {
|
|
|
|
return nil, E.Cause(err, "domain_regex")
|
|
|
|
}
|
2022-09-14 22:03:26 +08:00
|
|
|
rule.destinationAddressItems = append(rule.destinationAddressItems, item)
|
2022-07-10 14:22:28 +08:00
|
|
|
rule.allItems = append(rule.allItems, item)
|
2022-07-07 21:47:21 +08:00
|
|
|
}
|
|
|
|
if len(options.Geosite) > 0 {
|
|
|
|
item := NewGeositeItem(router, logger, options.Geosite)
|
2022-09-14 22:03:26 +08:00
|
|
|
rule.destinationAddressItems = append(rule.destinationAddressItems, item)
|
2022-07-10 14:22:28 +08:00
|
|
|
rule.allItems = append(rule.allItems, item)
|
2022-07-07 21:47:21 +08:00
|
|
|
}
|
|
|
|
if len(options.SourceGeoIP) > 0 {
|
|
|
|
item := NewGeoIPItem(router, logger, true, options.SourceGeoIP)
|
2022-09-14 22:03:26 +08:00
|
|
|
rule.sourceAddressItems = append(rule.sourceAddressItems, item)
|
2022-07-10 14:22:28 +08:00
|
|
|
rule.allItems = append(rule.allItems, item)
|
2022-07-07 21:47:21 +08:00
|
|
|
}
|
2024-02-03 17:45:27 +08:00
|
|
|
if len(options.GeoIP) > 0 {
|
|
|
|
item := NewGeoIPItem(router, logger, false, options.GeoIP)
|
|
|
|
rule.destinationIPCIDRItems = append(rule.destinationIPCIDRItems, item)
|
|
|
|
rule.allItems = append(rule.allItems, item)
|
|
|
|
}
|
2022-07-07 21:47:21 +08:00
|
|
|
if len(options.SourceIPCIDR) > 0 {
|
|
|
|
item, err := NewIPCIDRItem(true, options.SourceIPCIDR)
|
|
|
|
if err != nil {
|
2024-01-14 13:01:57 +08:00
|
|
|
return nil, E.Cause(err, "source_ip_cidr")
|
2022-07-07 21:47:21 +08:00
|
|
|
}
|
2022-09-14 22:03:26 +08:00
|
|
|
rule.sourceAddressItems = append(rule.sourceAddressItems, item)
|
2022-07-10 14:22:28 +08:00
|
|
|
rule.allItems = append(rule.allItems, item)
|
2022-07-07 21:47:21 +08:00
|
|
|
}
|
2024-02-03 17:45:27 +08:00
|
|
|
if len(options.IPCIDR) > 0 {
|
|
|
|
item, err := NewIPCIDRItem(false, options.IPCIDR)
|
|
|
|
if err != nil {
|
|
|
|
return nil, E.Cause(err, "ip_cidr")
|
|
|
|
}
|
|
|
|
rule.destinationIPCIDRItems = append(rule.destinationIPCIDRItems, item)
|
|
|
|
rule.allItems = append(rule.allItems, item)
|
|
|
|
}
|
2023-12-01 13:24:39 +08:00
|
|
|
if options.SourceIPIsPrivate {
|
|
|
|
item := NewIPIsPrivateItem(true)
|
|
|
|
rule.sourceAddressItems = append(rule.sourceAddressItems, item)
|
|
|
|
rule.allItems = append(rule.allItems, item)
|
|
|
|
}
|
2024-02-03 17:45:27 +08:00
|
|
|
if options.IPIsPrivate {
|
|
|
|
item := NewIPIsPrivateItem(false)
|
|
|
|
rule.destinationIPCIDRItems = append(rule.destinationIPCIDRItems, item)
|
|
|
|
rule.allItems = append(rule.allItems, item)
|
|
|
|
}
|
2022-07-07 21:47:21 +08:00
|
|
|
if len(options.SourcePort) > 0 {
|
|
|
|
item := NewPortItem(true, options.SourcePort)
|
2022-09-14 22:03:26 +08:00
|
|
|
rule.sourcePortItems = append(rule.sourcePortItems, item)
|
2022-07-10 14:22:28 +08:00
|
|
|
rule.allItems = append(rule.allItems, item)
|
2022-07-07 21:47:21 +08:00
|
|
|
}
|
2022-07-24 16:21:06 +08:00
|
|
|
if len(options.SourcePortRange) > 0 {
|
|
|
|
item, err := NewPortRangeItem(true, options.SourcePortRange)
|
|
|
|
if err != nil {
|
|
|
|
return nil, E.Cause(err, "source_port_range")
|
|
|
|
}
|
2022-09-14 22:03:26 +08:00
|
|
|
rule.sourcePortItems = append(rule.sourcePortItems, item)
|
2022-07-24 16:21:06 +08:00
|
|
|
rule.allItems = append(rule.allItems, item)
|
|
|
|
}
|
2022-07-07 21:47:21 +08:00
|
|
|
if len(options.Port) > 0 {
|
|
|
|
item := NewPortItem(false, options.Port)
|
2022-09-14 22:03:26 +08:00
|
|
|
rule.destinationPortItems = append(rule.destinationPortItems, item)
|
2022-07-10 14:22:28 +08:00
|
|
|
rule.allItems = append(rule.allItems, item)
|
2022-07-07 21:47:21 +08:00
|
|
|
}
|
2022-07-24 16:21:06 +08:00
|
|
|
if len(options.PortRange) > 0 {
|
|
|
|
item, err := NewPortRangeItem(false, options.PortRange)
|
|
|
|
if err != nil {
|
|
|
|
return nil, E.Cause(err, "port_range")
|
|
|
|
}
|
2022-09-14 22:03:26 +08:00
|
|
|
rule.destinationPortItems = append(rule.destinationPortItems, item)
|
2022-07-24 16:21:06 +08:00
|
|
|
rule.allItems = append(rule.allItems, item)
|
|
|
|
}
|
2022-07-23 19:01:41 +08:00
|
|
|
if len(options.ProcessName) > 0 {
|
|
|
|
item := NewProcessItem(options.ProcessName)
|
|
|
|
rule.items = append(rule.items, item)
|
|
|
|
rule.allItems = append(rule.allItems, item)
|
|
|
|
}
|
2022-08-30 10:44:40 +08:00
|
|
|
if len(options.ProcessPath) > 0 {
|
|
|
|
item := NewProcessPathItem(options.ProcessPath)
|
|
|
|
rule.items = append(rule.items, item)
|
|
|
|
rule.allItems = append(rule.allItems, item)
|
|
|
|
}
|
2024-09-15 11:42:57 +08:00
|
|
|
if len(options.ProcessPathRegex) > 0 {
|
|
|
|
item, err := NewProcessPathRegexItem(options.ProcessPathRegex)
|
|
|
|
if err != nil {
|
|
|
|
return nil, E.Cause(err, "process_path_regex")
|
|
|
|
}
|
|
|
|
rule.items = append(rule.items, item)
|
|
|
|
rule.allItems = append(rule.allItems, item)
|
|
|
|
}
|
2022-07-23 19:01:41 +08:00
|
|
|
if len(options.PackageName) > 0 {
|
|
|
|
item := NewPackageNameItem(options.PackageName)
|
|
|
|
rule.items = append(rule.items, item)
|
|
|
|
rule.allItems = append(rule.allItems, item)
|
|
|
|
}
|
|
|
|
if len(options.User) > 0 {
|
|
|
|
item := NewUserItem(options.User)
|
|
|
|
rule.items = append(rule.items, item)
|
|
|
|
rule.allItems = append(rule.allItems, item)
|
|
|
|
}
|
|
|
|
if len(options.UserID) > 0 {
|
|
|
|
item := NewUserIDItem(options.UserID)
|
|
|
|
rule.items = append(rule.items, item)
|
|
|
|
rule.allItems = append(rule.allItems, item)
|
|
|
|
}
|
2022-07-07 21:47:21 +08:00
|
|
|
if len(options.Outbound) > 0 {
|
|
|
|
item := NewOutboundRule(options.Outbound)
|
|
|
|
rule.items = append(rule.items, item)
|
2022-07-10 14:22:28 +08:00
|
|
|
rule.allItems = append(rule.allItems, item)
|
2022-07-07 21:47:21 +08:00
|
|
|
}
|
2022-09-10 14:09:47 +08:00
|
|
|
if options.ClashMode != "" {
|
|
|
|
item := NewClashModeItem(router, options.ClashMode)
|
|
|
|
rule.items = append(rule.items, item)
|
|
|
|
rule.allItems = append(rule.allItems, item)
|
|
|
|
}
|
2023-11-24 20:58:07 +08:00
|
|
|
if len(options.WIFISSID) > 0 {
|
|
|
|
item := NewWIFISSIDItem(router, options.WIFISSID)
|
|
|
|
rule.items = append(rule.items, item)
|
|
|
|
rule.allItems = append(rule.allItems, item)
|
|
|
|
}
|
|
|
|
if len(options.WIFIBSSID) > 0 {
|
|
|
|
item := NewWIFIBSSIDItem(router, options.WIFIBSSID)
|
|
|
|
rule.items = append(rule.items, item)
|
|
|
|
rule.allItems = append(rule.allItems, item)
|
|
|
|
}
|
2023-12-01 13:24:12 +08:00
|
|
|
if len(options.RuleSet) > 0 {
|
2024-10-18 09:58:03 +08:00
|
|
|
var matchSource bool
|
|
|
|
if options.RuleSetIPCIDRMatchSource {
|
|
|
|
matchSource = true
|
|
|
|
} else
|
|
|
|
//nolint:staticcheck
|
|
|
|
if options.Deprecated_RulesetIPCIDRMatchSource {
|
|
|
|
matchSource = true
|
|
|
|
deprecated.Report(ctx, deprecated.OptionBadMatchSource)
|
|
|
|
}
|
|
|
|
item := NewRuleSetItem(router, options.RuleSet, matchSource, options.RuleSetIPCIDRAcceptEmpty)
|
2023-12-01 13:24:12 +08:00
|
|
|
rule.items = append(rule.items, item)
|
|
|
|
rule.allItems = append(rule.allItems, item)
|
|
|
|
}
|
2022-07-07 21:47:21 +08:00
|
|
|
return rule, nil
|
|
|
|
}
|
|
|
|
|
2024-10-21 23:38:34 +08:00
|
|
|
func (r *DefaultDNSRule) Action() adapter.RuleAction {
|
|
|
|
return r.action
|
2024-02-09 18:37:25 +08:00
|
|
|
}
|
|
|
|
|
2024-02-03 17:45:27 +08:00
|
|
|
func (r *DefaultDNSRule) WithAddressLimit() bool {
|
|
|
|
if len(r.destinationIPCIDRItems) > 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for _, rawRule := range r.items {
|
|
|
|
ruleSet, isRuleSet := rawRule.(*RuleSetItem)
|
|
|
|
if !isRuleSet {
|
|
|
|
continue
|
|
|
|
}
|
2024-02-21 14:27:55 +08:00
|
|
|
if ruleSet.ContainsDestinationIPCIDRRule() {
|
2024-02-03 17:45:27 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *DefaultDNSRule) Match(metadata *adapter.InboundContext) bool {
|
|
|
|
metadata.IgnoreDestinationIPCIDRMatch = true
|
|
|
|
defer func() {
|
|
|
|
metadata.IgnoreDestinationIPCIDRMatch = false
|
|
|
|
}()
|
|
|
|
return r.abstractDefaultRule.Match(metadata)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *DefaultDNSRule) MatchAddressLimit(metadata *adapter.InboundContext) bool {
|
|
|
|
return r.abstractDefaultRule.Match(metadata)
|
|
|
|
}
|
|
|
|
|
2022-07-24 14:05:06 +08:00
|
|
|
var _ adapter.DNSRule = (*LogicalDNSRule)(nil)
|
2022-07-07 21:47:21 +08:00
|
|
|
|
|
|
|
type LogicalDNSRule struct {
|
2023-06-07 20:28:21 +08:00
|
|
|
abstractLogicalRule
|
2022-07-24 14:05:06 +08:00
|
|
|
}
|
|
|
|
|
2024-10-18 09:58:03 +08:00
|
|
|
func NewLogicalDNSRule(ctx context.Context, router adapter.Router, logger log.ContextLogger, options option.LogicalDNSRule) (*LogicalDNSRule, error) {
|
2022-07-24 14:05:06 +08:00
|
|
|
r := &LogicalDNSRule{
|
2023-06-07 20:28:21 +08:00
|
|
|
abstractLogicalRule: abstractLogicalRule{
|
2024-10-21 23:38:34 +08:00
|
|
|
rules: make([]adapter.HeadlessRule, len(options.Rules)),
|
|
|
|
invert: options.Invert,
|
|
|
|
action: NewDNSRuleAction(options.DNSRuleAction),
|
2023-06-07 20:28:21 +08:00
|
|
|
},
|
2022-07-24 14:05:06 +08:00
|
|
|
}
|
|
|
|
switch options.Mode {
|
|
|
|
case C.LogicalTypeAnd:
|
|
|
|
r.mode = C.LogicalTypeAnd
|
|
|
|
case C.LogicalTypeOr:
|
|
|
|
r.mode = C.LogicalTypeOr
|
|
|
|
default:
|
|
|
|
return nil, E.New("unknown logical mode: ", options.Mode)
|
|
|
|
}
|
|
|
|
for i, subRule := range options.Rules {
|
2024-10-18 09:58:03 +08:00
|
|
|
rule, err := NewDNSRule(ctx, router, logger, subRule, false)
|
2022-07-24 14:05:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, E.Cause(err, "sub rule[", i, "]")
|
|
|
|
}
|
|
|
|
r.rules[i] = rule
|
|
|
|
}
|
|
|
|
return r, nil
|
2022-07-07 21:47:21 +08:00
|
|
|
}
|
|
|
|
|
2024-10-21 23:38:34 +08:00
|
|
|
func (r *LogicalDNSRule) Action() adapter.RuleAction {
|
|
|
|
return r.action
|
2024-02-09 18:37:25 +08:00
|
|
|
}
|
|
|
|
|
2024-02-03 17:45:27 +08:00
|
|
|
func (r *LogicalDNSRule) WithAddressLimit() bool {
|
|
|
|
for _, rawRule := range r.rules {
|
|
|
|
switch rule := rawRule.(type) {
|
|
|
|
case *DefaultDNSRule:
|
|
|
|
if rule.WithAddressLimit() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
case *LogicalDNSRule:
|
|
|
|
if rule.WithAddressLimit() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *LogicalDNSRule) Match(metadata *adapter.InboundContext) bool {
|
|
|
|
if r.mode == C.LogicalTypeAnd {
|
|
|
|
return common.All(r.rules, func(it adapter.HeadlessRule) bool {
|
|
|
|
metadata.ResetRuleCache()
|
|
|
|
return it.(adapter.DNSRule).Match(metadata)
|
|
|
|
}) != r.invert
|
|
|
|
} else {
|
|
|
|
return common.Any(r.rules, func(it adapter.HeadlessRule) bool {
|
|
|
|
metadata.ResetRuleCache()
|
|
|
|
return it.(adapter.DNSRule).Match(metadata)
|
|
|
|
}) != r.invert
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *LogicalDNSRule) MatchAddressLimit(metadata *adapter.InboundContext) bool {
|
|
|
|
if r.mode == C.LogicalTypeAnd {
|
|
|
|
return common.All(r.rules, func(it adapter.HeadlessRule) bool {
|
|
|
|
metadata.ResetRuleCache()
|
|
|
|
return it.(adapter.DNSRule).MatchAddressLimit(metadata)
|
|
|
|
}) != r.invert
|
|
|
|
} else {
|
|
|
|
return common.Any(r.rules, func(it adapter.HeadlessRule) bool {
|
|
|
|
metadata.ResetRuleCache()
|
|
|
|
return it.(adapter.DNSRule).MatchAddressLimit(metadata)
|
|
|
|
}) != r.invert
|
|
|
|
}
|
|
|
|
}
|