From 7d0aa33fc3c0d67376d6837c3073a78bcad10786 Mon Sep 17 00:00:00 2001 From: Kwabena N Amponsah Date: Fri, 12 Jul 2024 12:38:40 +0100 Subject: [PATCH] Remove insecure ip package --- package-lock.json | 7 ------- package.json | 1 - server.js | 19 +++++++++++++++++-- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/package-lock.json b/package-lock.json index ef54774..7bf9385 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,6 @@ "version": "2.0.0", "dependencies": { "chokidar": "^3.6.0", - "ip": "^2.0.1", "tiny-json-http": "^7.5.1", "uuid": "^10.0.0" } @@ -113,12 +112,6 @@ "node": ">= 6" } }, - "node_modules/ip": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/ip/-/ip-2.0.1.tgz", - "integrity": "sha512-lJUL9imLTNi1ZfXT+DU6rBBdbiKGBuay9B6xGSPVjUeQwaH1RIGqef8RZkUtHioLmSNpPR5M4HVKJGm1j8FWVQ==", - "license": "MIT" - }, "node_modules/is-binary-path": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/is-binary-path/-/is-binary-path-2.1.0.tgz", diff --git a/package.json b/package.json index 14178e2..03cc164 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,6 @@ "version": "2.0.0", "dependencies": { "chokidar": "^3.6.0", - "ip": "^2.0.1", "tiny-json-http": "^7.5.1", "uuid": "^10.0.0" } diff --git a/server.js b/server.js index 9587958..834e649 100644 --- a/server.js +++ b/server.js @@ -12,7 +12,7 @@ const { v4: uuidv4 } = require('uuid'); const chokidar = require('chokidar'); const tiny = require('tiny-json-http'); -const ip = require('ip'); +const os = require('os'); // These directory names are also referenced in other scripts! const DIR_APPREDICT_RESULTS = concatenator([ __dirname, 'res'], false); @@ -846,6 +846,21 @@ function write_stderr(std_file_prefix, stderr_file, stop_file, messages) { }); } +/** + * Return the first non-internal IPv4 address, or the loopback address if none. + */ +function ip_address() { + const interfaces = os.networkInterfaces(); + for (const networks of Object.values(interfaces)) { + for (const network of networks) { + if (network.family === 'IPv4' && !network.internal) { + return network.address; + } + } + } + return '127.0.0.1'; +} + /******************************************************************************/ if (typeof process.env.REST_API_URL_DATA !== 'undefined' && process.env.REST_API_URL_DATA != '') { @@ -946,7 +961,7 @@ const server = http.createServer((request, response) => { return_obj = { 'success': { 'id': simulation_id, - 'ip': ip.address() + 'ip': ip_address() } };