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