pax_global_header00006660000000000000000000000064145033105350014511gustar00rootroot0000000000000052 comment=96f778abbf8d6027c0b7ce873e31131b339b1d6d address-2.0.1/000077500000000000000000000000001450331053500131365ustar00rootroot00000000000000address-2.0.1/.eslintrc000066400000000000000000000001611450331053500147600ustar00rootroot00000000000000{ "extends": [ "eslint-config-egg/typescript", "eslint-config-egg/lib/rules/enforce-node-prefix" ] } address-2.0.1/.github/000077500000000000000000000000001450331053500144765ustar00rootroot00000000000000address-2.0.1/.github/workflows/000077500000000000000000000000001450331053500165335ustar00rootroot00000000000000address-2.0.1/.github/workflows/nodejs.yml000066400000000000000000000004241450331053500205400ustar00rootroot00000000000000name: CI on: push: branches: [ master ] pull_request: branches: [ master ] jobs: Job: name: Node.js uses: node-modules/github-actions/.github/workflows/node-test.yml@master with: os: 'ubuntu-latest, macos-latest' version: '16, 18, 20' address-2.0.1/.github/workflows/release.yml000066400000000000000000000004521450331053500206770ustar00rootroot00000000000000name: Release on: push: branches: [ master ] jobs: release: name: Node.js uses: node-modules/github-actions/.github/workflows/node-release.yml@master secrets: NPM_TOKEN: ${{ secrets.NPM_TOKEN }} GIT_TOKEN: ${{ secrets.GIT_TOKEN }} with: checkTest: false address-2.0.1/.gitignore000066400000000000000000000002271450331053500151270ustar00rootroot00000000000000coverage *.seed *.log *.csv *.dat *.out *.pid *.gz pids logs results node_modules npm-debug.log .DS_Store .idea/ test/fixtures/ts/*.js .tshy* dist/ address-2.0.1/CHANGELOG.md000066400000000000000000000060311450331053500147470ustar00rootroot00000000000000# Changelog ## [2.0.0](https://github.com/node-modules/address/compare/v1.2.2...v2.0.0) (2023-09-22) ### ⚠ BREAKING CHANGES * Drop Node.js < 16 support - rename `interface()` to `getInterfaceAddress()` ### Features * refactor with typescript to support cjs and esm both ([#37](https://github.com/node-modules/address/issues/37)) ([8981f21](https://github.com/node-modules/address/commit/8981f214084326c55b053c76182acd741262c057)) ## [1.2.2](https://github.com/node-modules/address/compare/v1.2.1...v1.2.2) (2022-12-17) ### Bug Fixes * Auto release on action ([#33](https://github.com/node-modules/address/issues/33)) ([36b30d3](https://github.com/node-modules/address/commit/36b30d3708bd5fb09dd2e9a9d4741dd9112eb890)) --- 1.2.1 / 2022-09-13 ================== **fixes** * [[`c316a04`](http://github.com/node-modules/address/commit/c316a044aedeadf438c2c2a7278d51f0861df8af)] - fix: local addresses should be ignored on interface (#32) (Jon Kelley <>) **others** * [[`823b70c`](http://github.com/node-modules/address/commit/823b70c2f53ab96d6e25041aa444436d906c59ef)] - 🤖 TEST: Run test on GitHub action (#27) (fengmk2 <>) * [[`2b5fb44`](http://github.com/node-modules/address/commit/2b5fb44ca1ff1a0d64768e8d42845ec377b9e3f8)] - Create codeql-analysis.yml (fengmk2 <>) 1.2.0 / 2022-04-29 ================== **features** * [[`fe81a41`](http://github.com/node-modules/address/commit/fe81a415403ba46d7bc09d76a2f9fc46bc2fc803)] - feat: address.ip() supports node 18 (#26) (Yuheng Zhang <>) 1.1.2 / 2019-08-26 ================== **fixes** * [[`304754e`](http://github.com/node-modules/address/commit/304754ea4ef0dd34db7ba34745f4f4543afc064c)] - fix: fix declarations (#20) (吖猩 <>) 1.1.1 / 2019-08-22 ================== **fixes** * [[`19884ff`](http://github.com/node-modules/address/commit/19884ffbe9ad75f9a66889a031db4b806da1f822)] - fix: update d.ts (#18) (MarioDu <>) 1.1.0 / 2019-04-24 ================== **features** * [[`7544592`](http://github.com/node-modules/address/commit/75445923a6f737fc21e3cf592f749bf014b7b4ce)] - feat: Add typings (#15) (Mathieu TUDISCO <>) 1.0.3 / 2017-08-24 ================== **fixes** * [[`ed491c5`](http://github.com/node-modules/address/commit/ed491c5bd353118e4e4d384f47f13c3e1cfeb80e)] - fix: ignore wrong mac address on node 8.x (#10) (fengmk2 <>) 1.0.2 / 2017-05-26 ================== * fix: win32 get mac failed (#9) 1.0.1 / 2016-09-30 ================== * test: remove 0.12 * fix: search interface before family match * add contributors 1.0.0 / 2015-08-06 ================== * chore: use npm scripts instead of Makefile * add benchmark 0.0.3 / 2013-11-04 ================== * get the first not local ip when interface not exists 0.0.2 / 2013-08-08 ================== * use networkInterface() to get mac fix #3 0.0.1 / 2013-07-31 ================== * ip(), ipv6(), mac(), dns() work on osx and linux now. * first commit address-2.0.1/LICENSE.txt000066400000000000000000000022521450331053500147620ustar00rootroot00000000000000This software is licensed under the MIT License. Copyright (C) 2013 - 2014 fengmk2 Copyright (C) 2015 - present node-modules and other contributors. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. address-2.0.1/README.md000066400000000000000000000121141450331053500144140ustar00rootroot00000000000000# address [![NPM version][npm-image]][npm-url] [![Node.js CI](https://github.com/node-modules/address/actions/workflows/nodejs.yml/badge.svg)](https://github.com/node-modules/address/actions/workflows/nodejs.yml) [![Test coverage][coveralls-image]][coveralls-url] [![npm download][download-image]][download-url] [npm-image]: https://img.shields.io/npm/v/address.svg?style=flat-square [npm-url]: https://npmjs.org/package/address [coveralls-image]: https://img.shields.io/coveralls/node-modules/address.svg?style=flat-square [coveralls-url]: https://coveralls.io/r/node-modules/address?branch=master [download-image]: https://img.shields.io/npm/dm/address.svg?style=flat-square [download-url]: https://npmjs.org/package/address Get current machine IPv4, IPv6, MAC and DNS servers. DNS servers receive from `/etc/resolv.conf`. ## Install ```bash npm install address ``` ## Usage Get IP is sync and get MAC is async for now. - esm & typescript ```ts import { ip, ipv6, mac } from 'address'; // default interface 'eth' on linux, 'en' on osx. ip(); // '192.168.0.2' ipv6(); // 'fe80::7aca:39ff:feb0:e67d' mac(function (err, addr) { console.log(addr); // '78:ca:39:b0:e6:7d' }); // local loopback ip('lo'); // '127.0.0.1' // vboxnet MAC mac('vboxnet', function (err, addr) { console.log(addr); // '0a:00:27:00:00:00' }); ``` - commonjs ```js const { ip, ipv6, mac } = require('address'); // default interface 'eth' on linux, 'en' on osx. ip(); // '192.168.0.2' ipv6(); // 'fe80::7aca:39ff:feb0:e67d' mac(function (err, addr) { console.log(addr); // '78:ca:39:b0:e6:7d' }); // local loopback ip('lo'); // '127.0.0.1' // vboxnet MAC mac('vboxnet', function (err, addr) { console.log(addr); // '0a:00:27:00:00:00' }); ``` ### Get all addresses: IPv4, IPv6 and MAC - esm & typescript ```ts import { address } from 'address'; address((err, addrs) => { console.log(addrs.ip, addrs.ipv6, addrs.mac); // '192.168.0.2', 'fe80::7aca:39ff:feb0:e67d', '78:ca:39:b0:e6:7d' }); address('vboxnet', (err, addrs) => { console.log(addrs.ip, addrs.ipv6, addrs.mac); // '192.168.56.1', null, '0a:00:27:00:00:00' }); ``` - commonjs ```js const { address } = require('address'); address((err, addrs) => { console.log(addrs.ip, addrs.ipv6, addrs.mac); // '192.168.0.2', 'fe80::7aca:39ff:feb0:e67d', '78:ca:39:b0:e6:7d' }); address('vboxnet', (err, addrs) => { console.log(addrs.ip, addrs.ipv6, addrs.mac); // '192.168.56.1', null, '0a:00:27:00:00:00' }); ``` ### Get an interface info with family - esm & typescript ```ts import { getInterfaceAddress } from 'address'; getInterfaceAddress('IPv4', 'eth1'); // { address: '192.168.1.1', family: 'IPv4', mac: '78:ca:39:b0:e6:7d' } ``` - commonjs ```js const { getInterfaceAddress } = require('address'); getInterfaceAddress('IPv4', 'eth1'); // { address: '192.168.1.1', family: 'IPv4', mac: '78:ca:39:b0:e6:7d' } ``` ### Get DNS servers - esm & typescript ```js import { dns } from 'address'; dns((err, servers) => { console.log(servers); // ['10.13.2.1', '10.13.2.6'] }); ``` - commonjs ```js const { dns } = require('address'); dns((err, servers) => { console.log(servers); // ['10.13.2.1', '10.13.2.6'] }); ``` ### Promise style apis ```ts import { address, mac, dns } from 'address/promises'; const addr = await address(); const macAddress = await mac(); const servers = await dns(); ``` ## License [MIT](LICENSE.txt) ## Contributors |[
fengmk2](https://github.com/fengmk2)
|[
alsotang](https://github.com/alsotang)
|[
jkelleyrtp](https://github.com/jkelleyrtp)
|[
slyon](https://github.com/slyon)
|[
mariodu](https://github.com/mariodu)
|[
mathieutu](https://github.com/mathieutu)
| | :---: | :---: | :---: | :---: | :---: | :---: | [
zhangyuheng](https://github.com/zhangyuheng)
|[
semantic-release-bot](https://github.com/semantic-release-bot)
|[
coolme200](https://github.com/coolme200)
|[
whxaxes](https://github.com/whxaxes)
This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Fri Sep 22 2023 20:49:32 GMT+0800`. address-2.0.1/example/000077500000000000000000000000001450331053500145715ustar00rootroot00000000000000address-2.0.1/example/hello.cjs000066400000000000000000000005511450331053500163760ustar00rootroot00000000000000const address = require('address').default; const { ip, ipv6 } = require('address'); const { address: addr } = require('address/promises'); async function main() { console.log('ip=%o', ip()); console.log('ipv6=%o', ipv6()); console.log('await address=%o', await addr()); address((_, a) => { console.log('callback address=%o', a); }); } main(); address-2.0.1/example/hello.mjs000066400000000000000000000004451450331053500164120ustar00rootroot00000000000000import address from 'address'; import { ip, ipv6 } from 'address'; import { address as addr } from 'address/promises'; console.log('ip=%o', ip()); console.log('ipv6=%o', ipv6()); console.log('await address=%o', await addr()); address((_, a) => { console.log('callback address=%o', a); }); address-2.0.1/package.json000066400000000000000000000035241450331053500154300ustar00rootroot00000000000000{ "name": "address", "version": "2.0.1", "description": "Get current machine IP, MAC and DNS servers.", "scripts": { "lint": "eslint src test --ext .ts", "pretest": "npm run lint -- --fix && npm run prepublishOnly", "test": "egg-bin test", "preci": "npm run prepublishOnly", "ci": "egg-bin cov", "contributor": "git-contributor", "prepublishOnly": "tshy && tshy-after" }, "devDependencies": { "@eggjs/tsconfig": "^1.3.3", "@types/mocha": "^10.0.1", "@types/node": "^20.6.3", "egg-bin": "^6.5.2", "eslint": "^8.49.0", "eslint-config-egg": "^13.0.0", "git-contributor": "^2.1.5", "mm": "^3.3.0", "runscript": "^1.5.3", "tshy": "^1.1.1", "tshy-after": "^1.0.0", "typescript": "^5.2.2" }, "repository": { "type": "git", "url": "git://github.com/node-modules/address.git" }, "keywords": [ "address", "ip", "ipv4", "mac" ], "engines": { "node": ">= 16.0.0" }, "author": "fengmk2 ", "license": "MIT", "files": [ "dist", "src" ], "type": "module", "tshy": { "exports": { "./package.json": "./package.json", "./promises": "./src/promises.ts", ".": "./src/index.ts" } }, "exports": { "./package.json": "./package.json", "./promises": { "import": { "types": "./dist/esm/promises.d.ts", "default": "./dist/esm/promises.js" }, "require": { "types": "./dist/commonjs/promises.d.ts", "default": "./dist/commonjs/promises.js" } }, ".": { "import": { "types": "./dist/esm/index.d.ts", "default": "./dist/esm/index.js" }, "require": { "types": "./dist/commonjs/index.d.ts", "default": "./dist/commonjs/index.js" } } }, "types": "./dist/commonjs/index.d.ts" } address-2.0.1/src/000077500000000000000000000000001450331053500137255ustar00rootroot00000000000000address-2.0.1/src/address.ts000066400000000000000000000170121450331053500157230ustar00rootroot00000000000000import os from 'node:os'; import fs from 'node:fs/promises'; import childProcess from 'node:child_process'; const DEFAULT_RESOLV_FILE = '/etc/resolv.conf'; // osx start line 'en0: flags=8863 mtu 1500' // linux start line 'eth0 Link encap:Ethernet HWaddr 00:16:3E:00:0A:29 ' const MAC_OSX_START_LINE = /^(\w+)\:\s+flags=/; const MAC_LINUX_START_LINE = /^(\w+)\s{2,}link encap:\w+/i; // ether 78:ca:39:b0:e6:7d // HWaddr 00:16:3E:00:0A:29 export const MAC_RE = /(?:ether|HWaddr)\s+((?:[a-z0-9]{2}\:){5}[a-z0-9]{2})/i; // osx: inet 192.168.2.104 netmask 0xffffff00 broadcast 192.168.2.255 // linux: inet addr:10.125.5.202 Bcast:10.125.15.255 Mask:255.255.240.0 export const MAC_IP_RE = /inet\s(?:addr\:)?(\d+\.\d+\.\d+\.\d+)/; export interface Address { ip?: string; ipv6?: string; mac?: string; } export type AddressCallback = (err: Error | null, addr: Address) => void; export type MacCallback = (err?: Error | null, addr?: string | null) => void; export type DnsCallback = (err?: Error | null, servers?: string[]) => void; function getDefaultInterfaceName() { let val: string | undefined = 'eth'; const platform = os.platform(); if (platform === 'darwin') { val = 'en'; } else if (platform === 'win32') { val = undefined; } return val; } function getIfconfigCMD() { if (os.platform() === 'win32') { return 'ipconfig/all'; } return '/sbin/ifconfig'; } // typeof os.networkInterfaces family is a number (v18.0.0) // types: 'IPv4' | 'IPv6' => 4 | 6 // @see https://github.com/nodejs/node/issues/42861 function matchName(actualFamily: string | number, expectedFamily: string | number) { if (expectedFamily === 'IPv4') { return actualFamily === 'IPv4' || actualFamily === 4; } if (expectedFamily === 'IPv6') { return actualFamily === 'IPv6' || actualFamily === 6; } return actualFamily === expectedFamily; } export function getInterfaceAddress(family?: string, name?: string) { const interfaces = os.networkInterfaces(); const noName = !name; name = name || getDefaultInterfaceName(); family = family || 'IPv4'; if (name) { for (let i = -1; i < 8; i++) { 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; } } } } } if (noName) { // filter all loopback or local addresses 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; } } } } } return; } /** * Get current machine IPv4 * * interfaceName: interface name, default is 'eth' on linux, 'en' on mac os. */ export function ip(interfaceName?: string) { const item = getInterfaceAddress('IPv4', interfaceName); return item?.address; } /** * Get current machine IPv6 * * interfaceName: interface name, default is 'eth' on linux, 'en' on mac os. */ export function ipv6(interfaceName?: string) { const item = getInterfaceAddress('IPv6', interfaceName); return item?.address; } function getMAC(content: string, interfaceName: string, matchIP: string) { const lines = content.split('\n'); for (let i = 0; i < lines.length; i++) { let line = lines[i].trimEnd(); const m = MAC_OSX_START_LINE.exec(line) || MAC_LINUX_START_LINE.exec(line); if (!m) { continue; } // check interface name const name = m[1]; if (name.indexOf(interfaceName) !== 0) { continue; } let ip: string | null = null; let mac: string | null = null; let match = MAC_RE.exec(line); if (match) { mac = match[1]; } i++; // eslint-disable-next-line no-constant-condition while (true) { line = lines[i]; if (!line || MAC_OSX_START_LINE.exec(line) || MAC_LINUX_START_LINE.exec(line)) { i--; break; // hit next interface, handle next interface } if (!mac) { match = MAC_RE.exec(line); if (match) { mac = match[1]; } } if (!ip) { match = MAC_IP_RE.exec(line); if (match) { ip = match[1]; } } i++; } if (ip === matchIP) { return mac; } } return null; } /** * Get current machine MAC address * * interfaceName: name, default is 'eth' on linux, 'en' on mac os. */ export function mac(callback: MacCallback): void; export function mac(interfaceName: string, callback: MacCallback): void; export function mac(interfaceNameOrCallback: string | MacCallback, callback?: MacCallback) { let interfaceName: string | undefined; if (typeof interfaceNameOrCallback === 'function') { callback = interfaceNameOrCallback; } else { interfaceName = interfaceNameOrCallback; } interfaceName = interfaceName || getDefaultInterfaceName(); const item = getInterfaceAddress('IPv4', interfaceName); if (!item) { return callback!(); } // https://github.com/nodejs/node/issues/13581 // bug in node 7.x and <= 8.4.0 if (!process.env.CI && (item.mac === 'ff:00:00:00:00:00' || item.mac === '00:00:00:00:00:00')) { // wrong address, ignore it item.mac = ''; } if (item.mac) { return callback!(null, item.mac); } childProcess.exec(getIfconfigCMD(), { timeout: 5000 }, (err, stdout) => { if (err || !stdout) { return callback!(err); } if (!interfaceName) { return callback!(); } const mac = getMAC(stdout || '', interfaceName, item.address); callback!(null, mac); }); } // nameserver 172.24.102.254 const DNS_SERVER_RE = /^nameserver\s+(\d+\.\d+\.\d+\.\d+)$/i; /** * Get DNS servers. * * filepath: resolv config file path. default is '/etc/resolv.conf'. */ export function dns(callback: DnsCallback): void; export function dns(filepath: string, callback: DnsCallback): void; export function dns(filepathOrCallback: string | DnsCallback, callback?: DnsCallback) { let filepath: string | undefined; if (typeof filepathOrCallback === 'function') { callback = filepathOrCallback; } else { filepath = filepathOrCallback; } filepath = filepath || DEFAULT_RESOLV_FILE; fs.readFile(filepath, 'utf8') .then(content => { const servers: string[] = []; content = content || ''; const lines = content.split('\n'); for (const line of lines) { const m = DNS_SERVER_RE.exec(line.trim()); if (m) { servers.push(m[1]); } } callback!(null, servers); }) .catch(err => { callback!(err); }); } /** * Get all addresses. * * interfaceName: interface name, default is 'eth' on linux, 'en' on mac os. */ export function address(callback: AddressCallback): void; export function address(interfaceName: string, callback: AddressCallback): void; export function address(interfaceNameOrCallback: string | AddressCallback, callback?: AddressCallback) { let interfaceName: string | undefined; if (typeof interfaceNameOrCallback === 'function') { callback = interfaceNameOrCallback; } else { interfaceName = interfaceNameOrCallback; } const addr: Address = { ip: ip(interfaceName), ipv6: ipv6(interfaceName), mac: undefined, }; mac(interfaceName || '', (err?: Error | null, mac?: string | null) => { if (mac) { addr.mac = mac; } callback!(err || null, addr); }); } address-2.0.1/src/index.ts000066400000000000000000000001371450331053500154050ustar00rootroot00000000000000import { address } from './address.js'; export * from './address.js'; export default address; address-2.0.1/src/promises.ts000066400000000000000000000014401450331053500161350ustar00rootroot00000000000000import { Address, address as getAddress, mac as getMac, dns as getDns, } from './address.js'; export function address(interfaceName?: string) { return new Promise
((resolve, reject) => { getAddress(interfaceName || '', (err, address) => { if (err) return reject(err); resolve(address); }); }); } export function mac(interfaceName?: string) { return new Promise((resolve, reject) => { getMac(interfaceName || '', (err, address) => { if (err) return reject(err); resolve(address || null); }); }); } export function dns(filepath?: string) { return new Promise((resolve, reject) => { getDns(filepath || '', (err, servers) => { if (err) return reject(err); resolve(servers || []); }); }); } address-2.0.1/test/000077500000000000000000000000001450331053500141155ustar00rootroot00000000000000address-2.0.1/test/address.test.ts000066400000000000000000000200401450331053500170640ustar00rootroot00000000000000import { strict as assert } from 'node:assert'; import os from 'node:os'; import child from 'node:child_process'; import path from 'node:path'; import fs from 'node:fs'; import { fileURLToPath } from 'node:url'; import mm from 'mm'; import * as addressAll from '../src/index.js'; import address from '../src/index.js'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const fixtures = path.join(__dirname, 'fixtures'); describe('test/address.test.ts', () => { beforeEach(mm.restore); afterEach(mm.restore); describe('regex check', () => { it('should MAC_IP_RE pass', () => { assert(addressAll.MAC_IP_RE.test(' inet 10.7.84.211 netmask 0xfffffc00 broadcast 10.7.87.255')); assert(addressAll.MAC_IP_RE.test(' inet addr:10.125.5.202 Bcast:10.125.15.255 Mask:255.255.240.0')); }); it('should MAC_RE pass', () => { assert(addressAll.MAC_RE.test(' ether c4:2c:03:32:d5:3d ')); assert(addressAll.MAC_RE.test('eth0 Link encap:Ethernet HWaddr 00:16:3E:00:0A:29 ')); }); }); describe('address()', () => { it('should return first ethernet addresses', done => { address((err, addr) => { assert(!err); assert.deepEqual(Object.keys(addr), [ 'ip', 'ipv6', 'mac' ]); addr.mac && assert.match(addr.mac, /^(?:[a-z0-9]{2}\:){5}[a-z0-9]{2}$/i); addr.ip && assert.match(addr.ip, /^\d+\.\d+\.\d+\.\d+$/); done(); }); }); it('should return first ethernet addresses from osx', done => { mm(os, 'networkInterfaces', () => { return { en1: [ { address: '192.168.2.104', family: 'IPv4' }, ], }; }); mm.data(child, 'exec', fs.readFileSync(path.join(fixtures, 'darwin.txt'), 'utf8')); address('en', (err, addr) => { assert(!err); assert.deepEqual(Object.keys(addr), [ 'ip', 'ipv6', 'mac' ]); assert.equal(addr.ip, '192.168.2.104'); assert.equal(addr.mac, '78:ca:39:b0:e6:7d'); done(); }); }); it('should return first ethernet addresses from linux', done => { mm(os, 'networkInterfaces', () => { return { eth: [ { address: '10.125.5.202', family: 'IPv4' }, ], }; }); mm.data(child, 'exec', fs.readFileSync(path.join(fixtures, 'linux.txt'), 'utf8')); address('eth', (err, addr) => { assert(!err); assert.deepEqual(Object.keys(addr), [ 'ip', 'ipv6', 'mac' ]); assert.equal(addr.ip, '10.125.5.202'); assert.equal(addr.mac, '00:16:3E:00:0A:29'); done(); }); }); it('should return first vnic interface addresses from osx', done => { mm(os, 'networkInterfaces', () => { return { vnic: [ { address: '10.211.55.2', family: 'IPv4' }, ], }; }); mm.data(child, 'exec', fs.readFileSync(path.join(fixtures, 'darwin.txt'), 'utf8')); address('vnic', (err, addr) => { assert(!err); assert.equal(addr.ip, '10.211.55.2'); assert(!addr.ipv6); done(); }); }); it('should return first local loopback addresses', done => { address('lo', (err, addr) => { assert(!err); assert.deepEqual(Object.keys(addr), [ 'ip', 'ipv6', 'mac' ]); assert.equal(addr.ip, '127.0.0.1'); done(); }); }); it('should return first local loopback addresses from linux', done => { mm.data(child, 'exec', fs.readFileSync(path.join(fixtures, 'linux.txt'), 'utf8')); address('lo', (err, addr) => { assert(!err); assert.deepEqual(Object.keys(addr), [ 'ip', 'ipv6', 'mac' ]); assert.equal(addr.ip, '127.0.0.1'); done(); }); }); }); describe('interface()', () => { it('should return interface with family', () => { // interface rename to getInterfaceAddress // const item = addressAll.interface(); const item = addressAll.getInterfaceAddress(); assert(item); assert(item.address); assert(item.family); }); }); describe('address.mac()', () => { it('should return mac', done => { addressAll.mac((err, mac) => { assert(!err); assert(mac); assert.match(mac, /(?:[a-z0-9]{2}\:){5}[a-z0-9]{2}/i); done(); }); }); it('should return mock mac address', done => { mm(os, 'networkInterfaces', () => { return { en1: [ { address: os.platform() === 'linux' ? '10.125.5.202' : '192.168.2.104', family: 'IPv4' }, ], eth1: [ { address: os.platform() === 'linux' ? '10.125.5.202' : '192.168.2.104', family: 'IPv4' }, ], }; }); mm.data(child, 'exec', fs.readFileSync(path.join(fixtures, os.platform() + '.txt'), 'utf8')); addressAll.mac(os.platform() === 'linux' ? 'eth' : 'en', (err, mac) => { assert(!err); assert(mac); assert.match(mac, /(?:[a-z0-9]{2}\:){5}[a-z0-9]{2}/i); done(); }); }); it('should return null when ip not exists', done => { mm(os, 'networkInterfaces', () => { return {}; }); addressAll.mac((err, mac) => { assert(!err); assert(!mac); done(); }); }); it('should return err when ifconfig cmd exec error', done => { mm(os, 'networkInterfaces', () => { return {}; }); mm.error(child, 'exec'); addressAll.mac((err, mac) => { assert(!err); assert(!mac); done(); }); }); it('should return mac mock win32', done => { mm(os, 'platform', () => { return 'win32'; }); mm(os, 'networkInterfaces', () => { return JSON.parse(fs.readFileSync(path.join(__dirname, './fixtures/win32_interfaces.json'), 'utf-8')); }); addressAll.mac((err, mac) => { assert(!err); assert(mac); assert.equal(mac, 'e8:2a:ea:8b:c2:20'); done(); }); }); }); describe('address.ip()', () => { it('should return 127.0.0.1', () => { assert.equal(addressAll.ip('lo'), '127.0.0.1'); }); it('should return the first not 127.0.0.1 interface', () => { mm(os, 'networkInterfaces', () => { return { lo: [{ address: '127.0.0.1', family: 'IPv4', internal: true }], bond0: [{ address: '10.206.52.79', family: 'IPv4', internal: false }] }; }); assert.equal(addressAll.ip(), '10.206.52.79'); }); it('should return utun1', () => { 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 }], 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:eeec:1722'); }); }); describe('address.dns()', () => { it('should return dns servers from osx', done => { mm.data(fs, 'readFile', fs.readFileSync(path.join(fixtures, 'dns_darwin.txt'), 'utf8')); addressAll.dns((err, servers) => { assert(!err); assert(servers); assert(Array.isArray(servers)); assert(servers.length > 0); done(); }); }); it('should return dns servers from linux', done => { mm.data(fs, 'readFile', fs.readFileSync(path.join(fixtures, 'dns_linux.txt'), 'utf8')); addressAll.dns((err, servers) => { assert(!err); assert(servers); assert(Array.isArray(servers)); assert(servers.length > 0); done(); }); }); it('should return err when fs error', done => { mm.error(fs, 'readFile'); addressAll.dns((err, servers) => { assert(err); assert(!servers); done(); }); }); }); }); address-2.0.1/test/fixtures/000077500000000000000000000000001450331053500157665ustar00rootroot00000000000000address-2.0.1/test/fixtures/darwin.txt000066400000000000000000000032141450331053500200130ustar00rootroot00000000000000lo0: flags=8049 mtu 16384 options=3 inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 inet 127.0.0.1 netmask 0xff000000 inet6 ::1 prefixlen 128 gif0: flags=8010 mtu 1280 stf0: flags=0<> mtu 1280 en0: flags=8863 mtu 1500 options=2b ether c4:2c:03:32:d5:3d media: autoselect (none) status: inactive en1: flags=8863 mtu 1500 ether 78:ca:39:b0:e6:7d inet6 fe80::7aca:39ff:feb0:e67d%en1 prefixlen 64 scopeid 0x5 inet 192.168.2.104 netmask 0xffffff00 broadcast 192.168.2.255 media: autoselect status: active p2p0: flags=8843 mtu 2304 ether 0a:ca:39:b0:e6:7d media: autoselect status: inactive fw0: flags=8863 mtu 4078 lladdr 60:33:4b:ff:fe:93:2a:62 media: autoselect status: inactive vnic0: flags=8843 mtu 1500 options=3 ether 00:1c:42:00:00:08 inet 10.211.55.2 netmask 0xffffff00 broadcast 10.211.55.255 media: autoselect status: active vnic1: flags=8843 mtu 1500 options=3 ether 00:1c:42:00:00:09 inet 10.37.129.2 netmask 0xffffff00 broadcast 10.37.129.255 media: autoselect status: active vboxnet0: flags=8843 mtu 1500 ether 0a:00:27:00:00:00 inet 192.168.56.1 netmask 0xffffff00 broadcast 192.168.56.255 address-2.0.1/test/fixtures/dns_darwin.txt000066400000000000000000000004251450331053500206600ustar00rootroot00000000000000# # Mac OS X Notice # # This file is not used by the host name and address resolution # or the DNS query routing mechanisms used by most processes on # this Mac OS X system. # # This file is automatically generated. # domain hz.ali.com nameserver 10.13.2.1 nameserver 10.13.2.6address-2.0.1/test/fixtures/dns_linux.txt000066400000000000000000000001421450331053500205270ustar00rootroot00000000000000search tbsite.net options attempts:1 timeout:2 nameserver 10.125.250.210 nameserver 172.24.102.254address-2.0.1/test/fixtures/linux.txt000066400000000000000000000015031450331053500176650ustar00rootroot00000000000000eth0 Link encap:Ethernet HWaddr 00:16:3E:00:0A:29 inet addr:10.125.5.202 Bcast:10.125.15.255 Mask:255.255.240.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:4034645 errors:0 dropped:0 overruns:0 frame:0 TX packets:1877492 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:926626551 (883.6 MiB) TX bytes:284125548 (270.9 MiB) Interrupt:14 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:17633 errors:0 dropped:0 overruns:0 frame:0 TX packets:17633 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:2496787 (2.3 MiB) TX bytes:2496787 (2.3 MiB)address-2.0.1/test/fixtures/linux_with_can.txt000066400000000000000000000024021450331053500215400ustar00rootroot00000000000000can0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 UP RUNNING NOARP MTU:16 Metric:1 RX packets:282 errors:0 dropped:0 overruns:0 frame:0 TX packets:2909 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:10 RX bytes:1975 (1.9 KiB) TX bytes:20083 (19.6 KiB) Interrupt:55 eth0 Link encap:Ethernet HWaddr 00:18:31:8A:41:C6 inet addr:10.42.193.19 Bcast:10.42.193.255 Mask:255.255.255.0 inet6 addr: fe80::218:31ff:fe8a:41c6/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:305126 errors:0 dropped:12547 overruns:0 frame:0 TX packets:49447 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:63758575 (60.8 MiB) TX bytes:4862291 (4.6 MiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:2404 errors:0 dropped:0 overruns:0 frame:0 TX packets:2404 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:230580 (225.1 KiB) TX bytes:230580 (225.1 KiB)address-2.0.1/test/fixtures/ts/000077500000000000000000000000001450331053500164145ustar00rootroot00000000000000address-2.0.1/test/fixtures/ts/test.ts000066400000000000000000000026641450331053500177530ustar00rootroot00000000000000import assert from 'node:assert'; import os from 'node:os'; import mm from 'mm'; import path from 'node:path'; import child from 'node:child_process'; import fs from 'node:fs'; import * as address from 'address'; mm(address, 'interface', () => ({ address: os.platform() === 'linux' ? '10.125.5.202' : '192.168.2.104' })); mm.data(child, 'exec', fs.readFileSync(path.join(__dirname, `../${os.platform() + '.txt'}`), 'utf8')); address.dns((err, servers) => { assert(!err); assert(Array.isArray(servers)); assert(servers.length > 0); }); address.dns('/etc/resolv.conf', (err, servers) => { assert(!err); assert(Array.isArray(servers)); assert(servers.length > 0); }); address.ip('utun'); address.ip(); address.ipv6(); address.ipv6('utun'); address.mac((err, mac) => { assert(!err); assert(mac); assert(mac.substring(0)); }); const interfaceName = (os.platform() === 'darwin') ? 'en' : 'eth'; address.mac(interfaceName, (err, mac) => { assert(!err); assert(mac); assert(mac.substring(0)); }); address.address(interfaceName, (err, addr) => { assert(!err); assert(addr.ip); assert(addr.ip.substring(0)); assert(addr.ipv6); assert(addr.ipv6.substring(0)); assert(addr.mac); assert(addr.mac.substring(0)); }); address.address((err, addr) => { assert(!err); assert(addr.ip); assert(addr.ip.substring(0)); assert(addr.ipv6); assert(addr.ipv6.substring(0)); assert(addr.mac); assert(addr.mac.substring(0)); }); address-2.0.1/test/fixtures/ts/tsconfig.json000066400000000000000000000004171450331053500211250ustar00rootroot00000000000000{ "extends": "@eggjs/tsconfig", "compilerOptions": { "target": "es5", "baseUrl": ".", "paths": { "address": [ "../../.." ] }, "module": "commonjs", "strict": true, "noImplicitAny": false, "declaration": false } } address-2.0.1/test/fixtures/win32.txt000066400000000000000000000050101450331053500174650ustar00rootroot00000000000000Windows IP Configuration Host Name . . . . . . . . . . . . : CJ-VII Primary Dns Suffix . . . . . . . : Node Type . . . . . . . . . . . . : Hybrid IP Routing Enabled. . . . . . . . : No WINS Proxy Enabled. . . . . . . . : No Ethernet adapter Local Area Connection 2: Media State . . . . . . . . . . . : Media disconnected Connection-specific DNS Suffix . : Description . . . . . . . . . . . : Anchorfree HSS Adapter Physical Address. . . . . . . . . : 00-FF-95-E2-1A-B2 DHCP Enabled. . . . . . . . . . . : Yes Autoconfiguration Enabled . . . . : Yes Ethernet adapter Local Area Connection: Connection-specific DNS Suffix . : Description . . . . . . . . . . . : NVIDIA nForce Networking Controller Physical Address. . . . . . . . . : F8-0F-41-13-79-EE DHCP Enabled. . . . . . . . . . . : Yes Autoconfiguration Enabled . . . . : Yes Link-local IPv6 Address . . . . . : fe80::f400:f7b2:ec8a:a958%12(Preferred) IPv4 Address. . . . . . . . . . . : 192.168.0.4(Preferred) Subnet Mask . . . . . . . . . . . : 255.255.255.0 Lease Obtained. . . . . . . . . . : Monday, 10 September, 2012 00:09:01 Lease Expires . . . . . . . . . . : Monday, 10 September, 2012 06:39:16 Default Gateway . . . . . . . . . : 192.168.0.1 DHCP Server . . . . . . . . . . . : 192.168.0.1 DHCPv6 IAID . . . . . . . . . . . : 184028993 DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-16-0A-83-DC-F8-0F-41-13-79-EE DNS Servers . . . . . . . . . . . : 65.32.5.111 65.32.5.112 NetBIOS over Tcpip. . . . . . . . : Enabled Tunnel adapter Teredo Tunneling Pseudo-Interface: Connection-specific DNS Suffix . : Description . . . . . . . . . . . : Teredo Tunneling Pseudo-Interface Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0 DHCP Enabled. . . . . . . . . . . : No Autoconfiguration Enabled . . . . : Yes IPv6 Address. . . . . . . . . . . : 2001:0:9d38:953c:43d:ac2:cda7:67b0(Preferred) Link-local IPv6 Address . . . . . : fe80::43d:ac2:cda7:67b0%14(Preferred) Default Gateway . . . . . . . . . : :: NetBIOS over Tcpip. . . . . . . . : Disabled Tunnel adapter isatap.{0B47D2C2-3636-4E51-B6DA-6EEF1042FFAC}: Media State . . . . . . . . . . . : Media disconnected Connection-specific DNS Suffix . : Description . . . . . . . . . . . : Microsoft ISATAP Adapter #3 Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0 DHCP Enabled. . . . . . . . . . . : No Autoconfiguration Enabled . . . . : Yesaddress-2.0.1/test/fixtures/win32_cn.txt000066400000000000000000000034461450331053500201600ustar00rootroot00000000000000 Windows IP 配置 无线局域网适配器 无线网络连接: 连接特定的 DNS 后缀 . . . . . . . : hz.ali.com 本地链接 IPv6 地址. . . . . . . . : fe80::8998:cb2d:9da9:8c5a%13 IPv4 地址 . . . . . . . . . . . . : 10.7.85.99 子网掩码 . . . . . . . . . . . . : 255.255.252.0 默认网关. . . . . . . . . . . . . : 10.7.84.1 以太网适配器 本地连接: 媒体状态 . . . . . . . . . . . . : 媒体已断开 连接特定的 DNS 后缀 . . . . . . . : taobao.ali.com 以太网适配器 VirtualBox Host-Only Network: 连接特定的 DNS 后缀 . . . . . . . : 本地链接 IPv6 地址. . . . . . . . : fe80::28ee:200e:3cb5:b433%18 IPv4 地址 . . . . . . . . . . . . : 192.168.56.1 子网掩码 . . . . . . . . . . . . : 255.255.255.0 默认网关. . . . . . . . . . . . . : 隧道适配器 Teredo Tunneling Pseudo-Interface: 媒体状态 . . . . . . . . . . . . : 媒体已断开 连接特定的 DNS 后缀 . . . . . . . : 隧道适配器 isatap.taobao.ali.com: 媒体状态 . . . . . . . . . . . . : 媒体已断开 连接特定的 DNS 后缀 . . . . . . . : 隧道适配器 isatap.{0E89351D-D9F0-4095-B876-BD3F17DC41EE}: 媒体状态 . . . . . . . . . . . . : 媒体已断开 连接特定的 DNS 后缀 . . . . . . . : 隧道适配器 isatap.hz.ali.com: 媒体状态 . . . . . . . . . . . . : 媒体已断开 连接特定的 DNS 后缀 . . . . . . . : hz.ali.com C:\Users\tangyao>getmac 物理地址 传输名称 =================== ========================================================== 5C-26-0A-84-A3-1B 媒体已断开连接 已禁用 已断开 74-DE-2B-1E-56-85 \Device\Tcpip_{E112BD43-B42F-483A-82DE-D57D4A7ED5E4} 08-00-27-00-C0-F3 \Device\Tcpip_{0E89351D-D9F0-4095-B876-BD3F17DC41EE} address-2.0.1/test/fixtures/win32_interfaces.json000066400000000000000000000014261450331053500220310ustar00rootroot00000000000000{ "无线网络连接": [ { "address": "fe80::f96c:4017:300f:e9ea", "netmask": "ffff:ffff:ffff:ffff::", "family": "IPv6", "mac": "e8:2a:ea:8b:c2:20", "scopeid": 14, "internal": false }, { "address": "10.15.230.231", "netmask": "255.255.254.0", "family": "IPv4", "mac": "e8:2a:ea:8b:c2:20", "internal": false } ], "Loopback Pseudo-Interface 1": [ { "address": "::1", "netmask": "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", "family": "IPv6", "mac": "00:00:00:00:00:00", "scopeid": 0, "internal": true }, { "address": "127.0.0.1", "netmask": "255.0.0.0", "family": "IPv4", "mac": "00:00:00:00:00:00", "internal": true } ] }address-2.0.1/test/promises.test.ts000066400000000000000000000010131450331053500172770ustar00rootroot00000000000000import { strict as assert } from 'node:assert'; import { address, mac, dns } from '../src/promises.js'; describe('test/promises.test.ts', () => { it('should address work', async () => { const addr = await address(); assert(addr.ip); assert(addr.ipv6); assert(addr.mac); }); it('should mac work', async () => { const addr = await mac(); assert(addr); }); it('should dns work', async () => { const servers = await dns(); assert(servers); assert(servers.length > 0); }); }); address-2.0.1/test/ts.test.ts000066400000000000000000000010141450331053500160650ustar00rootroot00000000000000import path from 'node:path'; import { fileURLToPath } from 'node:url'; import runscript from 'runscript'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); describe('test/ts.test.ts', () => { it('should works with ts without error', done => { const cwd = path.join(__dirname, './fixtures/ts'); const tsconfigPath = path.join(cwd, 'tsconfig.json'); runscript(`tsc -p ${tsconfigPath}`, { stdio: 'inherit' }) .then(() => done()) .catch(done); }); }); address-2.0.1/tsconfig.json000066400000000000000000000003031450331053500156410ustar00rootroot00000000000000{ "extends": "@eggjs/tsconfig", "compilerOptions": { "strict": true, "noImplicitAny": true, "target": "ES2022", "module": "NodeNext", "moduleResolution": "NodeNext" } }