sing-box/route/rule/rule_item_ipversion.go

31 lines
627 B
Go
Raw Normal View History

2024-10-21 23:38:34 +08:00
package rule
2022-07-02 22:55:10 +08:00
import (
"github.com/sagernet/sing-box/adapter"
)
var _ RuleItem = (*IPVersionItem)(nil)
type IPVersionItem struct {
isIPv6 bool
}
func NewIPVersionItem(isIPv6 bool) *IPVersionItem {
return &IPVersionItem{isIPv6}
}
func (r *IPVersionItem) Match(metadata *adapter.InboundContext) bool {
2022-08-16 23:46:05 +08:00
return metadata.IPVersion != 0 && metadata.IPVersion == 6 == r.isIPv6 ||
metadata.Destination.IsIP() && metadata.Destination.IsIPv6() == r.isIPv6
2022-07-02 22:55:10 +08:00
}
func (r *IPVersionItem) String() string {
var versionStr string
if r.isIPv6 {
versionStr = "6"
} else {
versionStr = "4"
}
return "ip_version=" + versionStr
}