Skip to content

Commit

Permalink
perf: reduce ipv4 entries
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Feb 25, 2025
1 parent 0f97f76 commit 8a2c819
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 27 deletions.
32 changes: 20 additions & 12 deletions src/ipv4.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
'use strict'

const IP_RANGES = [
// 0.0.0.0 - 0.255.255.255
'0(?:\\.\\d{1,3}){3}',
// 10.0.0.0 - 10.255.255.255
/^(:{2}f{4}:)?10(?:\.\d{1,3}){3}$/,
'10(?:\\.\\d{1,3}){3}',
// 127.0.0.0 - 127.255.255.255
/^(:{2}f{4}:)?127(?:\.\d{1,3}){3}$/,
'127(?:\\.\\d{1,3}){3}',
// 169.254.1.0 - 169.254.254.255
/^(::f{4}:)?169\.254\.([1-9]|1?\d\d|2[0-4]\d|25[0-4])\.\d{1,3}$/,
'169\\.254\\.(?:[1-9]|1?\\d\\d|2[0-4]\\d|25[0-4])\\.\\d{1,3}',
// 172.16.0.0 - 172.31.255.255
/^(:{2}f{4}:)?(172\.1[6-9]|172\.2\d|172\.3[01])(?:\.\d{1,3}){2}$/,
// 192.168.0.0 - 192.168.255.255
/^(:{2}f{4}:)?192\.168(?:\.\d{1,3}){2}$/,
// fc00::/7
/^f[cd][\da-f]{2}(::1$|:[\da-f]{1,4}){1,7}$/,
// fe80::/10s
/^fe[89ab][\da-f](::1$|:[\da-f]{1,4}){1,7}$/,
'172\\.(?:1[6-9]|2\\d|3[01])(?:\\.\\d{1,3}){2}',
// 192.0.0.0 - 192.0.0.255, 192.0.2.0 - 192.0.2.255, 192.168.0.0 - 192.168.255.255
'192\\.(?:0\\.0(?:\\.\\d{1,3})|0\\.2(?:\\.\\d{1,3})|168(?:\\.\\d{1,3}){2})',
// 100.64.0.0 - 100.127.255.255
'100\\.(?:6[4-9]|[7-9]\\d|1[01]\\d|12[0-7])(?:\\.\\d{1,3}){2}',
// 198.18.0.0 - 198.19.255.255, 198.51.100.0 - 198.51.100.255
'198\\.(?:1[89](?:\\.\\d{1,3}){2}|51\\.100(?:\\.\\d{1,3}))',
// 203.0.113.0 - 203.0.113.255
'203\\.0\\.113(?:\\.\\d{1,3})',
// 224.0.0.0 - 239.255.255.255
'22[4-9](?:\\.\\d{1,3}){3}|23[0-9](?:\\.\\d{1,3}){3}',
// 240.0.0.0 - 255.255.255.255
'24[0-9](?:\\.\\d{1,3}){3}|25[0-5](?:\\.\\d{1,3}){3}',
// localhost in IPv4
/^localhost$|^0\.0\.0\.0$/
'localhost'
]

const regex = new RegExp(`^(${IP_RANGES.map(re => re.source).join('|')})$`)
const regex = new RegExp(`^(:{2}f{4}:)?(${IP_RANGES.join('|')})$`)

module.exports = regex.test.bind(regex)
module.exports.regex = regex
38 changes: 25 additions & 13 deletions test/cases.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
const { isIP } = require('net')

const IP_RANGES = [
'localhost',
'0.0.0.0',
'0.0.0.1',
'10.0.0.0',
'10.0.0.1',
'10.0.0.01',
'10.0.0.001',
'10.1.1.1',
'10.255.255.254',
'10.255.255.255',
'100.64.1.1',
'127.0.0.0',
'127.0.0.1',
'127.0.0.01',
'127.000.000.1',
'127.0.53.53',
'127.255.255.254',
'127.255.255.255',
'169.254.1.1',
'172.16.0.0',
'172.16.0.1',
'172.16.0.001',
'172.16.1.1',
'172.31.255.254',
'172.31.255.255',
'192.0.0.1',
'192.0.2.1',
'198.51.100.1',
'198.18.1.1',
'192.168.0.0',
'192.168.0.1',
'192.168.1.1',
'192.168.255.254',
'192.168.255.255',
'203.0.113.1',
'224.0.0.0',
'226.84.185.150',
'227.202.96.196',
'240.0.0.1',
'255.255.255.255',
'::',
'::1',
'::ffff:0.0.0.0',
Expand All @@ -48,7 +61,11 @@ const IP_RANGES = [
'2002::',
'2002::1',
'2002::abc:1',
'2002:ffff:ffff:ffff:ffff:ffff:ffff:ffff'
// '2001:2:ffff:ffff:ffff:ffff:ffff:ffff',
// '2001:1f:ffff:ffff:ffff:ffff:ffff:ffff',
'2002:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
// 'fbff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
// 'fec0::'
]

const internalIPs = IP_RANGES.map(ip => ({ type: isIP(ip), ip }))
Expand All @@ -65,13 +82,10 @@ const externalIPs = [
'156.238.194.84',
'164.101.185.82',
'223.231.138.242',
'226.84.185.150',
'227.202.96.196',
'::1fff:0.0.0.0',
'::1fff:10.0.0.0',
'::1fff:0:0.0.0.0',
'::1fff:0:10.0.0.0',
'2001:2:ffff:ffff:ffff:ffff:ffff:ffff',
'64:ff9a::0.0.0.0',
'64:ff9a::255.255.255.255',
'99::',
Expand All @@ -81,16 +95,14 @@ const externalIPs = [
'2000::',
'2000::ffff:ffff:ffff:ffff:ffff:ffff',
'2001:10::',
'2001:1f:ffff:ffff:ffff:ffff:ffff:ffff',
'2001:db7::',
'2001:db7:ffff:ffff:ffff:ffff:ffff:ffff',
'2001:db9::',
'fb00::',
'fbff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
'fec0::'
'fb00::'
].map(ip => ({ type: isIP(ip), ip }))

module.exports = {
IP_RANGES,
internalIPs,
externalIPs,
externalIpv4s: externalIPs.filter(({ type }) => type === 4 || type === 0),
Expand Down
4 changes: 2 additions & 2 deletions test/ipv4.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ const test = require('ava')

const isLocalhost = require('is-local-address/ipv4')

const { externalIPs, internalIPv4s, internalIPv6s } = require('./cases')
const { externalIPs, internalIPv4s } = require('./cases')

internalIPv4s.forEach(({ ip }) => {
test(`internal » true » ${ip}`, t => {
t.true(isLocalhost(ip), ip)
})
})

externalIPs.concat(internalIPv6s).forEach(({ ip }) => {
externalIPs.forEach(({ ip }) => {
test(`external » false » ${ip}`, t => {
t.false(isLocalhost(ip), ip)
})
Expand Down

0 comments on commit 8a2c819

Please sign in to comment.