mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-06-25 20:03:12 +03:00
Refactor rules
This commit is contained in:
42
route/rule_item_network.go
Normal file
42
route/rule_item_network.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package route
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
F "github.com/sagernet/sing/common/format"
|
||||
)
|
||||
|
||||
var _ RuleItem = (*NetworkItem)(nil)
|
||||
|
||||
type NetworkItem struct {
|
||||
networks []string
|
||||
networkMap map[string]bool
|
||||
}
|
||||
|
||||
func NewNetworkItem(networks []string) *NetworkItem {
|
||||
networkMap := make(map[string]bool)
|
||||
for _, network := range networks {
|
||||
networkMap[network] = true
|
||||
}
|
||||
return &NetworkItem{
|
||||
networks: networks,
|
||||
networkMap: networkMap,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *NetworkItem) Match(metadata *adapter.InboundContext) bool {
|
||||
return r.networkMap[metadata.Network]
|
||||
}
|
||||
|
||||
func (r *NetworkItem) String() string {
|
||||
description := "network="
|
||||
|
||||
pLen := len(r.networks)
|
||||
if pLen == 1 {
|
||||
description += F.ToString(r.networks[0])
|
||||
} else {
|
||||
description += "[" + strings.Join(F.MapToString(r.networks), " ") + "]"
|
||||
}
|
||||
return description
|
||||
}
|
||||
Reference in New Issue
Block a user