1
0

rule_item_ip_is_private.go 910 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package route
  2. import (
  3. "net/netip"
  4. "github.com/sagernet/sing-box/adapter"
  5. N "github.com/sagernet/sing/common/network"
  6. )
  7. var _ RuleItem = (*IPIsPrivateItem)(nil)
  8. type IPIsPrivateItem struct {
  9. isSource bool
  10. }
  11. func NewIPIsPrivateItem(isSource bool) *IPIsPrivateItem {
  12. return &IPIsPrivateItem{isSource}
  13. }
  14. func (r *IPIsPrivateItem) Match(metadata *adapter.InboundContext) bool {
  15. var destination netip.Addr
  16. if r.isSource {
  17. destination = metadata.Source.Addr
  18. } else {
  19. destination = metadata.Destination.Addr
  20. }
  21. if destination.IsValid() {
  22. return !N.IsPublicAddr(destination)
  23. }
  24. if !r.isSource {
  25. for _, destinationAddress := range metadata.DestinationAddresses {
  26. if !N.IsPublicAddr(destinationAddress) {
  27. return true
  28. }
  29. }
  30. }
  31. return false
  32. }
  33. func (r *IPIsPrivateItem) String() string {
  34. if r.isSource {
  35. return "source_ip_is_private=true"
  36. } else {
  37. return "ip_is_private=true"
  38. }
  39. }