2024-10-21 15:38:34 +00:00
|
|
|
package rule
|
2022-07-02 06:07:50 +00:00
|
|
|
|
|
|
|
import (
|
2024-10-18 01:58:03 +00:00
|
|
|
"context"
|
|
|
|
|
2022-07-02 06:07:50 +00:00
|
|
|
"github.com/sagernet/sing-box/adapter"
|
2022-07-02 14:55:10 +00:00
|
|
|
C "github.com/sagernet/sing-box/constant"
|
2024-10-18 01:58:03 +00:00
|
|
|
"github.com/sagernet/sing-box/experimental/deprecated"
|
2022-07-02 14:55:10 +00:00
|
|
|
"github.com/sagernet/sing-box/log"
|
2022-07-02 06:07:50 +00:00
|
|
|
"github.com/sagernet/sing-box/option"
|
2024-11-13 11:05:28 +00:00
|
|
|
"github.com/sagernet/sing/common"
|
2022-07-08 15:03:57 +00:00
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
2024-11-10 04:11:21 +00:00
|
|
|
"github.com/sagernet/sing/service"
|
2022-07-02 06:07:50 +00:00
|
|
|
)
|
|
|
|
|
2024-11-10 04:11:21 +00:00
|
|
|
func NewRule(ctx context.Context, logger log.ContextLogger, options option.Rule, checkOutbound bool) (adapter.Rule, error) {
|
2022-07-02 14:55:10 +00:00
|
|
|
switch options.Type {
|
|
|
|
case "", C.RuleTypeDefault:
|
2022-07-02 17:57:04 +00:00
|
|
|
if !options.DefaultOptions.IsValid() {
|
|
|
|
return nil, E.New("missing conditions")
|
|
|
|
}
|
2024-10-21 15:38:34 +00:00
|
|
|
switch options.DefaultOptions.Action {
|
|
|
|
case "", C.RuleActionTypeRoute:
|
|
|
|
if options.DefaultOptions.RouteOptions.Outbound == "" && checkOutbound {
|
|
|
|
return nil, E.New("missing outbound field")
|
|
|
|
}
|
2022-07-02 17:57:04 +00:00
|
|
|
}
|
2024-11-10 04:11:21 +00:00
|
|
|
return NewDefaultRule(ctx, logger, options.DefaultOptions)
|
2022-07-02 14:55:10 +00:00
|
|
|
case C.RuleTypeLogical:
|
2022-07-02 17:57:04 +00:00
|
|
|
if !options.LogicalOptions.IsValid() {
|
|
|
|
return nil, E.New("missing conditions")
|
|
|
|
}
|
2024-10-21 15:38:34 +00:00
|
|
|
switch options.LogicalOptions.Action {
|
|
|
|
case "", C.RuleActionTypeRoute:
|
|
|
|
if options.LogicalOptions.RouteOptions.Outbound == "" && checkOutbound {
|
|
|
|
return nil, E.New("missing outbound field")
|
|
|
|
}
|
2022-07-02 17:57:04 +00:00
|
|
|
}
|
2024-11-10 04:11:21 +00:00
|
|
|
return NewLogicalRule(ctx, logger, options.LogicalOptions)
|
2022-07-02 14:55:10 +00:00
|
|
|
default:
|
|
|
|
return nil, E.New("unknown rule type: ", options.Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-02 06:07:50 +00:00
|
|
|
var _ adapter.Rule = (*DefaultRule)(nil)
|
|
|
|
|
|
|
|
type DefaultRule struct {
|
2023-06-07 12:28:21 +00:00
|
|
|
abstractDefaultRule
|
2022-07-02 06:07:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type RuleItem interface {
|
2022-07-02 14:55:10 +00:00
|
|
|
Match(metadata *adapter.InboundContext) bool
|
2022-07-02 06:07:50 +00:00
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
2024-11-10 04:11:21 +00:00
|
|
|
func NewDefaultRule(ctx context.Context, logger log.ContextLogger, options option.DefaultRule) (*DefaultRule, error) {
|
2024-11-09 13:16:11 +00:00
|
|
|
action, err := NewRuleAction(ctx, logger, options.RuleAction)
|
2024-10-21 15:38:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, E.Cause(err, "action")
|
|
|
|
}
|
2022-07-02 06:07:50 +00:00
|
|
|
rule := &DefaultRule{
|
2023-06-07 12:28:21 +00:00
|
|
|
abstractDefaultRule{
|
2024-10-21 15:38:34 +00:00
|
|
|
invert: options.Invert,
|
|
|
|
action: action,
|
2023-06-07 12:28:21 +00:00
|
|
|
},
|
2022-07-02 06:07:50 +00:00
|
|
|
}
|
2024-11-10 04:11:21 +00:00
|
|
|
router := service.FromContext[adapter.Router](ctx)
|
|
|
|
networkManager := service.FromContext[adapter.NetworkManager](ctx)
|
2022-07-02 06:07:50 +00:00
|
|
|
if len(options.Inbound) > 0 {
|
2022-07-05 05:23:47 +00:00
|
|
|
item := NewInboundRule(options.Inbound)
|
|
|
|
rule.items = append(rule.items, item)
|
|
|
|
rule.allItems = append(rule.allItems, item)
|
2022-07-02 06:07:50 +00:00
|
|
|
}
|
2022-07-02 14:55:10 +00:00
|
|
|
if options.IPVersion > 0 {
|
|
|
|
switch options.IPVersion {
|
|
|
|
case 4, 6:
|
2022-07-05 05:23:47 +00:00
|
|
|
item := NewIPVersionItem(options.IPVersion == 6)
|
|
|
|
rule.items = append(rule.items, item)
|
|
|
|
rule.allItems = append(rule.allItems, item)
|
2022-07-02 14:55:10 +00:00
|
|
|
default:
|
|
|
|
return nil, E.New("invalid ip version: ", options.IPVersion)
|
|
|
|
}
|
|
|
|
}
|
2023-06-07 12:28:21 +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-02 14:55:10 +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-02 14:55:10 +00:00
|
|
|
if len(options.Protocol) > 0 {
|
2022-07-20 01:41:44 +00:00
|
|
|
item := NewProtocolItem(options.Protocol)
|
2022-07-05 05:23:47 +00:00
|
|
|
rule.items = append(rule.items, item)
|
|
|
|
rule.allItems = append(rule.allItems, item)
|
2022-07-02 14:55:10 +00:00
|
|
|
}
|
2024-07-07 07:45:50 +00:00
|
|
|
if len(options.Client) > 0 {
|
|
|
|
item := NewClientItem(options.Client)
|
|
|
|
rule.items = append(rule.items, item)
|
|
|
|
rule.allItems = append(rule.allItems, item)
|
|
|
|
}
|
2022-07-02 14:55:10 +00:00
|
|
|
if len(options.Domain) > 0 || len(options.DomainSuffix) > 0 {
|
2022-07-05 05:23:47 +00:00
|
|
|
item := NewDomainItem(options.Domain, options.DomainSuffix)
|
|
|
|
rule.destinationAddressItems = append(rule.destinationAddressItems, item)
|
|
|
|
rule.allItems = append(rule.allItems, item)
|
2022-07-02 14:55:10 +00:00
|
|
|
}
|
|
|
|
if len(options.DomainKeyword) > 0 {
|
2022-07-05 05:23:47 +00:00
|
|
|
item := NewDomainKeywordItem(options.DomainKeyword)
|
|
|
|
rule.destinationAddressItems = append(rule.destinationAddressItems, item)
|
|
|
|
rule.allItems = append(rule.allItems, item)
|
2022-07-02 14:55:10 +00:00
|
|
|
}
|
2022-07-04 07:51:41 +00:00
|
|
|
if len(options.DomainRegex) > 0 {
|
|
|
|
item, err := NewDomainRegexItem(options.DomainRegex)
|
|
|
|
if err != nil {
|
|
|
|
return nil, E.Cause(err, "domain_regex")
|
|
|
|
}
|
2022-07-04 11:34:45 +00:00
|
|
|
rule.destinationAddressItems = append(rule.destinationAddressItems, item)
|
2022-07-05 05:23:47 +00:00
|
|
|
rule.allItems = append(rule.allItems, item)
|
2022-07-04 07:51:41 +00:00
|
|
|
}
|
2022-07-05 01:05:35 +00:00
|
|
|
if len(options.Geosite) > 0 {
|
2022-07-05 05:23:47 +00:00
|
|
|
item := NewGeositeItem(router, logger, options.Geosite)
|
|
|
|
rule.destinationAddressItems = append(rule.destinationAddressItems, item)
|
|
|
|
rule.allItems = append(rule.allItems, item)
|
2022-07-05 01:05:35 +00:00
|
|
|
}
|
2022-07-02 14:55:10 +00:00
|
|
|
if len(options.SourceGeoIP) > 0 {
|
2022-07-05 05:23:47 +00:00
|
|
|
item := NewGeoIPItem(router, logger, true, options.SourceGeoIP)
|
|
|
|
rule.sourceAddressItems = append(rule.sourceAddressItems, item)
|
|
|
|
rule.allItems = append(rule.allItems, item)
|
2022-07-02 14:55:10 +00:00
|
|
|
}
|
|
|
|
if len(options.GeoIP) > 0 {
|
2022-07-05 05:23:47 +00:00
|
|
|
item := NewGeoIPItem(router, logger, false, options.GeoIP)
|
2024-02-03 09:45:27 +00:00
|
|
|
rule.destinationIPCIDRItems = append(rule.destinationIPCIDRItems, item)
|
2022-07-05 05:23:47 +00:00
|
|
|
rule.allItems = append(rule.allItems, item)
|
2022-07-02 14:55:10 +00:00
|
|
|
}
|
|
|
|
if len(options.SourceIPCIDR) > 0 {
|
|
|
|
item, err := NewIPCIDRItem(true, options.SourceIPCIDR)
|
|
|
|
if err != nil {
|
2024-01-14 05:01:57 +00:00
|
|
|
return nil, E.Cause(err, "source_ip_cidr")
|
2022-07-02 14:55:10 +00:00
|
|
|
}
|
2022-07-04 11:34:45 +00:00
|
|
|
rule.sourceAddressItems = append(rule.sourceAddressItems, item)
|
2022-07-05 05:23:47 +00:00
|
|
|
rule.allItems = append(rule.allItems, item)
|
2022-07-02 14:55:10 +00:00
|
|
|
}
|
2023-12-01 05:24:39 +00:00
|
|
|
if options.SourceIPIsPrivate {
|
|
|
|
item := NewIPIsPrivateItem(true)
|
|
|
|
rule.sourceAddressItems = append(rule.sourceAddressItems, item)
|
|
|
|
rule.allItems = append(rule.allItems, item)
|
|
|
|
}
|
2022-07-02 14:55:10 +00:00
|
|
|
if len(options.IPCIDR) > 0 {
|
|
|
|
item, err := NewIPCIDRItem(false, options.IPCIDR)
|
|
|
|
if err != nil {
|
2022-07-04 07:51:41 +00:00
|
|
|
return nil, E.Cause(err, "ipcidr")
|
2022-07-02 14:55:10 +00:00
|
|
|
}
|
2024-02-03 09:45:27 +00:00
|
|
|
rule.destinationIPCIDRItems = append(rule.destinationIPCIDRItems, item)
|
2022-07-05 05:23:47 +00:00
|
|
|
rule.allItems = append(rule.allItems, item)
|
2022-07-02 14:55:10 +00:00
|
|
|
}
|
2023-12-01 05:24:39 +00:00
|
|
|
if options.IPIsPrivate {
|
|
|
|
item := NewIPIsPrivateItem(false)
|
2024-02-03 09:45:27 +00:00
|
|
|
rule.destinationIPCIDRItems = append(rule.destinationIPCIDRItems, item)
|
2023-12-01 05:24:39 +00:00
|
|
|
rule.allItems = append(rule.allItems, item)
|
|
|
|
}
|
2022-07-02 14:55:10 +00:00
|
|
|
if len(options.SourcePort) > 0 {
|
2022-07-05 05:23:47 +00:00
|
|
|
item := NewPortItem(true, options.SourcePort)
|
2022-09-14 14:03:26 +00:00
|
|
|
rule.sourcePortItems = append(rule.sourcePortItems, item)
|
2022-07-05 05:23:47 +00:00
|
|
|
rule.allItems = append(rule.allItems, item)
|
2022-07-02 14:55:10 +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-02 14:55:10 +00:00
|
|
|
if len(options.Port) > 0 {
|
2022-07-05 05:23:47 +00:00
|
|
|
item := NewPortItem(false, options.Port)
|
2022-09-14 14:03:26 +00:00
|
|
|
rule.destinationPortItems = append(rule.destinationPortItems, item)
|
2022-07-05 05:23:47 +00:00
|
|
|
rule.allItems = append(rule.allItems, item)
|
2022-07-02 14:55:10 +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)
|
2024-09-15 03:42:57 +00: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 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-09-10 06:09:47 +00:00
|
|
|
if options.ClashMode != "" {
|
2024-11-13 09:06:34 +00:00
|
|
|
item := NewClashModeItem(ctx, options.ClashMode)
|
2022-09-10 06:09:47 +00:00
|
|
|
rule.items = append(rule.items, item)
|
|
|
|
rule.allItems = append(rule.allItems, item)
|
|
|
|
}
|
2024-11-11 08:30:25 +00:00
|
|
|
if len(options.NetworkType) > 0 {
|
2024-11-13 11:05:28 +00:00
|
|
|
item := NewNetworkTypeItem(networkManager, common.Map(options.NetworkType, option.InterfaceType.Build))
|
2024-11-11 08:30:25 +00:00
|
|
|
rule.items = append(rule.items, item)
|
|
|
|
rule.allItems = append(rule.allItems, item)
|
|
|
|
}
|
|
|
|
if options.NetworkIsExpensive {
|
|
|
|
item := NewNetworkIsExpensiveItem(networkManager)
|
|
|
|
rule.items = append(rule.items, item)
|
|
|
|
rule.allItems = append(rule.allItems, item)
|
|
|
|
}
|
|
|
|
if options.NetworkIsConstrained {
|
|
|
|
item := NewNetworkIsConstrainedItem(networkManager)
|
|
|
|
rule.items = append(rule.items, item)
|
|
|
|
rule.allItems = append(rule.allItems, item)
|
|
|
|
}
|
2023-11-24 12:58:07 +00:00
|
|
|
if len(options.WIFISSID) > 0 {
|
2024-11-10 04:11:21 +00:00
|
|
|
item := NewWIFISSIDItem(networkManager, options.WIFISSID)
|
2023-11-24 12:58:07 +00:00
|
|
|
rule.items = append(rule.items, item)
|
|
|
|
rule.allItems = append(rule.allItems, item)
|
|
|
|
}
|
|
|
|
if len(options.WIFIBSSID) > 0 {
|
2024-11-10 04:11:21 +00:00
|
|
|
item := NewWIFIBSSIDItem(networkManager, options.WIFIBSSID)
|
2023-11-24 12:58:07 +00:00
|
|
|
rule.items = append(rule.items, item)
|
|
|
|
rule.allItems = append(rule.allItems, item)
|
|
|
|
}
|
2023-12-01 05:24:12 +00:00
|
|
|
if len(options.RuleSet) > 0 {
|
2024-10-18 01:58:03 +00: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, false)
|
2023-12-01 05:24:12 +00:00
|
|
|
rule.items = append(rule.items, item)
|
|
|
|
rule.allItems = append(rule.allItems, item)
|
|
|
|
}
|
2022-07-02 14:55:10 +00:00
|
|
|
return rule, nil
|
2022-07-02 06:07:50 +00:00
|
|
|
}
|
|
|
|
|
2022-07-07 13:47:21 +00:00
|
|
|
var _ adapter.Rule = (*LogicalRule)(nil)
|
|
|
|
|
|
|
|
type LogicalRule struct {
|
2023-06-07 12:28:21 +00:00
|
|
|
abstractLogicalRule
|
2022-07-07 13:47:21 +00:00
|
|
|
}
|
|
|
|
|
2024-11-10 04:11:21 +00:00
|
|
|
func NewLogicalRule(ctx context.Context, logger log.ContextLogger, options option.LogicalRule) (*LogicalRule, error) {
|
2024-11-09 13:16:11 +00:00
|
|
|
action, err := NewRuleAction(ctx, logger, options.RuleAction)
|
2024-10-21 15:38:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, E.Cause(err, "action")
|
|
|
|
}
|
|
|
|
rule := &LogicalRule{
|
2023-06-07 12:28:21 +00:00
|
|
|
abstractLogicalRule{
|
2024-10-21 15:38:34 +00:00
|
|
|
rules: make([]adapter.HeadlessRule, len(options.Rules)),
|
|
|
|
invert: options.Invert,
|
|
|
|
action: action,
|
2023-06-07 12:28:21 +00:00
|
|
|
},
|
2022-07-24 06:05:06 +00:00
|
|
|
}
|
|
|
|
switch options.Mode {
|
|
|
|
case C.LogicalTypeAnd:
|
2024-10-21 15:38:34 +00:00
|
|
|
rule.mode = C.LogicalTypeAnd
|
2022-07-24 06:05:06 +00:00
|
|
|
case C.LogicalTypeOr:
|
2024-10-21 15:38:34 +00:00
|
|
|
rule.mode = C.LogicalTypeOr
|
2022-07-24 06:05:06 +00:00
|
|
|
default:
|
|
|
|
return nil, E.New("unknown logical mode: ", options.Mode)
|
|
|
|
}
|
2024-10-21 15:38:34 +00:00
|
|
|
for i, subOptions := range options.Rules {
|
2024-11-10 04:11:21 +00:00
|
|
|
subRule, err := NewRule(ctx, logger, subOptions, false)
|
2022-07-24 06:05:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, E.Cause(err, "sub rule[", i, "]")
|
|
|
|
}
|
2024-10-21 15:38:34 +00:00
|
|
|
rule.rules[i] = subRule
|
2022-07-24 06:05:06 +00:00
|
|
|
}
|
2024-10-21 15:38:34 +00:00
|
|
|
return rule, nil
|
2022-07-24 06:05:06 +00:00
|
|
|
}
|