Skip to content

Commit

Permalink
fix: should return scopeid = 0 address on IPv6
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Feb 29, 2024
1 parent 96f778a commit acb82f3
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ npm-debug.log
test/fixtures/ts/*.js
.tshy*
dist/
package-lock.json
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,6 @@
}
}
},
"types": "./dist/commonjs/index.d.ts"
"types": "./dist/commonjs/index.d.ts",
"main": "./dist/commonjs/index.js"
}
38 changes: 29 additions & 9 deletions src/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,28 @@ function matchName(actualFamily: string | number, expectedFamily: string | numbe
return actualFamily === expectedFamily;
}

function findAddressFromInterface(items: os.NetworkInterfaceInfo[], expectedFamily: string | number,
ignoreLoAddress = false) {
let firstMatchItem;
for (const item of items) {
if (matchName(item.family, expectedFamily)) {
if (ignoreLoAddress && item.address.startsWith('127.')) {
continue;
}
if (expectedFamily === 'IPv6') {
// find the scopeid = 0 item
if (item.scopeid === 0) return item;
if (!firstMatchItem) {
firstMatchItem = item;
}
} else {
return item;
}
}
}
return firstMatchItem;
}

export function getInterfaceAddress(family?: string, name?: string) {
const interfaces = os.networkInterfaces();
const noName = !name;
Expand All @@ -68,10 +90,9 @@ export function getInterfaceAddress(family?: string, name?: string) {
const interfaceName = name + (i >= 0 ? i : ''); // support 'lo' and 'lo0'
const items = interfaces[interfaceName];
if (items) {
for (const item of items) {
if (matchName(item.family, family)) {
return item;
}
const item = findAddressFromInterface(items, family);
if (item) {
return item;
}
}
}
Expand All @@ -82,11 +103,10 @@ export function getInterfaceAddress(family?: string, name?: string) {
for (const k in interfaces) {
const items = interfaces[k];
if (items) {
for (const item of items) {
// all 127 addresses are local and should be ignored
if (matchName(item.family, family) && !item.address.startsWith('127.')) {
return item;
}
// all 127 addresses are local and should be ignored
const item = findAddressFromInterface(items, family, true);
if (item) {
return item;
}
}
}
Expand Down
31 changes: 31 additions & 0 deletions test/address.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,37 @@ describe('test/address.test.ts', () => {
assert.equal(addressAll.ip('utun'), '10.206.52.79');
assert.equal(addressAll.ipv6('utun'), 'fe80::696:ad3d:eeec:1722');
});

it('should return scopeid = 0 address', () => {
mm(os, 'networkInterfaces', () => {
return {
lo:
[{ address: '127.0.0.1',
family: 'IPv4',
internal: true }],
utun0:
[
{
address: 'fe80::696:ad3d:eeec:1722',
family: 'IPv6',
internal: false,
scopeid: 20,
},
{
address: 'fe80::696:ad3d:eeee:ffff:1:1000:3380:b81a:2dd4:373e:1234',
family: 'IPv6',
internal: false,
scopeid: 0,
},
],
utun1:
[{ address: '10.206.52.79',
family: 'IPv4',
internal: false }] };
});
assert.equal(addressAll.ip('utun'), '10.206.52.79');
assert.equal(addressAll.ipv6('utun'), 'fe80::696:ad3d:eeee:ffff:1:1000:3380:b81a:2dd4:373e:1234');
});
});

describe('address.dns()', () => {
Expand Down

0 comments on commit acb82f3

Please sign in to comment.