Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improve ip sorting #5

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion cmd/pre-build/pre-build.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/hex"
"flag"
"fmt"
"net"
"os"
"sort"
"strings"
Expand Down Expand Up @@ -122,9 +123,31 @@ func computeRangesHash(sortedRanges []*source.IPRange) string {
return hex.EncodeToString(hash)
}

func compareIPs(a, b net.IP) int {
a = a.To16()
b = b.To16()
for i := 0; i < len(a); i++ {
if a[i] != b[i] {
if a[i] < b[i] {
return -1
}
return 1
}
}
return 0
}

func sortRanges(ranges []*source.IPRange) {
sort.Slice(ranges, func(i, j int) bool {
return ranges[i].String() > ranges[j].String()
// Compare IP addresses
ipCompare := compareIPs(ranges[i].Network.IP, ranges[j].Network.IP)
if ipCompare != 0 {
return ipCompare < 0
}
// If IP addresses are equal, compare prefix lengths (shorter prefixes first)
iMaskSize, _ := ranges[i].Network.Mask.Size()
jMaskSize, _ := ranges[j].Network.Mask.Size()
return iMaskSize < jMaskSize
})
}

Expand Down
2 changes: 1 addition & 1 deletion internal/static/hash.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4026297c7ad2c0946cc15bc0513819f053894ece38c9259e25895ce31e0cdc60
52b8c3cf76f519279f8d1592e351bfbc0a099bc2f8e787a16df3065097f9962a
Binary file modified internal/static/ipv4.gob
Binary file not shown.
Binary file modified internal/static/ipv6.gob
Binary file not shown.
272 changes: 136 additions & 136 deletions ranges/alibaba.txt
Original file line number Diff line number Diff line change
@@ -1,143 +1,143 @@
8.144.0.0/14
8.136.0.0/13
8.132.0.0/14
8.130.0.0/15
8.129.0.0/16
60.205.0.0/16
59.82.96.0/22
59.82.64.0/19
59.82.128.0/21
59.82.112.0/20
59.82.104.0/21
59.82.0.0/18
59.110.0.0/16
47.96.0.0/12
47.92.0.0/14
47.89.66.0/24
8.130.0.0/15
8.132.0.0/14
8.136.0.0/13
8.144.0.0/14
39.96.0.0/13
39.104.0.0/14
39.108.0.0/16
42.96.128.0/17
42.120.0.0/15
42.156.128.0/17
47.89.64.0/24
47.89.66.0/24
47.89.121.0/24
47.246.60.0/22
47.246.58.0/24
47.246.57.0/24
47.246.50.0/24
47.246.48.0/23
47.246.46.0/24
47.246.44.0/23
47.246.42.0/23
47.246.41.0/24
47.246.36.0/22
47.246.28.0/22
47.246.26.0/24
47.246.24.0/24
47.246.22.0/23
47.246.20.0/24
47.246.16.0/22
47.123.0.0/16
47.122.0.0/17
47.120.0.0/15
47.92.0.0/14
47.96.0.0/12
47.112.0.0/13
42.96.128.0/17
42.156.128.0/17
42.120.0.0/15
39.96.0.0/13
39.108.0.0/16
39.104.0.0/14
223.4.0.0/14
218.244.128.0/19
203.119.192.0/18
203.119.176.0/20
203.119.168.0/21
203.119.160.0/24
203.119.128.0/19
203.107.96.0/23
203.107.80.0/21
203.107.72.0/21
203.107.6.0/24
203.107.32.0/19
203.107.28.0/22
203.107.26.0/23
203.107.24.0/24
203.107.20.0/22
203.107.13.0/24
203.107.108.0/23
203.107.104.0/22
203.107.100.0/22
203.107.1.0/24
182.92.0.0/16
163.181.99.0/24
163.181.97.0/24
163.181.94.0/23
163.181.92.0/24
163.181.90.0/24
163.181.89.0/24
163.181.85.0/24
163.181.80.0/22
163.181.78.0/23
163.181.77.0/24
163.181.74.0/24
163.181.72.0/23
163.181.70.0/23
163.181.66.0/23
163.181.61.0/24
163.181.59.0/24
163.181.56.0/23
163.181.52.0/24
163.181.50.0/24
163.181.49.0/24
163.181.44.0/24
163.181.42.0/23
163.181.40.0/24
163.181.32.0/21
163.181.28.0/24
163.181.26.0/24
163.181.25.0/24
163.181.22.0/23
47.120.0.0/15
47.122.0.0/17
47.123.0.0/16
47.246.16.0/22
47.246.20.0/24
47.246.22.0/23
47.246.24.0/24
47.246.26.0/24
47.246.28.0/22
47.246.36.0/22
47.246.41.0/24
47.246.42.0/23
47.246.44.0/23
47.246.46.0/24
47.246.48.0/23
47.246.50.0/24
47.246.57.0/24
47.246.58.0/24
47.246.60.0/22
59.82.0.0/18
59.82.64.0/19
59.82.96.0/22
59.82.104.0/21
59.82.112.0/20
59.82.128.0/21
59.110.0.0/16
60.205.0.0/16
101.37.0.0/16
101.132.0.0/15
101.200.0.0/15
106.14.0.0/15
110.76.32.0/20
112.74.0.0/16
112.124.0.0/14
114.55.0.0/16
114.215.0.0/16
115.28.0.0/15
116.0.81.0/24
116.0.89.0/24
116.62.0.0/16
118.31.0.0/16
118.178.0.0/16
118.190.0.0/16
119.23.0.0/16
119.38.208.0/20
119.42.224.0/21
119.42.232.0/24
119.42.234.0/24
120.24.0.0/14
120.55.0.0/16
120.76.0.0/14
121.0.16.0/20
121.40.0.0/14
121.89.0.0/16
121.196.0.0/14
123.56.0.0/15
139.129.0.0/16
139.196.0.0/16
139.224.0.0/16
163.181.0.0/23
163.181.2.0/24
163.181.16.0/22
163.181.15.0/24
163.181.144.0/22
163.181.136.0/22
163.181.132.0/24
163.181.128.0/24
163.181.126.0/23
163.181.120.0/22
163.181.118.0/24
163.181.116.0/23
163.181.112.0/23
163.181.106.0/23
163.181.105.0/24
163.181.102.0/23
163.181.16.0/22
163.181.22.0/23
163.181.25.0/24
163.181.26.0/24
163.181.28.0/24
163.181.32.0/21
163.181.40.0/24
163.181.42.0/23
163.181.44.0/24
163.181.49.0/24
163.181.50.0/24
163.181.52.0/24
163.181.56.0/23
163.181.59.0/24
163.181.61.0/24
163.181.66.0/23
163.181.70.0/23
163.181.72.0/23
163.181.74.0/24
163.181.77.0/24
163.181.78.0/23
163.181.80.0/22
163.181.85.0/24
163.181.89.0/24
163.181.90.0/24
163.181.92.0/24
163.181.94.0/23
163.181.97.0/24
163.181.99.0/24
163.181.100.0/24
163.181.0.0/23
139.224.0.0/16
139.196.0.0/16
139.129.0.0/16
123.56.0.0/15
121.89.0.0/16
121.40.0.0/14
121.196.0.0/14
121.0.16.0/20
120.76.0.0/14
120.55.0.0/16
120.24.0.0/14
119.42.234.0/24
119.42.232.0/24
119.42.224.0/21
119.38.208.0/20
119.23.0.0/16
118.31.0.0/16
118.190.0.0/16
118.178.0.0/16
116.62.0.0/16
116.0.89.0/24
116.0.81.0/24
115.28.0.0/15
114.55.0.0/16
114.215.0.0/16
112.74.0.0/16
112.124.0.0/14
110.76.32.0/20
106.14.0.0/15
101.37.0.0/16
101.200.0.0/15
101.132.0.0/15
163.181.102.0/23
163.181.105.0/24
163.181.106.0/23
163.181.112.0/23
163.181.116.0/23
163.181.118.0/24
163.181.120.0/22
163.181.126.0/23
163.181.128.0/24
163.181.132.0/24
163.181.136.0/22
163.181.144.0/22
182.92.0.0/16
203.107.1.0/24
203.107.6.0/24
203.107.13.0/24
203.107.20.0/22
203.107.24.0/24
203.107.26.0/23
203.107.28.0/22
203.107.32.0/19
203.107.72.0/21
203.107.80.0/21
203.107.96.0/23
203.107.100.0/22
203.107.104.0/22
203.107.108.0/23
203.119.128.0/19
203.119.160.0/24
203.119.168.0/21
203.119.176.0/20
203.119.192.0/18
218.244.128.0/19
223.4.0.0/14
Loading
Loading