rule_cidr.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package route
  2. import (
  3. "net/netip"
  4. "strings"
  5. "github.com/sagernet/sing-box/adapter"
  6. "github.com/sagernet/sing/common"
  7. F "github.com/sagernet/sing/common/format"
  8. )
  9. var _ RuleItem = (*IPCIDRItem)(nil)
  10. type IPCIDRItem struct {
  11. prefixes []netip.Prefix
  12. isSource bool
  13. }
  14. func NewIPCIDRItem(isSource bool, prefixStrings []string) (*IPCIDRItem, error) {
  15. prefixes := make([]netip.Prefix, 0, len(prefixStrings))
  16. for _, prefixString := range prefixStrings {
  17. prefix, err := netip.ParsePrefix(prefixString)
  18. if err != nil {
  19. return nil, err
  20. }
  21. prefixes = append(prefixes, prefix)
  22. }
  23. return &IPCIDRItem{
  24. prefixes: prefixes,
  25. isSource: isSource,
  26. }, nil
  27. }
  28. func (r *IPCIDRItem) Match(metadata *adapter.InboundContext) bool {
  29. if r.isSource {
  30. for _, prefix := range r.prefixes {
  31. if prefix.Contains(metadata.Source.Addr) {
  32. return true
  33. }
  34. }
  35. } else {
  36. if metadata.Destination.IsFqdn() {
  37. return false
  38. }
  39. for _, prefix := range r.prefixes {
  40. if prefix.Contains(metadata.Destination.Addr) {
  41. return true
  42. }
  43. }
  44. }
  45. return false
  46. }
  47. func (r *IPCIDRItem) String() string {
  48. var description string
  49. if r.isSource {
  50. description = "source_ipcidr="
  51. } else {
  52. description = "ipcidr="
  53. }
  54. pLen := len(r.prefixes)
  55. if pLen == 1 {
  56. description += r.prefixes[0].String()
  57. } else {
  58. description += "[" + strings.Join(common.Map(r.prefixes, F.ToString0[netip.Prefix]), " ") + "]"
  59. }
  60. return description
  61. }