From 4c230d26ad9c65db7701c34a9ab272789bea99b8 Mon Sep 17 00:00:00 2001 From: Igor Zaporozhets Date: Fri, 16 Nov 2018 14:48:07 +0200 Subject: [PATCH] remove usage of "ip" package --- index.js | 24 +++++++++++++++++++++++- package.json | 1 - 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index a9615b1..c285677 100644 --- a/index.js +++ b/index.js @@ -9,6 +9,28 @@ var name = 'static', log = require('runner-logger').wrap(name); +function getExternalIp ( family ) { + var os = require('os'), + interfaces = os.networkInterfaces(), + index = 0, + interfaceName, iface; + + for ( interfaceName in interfaces ) { + iface = interfaces[interfaceName]; + + index = 0; + while ( index < iface.length ) { + if ( iface[index].family === family && !iface[index].internal ) { + return iface[index].address; + } + ++index; + } + } + + return '0.0.0.0'; +} + + function start ( config, done ) { var files = new (require('node-static').Server)(config.path, config.staticOptions), server; @@ -39,7 +61,7 @@ function start ( config, done ) { // port can be 0 from the start config.port = server.address().port; - config.uri = 'http://' + require('ip').address() + ':' + config.port + '/' + config.open; + config.uri = 'http://' + getExternalIp('IPv4') + ':' + config.port + '/' + config.open; log.info('serving directory: ' + log.colors.bold(config.path)); log.info('web address: ' + log.colors.green.bold(config.uri)); diff --git a/package.json b/package.json index 45d91d8..c9456cb 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,6 @@ }, "dependencies": { "node-static": "^0.7.10", - "ip": "^1.1.5", "opn": "^5.3.0", "runner-logger": "^1.0.1" },