github.com/inazumav/sing-box@v0.0.0-20230926072359-ab51429a14f1/route/rule_item_ipversion.go (about)

     1  package route
     2  
     3  import (
     4  	"github.com/inazumav/sing-box/adapter"
     5  )
     6  
     7  var _ RuleItem = (*IPVersionItem)(nil)
     8  
     9  type IPVersionItem struct {
    10  	isIPv6 bool
    11  }
    12  
    13  func NewIPVersionItem(isIPv6 bool) *IPVersionItem {
    14  	return &IPVersionItem{isIPv6}
    15  }
    16  
    17  func (r *IPVersionItem) Match(metadata *adapter.InboundContext) bool {
    18  	return metadata.IPVersion != 0 && metadata.IPVersion == 6 == r.isIPv6 ||
    19  		metadata.Destination.IsIP() && metadata.Destination.IsIPv6() == r.isIPv6
    20  }
    21  
    22  func (r *IPVersionItem) String() string {
    23  	var versionStr string
    24  	if r.isIPv6 {
    25  		versionStr = "6"
    26  	} else {
    27  		versionStr = "4"
    28  	}
    29  	return "ip_version=" + versionStr
    30  }