replace cidr-tools with fast-cidr-tools for improved CIDR merging functionality

This commit is contained in:
metowolf
2025-02-13 11:55:57 +08:00
parent c5e049bc6a
commit 02af91c814
5 changed files with 15 additions and 113 deletions

View File

@@ -79,7 +79,7 @@
- 感谢 [IPIP.net](https://www.ipip.net/) 设计的 ipdb 数据库格式 - 感谢 [IPIP.net](https://www.ipip.net/) 设计的 ipdb 数据库格式
- 感谢 ipdb 项目提供 ipdb 数据库格式的读取工具 - 感谢 ipdb 项目提供 ipdb 数据库格式的读取工具
- 感谢 @ipdb/range 项目提供 ipdb 数据库格式的分段工具 - 感谢 @ipdb/range 项目提供 ipdb 数据库格式的分段工具
- 感谢 [cidr-tools](https://github.com/silverwind/cidr-tools) 项目提供可靠的 CIDR 合并工具 - 感谢 [fast-cidr-tools](https://github.com/SukkaW/fast-cidr-tools) 项目提供高效、可靠的 CIDR 合并工具
- 感谢专注于 IP 数据库收集整理工作的公司和热心网友们 - 感谢专注于 IP 数据库收集整理工作的公司和热心网友们

View File

@@ -12,7 +12,7 @@
"private": true, "private": true,
"dependencies": { "dependencies": {
"@ipdb/range": "^0.1.1", "@ipdb/range": "^0.1.1",
"cidr-tools": "^3.0.5", "fast-cidr-tools": "^0.3.1",
"glob": "^7.2.3", "glob": "^7.2.3",
"gulp": "^4.0.2", "gulp": "^4.0.2",
"ipdb": "^0.3.3", "ipdb": "^0.3.3",

103
pnpm-lock.yaml generated
View File

@@ -11,9 +11,9 @@ importers:
'@ipdb/range': '@ipdb/range':
specifier: ^0.1.1 specifier: ^0.1.1
version: 0.1.1 version: 0.1.1
cidr-tools: fast-cidr-tools:
specifier: ^3.0.5 specifier: ^0.3.1
version: 3.0.6 version: 0.3.1
glob: glob:
specifier: ^7.2.3 specifier: ^7.2.3
version: 7.2.3 version: 7.2.3
@@ -183,14 +183,6 @@ packages:
chokidar@2.1.8: chokidar@2.1.8:
resolution: {integrity: sha512-ZmZUazfOzf0Nve7duiCKD23PFSCs4JPoYyccjUFF3aQkQadqBhfzhjkwBH2mNOG9cTBwhamM37EIsIkZw3nRgg==} resolution: {integrity: sha512-ZmZUazfOzf0Nve7duiCKD23PFSCs4JPoYyccjUFF3aQkQadqBhfzhjkwBH2mNOG9cTBwhamM37EIsIkZw3nRgg==}
cidr-regex@3.1.1:
resolution: {integrity: sha512-RBqYd32aDwbCMFJRL6wHOlDNYJsPNTt8vC82ErHF5vKt8QQzxm1FrkW8s/R5pVrXMf17sba09Uoy91PKiddAsw==}
engines: {node: '>=10'}
cidr-tools@3.0.6:
resolution: {integrity: sha512-6PvVezFGMdoaPkTJFvFlz1S61SeE+3SI+6jj+lAufPXtsyxyj9LnlhQkBuNNu004RuFPkkwKLX3/w4eOQqMaVg==}
engines: {node: '>=10'}
class-utils@0.3.6: class-utils@0.3.6:
resolution: {integrity: sha512-qOhPa/Fj7s6TY8H8esGu5QNpMMQxz79h+urzrNYN6mn+9BnxlDGf5QZ+XeCDsxSjPqsSR56XOZOJmpeurnLMeg==} resolution: {integrity: sha512-qOhPa/Fj7s6TY8H8esGu5QNpMMQxz79h+urzrNYN6mn+9BnxlDGf5QZ+XeCDsxSjPqsSR56XOZOJmpeurnLMeg==}
engines: {node: '>=0.10.0'} engines: {node: '>=0.10.0'}
@@ -382,6 +374,10 @@ packages:
resolution: {integrity: sha512-k9oEhlyc0FrVh25qYuSELjr8oxsCoc4/LEZfg2iJJrfEk/tZL9bCoJE47gqAvI2m/AUjluCS4+3I0eTx8n3AEw==} resolution: {integrity: sha512-k9oEhlyc0FrVh25qYuSELjr8oxsCoc4/LEZfg2iJJrfEk/tZL9bCoJE47gqAvI2m/AUjluCS4+3I0eTx8n3AEw==}
engines: {node: '>= 0.10'} engines: {node: '>= 0.10'}
fast-cidr-tools@0.3.1:
resolution: {integrity: sha512-CixbR87xxUYpoZBR6xyFmOkhoOnd8YtmSW956evigw+AOc20fXAn3FXGL5eFAPpGcrs7qzOOEjEPxVMnvRJ87Q==}
engines: {node: '>=16'}
fast-levenshtein@1.1.4: fast-levenshtein@1.1.4:
resolution: {integrity: sha512-Ia0sQNrMPXXkqVFt6w6M1n1oKo3NfKs+mvaV811Jwir7vAk9a6PVV9VPYf6X3BU97QiLEmuW3uXH9u87zDFfdw==} resolution: {integrity: sha512-Ia0sQNrMPXXkqVFt6w6M1n1oKo3NfKs+mvaV811Jwir7vAk9a6PVV9VPYf6X3BU97QiLEmuW3uXH9u87zDFfdw==}
@@ -558,29 +554,10 @@ packages:
resolution: {integrity: sha512-xgs2NH9AE66ucSq4cNG1nhSFghr5l6tdL15Pk+jl46bmmBapgoaY/AacXyaDznAqmGL99TiLSQgO/XazFSKYeQ==} resolution: {integrity: sha512-xgs2NH9AE66ucSq4cNG1nhSFghr5l6tdL15Pk+jl46bmmBapgoaY/AacXyaDznAqmGL99TiLSQgO/XazFSKYeQ==}
engines: {node: '>=0.10.0'} engines: {node: '>=0.10.0'}
ip-address@7.1.0:
resolution: {integrity: sha512-V9pWC/VJf2lsXqP7IWJ+pe3P1/HCYGBMZrrnT62niLGjAfCbeiwXMUxaeHvnVlz19O27pvXP4azs+Pj/A0x+SQ==}
engines: {node: '>= 10'}
ip-address@8.1.0:
resolution: {integrity: sha512-Wz91gZKpNKoXtqvY8ScarKYwhXoK4r/b5QuT+uywe/azv0/nUCo7Bh0IRRI7F9DHR06kJNWtzMGLIbXavngbKA==}
engines: {node: '>= 12'}
ip-cidr@3.1.0:
resolution: {integrity: sha512-HUCn4snshEX1P8cja/IyU3qk8FVDW8T5zZcegDFbu4w7NojmAhk5NcOgj3M8+0fmumo1afJTPDtJlzsxLdOjtg==}
engines: {node: '>=10.0.0'}
ip-regex@4.3.0:
resolution: {integrity: sha512-B9ZWJxHHOHUhUjCPrMpLD4xEq35bUTClHM1S6CBU5ixQnkZmwipwgc96vAd7AAGM9TGHvJR+Uss+/Ak6UphK+Q==}
engines: {node: '>=8'}
ipdb@0.3.3: ipdb@0.3.3:
resolution: {integrity: sha512-lQIa/I5mQ7WbT0h1MCkbixgZslJeEdjBN0a+ElDV2XDfdc0bgGWxUq6+QmMmcx3epu95xBGstmbR+1mX9m4EZQ==} resolution: {integrity: sha512-lQIa/I5mQ7WbT0h1MCkbixgZslJeEdjBN0a+ElDV2XDfdc0bgGWxUq6+QmMmcx3epu95xBGstmbR+1mX9m4EZQ==}
engines: {node: '>=6'} engines: {node: '>=6'}
ipv6-normalize@1.0.1:
resolution: {integrity: sha512-Bm6H79i01DjgGTCWjUuCjJ6QDo1HB96PT/xCYuyJUP9WFbVDrLSbG4EZCvOCun2rNswZb0c3e4Jt/ws795esHA==}
is-absolute@1.0.0: is-absolute@1.0.0:
resolution: {integrity: sha512-dOWoqflvcydARa360Gvv18DZ/gRuHKi2NU/wU5X1ZFzdYfH29nkiNZsF3mp4OJ3H4yo9Mx8A/uAGNzpzPN3yBA==} resolution: {integrity: sha512-dOWoqflvcydARa360Gvv18DZ/gRuHKi2NU/wU5X1ZFzdYfH29nkiNZsF3mp4OJ3H4yo9Mx8A/uAGNzpzPN3yBA==}
engines: {node: '>=0.10.0'} engines: {node: '>=0.10.0'}
@@ -599,10 +576,6 @@ packages:
is-buffer@1.1.6: is-buffer@1.1.6:
resolution: {integrity: sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w==} resolution: {integrity: sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w==}
is-cidr@4.0.2:
resolution: {integrity: sha512-z4a1ENUajDbEl/Q6/pVBpTR1nBjjEE1X7qb7bmWYanNnPoKAvUCPFKeXV6Fe4mgTkWKBqiHIcwsI3SndiO5FeA==}
engines: {node: '>=10'}
is-core-module@2.16.1: is-core-module@2.16.1:
resolution: {integrity: sha512-UfoeMA6fIJ8wTYFEUjelnaGI67v6+N7qXJEvQuIGa99l4xsCruSYOVSQ0uPANn4dAzm8lkYPaKLrrijLq7x23w==} resolution: {integrity: sha512-UfoeMA6fIJ8wTYFEUjelnaGI67v6+N7qXJEvQuIGa99l4xsCruSYOVSQ0uPANn4dAzm8lkYPaKLrrijLq7x23w==}
engines: {node: '>= 0.4'} engines: {node: '>= 0.4'}
@@ -643,10 +616,6 @@ packages:
resolution: {integrity: sha512-xelSayHH36ZgE7ZWhli7pW34hNbNl8Ojv5KVmkJD4hBdD3th8Tfk9vYasLM+mXWOZhFkgZfxhLSnrwRr4elSSg==} resolution: {integrity: sha512-xelSayHH36ZgE7ZWhli7pW34hNbNl8Ojv5KVmkJD4hBdD3th8Tfk9vYasLM+mXWOZhFkgZfxhLSnrwRr4elSSg==}
engines: {node: '>=0.10.0'} engines: {node: '>=0.10.0'}
is-ip@3.1.0:
resolution: {integrity: sha512-35vd5necO7IitFPjd/YBeqwWnyDWbuLH9ZXQdMfDA8TEo7pv5X8yfrvVO3xbJbLUlERCMvf6X0hTUamQxCYJ9Q==}
engines: {node: '>=8'}
is-negated-glob@1.0.0: is-negated-glob@1.0.0:
resolution: {integrity: sha512-czXVVn/QEmgvej1f50BZ648vUI+em0xqMq2Sn+QncCLN4zj1UAxlT+kw/6ggQTOaZPd1HqKQGEqbpQVtJucWug==} resolution: {integrity: sha512-czXVVn/QEmgvej1f50BZ648vUI+em0xqMq2Sn+QncCLN4zj1UAxlT+kw/6ggQTOaZPd1HqKQGEqbpQVtJucWug==}
engines: {node: '>=0.10.0'} engines: {node: '>=0.10.0'}
@@ -700,9 +669,6 @@ packages:
resolution: {integrity: sha512-WhB9zCku7EGTj/HQQRz5aUQEUeoQZH2bWcltRErOpymJ4boYE6wL9Tbr23krRPSZ+C5zqNSrSw+Cc7sZZ4b7vg==} resolution: {integrity: sha512-WhB9zCku7EGTj/HQQRz5aUQEUeoQZH2bWcltRErOpymJ4boYE6wL9Tbr23krRPSZ+C5zqNSrSw+Cc7sZZ4b7vg==}
engines: {node: '>=0.10.0'} engines: {node: '>=0.10.0'}
jsbn@1.1.0:
resolution: {integrity: sha512-4bYVV3aAMtDTTu4+xsDYa6sy9GyJ69/amsu9sYF2zqjiEoZA5xJi3BrfX3uY+/IekIu7MwdObdbDWpoZdBv3/A==}
json-stable-stringify-without-jsonify@1.0.1: json-stable-stringify-without-jsonify@1.0.1:
resolution: {integrity: sha512-Bdboy+l7tA3OGW6FjyFHWkP5LuByj1Tk33Ljyq0axyzdk9//JSi2u3fP1QSmd1KNwq6VOKYGlAu87CisVir6Pw==} resolution: {integrity: sha512-Bdboy+l7tA3OGW6FjyFHWkP5LuByj1Tk33Ljyq0axyzdk9//JSi2u3fP1QSmd1KNwq6VOKYGlAu87CisVir6Pw==}
@@ -1091,9 +1057,6 @@ packages:
resolution: {integrity: sha512-NzNVhJDYpwceVVii8/Hu6DKfD2G+NrQHlS/V/qgv763EYudVwEcMQNxd2lh+0VrUByXN/oJkl5grOhYWvQUYiw==} resolution: {integrity: sha512-NzNVhJDYpwceVVii8/Hu6DKfD2G+NrQHlS/V/qgv763EYudVwEcMQNxd2lh+0VrUByXN/oJkl5grOhYWvQUYiw==}
engines: {node: '>=0.10.0'} engines: {node: '>=0.10.0'}
sprintf-js@1.1.2:
resolution: {integrity: sha512-VE0SOVEHCk7Qc8ulkWw3ntAzXuqf7S2lvwQaDLRnUeIEaKNQJzV6BwmLKhOqT61aGhfUMrXeaBk+oDGCzvhcug==}
stack-trace@0.0.10: stack-trace@0.0.10:
resolution: {integrity: sha512-KGzahc7puUKkzyMt+IqAep+TVNbKP+k2Lmwhub39m1AsTSkaDutx56aDCo+HLDzf/D26BIHTJWNiTG1KAJiQCg==} resolution: {integrity: sha512-KGzahc7puUKkzyMt+IqAep+TVNbKP+k2Lmwhub39m1AsTSkaDutx56aDCo+HLDzf/D26BIHTJWNiTG1KAJiQCg==}
@@ -1107,9 +1070,6 @@ packages:
stream-shift@1.0.3: stream-shift@1.0.3:
resolution: {integrity: sha512-76ORR0DO1o1hlKwTbi/DM3EXWGf3ZJYO8cXX5RJwnul2DEg2oyoZyjLNoQM8WsvZiFKCRfC1O0J7iCvie3RZmQ==} resolution: {integrity: sha512-76ORR0DO1o1hlKwTbi/DM3EXWGf3ZJYO8cXX5RJwnul2DEg2oyoZyjLNoQM8WsvZiFKCRfC1O0J7iCvie3RZmQ==}
string-natural-compare@3.0.1:
resolution: {integrity: sha512-n3sPwynL1nwKi3WJ6AIsClwBMa0zTi54fn2oLU6ndfTSIO05xaznjSf15PcBZU6FNWbmN5Q6cxT4V5hGvB4taw==}
string-width@1.0.2: string-width@1.0.2:
resolution: {integrity: sha512-0XsVpQLnVCXHJfyEs8tC0zpTVIr5PKKsQtkT29IwupnPTjtPmQ3xT/4yCREF9hYkV/3M3kzcUTSAZT6a6h81tw==} resolution: {integrity: sha512-0XsVpQLnVCXHJfyEs8tC0zpTVIr5PKKsQtkT29IwupnPTjtPmQ3xT/4yCREF9hYkV/3M3kzcUTSAZT6a6h81tw==}
engines: {node: '>=0.10.0'} engines: {node: '>=0.10.0'}
@@ -1451,20 +1411,6 @@ snapshots:
transitivePeerDependencies: transitivePeerDependencies:
- supports-color - supports-color
cidr-regex@3.1.1:
dependencies:
ip-regex: 4.3.0
cidr-tools@3.0.6:
dependencies:
ip-address: 8.1.0
ip-cidr: 3.1.0
ipv6-normalize: 1.0.1
is-cidr: 4.0.2
is-ip: 3.1.0
jsbn: 1.1.0
string-natural-compare: 3.0.1
class-utils@0.3.6: class-utils@0.3.6:
dependencies: dependencies:
arr-union: 3.1.0 arr-union: 3.1.0
@@ -1695,6 +1641,8 @@ snapshots:
parse-node-version: 1.0.1 parse-node-version: 1.0.1
time-stamp: 1.1.0 time-stamp: 1.1.0
fast-cidr-tools@0.3.1: {}
fast-levenshtein@1.1.4: {} fast-levenshtein@1.1.4: {}
file-uri-to-path@1.0.0: file-uri-to-path@1.0.0:
@@ -1937,27 +1885,8 @@ snapshots:
invert-kv@1.0.0: {} invert-kv@1.0.0: {}
ip-address@7.1.0:
dependencies:
jsbn: 1.1.0
sprintf-js: 1.1.2
ip-address@8.1.0:
dependencies:
jsbn: 1.1.0
sprintf-js: 1.1.2
ip-cidr@3.1.0:
dependencies:
ip-address: 7.1.0
jsbn: 1.1.0
ip-regex@4.3.0: {}
ipdb@0.3.3: {} ipdb@0.3.3: {}
ipv6-normalize@1.0.1: {}
is-absolute@1.0.0: is-absolute@1.0.0:
dependencies: dependencies:
is-relative: 1.0.0 is-relative: 1.0.0
@@ -1975,10 +1904,6 @@ snapshots:
is-buffer@1.1.6: {} is-buffer@1.1.6: {}
is-cidr@4.0.2:
dependencies:
cidr-regex: 3.1.1
is-core-module@2.16.1: is-core-module@2.16.1:
dependencies: dependencies:
hasown: 2.0.2 hasown: 2.0.2
@@ -2017,10 +1942,6 @@ snapshots:
dependencies: dependencies:
is-extglob: 2.1.1 is-extglob: 2.1.1
is-ip@3.1.0:
dependencies:
ip-regex: 4.3.0
is-negated-glob@1.0.0: {} is-negated-glob@1.0.0: {}
is-number@3.0.0: is-number@3.0.0:
@@ -2059,8 +1980,6 @@ snapshots:
isobject@3.0.1: {} isobject@3.0.1: {}
jsbn@1.1.0: {}
json-stable-stringify-without-jsonify@1.0.1: {} json-stable-stringify-without-jsonify@1.0.1: {}
just-debounce@1.1.0: {} just-debounce@1.1.0: {}
@@ -2505,8 +2424,6 @@ snapshots:
dependencies: dependencies:
extend-shallow: 3.0.2 extend-shallow: 3.0.2
sprintf-js@1.1.2: {}
stack-trace@0.0.10: {} stack-trace@0.0.10: {}
static-extend@0.1.2: static-extend@0.1.2:
@@ -2518,8 +2435,6 @@ snapshots:
stream-shift@1.0.3: {} stream-shift@1.0.3: {}
string-natural-compare@3.0.1: {}
string-width@1.0.2: string-width@1.0.2:
dependencies: dependencies:
code-point-at: 1.1.0 code-point-at: 1.1.0

View File

@@ -1,4 +1,4 @@
const cidrTools = require('cidr-tools') const { merge } = require('fast-cidr-tools')
const plugin = (through2, file, cb) => { const plugin = (through2, file, cb) => {
@@ -28,7 +28,7 @@ const plugin = (through2, file, cb) => {
223.120.0.0/13 223.120.0.0/13
` `
white = white.trim().split('\n') white = white.trim().split('\n')
data = cidrTools.merge([...data, ...white]) data = merge([...data, ...white], true)
file.contents = Buffer.from(data.join('\n')) file.contents = Buffer.from(data.join('\n'))
file.path = file.path.replace('CN.txt', 'china.txt') file.path = file.path.replace('CN.txt', 'china.txt')

View File

@@ -1,5 +1,4 @@
const cidrTools = require('cidr-tools') const { merge } = require('fast-cidr-tools')
const ProgressBar = require('progress')
const plugin = (file, _, cb) => { const plugin = (file, _, cb) => {
let cidrs = file.contents.toString().split('\n') let cidrs = file.contents.toString().split('\n')
@@ -7,19 +6,7 @@ const plugin = (file, _, cb) => {
console.log(file.path) console.log(file.path)
if (cidrs.length) { if (cidrs.length) {
let result = [cidrs[0]] cidrs = merge(cidrs, true)
let bar = new ProgressBar(':bar :current/:total', { total: cidrs.length })
for (let i = 1; i < cidrs.length; i += 1) {
let t = cidrTools.merge([result[result.length - 1], cidrs[i]])
if (t.length === 1) {
result[result.length - 1] = t[0]
} else {
result.push(cidrs[i])
}
bar.tick()
}
cidrs = result
console.log()
} }
file.contents = Buffer.from(cidrs.join('\n')) file.contents = Buffer.from(cidrs.join('\n'))