Add invert rule item

This commit is contained in:
世界
2022-07-24 13:44:26 +08:00
parent fbdf3e4b38
commit d0f1d7f3dc
4 changed files with 14 additions and 7 deletions

View File

@@ -107,6 +107,7 @@ type DefaultDNSRule struct {
User Listable[string] `json:"user,omitempty"` User Listable[string] `json:"user,omitempty"`
UserID Listable[int32] `json:"user_id,omitempty"` UserID Listable[int32] `json:"user_id,omitempty"`
Outbound Listable[string] `json:"outbound,omitempty"` Outbound Listable[string] `json:"outbound,omitempty"`
Invert bool `json:"invert,omitempty"`
Server string `json:"server,omitempty"` Server string `json:"server,omitempty"`
} }

View File

@@ -107,6 +107,7 @@ type DefaultRule struct {
PackageName Listable[string] `json:"package_name,omitempty"` PackageName Listable[string] `json:"package_name,omitempty"`
User Listable[string] `json:"user,omitempty"` User Listable[string] `json:"user,omitempty"`
UserID Listable[int32] `json:"user_id,omitempty"` UserID Listable[int32] `json:"user_id,omitempty"`
Invert bool `json:"invert,omitempty"`
Outbound string `json:"outbound,omitempty"` Outbound string `json:"outbound,omitempty"`
} }
@@ -137,6 +138,7 @@ func (r DefaultRule) Equals(other DefaultRule) bool {
common.ComparableSliceEquals(r.PackageName, other.PackageName) && common.ComparableSliceEquals(r.PackageName, other.PackageName) &&
common.ComparableSliceEquals(r.User, other.User) && common.ComparableSliceEquals(r.User, other.User) &&
common.ComparableSliceEquals(r.UserID, other.UserID) && common.ComparableSliceEquals(r.UserID, other.UserID) &&
r.Invert == other.Invert &&
r.Outbound == other.Outbound r.Outbound == other.Outbound
} }

View File

@@ -45,6 +45,7 @@ type DefaultRule struct {
sourceAddressItems []RuleItem sourceAddressItems []RuleItem
destinationAddressItems []RuleItem destinationAddressItems []RuleItem
allItems []RuleItem allItems []RuleItem
invert bool
outbound string outbound string
} }
@@ -59,6 +60,7 @@ type RuleItem interface {
func NewDefaultRule(router adapter.Router, logger log.ContextLogger, options option.DefaultRule) (*DefaultRule, error) { func NewDefaultRule(router adapter.Router, logger log.ContextLogger, options option.DefaultRule) (*DefaultRule, error) {
rule := &DefaultRule{ rule := &DefaultRule{
invert: options.Invert,
outbound: options.Outbound, outbound: options.Outbound,
} }
if len(options.Inbound) > 0 { if len(options.Inbound) > 0 {
@@ -213,7 +215,7 @@ func (r *DefaultRule) UpdateGeosite() error {
func (r *DefaultRule) Match(metadata *adapter.InboundContext) bool { func (r *DefaultRule) Match(metadata *adapter.InboundContext) bool {
for _, item := range r.items { for _, item := range r.items {
if !item.Match(metadata) { if !item.Match(metadata) {
return false return r.invert
} }
} }
@@ -226,7 +228,7 @@ func (r *DefaultRule) Match(metadata *adapter.InboundContext) bool {
} }
} }
if !sourceAddressMatch { if !sourceAddressMatch {
return false return r.invert
} }
} }
@@ -239,11 +241,11 @@ func (r *DefaultRule) Match(metadata *adapter.InboundContext) bool {
} }
} }
if !destinationAddressMatch { if !destinationAddressMatch {
return false return r.invert
} }
} }
return true return !r.invert
} }
func (r *DefaultRule) Outbound() string { func (r *DefaultRule) Outbound() string {

View File

@@ -44,6 +44,7 @@ type DefaultDNSRule struct {
items []RuleItem items []RuleItem
addressItems []RuleItem addressItems []RuleItem
allItems []RuleItem allItems []RuleItem
invert bool
outbound string outbound string
} }
@@ -53,6 +54,7 @@ func (r *DefaultDNSRule) Type() string {
func NewDefaultDNSRule(router adapter.Router, logger log.ContextLogger, options option.DefaultDNSRule) (*DefaultDNSRule, error) { func NewDefaultDNSRule(router adapter.Router, logger log.ContextLogger, options option.DefaultDNSRule) (*DefaultDNSRule, error) {
rule := &DefaultDNSRule{ rule := &DefaultDNSRule{
invert: true,
outbound: options.Server, outbound: options.Server,
} }
if len(options.Inbound) > 0 { if len(options.Inbound) > 0 {
@@ -189,7 +191,7 @@ func (r *DefaultDNSRule) UpdateGeosite() error {
func (r *DefaultDNSRule) Match(metadata *adapter.InboundContext) bool { func (r *DefaultDNSRule) Match(metadata *adapter.InboundContext) bool {
for _, item := range r.items { for _, item := range r.items {
if !item.Match(metadata) { if !item.Match(metadata) {
return false return r.invert
} }
} }
if len(r.addressItems) > 0 { if len(r.addressItems) > 0 {
@@ -201,10 +203,10 @@ func (r *DefaultDNSRule) Match(metadata *adapter.InboundContext) bool {
} }
} }
if !addressMatch { if !addressMatch {
return false return r.invert
} }
} }
return true return !r.invert
} }
func (r *DefaultDNSRule) Outbound() string { func (r *DefaultDNSRule) Outbound() string {