From ee74256aac70109e7a8348d905e8b86ed0b374d3 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Thu, 1 Nov 2018 08:44:11 +0100 Subject: [PATCH] sort.Interface for CIDR list --- app/router/config.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/app/router/config.go b/app/router/config.go index 049dc65df..9416ec132 100644 --- a/app/router/config.go +++ b/app/router/config.go @@ -6,6 +6,41 @@ import ( "v2ray.com/core/common/net" ) +type CIDRList []*CIDR + +func (l *CIDRList) Len() int { + return len(*l) +} + +func (l *CIDRList) Less(i int, j int) bool { + ci := (*l)[i] + cj := (*l)[j] + + if len(ci.Ip) < len(cj.Ip) { + return true + } + + if len(ci.Ip) > len(cj.Ip) { + return false + } + + for k := 0; k < len(ci.Ip); k++ { + if ci.Ip[k] < cj.Ip[k] { + return true + } + + if ci.Ip[k] > cj.Ip[k] { + return false + } + } + + return ci.Prefix < cj.Prefix +} + +func (l *CIDRList) Swap(i int, j int) { + (*l)[i], (*l)[j] = (*l)[j], (*l)[i] +} + type Rule struct { Tag string Condition Condition