sing-box/route/rule.go

328 lines
8.2 KiB
Go
Raw Normal View History

2022-07-02 14:07:50 +08:00
package route
import (
2022-07-02 22:55:10 +08:00
"strings"
2022-07-02 14:07:50 +08:00
"github.com/sagernet/sing-box/adapter"
2022-07-02 22:55:10 +08:00
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
2022-07-02 14:07:50 +08:00
"github.com/sagernet/sing-box/option"
2022-07-08 23:03:57 +08:00
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions"
F "github.com/sagernet/sing/common/format"
2022-07-02 14:07:50 +08:00
)
2022-07-12 15:17:29 +08:00
func NewRule(router adapter.Router, logger log.ContextLogger, options option.Rule) (adapter.Rule, error) {
2022-07-03 01:57:04 +08:00
if common.IsEmptyByEquals(options) {
return nil, E.New("empty rule config")
}
2022-07-02 22:55:10 +08:00
switch options.Type {
case "", C.RuleTypeDefault:
2022-07-03 01:57:04 +08:00
if !options.DefaultOptions.IsValid() {
return nil, E.New("missing conditions")
}
if options.DefaultOptions.Outbound == "" {
return nil, E.New("missing outbound field")
}
return NewDefaultRule(router, logger, options.DefaultOptions)
2022-07-02 22:55:10 +08:00
case C.RuleTypeLogical:
2022-07-03 01:57:04 +08:00
if !options.LogicalOptions.IsValid() {
return nil, E.New("missing conditions")
}
if options.LogicalOptions.Outbound == "" {
return nil, E.New("missing outbound field")
}
return NewLogicalRule(router, logger, options.LogicalOptions)
2022-07-02 22:55:10 +08:00
default:
return nil, E.New("unknown rule type: ", options.Type)
}
}
2022-07-02 14:07:50 +08:00
var _ adapter.Rule = (*DefaultRule)(nil)
type DefaultRule struct {
2022-07-04 19:34:45 +08:00
items []RuleItem
sourceAddressItems []RuleItem
destinationAddressItems []RuleItem
2022-07-05 13:23:47 +08:00
allItems []RuleItem
2022-07-04 19:34:45 +08:00
outbound string
2022-07-02 14:07:50 +08:00
}
2022-07-19 22:16:49 +08:00
func (r *DefaultRule) Type() string {
return C.RuleTypeDefault
}
2022-07-02 14:07:50 +08:00
type RuleItem interface {
2022-07-02 22:55:10 +08:00
Match(metadata *adapter.InboundContext) bool
2022-07-02 14:07:50 +08:00
String() string
}
2022-07-12 15:17:29 +08:00
func NewDefaultRule(router adapter.Router, logger log.ContextLogger, options option.DefaultRule) (*DefaultRule, error) {
2022-07-02 14:07:50 +08:00
rule := &DefaultRule{
outbound: options.Outbound,
}
if len(options.Inbound) > 0 {
2022-07-05 13:23:47 +08:00
item := NewInboundRule(options.Inbound)
rule.items = append(rule.items, item)
rule.allItems = append(rule.allItems, item)
2022-07-02 14:07:50 +08:00
}
2022-07-02 22:55:10 +08:00
if options.IPVersion > 0 {
switch options.IPVersion {
case 4, 6:
2022-07-05 13:23:47 +08:00
item := NewIPVersionItem(options.IPVersion == 6)
rule.items = append(rule.items, item)
rule.allItems = append(rule.allItems, item)
2022-07-02 22:55:10 +08:00
default:
return nil, E.New("invalid ip version: ", options.IPVersion)
}
}
if options.Network != "" {
switch options.Network {
case C.NetworkTCP, C.NetworkUDP:
2022-07-05 13:23:47 +08:00
item := NewNetworkItem(options.Network)
rule.items = append(rule.items, item)
rule.allItems = append(rule.allItems, item)
2022-07-02 22:55:10 +08:00
default:
return nil, E.New("invalid network: ", options.Network)
}
}
2022-07-17 15:11:26 +08:00
if len(options.User) > 0 {
item := NewUserItem(options.User)
rule.items = append(rule.items, item)
rule.allItems = append(rule.allItems, item)
}
2022-07-02 22:55:10 +08:00
if len(options.Protocol) > 0 {
2022-07-20 09:41:44 +08:00
item := NewProtocolItem(options.Protocol)
2022-07-05 13:23:47 +08:00
rule.items = append(rule.items, item)
rule.allItems = append(rule.allItems, item)
2022-07-02 22:55:10 +08:00
}
if len(options.Domain) > 0 || len(options.DomainSuffix) > 0 {
2022-07-05 13:23:47 +08:00
item := NewDomainItem(options.Domain, options.DomainSuffix)
rule.destinationAddressItems = append(rule.destinationAddressItems, item)
rule.allItems = append(rule.allItems, item)
2022-07-02 22:55:10 +08:00
}
if len(options.DomainKeyword) > 0 {
2022-07-05 13:23:47 +08:00
item := NewDomainKeywordItem(options.DomainKeyword)
rule.destinationAddressItems = append(rule.destinationAddressItems, item)
rule.allItems = append(rule.allItems, item)
2022-07-02 22:55:10 +08:00
}
2022-07-04 15:51:41 +08:00
if len(options.DomainRegex) > 0 {
item, err := NewDomainRegexItem(options.DomainRegex)
if err != nil {
return nil, E.Cause(err, "domain_regex")
}
2022-07-04 19:34:45 +08:00
rule.destinationAddressItems = append(rule.destinationAddressItems, item)
2022-07-05 13:23:47 +08:00
rule.allItems = append(rule.allItems, item)
2022-07-04 15:51:41 +08:00
}
2022-07-05 09:05:35 +08:00
if len(options.Geosite) > 0 {
2022-07-05 13:23:47 +08:00
item := NewGeositeItem(router, logger, options.Geosite)
rule.destinationAddressItems = append(rule.destinationAddressItems, item)
rule.allItems = append(rule.allItems, item)
2022-07-05 09:05:35 +08:00
}
2022-07-02 22:55:10 +08:00
if len(options.SourceGeoIP) > 0 {
2022-07-05 13:23:47 +08:00
item := NewGeoIPItem(router, logger, true, options.SourceGeoIP)
rule.sourceAddressItems = append(rule.sourceAddressItems, item)
rule.allItems = append(rule.allItems, item)
2022-07-02 22:55:10 +08:00
}
if len(options.GeoIP) > 0 {
2022-07-05 13:23:47 +08:00
item := NewGeoIPItem(router, logger, false, options.GeoIP)
rule.destinationAddressItems = append(rule.destinationAddressItems, item)
rule.allItems = append(rule.allItems, item)
2022-07-02 22:55:10 +08:00
}
if len(options.SourceIPCIDR) > 0 {
item, err := NewIPCIDRItem(true, options.SourceIPCIDR)
if err != nil {
2022-07-04 15:51:41 +08:00
return nil, E.Cause(err, "source_ipcidr")
2022-07-02 22:55:10 +08:00
}
2022-07-04 19:34:45 +08:00
rule.sourceAddressItems = append(rule.sourceAddressItems, item)
2022-07-05 13:23:47 +08:00
rule.allItems = append(rule.allItems, item)
2022-07-02 22:55:10 +08:00
}
if len(options.IPCIDR) > 0 {
item, err := NewIPCIDRItem(false, options.IPCIDR)
if err != nil {
2022-07-04 15:51:41 +08:00
return nil, E.Cause(err, "ipcidr")
2022-07-02 22:55:10 +08:00
}
2022-07-04 19:34:45 +08:00
rule.destinationAddressItems = append(rule.destinationAddressItems, item)
2022-07-05 13:23:47 +08:00
rule.allItems = append(rule.allItems, item)
2022-07-02 22:55:10 +08:00
}
if len(options.SourcePort) > 0 {
2022-07-05 13:23:47 +08:00
item := NewPortItem(true, options.SourcePort)
rule.items = append(rule.items, item)
rule.allItems = append(rule.allItems, item)
2022-07-02 22:55:10 +08:00
}
if len(options.Port) > 0 {
2022-07-05 13:23:47 +08:00
item := NewPortItem(false, options.Port)
rule.items = append(rule.items, item)
rule.allItems = append(rule.allItems, item)
2022-07-02 22:55:10 +08:00
}
return rule, nil
2022-07-02 14:07:50 +08:00
}
2022-07-05 09:05:35 +08:00
func (r *DefaultRule) Start() error {
2022-07-05 13:23:47 +08:00
for _, item := range r.allItems {
2022-07-05 09:05:35 +08:00
err := common.Start(item)
if err != nil {
return err
}
}
return nil
}
func (r *DefaultRule) Close() error {
2022-07-05 13:23:47 +08:00
for _, item := range r.allItems {
2022-07-05 09:05:35 +08:00
err := common.Close(item)
if err != nil {
return err
}
}
2022-07-05 13:23:47 +08:00
return nil
}
func (r *DefaultRule) UpdateGeosite() error {
for _, item := range r.allItems {
if geositeItem, isSite := item.(*GeositeItem); isSite {
err := geositeItem.Update()
if err != nil {
return err
}
2022-07-05 09:05:35 +08:00
}
}
return nil
}
2022-07-02 22:55:10 +08:00
func (r *DefaultRule) Match(metadata *adapter.InboundContext) bool {
2022-07-02 14:07:50 +08:00
for _, item := range r.items {
2022-07-04 19:34:45 +08:00
if !item.Match(metadata) {
return false
2022-07-02 14:07:50 +08:00
}
}
2022-07-04 19:34:45 +08:00
if len(r.sourceAddressItems) > 0 {
var sourceAddressMatch bool
for _, item := range r.sourceAddressItems {
if item.Match(metadata) {
sourceAddressMatch = true
break
}
}
if !sourceAddressMatch {
return false
}
}
if len(r.destinationAddressItems) > 0 {
var destinationAddressMatch bool
for _, item := range r.destinationAddressItems {
if item.Match(metadata) {
destinationAddressMatch = true
break
}
}
if !destinationAddressMatch {
return false
}
}
return true
2022-07-02 14:07:50 +08:00
}
func (r *DefaultRule) Outbound() string {
return r.outbound
}
func (r *DefaultRule) String() string {
return strings.Join(F.MapToString(r.allItems), " ")
2022-07-02 14:07:50 +08:00
}
2022-07-07 21:47:21 +08:00
var _ adapter.Rule = (*LogicalRule)(nil)
type LogicalRule struct {
mode string
rules []*DefaultRule
outbound string
}
2022-07-19 22:16:49 +08:00
func (r *LogicalRule) Type() string {
return C.RuleTypeLogical
}
2022-07-07 21:47:21 +08:00
func (r *LogicalRule) UpdateGeosite() error {
for _, rule := range r.rules {
err := rule.UpdateGeosite()
if err != nil {
return err
}
}
return nil
}
func (r *LogicalRule) Start() error {
for _, rule := range r.rules {
err := rule.Start()
if err != nil {
return err
}
}
return nil
}
func (r *LogicalRule) Close() error {
for _, rule := range r.rules {
err := rule.Close()
if err != nil {
return err
}
}
return nil
}
2022-07-12 15:17:29 +08:00
func NewLogicalRule(router adapter.Router, logger log.ContextLogger, options option.LogicalRule) (*LogicalRule, error) {
2022-07-07 21:47:21 +08:00
r := &LogicalRule{
rules: make([]*DefaultRule, len(options.Rules)),
outbound: options.Outbound,
}
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 := NewDefaultRule(router, logger, subRule)
if err != nil {
return nil, E.Cause(err, "sub rule[", i, "]")
}
r.rules[i] = rule
}
return r, nil
}
func (r *LogicalRule) Match(metadata *adapter.InboundContext) bool {
if r.mode == C.LogicalTypeAnd {
return common.All(r.rules, func(it *DefaultRule) bool {
return it.Match(metadata)
})
} else {
return common.Any(r.rules, func(it *DefaultRule) bool {
return it.Match(metadata)
})
}
}
func (r *LogicalRule) Outbound() string {
return r.outbound
}
func (r *LogicalRule) String() string {
var op string
switch r.mode {
case C.LogicalTypeAnd:
op = "&&"
case C.LogicalTypeOr:
op = "||"
}
return "logical(" + strings.Join(F.MapToString(r.rules), " "+op+" ") + ")"
2022-07-07 21:47:21 +08:00
}