Skip to content

Commit

Permalink
feat: add isLinkLocalIp function (libp2p#2863)
Browse files Browse the repository at this point in the history
To detect when an IP address is link local, add a function,
  • Loading branch information
achingbrain authored and acul71 committed Dec 1, 2024
1 parent 771b3fb commit 143669c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@
"types": "./dist/src/is-promise.d.ts",
"import": "./dist/src/is-promise.js"
},
"./link-local-ip": {
"types": "./dist/src/link-local-ip.d.ts",
"import": "./dist/src/link-local-ip.js"
},
"./moving-average": {
"types": "./dist/src/moving-average.d.ts",
"import": "./dist/src/moving-average.js"
Expand Down
13 changes: 13 additions & 0 deletions packages/utils/src/link-local-ip.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { isIPv4, isIPv6 } from '@chainsafe/is-ip'

export function isLinkLocalIp (ip: string): boolean {
if (isIPv4(ip)) {
return ip.startsWith('169.254.')
}

if (isIPv6(ip)) {
return ip.toLowerCase().startsWith('fe80')
}

return false
}
58 changes: 58 additions & 0 deletions packages/utils/test/link-local-ip.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* eslint-env mocha */

import { expect } from 'aegir/chai'
import { isLinkLocalIp } from '../src/link-local-ip.js'

describe('isLinkLocalIp', () => {
it('identifies link-local ip4 multiaddrs', () => {
[
'169.254.35.4',
'169.254.35.4',
'169.254.0.0',
'169.254.255.255'
].forEach(ma => {
expect(isLinkLocalIp(ma)).to.be.true()
})
})

it('identifies non link-local ip4 multiaddrs', () => {
[
'101.0.26.90',
'10.0.0.1',
'192.168.0.1',
'172.16.0.1'
].forEach(ma => {
expect(isLinkLocalIp(ma)).to.be.false()
})
})

it('identifies link-local ip6 multiaddrs', () => {
[
'fe80::1%lo0',
'fe80::1%lo0',
'fe80::1893:def4:af04:635a%en',
'fe80::1893:def4:af04:635a',
'fe80::1893:def4:af04:635a'
].forEach(ma => {
expect(isLinkLocalIp(ma)).to.be.true()
})
})

it('identifies non link-local ip6 multiaddrs', () => {
[
'2001:8a0:7ac5:4201:3ac9:86ff:fe31',
'::'
].forEach(ma => {
expect(isLinkLocalIp(ma)).to.be.false()
})
})

it('identifies other multiaddrs as not link-local addresses', () => {
[
'wss0.bootstrap.libp2p.io',
'wss0.bootstrap.libp2p.io'
].forEach(ma => {
expect(isLinkLocalIp(ma)).to.be.false()
})
})
})

0 comments on commit 143669c

Please sign in to comment.