-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
network_[type/is_expensive/is_constrained]
rule items
- Loading branch information
1 parent
a8fe263
commit 4638d3c
Showing
12 changed files
with
215 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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), " ")+"]") | ||
} | ||
} |