mirror of
https://github.com/SagerNet/sing-box.git
synced 2025-04-03 03:47:37 +03:00
Add network_[type/is_expensive/is_constrained]
rule items
This commit is contained in:
parent
c098f282b1
commit
05ea0ca00e
23 changed files with 968 additions and 68 deletions
|
@ -223,6 +223,21 @@ func NewDefaultRule(ctx context.Context, logger log.ContextLogger, options optio
|
|||
rule.items = append(rule.items, item)
|
||||
rule.allItems = append(rule.allItems, item)
|
||||
}
|
||||
if len(options.NetworkType) > 0 {
|
||||
item := NewNetworkTypeItem(networkManager, options.NetworkType)
|
||||
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)
|
||||
}
|
||||
if len(options.WIFISSID) > 0 {
|
||||
item := NewWIFISSIDItem(networkManager, options.WIFISSID)
|
||||
rule.items = append(rule.items, item)
|
||||
|
|
|
@ -220,6 +220,21 @@ func NewDefaultDNSRule(ctx context.Context, logger log.ContextLogger, options op
|
|||
rule.items = append(rule.items, item)
|
||||
rule.allItems = append(rule.allItems, item)
|
||||
}
|
||||
if len(options.NetworkType) > 0 {
|
||||
item := NewNetworkTypeItem(networkManager, options.NetworkType)
|
||||
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)
|
||||
}
|
||||
if len(options.WIFISSID) > 0 {
|
||||
item := NewWIFISSIDItem(networkManager, options.WIFISSID)
|
||||
rule.items = append(rule.items, item)
|
||||
|
|
|
@ -140,18 +140,33 @@ func NewDefaultHeadlessRule(ctx context.Context, options option.DefaultHeadlessR
|
|||
rule.items = append(rule.items, item)
|
||||
rule.allItems = append(rule.allItems, item)
|
||||
}
|
||||
if len(options.WIFISSID) > 0 {
|
||||
if networkManager != nil {
|
||||
item := NewWIFISSIDItem(networkManager, options.WIFISSID)
|
||||
if networkManager != nil {
|
||||
if len(options.NetworkType) > 0 {
|
||||
item := NewNetworkTypeItem(networkManager, options.NetworkType)
|
||||
rule.items = append(rule.items, item)
|
||||
rule.allItems = append(rule.allItems, item)
|
||||
}
|
||||
}
|
||||
if len(options.WIFIBSSID) > 0 {
|
||||
if networkManager != nil {
|
||||
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)
|
||||
}
|
||||
if len(options.WIFISSID) > 0 {
|
||||
item := NewWIFISSIDItem(networkManager, options.WIFISSID)
|
||||
rule.items = append(rule.items, item)
|
||||
rule.allItems = append(rule.allItems, item)
|
||||
|
||||
}
|
||||
if len(options.WIFIBSSID) > 0 {
|
||||
item := NewWIFIBSSIDItem(networkManager, options.WIFIBSSID)
|
||||
rule.items = append(rule.items, item)
|
||||
rule.allItems = append(rule.allItems, item)
|
||||
|
||||
}
|
||||
}
|
||||
if len(options.AdGuardDomain) > 0 {
|
||||
|
|
29
route/rule/rule_item_network_is_constrained.go
Normal file
29
route/rule/rule_item_network_is_constrained.go
Normal file
|
@ -0,0 +1,29 @@
|
|||
package rule
|
||||
|
||||
import (
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
)
|
||||
|
||||
var _ RuleItem = (*NetworkIsConstrainedItem)(nil)
|
||||
|
||||
type NetworkIsConstrainedItem struct {
|
||||
networkManager adapter.NetworkManager
|
||||
}
|
||||
|
||||
func NewNetworkIsConstrainedItem(networkManager adapter.NetworkManager) *NetworkIsConstrainedItem {
|
||||
return &NetworkIsConstrainedItem{
|
||||
networkManager: networkManager,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *NetworkIsConstrainedItem) Match(metadata *adapter.InboundContext) bool {
|
||||
networkInterface := r.networkManager.DefaultNetworkInterface()
|
||||
if networkInterface == nil {
|
||||
return false
|
||||
}
|
||||
return networkInterface.Constrained
|
||||
}
|
||||
|
||||
func (r *NetworkIsConstrainedItem) String() string {
|
||||
return "network_is_expensive=true"
|
||||
}
|
29
route/rule/rule_item_network_is_expensive.go
Normal file
29
route/rule/rule_item_network_is_expensive.go
Normal file
|
@ -0,0 +1,29 @@
|
|||
package rule
|
||||
|
||||
import (
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
)
|
||||
|
||||
var _ RuleItem = (*NetworkIsExpensiveItem)(nil)
|
||||
|
||||
type NetworkIsExpensiveItem struct {
|
||||
networkManager adapter.NetworkManager
|
||||
}
|
||||
|
||||
func NewNetworkIsExpensiveItem(networkManager adapter.NetworkManager) *NetworkIsExpensiveItem {
|
||||
return &NetworkIsExpensiveItem{
|
||||
networkManager: networkManager,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *NetworkIsExpensiveItem) Match(metadata *adapter.InboundContext) bool {
|
||||
networkInterface := r.networkManager.DefaultNetworkInterface()
|
||||
if networkInterface == nil {
|
||||
return false
|
||||
}
|
||||
return networkInterface.Expensive
|
||||
}
|
||||
|
||||
func (r *NetworkIsExpensiveItem) String() string {
|
||||
return "network_is_expensive=true"
|
||||
}
|
39
route/rule/rule_item_network_type.go
Normal file
39
route/rule/rule_item_network_type.go
Normal file
|
@ -0,0 +1,39 @@
|
|||
package rule
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
"github.com/sagernet/sing/common"
|
||||
F "github.com/sagernet/sing/common/format"
|
||||
)
|
||||
|
||||
var _ RuleItem = (*NetworkTypeItem)(nil)
|
||||
|
||||
type NetworkTypeItem struct {
|
||||
networkManager adapter.NetworkManager
|
||||
networkType []string
|
||||
}
|
||||
|
||||
func NewNetworkTypeItem(networkManager adapter.NetworkManager, networkType []string) *NetworkTypeItem {
|
||||
return &NetworkTypeItem{
|
||||
networkManager: networkManager,
|
||||
networkType: networkType,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *NetworkTypeItem) Match(metadata *adapter.InboundContext) bool {
|
||||
networkInterface := r.networkManager.DefaultNetworkInterface()
|
||||
if networkInterface == nil {
|
||||
return false
|
||||
}
|
||||
return common.Contains(r.networkType, networkInterface.Type)
|
||||
}
|
||||
|
||||
func (r *NetworkTypeItem) String() string {
|
||||
if len(r.networkType) == 1 {
|
||||
return F.ToString("network_type=", r.networkType[0])
|
||||
} else {
|
||||
return F.ToString("network_type=", "["+strings.Join(F.MapToString(r.networkType), " ")+"]")
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue