From 953561daf1c46117266065bc6487a4f5b5516c6b Mon Sep 17 00:00:00 2001 From: hyj1991 Date: Wed, 11 Dec 2024 12:08:33 +0800 Subject: [PATCH] Revert "feat: remove address (#59)" This reverts commit 2a4bf4fbd1e95f32ccdb20158fb1863266d4cd85. --- .autod.conf.js | 1 + common/utils.js | 22 ++-------------------- package.json | 1 + test/fixtures/files/package-lock.json | 6 ++++++ test/fixtures/files/package.json | 1 + test/fixtures/files/test_error | 0 6 files changed, 11 insertions(+), 20 deletions(-) delete mode 100644 test/fixtures/files/test_error diff --git a/.autod.conf.js b/.autod.conf.js index 2a86e06..c5861d4 100644 --- a/.autod.conf.js +++ b/.autod.conf.js @@ -7,6 +7,7 @@ module.exports = { 'test', ], dep: [ + 'address', 'formstream', 'moment', 'nounou', diff --git a/common/utils.js b/common/utils.js index 4106516..bd15437 100644 --- a/common/utils.js +++ b/common/utils.js @@ -1,29 +1,11 @@ 'use strict'; const os = require('os'); +const address = require('address'); const crypto = require('crypto'); const { promisify } = require('util'); const path = require('path'); -exports.getIp = function getIp() { - const interfaces = os.networkInterfaces(); - for (const interfaceItems of Object.values(interfaces)) { - for (const item of interfaceItems) { - // skip local network ip - if (item.address.startsWith('10.') || - item.address.startsWith('172.16.') || - item.address.startsWith('192.168.') || - item.address.startsWith('169.254.')) { - continue; - } - if (item.family === 'IPv4' && !item.internal) { - return item.address; - } - } - } - return '127.0.0.1'; -}; - exports.regularWsServer = function(server) { return server.startsWith('ws://') || server.startsWith('wss://'); }; @@ -40,7 +22,7 @@ exports.getAgentId = function(customAgent, ipMode) { if (!ipMode) { return `${os.hostname()}`; } - return `${exports.getIp()}::${os.hostname()}`; + return `${address.ip()}::${os.hostname()}`; }; exports.sign = function(message, secret) { diff --git a/package.json b/package.json index cbbd467..12aff90 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ }, "homepage": "https://github.com/X-Profiler/xtransit#readme", "dependencies": { + "address": "^1.1.2", "form-data": "^4.0.0", "moment": "^2.28.0", "nounou": "^1.2.1", diff --git a/test/fixtures/files/package-lock.json b/test/fixtures/files/package-lock.json index 7fa8383..c381cb3 100644 --- a/test/fixtures/files/package-lock.json +++ b/test/fixtures/files/package-lock.json @@ -551,6 +551,11 @@ "integrity": "sha1-KUrbcbVzmLBoABXwo4xWPuHbU4Q=", "dev": true }, + "address": { + "version": "1.1.2", + "resolved": "https://registry.npm.taobao.org/address/download/address-1.1.2.tgz", + "integrity": "sha1-vxEWycdYxRt6kz0pa3LCIe2UKLY=" + }, "agent-base": { "version": "4.3.0", "resolved": "https://registry.npm.taobao.org/agent-base/download/agent-base-4.3.0.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fagent-base%2Fdownload%2Fagent-base-4.3.0.tgz", @@ -2536,6 +2541,7 @@ "resolved": "https://registry.npm.taobao.org/utility/download/utility-0.1.11.tgz?cache=0&sync_timestamp=1574243892588&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Futility%2Fdownload%2Futility-0.1.11.tgz", "integrity": "sha1-/eYM+bTkdRlHoM9dEEzik2ciZxU=", "requires": { + "address": ">=0.0.1" } } } diff --git a/test/fixtures/files/package.json b/test/fixtures/files/package.json index 99d80ca..b059fcd 100644 --- a/test/fixtures/files/package.json +++ b/test/fixtures/files/package.json @@ -40,6 +40,7 @@ }, "homepage": "https://github.com/X-Profiler/xtransit#readme", "dependencies": { + "address": "^1.1.2", "formstream": "^1.1.0", "moment": "^2.26.0", "nounou": "^1.2.1", diff --git a/test/fixtures/files/test_error b/test/fixtures/files/test_error deleted file mode 100644 index e69de29..0000000