rule_item_network.go 847 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package rule
  2. import (
  3. "strings"
  4. "github.com/sagernet/sing-box/adapter"
  5. F "github.com/sagernet/sing/common/format"
  6. )
  7. var _ RuleItem = (*NetworkItem)(nil)
  8. type NetworkItem struct {
  9. networks []string
  10. networkMap map[string]bool
  11. }
  12. func NewNetworkItem(networks []string) *NetworkItem {
  13. networkMap := make(map[string]bool)
  14. for _, network := range networks {
  15. networkMap[network] = true
  16. }
  17. return &NetworkItem{
  18. networks: networks,
  19. networkMap: networkMap,
  20. }
  21. }
  22. func (r *NetworkItem) Match(metadata *adapter.InboundContext) bool {
  23. return r.networkMap[metadata.Network]
  24. }
  25. func (r *NetworkItem) String() string {
  26. description := "network="
  27. pLen := len(r.networks)
  28. if pLen == 1 {
  29. description += F.ToString(r.networks[0])
  30. } else {
  31. description += "[" + strings.Join(F.MapToString(r.networks), " ") + "]"
  32. }
  33. return description
  34. }