2022-07-07 13:47:21 +00:00
|
|
|
package route
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
|
|
|
C "github.com/sagernet/sing-box/constant"
|
|
|
|
"github.com/sagernet/sing-box/log"
|
|
|
|
"github.com/sagernet/sing-box/option"
|
2022-07-08 15:03:57 +00:00
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
2022-07-07 13:47:21 +00:00
|
|
|
)
|
|
|
|
|
2022-07-24 06:05:06 +00:00
|
|
|
func NewDNSRule(router adapter.Router, logger log.ContextLogger, options option.DNSRule) (adapter.DNSRule, error) {
|
2022-07-07 13:47:21 +00:00
|
|
|
switch options.Type {
|
|
|
|
case "", C.RuleTypeDefault:
|
|
|
|
if !options.DefaultOptions.IsValid() {
|
|
|
|
return nil, E.New("missing conditions")
|
|
|
|
}
|
|
|
|
if options.DefaultOptions.Server == "" {
|
|
|
|
return nil, E.New("missing server field")
|
|
|
|
}
|
|
|
|
return NewDefaultDNSRule(router, logger, options.DefaultOptions)
|
|
|
|
case C.RuleTypeLogical:
|
|
|
|
if !options.LogicalOptions.IsValid() {
|
|
|
|
return nil, E.New("missing conditions")
|
|
|
|
}
|
|
|
|
if options.LogicalOptions.Server == "" {
|
|
|
|
return nil, E.New("missing server field")
|
|
|
|
}
|
|
|
|
return NewLogicalDNSRule(router, logger, options.LogicalOptions)
|
|
|
|
default:
|
|
|
|
return nil, E.New("unknown rule type: ", options.Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-24 06:05:06 +00:00
|
|
|
var _ adapter.DNSRule = (*DefaultDNSRule)(nil)
|
2022-07-07 13:47:21 +00:00
|
|
|
|
|
|
|
type DefaultDNSRule struct {
|
2023-03-21 13:36:17 +00:00
|
|
|
abstractDefaultRule
|
|
|
|
disableCache bool
|
|
|
|
rewriteTTL *uint32
|
2022-07-19 14:16:49 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 07:17:29 +00:00
|
|
|
func NewDefaultDNSRule(router adapter.Router, logger log.ContextLogger, options option.DefaultDNSRule) (*DefaultDNSRule, error) {
|
2022-07-07 13:47:21 +00:00
|
|
|
rule := &DefaultDNSRule{
|
2023-03-21 13:36:17 +00:00
|
|
|
abstractDefaultRule: abstractDefaultRule{
|
|
|
|
invert: options.Invert,
|
|
|
|
outbound: options.Server,
|
|
|
|
},
|
2022-07-24 06:05:06 +00:00
|
|
|
disableCache: options.DisableCache,
|
2023-03-21 13:36:17 +00:00
|
|
|
rewriteTTL: options.RewriteTTL,
|
2022-07-07 13:47:21 +00:00
|
|
|
}
|
|
|
|
if len(options.Inbound) > 0 {
|
|
|
|
item := NewInboundRule(options.Inbound)
|
|
|
|
rule.items = append(rule.items, item)
|
2022-07-10 06:22:28 +00:00
|
|
|
rule.allItems = append(rule.allItems, item)
|
2022-07-07 13:47:21 +00:00
|
|
|
}
|
2022-08-16 15:46:05 +00: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 08:18:40 +00:00
|
|
|
if len(options.QueryType) > 0 {
|
|
|
|
item := NewQueryTypeItem(options.QueryType)
|
|
|
|
rule.items = append(rule.items, item)
|
|
|
|
rule.allItems = append(rule.allItems, item)
|
|
|
|
}
|
2023-03-21 13:36:17 +00: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 13:47:21 +00:00
|
|
|
}
|
2022-07-23 11:01:41 +00:00
|
|
|
if len(options.AuthUser) > 0 {
|
|
|
|
item := NewAuthUserItem(options.AuthUser)
|
2022-07-17 07:11:26 +00:00
|
|
|
rule.items = append(rule.items, item)
|
|
|
|
rule.allItems = append(rule.allItems, item)
|
|
|
|
}
|
2022-07-07 13:47:21 +00:00
|
|
|
if len(options.Protocol) > 0 {
|
2022-07-20 01:41:44 +00:00
|
|
|
item := NewProtocolItem(options.Protocol)
|
2022-07-07 13:47:21 +00:00
|
|
|
rule.items = append(rule.items, item)
|
2022-07-10 06:22:28 +00:00
|
|
|
rule.allItems = append(rule.allItems, item)
|
2022-07-07 13:47:21 +00:00
|
|
|
}
|
|
|
|
if len(options.Domain) > 0 || len(options.DomainSuffix) > 0 {
|
|
|
|
item := NewDomainItem(options.Domain, options.DomainSuffix)
|
2022-09-14 14:03:26 +00:00
|
|
|
rule.destinationAddressItems = append(rule.destinationAddressItems, item)
|
2022-07-10 06:22:28 +00:00
|
|
|
rule.allItems = append(rule.allItems, item)
|
2022-07-07 13:47:21 +00:00
|
|
|
}
|
|
|
|
if len(options.DomainKeyword) > 0 {
|
|
|
|
item := NewDomainKeywordItem(options.DomainKeyword)
|
2022-09-14 14:03:26 +00:00
|
|
|
rule.destinationAddressItems = append(rule.destinationAddressItems, item)
|
2022-07-10 06:22:28 +00:00
|
|
|
rule.allItems = append(rule.allItems, item)
|
2022-07-07 13:47:21 +00:00
|
|
|
}
|
|
|
|
if len(options.DomainRegex) > 0 {
|
|
|
|
item, err := NewDomainRegexItem(options.DomainRegex)
|
|
|
|
if err != nil {
|
|
|
|
return nil, E.Cause(err, "domain_regex")
|
|
|
|
}
|
2022-09-14 14:03:26 +00:00
|
|
|
rule.destinationAddressItems = append(rule.destinationAddressItems, item)
|
2022-07-10 06:22:28 +00:00
|
|
|
rule.allItems = append(rule.allItems, item)
|
2022-07-07 13:47:21 +00:00
|
|
|
}
|
|
|
|
if len(options.Geosite) > 0 {
|
|
|
|
item := NewGeositeItem(router, logger, options.Geosite)
|
2022-09-14 14:03:26 +00:00
|
|
|
rule.destinationAddressItems = append(rule.destinationAddressItems, item)
|
2022-07-10 06:22:28 +00:00
|
|
|
rule.allItems = append(rule.allItems, item)
|
2022-07-07 13:47:21 +00:00
|
|
|
}
|
|
|
|
if len(options.SourceGeoIP) > 0 {
|
|
|
|
item := NewGeoIPItem(router, logger, true, options.SourceGeoIP)
|
2022-09-14 14:03:26 +00:00
|
|
|
rule.sourceAddressItems = append(rule.sourceAddressItems, item)
|
2022-07-10 06:22:28 +00:00
|
|
|
rule.allItems = append(rule.allItems, item)
|
2022-07-07 13:47:21 +00:00
|
|
|
}
|
|
|
|
if len(options.SourceIPCIDR) > 0 {
|
|
|
|
item, err := NewIPCIDRItem(true, options.SourceIPCIDR)
|
|
|
|
if err != nil {
|
|
|
|
return nil, E.Cause(err, "source_ipcidr")
|
|
|
|
}
|
2022-09-14 14:03:26 +00:00
|
|
|
rule.sourceAddressItems = append(rule.sourceAddressItems, item)
|
2022-07-10 06:22:28 +00:00
|
|
|
rule.allItems = append(rule.allItems, item)
|
2022-07-07 13:47:21 +00:00
|
|
|
}
|
|
|
|
if len(options.SourcePort) > 0 {
|
|
|
|
item := NewPortItem(true, options.SourcePort)
|
2022-09-14 14:03:26 +00:00
|
|
|
rule.sourcePortItems = append(rule.sourcePortItems, item)
|
2022-07-10 06:22:28 +00:00
|
|
|
rule.allItems = append(rule.allItems, item)
|
2022-07-07 13:47:21 +00:00
|
|
|
}
|
2022-07-24 08:21:06 +00: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 14:03:26 +00:00
|
|
|
rule.sourcePortItems = append(rule.sourcePortItems, item)
|
2022-07-24 08:21:06 +00:00
|
|
|
rule.allItems = append(rule.allItems, item)
|
|
|
|
}
|
2022-07-07 13:47:21 +00:00
|
|
|
if len(options.Port) > 0 {
|
|
|
|
item := NewPortItem(false, options.Port)
|
2022-09-14 14:03:26 +00:00
|
|
|
rule.destinationPortItems = append(rule.destinationPortItems, item)
|
2022-07-10 06:22:28 +00:00
|
|
|
rule.allItems = append(rule.allItems, item)
|
2022-07-07 13:47:21 +00:00
|
|
|
}
|
2022-07-24 08:21:06 +00: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 14:03:26 +00:00
|
|
|
rule.destinationPortItems = append(rule.destinationPortItems, item)
|
2022-07-24 08:21:06 +00:00
|
|
|
rule.allItems = append(rule.allItems, item)
|
|
|
|
}
|
2022-07-23 11:01:41 +00: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 02:44:40 +00:00
|
|
|
if len(options.ProcessPath) > 0 {
|
|
|
|
item := NewProcessPathItem(options.ProcessPath)
|
|
|
|
rule.items = append(rule.items, item)
|
|
|
|
rule.allItems = append(rule.allItems, item)
|
|
|
|
}
|
2022-07-23 11:01:41 +00: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 13:47:21 +00:00
|
|
|
if len(options.Outbound) > 0 {
|
|
|
|
item := NewOutboundRule(options.Outbound)
|
|
|
|
rule.items = append(rule.items, item)
|
2022-07-10 06:22:28 +00:00
|
|
|
rule.allItems = append(rule.allItems, item)
|
2022-07-07 13:47:21 +00:00
|
|
|
}
|
2022-09-10 06:09:47 +00:00
|
|
|
if options.ClashMode != "" {
|
|
|
|
item := NewClashModeItem(router, options.ClashMode)
|
|
|
|
rule.items = append(rule.items, item)
|
|
|
|
rule.allItems = append(rule.allItems, item)
|
|
|
|
}
|
2022-07-07 13:47:21 +00:00
|
|
|
return rule, nil
|
|
|
|
}
|
|
|
|
|
2022-07-24 06:05:06 +00:00
|
|
|
func (r *DefaultDNSRule) DisableCache() bool {
|
|
|
|
return r.disableCache
|
|
|
|
}
|
|
|
|
|
2023-03-21 13:36:17 +00:00
|
|
|
func (r *DefaultDNSRule) RewriteTTL() *uint32 {
|
|
|
|
return r.rewriteTTL
|
2022-07-07 13:47:21 +00:00
|
|
|
}
|
|
|
|
|
2022-07-24 06:05:06 +00:00
|
|
|
var _ adapter.DNSRule = (*LogicalDNSRule)(nil)
|
2022-07-07 13:47:21 +00:00
|
|
|
|
|
|
|
type LogicalDNSRule struct {
|
2023-03-21 13:36:17 +00:00
|
|
|
abstractLogicalRule
|
2022-07-24 06:05:06 +00:00
|
|
|
disableCache bool
|
2023-03-21 13:36:17 +00:00
|
|
|
rewriteTTL *uint32
|
2022-07-24 06:05:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewLogicalDNSRule(router adapter.Router, logger log.ContextLogger, options option.LogicalDNSRule) (*LogicalDNSRule, error) {
|
|
|
|
r := &LogicalDNSRule{
|
2023-03-21 13:36:17 +00:00
|
|
|
abstractLogicalRule: abstractLogicalRule{
|
|
|
|
rules: make([]adapter.Rule, len(options.Rules)),
|
|
|
|
invert: options.Invert,
|
|
|
|
outbound: options.Server,
|
|
|
|
},
|
2022-07-24 06:05:06 +00:00
|
|
|
disableCache: options.DisableCache,
|
2023-03-21 13:36:17 +00:00
|
|
|
rewriteTTL: options.RewriteTTL,
|
2022-07-24 06:05:06 +00: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 {
|
|
|
|
rule, err := NewDefaultDNSRule(router, logger, subRule)
|
|
|
|
if err != nil {
|
|
|
|
return nil, E.Cause(err, "sub rule[", i, "]")
|
|
|
|
}
|
|
|
|
r.rules[i] = rule
|
|
|
|
}
|
|
|
|
return r, nil
|
2022-07-07 13:47:21 +00:00
|
|
|
}
|
|
|
|
|
2022-07-24 06:05:06 +00:00
|
|
|
func (r *LogicalDNSRule) DisableCache() bool {
|
|
|
|
return r.disableCache
|
|
|
|
}
|
|
|
|
|
2023-03-21 13:36:17 +00:00
|
|
|
func (r *LogicalDNSRule) RewriteTTL() *uint32 {
|
|
|
|
return r.rewriteTTL
|
2022-07-07 13:47:21 +00:00
|
|
|
}
|