better code: using node's module instead
Massimo Melina committed
Sep 6, 2023 at 16:05 UTC
2f897da8cfcc6e341299a480ff28bfd213f68496
2 files changed
+21
-4
package.json
-1
@@ -70,7 +70,6 @@
70
"@node-rs/crc32": "^1.6.0",
71
"basic-auth": "^2.0.1",
72
"buffer-crc32": "^0.2.13",
73
- "cidr-tools": "^4.3.0",
73
"fast-glob": "^3.2.7",
74
"find-process": "^1.4.7",
75
"formidable": "^3.5.1",
src/misc.ts
+21
-3
@@ -11,7 +11,7 @@ export * from './util-files'
11
export * from './cross'
12
import { Readable } from 'stream'
13
import { matcher } from 'micromatch'
14
-import cidr from 'cidr-tools'
14
+import { SocketAddress, BlockList } from 'node:net'
15
import debounceAsync from './debounceAsync'
16
export { debounceAsync }
17
@@ -68,8 +68,26 @@ export function makeNetMatcher(mask: string, emptyMaskReturns=false) {
68
const neg = all[0]?.[0] === '!'
69
if (neg)
70
all[0] = all[0]!.slice(1)
71
- return (ip: string) =>
72
- neg !== all.some(x => cidr.contains(x, ip))
71
+ const bl = new BlockList()
72
+ for (const x of all) {
73
+ const m = /^([.:\d]+)(?:\/(\d+)|-(.+)|)$/.exec(x)
74
+ if (!m) {
75
+ console.warn("error in network mask", x)
76
+ continue
77
+ }
78
+ const address = parseAddress(m[1]!)
79
+ if (m[2])
80
+ bl.addSubnet(address, Number(m[2]))
81
+ else if (m[3])
82
+ bl.addRange(address, parseAddress(m[2]!))
83
+ else
84
+ bl.addAddress(address)
85
+ }
86
+ return (ip: string) => neg !== bl.check(ip)
87
+}
88
+
89
+function parseAddress(s: string) {
90
+ return new SocketAddress({ address: s, family: s.includes(':') ? 'ipv6' : 'ipv4' })
91
}
92
93
export function makeMatcher(mask: string, emptyMaskReturns=false) {