From 42de818a2ffd2ce3938604f186742ad6548ad42f Mon Sep 17 00:00:00 2001 From: Tobias Hopp Date: Mon, 13 Sep 2021 21:52:08 +0200 Subject: [PATCH] Update Took 12 minutes --- index.js | 11 +- node_modules/.bin/acorn | 13 +- node_modules/.bin/acorn.cmd | 17 ++ node_modules/.bin/acorn.ps1 | 28 +++ node_modules/.bin/mime | 13 +- node_modules/.bin/mime.cmd | 17 ++ node_modules/.bin/mime.ps1 | 28 +++ node_modules/.bin/parser | 13 +- node_modules/.bin/parser.cmd | 17 ++ node_modules/.bin/parser.ps1 | 28 +++ node_modules/.package-lock.json | 19 ++ node_modules/arpping/LICENSE | 21 ++ node_modules/arpping/README.md | 249 ++++++++++++++++++++++++ node_modules/arpping/index.js | 222 +++++++++++++++++++++ node_modules/arpping/macLookup.js | 55 ++++++ node_modules/arpping/package.json | 28 +++ node_modules/arpping/test.js | 63 ++++++ node_modules/arpping/test_class.js | 28 +++ node_modules/child_process/README.md | 9 + node_modules/child_process/package.json | 20 ++ node_modules/mime/package.json | 55 ++---- node_modules/os/README.md | 11 ++ node_modules/os/index.js | 1 + node_modules/os/package.json | 25 +++ package-lock.json | 39 ++++ package.json | 1 + 26 files changed, 977 insertions(+), 54 deletions(-) create mode 100644 node_modules/.bin/acorn.cmd create mode 100644 node_modules/.bin/acorn.ps1 create mode 100644 node_modules/.bin/mime.cmd create mode 100644 node_modules/.bin/mime.ps1 create mode 100644 node_modules/.bin/parser.cmd create mode 100644 node_modules/.bin/parser.ps1 create mode 100644 node_modules/arpping/LICENSE create mode 100644 node_modules/arpping/README.md create mode 100644 node_modules/arpping/index.js create mode 100644 node_modules/arpping/macLookup.js create mode 100644 node_modules/arpping/package.json create mode 100644 node_modules/arpping/test.js create mode 100644 node_modules/arpping/test_class.js create mode 100644 node_modules/child_process/README.md create mode 100644 node_modules/child_process/package.json create mode 100644 node_modules/os/README.md create mode 100644 node_modules/os/index.js create mode 100644 node_modules/os/package.json diff --git a/index.js b/index.js index 712ab62..92e1380 100755 --- a/index.js +++ b/index.js @@ -5,6 +5,8 @@ const fs = require("fs"); const readline = require("readline"); const ping = require('ping'); const ldevices = require("local-devices"); +const Arpping = require('arpping'); +const arpping = new Arpping(options); const app = Express(); const port = 3333; @@ -112,15 +114,6 @@ app.get( '/api/getDevices', async ( req, res ) => { thisData.macAddress = line.substr( 0, line.indexOf( ' ' ) ); thisData.ipAddress = line.substr( thisData.macAddress.length +1, line.substr( thisData.macAddress.length+1 ).indexOf( ' ') ); thisData.hostname = line.substr( thisData.macAddress.length + thisData.ipAddress.length +2, line.length-1 ); - /* if( thisData.hostname == 'unknown' || thisData.hostname.startsWith( '*' ) || thisData.hostname == '' ) - { - let device = await ldevices(req.params.ipAddress); - if( !device ) - { - thisData.hostname = device.name; - } - }*/ - //console.log( nline ); data.push( thisData ); } diff --git a/node_modules/.bin/acorn b/node_modules/.bin/acorn index cf76760..46a3e61 120000 --- a/node_modules/.bin/acorn +++ b/node_modules/.bin/acorn @@ -1 +1,12 @@ -../acorn/bin/acorn \ No newline at end of file +#!/bin/sh +basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')") + +case `uname` in + *CYGWIN*|*MINGW*|*MSYS*) basedir=`cygpath -w "$basedir"`;; +esac + +if [ -x "$basedir/node" ]; then + exec "$basedir/node" "$basedir/../acorn/bin/acorn" "$@" +else + exec node "$basedir/../acorn/bin/acorn" "$@" +fi diff --git a/node_modules/.bin/acorn.cmd b/node_modules/.bin/acorn.cmd new file mode 100644 index 0000000..a9324df --- /dev/null +++ b/node_modules/.bin/acorn.cmd @@ -0,0 +1,17 @@ +@ECHO off +GOTO start +:find_dp0 +SET dp0=%~dp0 +EXIT /b +:start +SETLOCAL +CALL :find_dp0 + +IF EXIST "%dp0%\node.exe" ( + SET "_prog=%dp0%\node.exe" +) ELSE ( + SET "_prog=node" + SET PATHEXT=%PATHEXT:;.JS;=;% +) + +endLocal & goto #_undefined_# 2>NUL || title %COMSPEC% & "%_prog%" "%dp0%\..\acorn\bin\acorn" %* diff --git a/node_modules/.bin/acorn.ps1 b/node_modules/.bin/acorn.ps1 new file mode 100644 index 0000000..6f6dcdd --- /dev/null +++ b/node_modules/.bin/acorn.ps1 @@ -0,0 +1,28 @@ +#!/usr/bin/env pwsh +$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent + +$exe="" +if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) { + # Fix case when both the Windows and Linux builds of Node + # are installed in the same directory + $exe=".exe" +} +$ret=0 +if (Test-Path "$basedir/node$exe") { + # Support pipeline input + if ($MyInvocation.ExpectingInput) { + $input | & "$basedir/node$exe" "$basedir/../acorn/bin/acorn" $args + } else { + & "$basedir/node$exe" "$basedir/../acorn/bin/acorn" $args + } + $ret=$LASTEXITCODE +} else { + # Support pipeline input + if ($MyInvocation.ExpectingInput) { + $input | & "node$exe" "$basedir/../acorn/bin/acorn" $args + } else { + & "node$exe" "$basedir/../acorn/bin/acorn" $args + } + $ret=$LASTEXITCODE +} +exit $ret diff --git a/node_modules/.bin/mime b/node_modules/.bin/mime index fbb7ee0..0a62a1b 120000 --- a/node_modules/.bin/mime +++ b/node_modules/.bin/mime @@ -1 +1,12 @@ -../mime/cli.js \ No newline at end of file +#!/bin/sh +basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')") + +case `uname` in + *CYGWIN*|*MINGW*|*MSYS*) basedir=`cygpath -w "$basedir"`;; +esac + +if [ -x "$basedir/node" ]; then + exec "$basedir/node" "$basedir/../mime/cli.js" "$@" +else + exec node "$basedir/../mime/cli.js" "$@" +fi diff --git a/node_modules/.bin/mime.cmd b/node_modules/.bin/mime.cmd new file mode 100644 index 0000000..54491f1 --- /dev/null +++ b/node_modules/.bin/mime.cmd @@ -0,0 +1,17 @@ +@ECHO off +GOTO start +:find_dp0 +SET dp0=%~dp0 +EXIT /b +:start +SETLOCAL +CALL :find_dp0 + +IF EXIST "%dp0%\node.exe" ( + SET "_prog=%dp0%\node.exe" +) ELSE ( + SET "_prog=node" + SET PATHEXT=%PATHEXT:;.JS;=;% +) + +endLocal & goto #_undefined_# 2>NUL || title %COMSPEC% & "%_prog%" "%dp0%\..\mime\cli.js" %* diff --git a/node_modules/.bin/mime.ps1 b/node_modules/.bin/mime.ps1 new file mode 100644 index 0000000..2222f40 --- /dev/null +++ b/node_modules/.bin/mime.ps1 @@ -0,0 +1,28 @@ +#!/usr/bin/env pwsh +$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent + +$exe="" +if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) { + # Fix case when both the Windows and Linux builds of Node + # are installed in the same directory + $exe=".exe" +} +$ret=0 +if (Test-Path "$basedir/node$exe") { + # Support pipeline input + if ($MyInvocation.ExpectingInput) { + $input | & "$basedir/node$exe" "$basedir/../mime/cli.js" $args + } else { + & "$basedir/node$exe" "$basedir/../mime/cli.js" $args + } + $ret=$LASTEXITCODE +} else { + # Support pipeline input + if ($MyInvocation.ExpectingInput) { + $input | & "node$exe" "$basedir/../mime/cli.js" $args + } else { + & "node$exe" "$basedir/../mime/cli.js" $args + } + $ret=$LASTEXITCODE +} +exit $ret diff --git a/node_modules/.bin/parser b/node_modules/.bin/parser index ce7bf97..cb5b10d 120000 --- a/node_modules/.bin/parser +++ b/node_modules/.bin/parser @@ -1 +1,12 @@ -../@babel/parser/bin/babel-parser.js \ No newline at end of file +#!/bin/sh +basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')") + +case `uname` in + *CYGWIN*|*MINGW*|*MSYS*) basedir=`cygpath -w "$basedir"`;; +esac + +if [ -x "$basedir/node" ]; then + exec "$basedir/node" "$basedir/../@babel/parser/bin/babel-parser.js" "$@" +else + exec node "$basedir/../@babel/parser/bin/babel-parser.js" "$@" +fi diff --git a/node_modules/.bin/parser.cmd b/node_modules/.bin/parser.cmd new file mode 100644 index 0000000..1ad5c81 --- /dev/null +++ b/node_modules/.bin/parser.cmd @@ -0,0 +1,17 @@ +@ECHO off +GOTO start +:find_dp0 +SET dp0=%~dp0 +EXIT /b +:start +SETLOCAL +CALL :find_dp0 + +IF EXIST "%dp0%\node.exe" ( + SET "_prog=%dp0%\node.exe" +) ELSE ( + SET "_prog=node" + SET PATHEXT=%PATHEXT:;.JS;=;% +) + +endLocal & goto #_undefined_# 2>NUL || title %COMSPEC% & "%_prog%" "%dp0%\..\@babel\parser\bin\babel-parser.js" %* diff --git a/node_modules/.bin/parser.ps1 b/node_modules/.bin/parser.ps1 new file mode 100644 index 0000000..8926517 --- /dev/null +++ b/node_modules/.bin/parser.ps1 @@ -0,0 +1,28 @@ +#!/usr/bin/env pwsh +$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent + +$exe="" +if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) { + # Fix case when both the Windows and Linux builds of Node + # are installed in the same directory + $exe=".exe" +} +$ret=0 +if (Test-Path "$basedir/node$exe") { + # Support pipeline input + if ($MyInvocation.ExpectingInput) { + $input | & "$basedir/node$exe" "$basedir/../@babel/parser/bin/babel-parser.js" $args + } else { + & "$basedir/node$exe" "$basedir/../@babel/parser/bin/babel-parser.js" $args + } + $ret=$LASTEXITCODE +} else { + # Support pipeline input + if ($MyInvocation.ExpectingInput) { + $input | & "node$exe" "$basedir/../@babel/parser/bin/babel-parser.js" $args + } else { + & "node$exe" "$basedir/../@babel/parser/bin/babel-parser.js" $args + } + $ret=$LASTEXITCODE +} +exit $ret diff --git a/node_modules/.package-lock.json b/node_modules/.package-lock.json index ebbf4b5..1e0f320 100644 --- a/node_modules/.package-lock.json +++ b/node_modules/.package-lock.json @@ -63,6 +63,15 @@ "resolved": "https://registry.npmjs.org/any-promise/-/any-promise-1.3.0.tgz", "integrity": "sha1-q8av7tzqUugJzcA3au0845Y10X8=" }, + "node_modules/arpping": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/arpping/-/arpping-2.0.0.tgz", + "integrity": "sha512-+bkS6pf3vvYRfC/zDKKP9saFw4u73i7JgD75s37LU8i/OffAFhrIvQaKTzv0x1/P0JqYho/97yHD8b8jkhT78w==", + "dependencies": { + "child_process": "^1.0.2", + "os": "^0.1.1" + } + }, "node_modules/array-flatten": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", @@ -137,6 +146,11 @@ "is-regex": "^1.0.3" } }, + "node_modules/child_process": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/child_process/-/child_process-1.0.2.tgz", + "integrity": "sha1-sffn/HPSXn/R1FWtyU4UODAYK1o=" + }, "node_modules/cidr-regex": { "version": "1.0.7", "resolved": "https://registry.npmjs.org/cidr-regex/-/cidr-regex-1.0.7.tgz", @@ -623,6 +637,11 @@ "node": ">= 0.8" } }, + "node_modules/os": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/os/-/os-0.1.2.tgz", + "integrity": "sha512-ZoXJkvAnljwvc56MbvhtKVWmSkzV712k42Is2mA0+0KTSRakq5XXuXpjZjgAt9ctzl51ojhQWakQQpmOvXWfjQ==" + }, "node_modules/parseurl": { "version": "1.3.3", "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", diff --git a/node_modules/arpping/LICENSE b/node_modules/arpping/LICENSE new file mode 100644 index 0000000..7dbc982 --- /dev/null +++ b/node_modules/arpping/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018 haf-decent + +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. diff --git a/node_modules/arpping/README.md b/node_modules/arpping/README.md new file mode 100644 index 0000000..5f6a7a0 --- /dev/null +++ b/node_modules/arpping/README.md @@ -0,0 +1,249 @@ +# arpping +Discover and search for internet-connected devices (locally) using ping and arp + +## Motivation +I was trying to find a quick and reliable way to ping and discover devices connected to my LAN. I tried out: + +* [node-nmap](https://www.npmjs.com/package/node-nmap) +* [libnmap](https://www.npmjs.com/package/libnmap) +* [node-arp](https://www.npmjs.com/package/node-arp) + +But both node-nmap and libnmap were slow and unreliable, and node-arp only had part of the functionality I needed, so I decided to make my own. This is the result, and it's been pretty helpful so far. + +## Installation +Using npm: + +`$ npm install -save arpping` + +## Usage +To include in a project file: + +```javascript +const Arpping = require('arpping'); +var arpping = new Arpping(options); +``` + +The arpping module returns a function that accepts an optional `options` object. Valid parameters include: + +|Parameter |Default |Description | +|-----------|---------|-------------| +| timeout | 5 | The time, in seconds, ping will try to send packets to a device before returning results | +| includeEndpoints | false | Specify if you'd like to include range endpoints (1, 255) in your scans | +| useCache | true | Specify if you'd like to temporarily cache results for quicker and convenient usage | +| cacheTimeout | 3600 | Specify the cache's TTL (time to live) in seconds | + +### Properties +Each parameter can be changed dynamically after initialization. In addition, the IP address of the host device is also available as the `myIP` property (once it is found) + +### Methods +The Arpping object has the following Promise-based methods (with properly structured Promise chains): + +#### findMyInfo +The findMyInfo method returns the ip, mac address, and mac type (if known) of the computer running the script (which is stored and used to get the LAN network ip range used in other methods) +```javascript +const Arpping = require('arpping'); +var arpping = new Arpping(options); + +arpping.findMyInfo() + .then(info => console.log(info)) // ex. {"ip": "192.168.0.20", "mac": "01:23:45:67:89:01", "type": "RaspberryPi"} + .catch(err => console.log(err)); +``` + +#### discover +The discover method returns an array of hosts found on the local network. Each host entry contains the host's ip and mac address, and can also be assigned a type based on its mac address VendorID. The host entry that represents the computer running the script will have a "isHostDevice" key set with a value of true. By default, the discover scan will reference the host device's IP address to dictate the target range, but you can manually override this by passing a valid IP address. +```javascript +const Arpping = require('arpping'); +var arpping = new Arpping(options); + +arpping.discover() + .then(hosts => console.log(JSON.stringify(hosts, null, 4))) + .catch(err => console.log(err)); + +/* Example output +[ + { + "ip": "192.168.0.3", + "mac": "01:01:01:01:01:01" + }, + { + "ip": "192.168.0.12", + "mac": "99:01:99:01:99:01" + }, + { + "ip": "192.168.0.20", + "mac": "01:23:45:67:89:01", + "type": "RaspberryPi", + "isYourDevice": true + } +] +*/ +``` + +#### search +The search functionality is broken up into three methods. For each, you may pass a reference IP address as the second argument to override the default behavior. + +###### searchByIpAddress +Searching by ip address runs a discovery scan and filters the result based on an input array of desired ip addresses. The Promise resolves an object with an array of found `hosts` and an array of `missing` ips. +```javascript +const Arpping = require('arpping'); +var arpping = new Arpping(options); + +var ipArray = [ + "192.168.0.3", + "192.168.0.12", + "192.168.0.24" +]; +arpping.searchByIpAddress(ipArray, '192.168.0.1') + .then(({ hosts, missing }) => { + var h = hosts.length, m = missing.length; + console.log(`${h} out of ${h + m} host(s) found:\n${JSON.stringify(hosts, null, 4)}`); + console.log(`${m} out of ${h + m} host(s) not found:\n${missing}`); + }) + .catch(err => console.log(err)); + +/* Example output +2 out of 3 host(s) found: +[ + { + "ip": "192.168.0.3", + "mac": "01:01:01:01:01:01" + }, + { + "ip": "192.168.0.12", + "mac": "01:01:01:99:99:99" + } +] +1 out of 3 host(s) not found: +["192.168.0.24"] +*/ +``` + +###### searchByMacAddress +Searching by mac address functions similarly to the byIpAddress method, with the notable additional ability to search by partial mac addresses (i.e. "01:23:45:67:89:10" which only matches one device vs "01:23:45" which may match multiple devices). Each device found will have a "matched" array specifying the corresponding searched mac address(es). Again, the Promise resolves an object with an array of found `hosts` and an array of `missing` search terms. +```javascript +const Arpping = require('arpping'); +var arpping = new Arpping(options); + +var macArray = [ + "01:01:01", + "01:01:01:99:99:99", + "7f:54:12" +]; +arpping.searchByMacAddress(macArray) + .then(({ hosts, missing }) => { + var h = hosts.length, m = missing.length; + console.log(`${h} matching host(s) found:\n${JSON.stringify(hosts, null, 4)}`); + console.log(`The following search term(s) returned no results:\n${missing}`); + }) + .catch(err => console.log(err)); + +/* Example output +2 matching host(s) found: +[ + { + "ip": "192.168.0.3", + "mac": "01:01:01:01:01:01", + "matched": [ + "01:01:01" + ] + }, + { + "ip": "192.168.0.12", + "mac": "01:01:01:99:99:99", + "matched": [ + "01:01:01", + "01:01:01:99:99:99" + ] + } +] +The following search term(s) returned no results: +["7f:54:12"] +*/ +``` + +###### searchByMacType +Searching by mac type returns all devices that are assigned the specified mac type/vendor (note: my mac address lookup table is painfully sparse) +```javascript +const Arpping = require('arpping'); +var arpping = new Arpping(options); + +var type = "RaspberryPi"; +arpping.searchByMacType(type) + .then(hosts => console.log(`${hosts.length} host(s) found with type: ${type}\n${JSON.stringify(hosts, null, 4)}`)) + .catch(err => console.log(err)); + +/* Example output +1 host(s) found with type: RaspberryPi +[ + { + "ip": "192.168.0.20", + "mac": "01:23:45:67:89:01", + "type": "RaspberryPi", + "isYourDevice": true + } +] +*/ +``` + +#### ping +The ping method pings a given array of ip addresses (or the full ip range) and returns an array of `hosts` that respond as well as an array of those `missing` hosts that do not +```javascript +const Arpping = require('arpping'); +var arpping = new Arpping(options); + +var ipArray = null; // set to null to scan the full ip range (xxx.xxx.x.2 - 254); +arpping.ping(ipArray) + .then(({ hosts, missing }) => console.log(`${hosts.length} host(s) found:\n${hosts}`)) + .catch(err => console.log(err)); + +/* Example output +3 host(s) found: +["192.168.0.3", "192.168.0.12", "192.168.0.20"] +*/ +``` + +#### arp +The arp method arps a given array of ip addresses and returns an array of `hosts` that respond as well as an array of `missing` hosts that do not +```javascript +const Arpping = require('arpping'); +var arpping = new Arpping(options); + +// must specify an array, unlike ping +var ipArray = [ + "192.168.0.3", + "192.168.0.12", + "192.168.0.24" +]; +arpping.ping(ipArray) + .then(({ hosts, missing }) => { + console.log(`${hosts.length} matching host(s) found:\n${JSON.stringify(hosts, null, 4)}`); + console.log(`The following ip address(es) returned no results:\n${missing}`) + }) + .catch(err => console.log(err)); + +/* Example output +2 host(s) found: +[ + { + "ip": "192.168.0.3", + "mac": "01:01:01:01:01:01" + }, + { + "ip": "192.168.0.12", + "mac": "01:01:01:99:99:99" + } +] +The following ip address(es) returned no results: +["192.168.0.24"] +*/ +``` + +## Updates +1. Build out vendorID lookup table, or find some third-party version to include in project +2. ~~Allow for more customization - custom ip ranges to scan, enable/disable scanning of xxx.xxx.x.1,255, etc.~~ +3. Other stuff I haven't thought of yet +4. ??? +5. Profit + +## Contributing +I made this module on my own. Any help/feedback is appreciated. diff --git a/node_modules/arpping/index.js b/node_modules/arpping/index.js new file mode 100644 index 0000000..78b3d14 --- /dev/null +++ b/node_modules/arpping/index.js @@ -0,0 +1,222 @@ +'use strict'; + +const os = require('os'); +const { exec } = require('child_process'); + +const macLookup = require('./macLookup.js'); + +var flag, + ipCommand, + osType = os.type(); + +switch(osType) { + case 'Windows_NT': + flag = '-w'; + ipCommand = 'ipconfig'; + break; + case 'Linux': + flag = '-w'; + ipCommand = 'ifconfig'; + break; + case 'Darwin': + flag = '-t'; + ipCommand = 'ifconfig'; + break; + default: + throw new Error(`Unsupported OS: ${osType}`); +} + +function Arpping({ timeout = 5, includeEndpoints = false, useCache = true, cacheTimeout = 3600 } = {}) { + if (timeout < 1 || timeout > 60) throw new Error(`Invalid timeout parameter: ${timeout}. Timeout should be between 1 and 60.`); + this.timeout = parseInt(timeout) || timeout.toFixed(0); + + this.includeEndpoints = includeEndpoints; + this.myIP = null; + + this.useCache = useCache; + this.cache = []; + this.cacheTimeout = cacheTimeout; + this.cacheUpdate = 0; +} + +/** +* Build array of full ip range (xxx.xxx.x.1-255) given example ip address +* @param {String} ip +*/ +Arpping.prototype._getFullRange = function(ip) { + // don't use default assignment so false-y values are overwritten + ip = ip || this.myIP; + var ipStart = ip.substr(0, ip.lastIndexOf('.') + 1); + return this.includeEndpoints ? + Array.from({ length: 255 }, (_, i) => ipStart + (i + 1)): + Array.from({ length: 253 }, (_, i) => ipStart + (i + 2)); +} + +/** +* Find ip and mac addresses of host device +*/ +Arpping.prototype.findMyInfo = function() { + return new Promise((resolve, reject) => { + exec(ipCommand, (err, stdout, stderr) => { + if (err) return reject(err); + + var output = null; + if (osType == 'Linux') { + if (stdout.indexOf('wlan0') == -1) return reject(new Error('No wifi connection')); + output = stdout.split('wlan0')[1]; + } + else { + output = stdout.slice(stdout.indexOf('en0')); + output = output.slice(0, output.indexOf('active\n')) + 'active'; + if (output.split('status: ')[1] == 'inactive') return reject(new Error('No wifi connection')); + } + var ip = output.slice(output.indexOf('inet ') + 5, output.indexOf(' netmask')).trim(); + var mac = output.slice(output.indexOf('ether ')).split('\n')[0].split(' ')[1].trim(); + var type = macLookup(mac); + + this.myIP = ip; + return resolve(type ? { ip, mac, type }: { ip, mac }); + }); + }); +} + +/** +* Discover all hosts connected to your local network or based on a reference IP address +* @param {String} refIP +* @param {Boolean} retry +*/ +Arpping.prototype.discover = function(refIP, retry = true) { + if (this.useCache && this.cache.length && Date.now() - this.cacheUpdate < this.cacheTimeout * 1000) { + return new Promise((resolve, reject) => resolve(this.cache)); + } + if (!refIP && !this.myIP) { + if (retry) return this.findMyInfo().then(info => this.discover(info.ip, false)); + return new Promise((resolve, reject) => reject(new Error('Failed to find host IP address'))); + } + + var range = this._getFullRange(refIP); + return this.ping(range).then(({ hosts }) => this.arp(hosts)).then(({ hosts }) => { + this.cache = hosts.slice(0); + this.cacheUpdate = Date.now(); + return hosts; + }); +} + +/** +* Ping a range of ip addresses +* @param {Array} range +*/ +Arpping.prototype.ping = function(range) { + if (!(Array.isArray(range) && range.length)) { + if (!this.myIP) return this.findMyInfo().then(() => this.ping(range)); + range = this._getFullRange(); + } + + return new Promise((resolve, reject) => { + var hosts = [], + missing =[], + checked = 0; + range.forEach(ip => { + exec(`ping ${flag} ${this.timeout} ${ip}`, (err, stdout, stderr) => { + checked++; + if (err || stdout.indexOf('100% packet loss') > -1) missing.push(ip); + else hosts.push(ip); + + if (checked == range.length) resolve({ hosts, missing }); + }); + }); + }); +} + +/** +* Arp a range of ip addresses +* @param {Array} range +*/ +Arpping.prototype.arp = function(range) { + return new Promise((resolve, reject) => { + if (typeof range == 'string') range = [ range ]; + else if (!Array.isArray(range)) return reject(new Error('range must be an array of IP addresses')); + else if (!range.length) return resolve({ hosts: [], missing: [] }); + + var hosts = [], + missing = [], + checked = 0; + range.forEach(ip => { + exec(`arp ${ip}`, (err, stdout, stderr) => { + checked++; + if (err || stdout.indexOf('no entry') > -1) missing.push(ip); + else { + var mac = (osType == 'Linux') ? + stdout.split('\n')[1].replace(/ +/g, ' ').split(' ')[2]: + stdout.split(' ')[3]; + if (mac.includes('incomplete')) missing.push(ip); + else { + var host = { ip, mac }; + var type = macLookup(mac); + if (type) host.type = type; + if (ip == this.myIP) host.isHostDevice = true; + hosts.push(host); + } + } + + if (checked == range.length) resolve({ hosts, missing }); + }); + }); + }); +} + +/** +* Search for one or multiple IP addresses +* @param {String/Array} ipArray +* @param {String} refIP +*/ +Arpping.prototype.searchByIpAddress = function(ipArray, refIP) { + if (typeof ipArray === 'string') ipArray = [ ipArray ]; + else if (!Array.isArray(ipArray) || !ipArray.length) { + return new Promise((resolve, reject) => reject(new Error(`Invalid ipArray: ${ipArray}. Search input should be one ip address string or an array of ip strings.`))); + } + + return this.discover(refIP || ipArray[0]).then(hosts => { + var hostIPs = hosts.map(h => h.ip); + return { + hosts: hosts.filter(h => ipArray.includes(h.ip)), + missing: ipArray.filter(ip => !hostIPs.includes(ip)) + } + }); +} + +/** +* Search for one or multiple, full or partial mac addresses +* @param {String/Array} macArray +* @param {String} refIP +*/ +Arpping.prototype.searchByMacAddress = function(macArray, refIP) { + if (typeof macArray == 'string') macArray = [ macArray ]; + else if (!Array.isArray(macArray) || !macArray.length) { + return new Promise((resolve, reject) => reject(`Invalid macArray: ${macArray}. Search input should be one mac address string or an array of mac address strings.`)); + } + + return this.discover(refIP).then(hosts => { + var check = JSON.stringify(hosts); + return { + hosts: hosts.filter(h => { + h.matched = []; + for (var m of macArray) if (h.mac.indexOf(m) > -1) h.matched.push(m); + return h.matched.length; + }), + missing: macArray.filter(m => check.indexOf(m) == -1) + } + }); +} + +/** +* Search for devices with the designated mac address type +* @param {String} macType +* @param {String} refIP +*/ +Arpping.prototype.searchByMacType = function(macType, refIP) { + macType = macType.toLowerCase(); + return this.discover(refIP).then(hosts => hosts.filter(h => h.type && h.type.toLowerCase() == macType)); +} + +module.exports = Arpping; diff --git a/node_modules/arpping/macLookup.js b/node_modules/arpping/macLookup.js new file mode 100644 index 0000000..fe40fed --- /dev/null +++ b/node_modules/arpping/macLookup.js @@ -0,0 +1,55 @@ +'use strict'; + +const addresses = { + "Apple": [ + "2:f:b5", + "1c:36:bb", + "8c:85:90", + "8:66:98", + "dc:2b:2a", + "34:8:bc", + "e0:ac:cb", + "dc:a9:04", + "dc:a9:4" + ], + "RaspberryPi": [ + "b8:27:eb" + ], + "ParticlePhoton": [ + "e0:4f:43" + ], + "Sonos": [ + "94:9f:3e", + "78:28:ca" + ], + "Netgear": [ + "a0:40:a0" + ], + "Roku": [ + "20:f5:43" + ] +} +const stringAddresses = JSON.stringify(addresses); + +/** +* Cross references provided mac address with lookup table (incomplete) +* @param {string} mac +* @param {string} type +* @return {string} +*/ + +function macLookup(mac, type) { + var leading = mac.split(':').slice(0, 3).join(':'); + + if (type && addresses[type]) { + if (addresses[type].indexOf(leading) > -1) return type; + } + + if (stringAddresses.indexOf(leading) == -1) return false; + for (var vendor in addresses) { + if (addresses[vendor].indexOf(leading) > -1) return vendor; + } + return false; +} + +module.exports = macLookup; \ No newline at end of file diff --git a/node_modules/arpping/package.json b/node_modules/arpping/package.json new file mode 100644 index 0000000..3c92502 --- /dev/null +++ b/node_modules/arpping/package.json @@ -0,0 +1,28 @@ +{ + "name": "arpping", + "version": "2.0.0", + "description": "Discover and search for internet-connected devices (locally) using ping and arp", + "main": "index.js", + "dependencies": { + "child_process": "^1.0.2", + "os": "^0.1.1" + }, + "devDependencies": {}, + "scripts": { + "test": "node test.js example" + }, + "keywords": [ + "ping", + "arp", + "nmap", + "ip", + "mac", + "scan" + ], + "repository": { + "type": "git", + "url": "https://github.com/haf-decent/arpping.git" + }, + "author": "Ryan Hafner ", + "license": "MIT" +} diff --git a/node_modules/arpping/test.js b/node_modules/arpping/test.js new file mode 100644 index 0000000..668ed13 --- /dev/null +++ b/node_modules/arpping/test.js @@ -0,0 +1,63 @@ +'use strict'; + +const Arpping = require('./index.js'); +const arpping = new Arpping({ + timeout: 4, + includeEndpoints: true, + useCache: true, + cacheTimeout: 30 +}); + +const tests = { + findMyInfo: info => console.log(info), + discover: hosts => console.log(`${hosts.length} host(s) found:\n${JSON.stringify(hosts, null, 4)}`), + searchByIpAddress: ({ hosts, missing }) => { + console.log(`Found ${hosts.length} host(s):\n${JSON.stringify(hosts, null, 4)}`); + console.log(`${missing.length} host(s) missing:\n${missing}`); + }, + searchByMacAddress: ({ hosts, missing }) => { + console.log(`Found ${hosts.length} host(s):\n${JSON.stringify(hosts, null, 4)}`); + console.log(`${missing.length} host(s) missing:\n${missing}`); + }, + searchByMacType: hosts => console.log(`Found ${hosts.length} host(s):\n${JSON.stringify(hosts, null, 4)}`), + ping: ({ hosts, missing }) => { + console.log(`Found ${hosts.length} host(s):\n${hosts.join('\n')}`); + console.log(`${missing.length} host(s) missing:\n${missing}`); + }, + arp: ({ hosts, missing }) => { + console.log(`Found ${hosts.length} host(s):\n${JSON.stringify(hosts, null, 4)}`); + console.log(`${missing.length} host(s) missing:\n${missing}`); + } +} + +var start = Date.now(); +var input = process.argv; + +const errHandler = err => console.log(`Error during ${input[2]}: ${err}`); +const timeHandler = () => console.log(`\nFinished ${input[2]} in ${(Date.now() - start)/1000}s`) + +console.log('\n--------------------------------'); + +if (input[2] == 'example') { + console.log('Finding devices on your network with the same macType as your device...'); + arpping.findMyInfo() + .then(info => { + if (info.type) return arpping.searchByMacType(info.type); + console.log(`No mac type found for your device`); + }) + .then(hosts => console.log(`Found ${hosts.length} host(s) with your Mac Type (${info.type}):\n${JSON.stringify(hosts, null, 4)}`)) + .catch(errHandler); +} +else if (!tests[input[2]]) return console.log( + `Invalid command: ${input[2]} + \nValid commands:\n- ${Object.keys(tests).join('\n- ')}` +); +else if (input[4] || input[2].indexOf('search') > -1) { + arpping[input[2]](input[3].trim().split(','), input[4]).then(tests[input[2]]).then(timeHandler).catch(errHandler); +} +else if (input[3]) { + arpping[input[2]](input[3].trim().split(',')).then(tests[input[2]]).then(timeHandler).catch(errHandler); +} +else { + arpping[input[2]]().then(tests[input[2]]).then(timeHandler).catch(errHandler); +} \ No newline at end of file diff --git a/node_modules/arpping/test_class.js b/node_modules/arpping/test_class.js new file mode 100644 index 0000000..f3282cb --- /dev/null +++ b/node_modules/arpping/test_class.js @@ -0,0 +1,28 @@ +const Arpping = require('./index.js'); + +class Test { + constructor() { + this.arpping = new Arpping(); + this.tryMe(() => this.arpping.discover(null, (err, hosts) => { + if (err) return console.log(err); + console.log('success'); + })); + } + + // tryMe() { + // this.arpping.discover() + // .then(hosts => console.log('success')) + // .catch(err => console.log(err)); + // } + // tryMe() { + // this.arpping.discover(null, (err, hosts) => { + // if (err) return console.log(err); + // console.log(hosts); + // }); + // } + tryMe(callback) { + callback(); + } +} + +const test = new Test(); \ No newline at end of file diff --git a/node_modules/child_process/README.md b/node_modules/child_process/README.md new file mode 100644 index 0000000..5e9a74c --- /dev/null +++ b/node_modules/child_process/README.md @@ -0,0 +1,9 @@ +# Security holding package + +This package name is not currently in use, but was formerly occupied +by another package. To avoid malicious use, npm is hanging on to the +package name, but loosely, and we'll probably give it to you if you +want it. + +You may adopt this package by contacting support@npmjs.com and +requesting the name. diff --git a/node_modules/child_process/package.json b/node_modules/child_process/package.json new file mode 100644 index 0000000..50ba9be --- /dev/null +++ b/node_modules/child_process/package.json @@ -0,0 +1,20 @@ +{ + "name": "child_process", + "version": "1.0.2", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/npm/security-holder.git" + }, + "keywords": [], + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/npm/security-holder/issues" + }, + "homepage": "https://github.com/npm/security-holder#readme" +} diff --git a/node_modules/mime/package.json b/node_modules/mime/package.json index 1457481..6bd24bc 100644 --- a/node_modules/mime/package.json +++ b/node_modules/mime/package.json @@ -1,73 +1,44 @@ { - "_from": "mime@1.6.0", - "_id": "mime@1.6.0", - "_inBundle": false, - "_integrity": "sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg==", - "_location": "/mime", - "_phantomChildren": {}, - "_requested": { - "type": "version", - "registry": true, - "raw": "mime@1.6.0", - "name": "mime", - "escapedName": "mime", - "rawSpec": "1.6.0", - "saveSpec": null, - "fetchSpec": "1.6.0" - }, - "_requiredBy": [ - "/send" - ], - "_resolved": "https://registry.npmjs.org/mime/-/mime-1.6.0.tgz", - "_shasum": "32cd9e5c64553bd58d19a568af452acff04981b1", - "_spec": "mime@1.6.0", - "_where": "/home/tobias/IdeaProjects/network-devices/node_modules/send", "author": { "name": "Robert Kieffer", - "email": "robert@broofa.com", - "url": "http://github.com/broofa" + "url": "http://github.com/broofa", + "email": "robert@broofa.com" }, "bin": { "mime": "cli.js" }, - "bugs": { - "url": "https://github.com/broofa/node-mime/issues" + "engines": { + "node": ">=4" }, - "bundleDependencies": false, "contributors": [ { "name": "Benjamin Thomas", - "email": "benjamin@benjaminthomas.org", - "url": "http://github.com/bentomas" + "url": "http://github.com/bentomas", + "email": "benjamin@benjaminthomas.org" } ], - "dependencies": {}, - "deprecated": false, "description": "A comprehensive library for mime-type mapping", + "license": "MIT", + "dependencies": {}, "devDependencies": { "github-release-notes": "0.13.1", "mime-db": "1.31.0", "mime-score": "1.1.0" }, - "engines": { - "node": ">=4" + "scripts": { + "prepare": "node src/build.js", + "changelog": "gren changelog --tags=all --generate --override", + "test": "node src/test.js" }, - "homepage": "https://github.com/broofa/node-mime#readme", "keywords": [ "util", "mime" ], - "license": "MIT", "main": "mime.js", "name": "mime", "repository": { - "url": "git+https://github.com/broofa/node-mime.git", + "url": "https://github.com/broofa/node-mime", "type": "git" }, - "scripts": { - "changelog": "gren changelog --tags=all --generate --override", - "prepare": "node src/build.js", - "test": "node src/test.js" - }, "version": "1.6.0" } diff --git a/node_modules/os/README.md b/node_modules/os/README.md new file mode 100644 index 0000000..6f04ca0 --- /dev/null +++ b/node_modules/os/README.md @@ -0,0 +1,11 @@ +# os + +This is a Node.js Core Module + +# There's no need to install through npm + +**Really, there's no need, just require/import it :)** + +## API + +https://nodejs.org/api/os.html diff --git a/node_modules/os/index.js b/node_modules/os/index.js new file mode 100644 index 0000000..889b75f --- /dev/null +++ b/node_modules/os/index.js @@ -0,0 +1 @@ +module.exports = require('os') diff --git a/node_modules/os/package.json b/node_modules/os/package.json new file mode 100644 index 0000000..ebcf799 --- /dev/null +++ b/node_modules/os/package.json @@ -0,0 +1,25 @@ +{ + "name": "os", + "version": "0.1.2", + "description": "NodeJS Core Module Extended", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/DiegoRBaquero/node-os.git" + }, + "keywords": [ + "node", + "os", + "core", + "module" + ], + "author": "Diego Rodríguez Baquero (https://diegorbaquero.com)", + "license": "MIT", + "bugs": { + "url": "https://github.com/DiegoRBaquero/node-os/issues" + }, + "homepage": "https://github.com/DiegoRBaquero/node-os#readme" +} diff --git a/package-lock.json b/package-lock.json index 0bea4a2..8436e95 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "1.0.0", "license": "ISC", "dependencies": { + "arpping": "^2.0.0", "express": "^4.17.1", "local-devices": "^3.1.0", "ping": "^0.4.1", @@ -74,6 +75,15 @@ "resolved": "https://registry.npmjs.org/any-promise/-/any-promise-1.3.0.tgz", "integrity": "sha1-q8av7tzqUugJzcA3au0845Y10X8=" }, + "node_modules/arpping": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/arpping/-/arpping-2.0.0.tgz", + "integrity": "sha512-+bkS6pf3vvYRfC/zDKKP9saFw4u73i7JgD75s37LU8i/OffAFhrIvQaKTzv0x1/P0JqYho/97yHD8b8jkhT78w==", + "dependencies": { + "child_process": "^1.0.2", + "os": "^0.1.1" + } + }, "node_modules/array-flatten": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", @@ -148,6 +158,11 @@ "is-regex": "^1.0.3" } }, + "node_modules/child_process": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/child_process/-/child_process-1.0.2.tgz", + "integrity": "sha1-sffn/HPSXn/R1FWtyU4UODAYK1o=" + }, "node_modules/cidr-regex": { "version": "1.0.7", "resolved": "https://registry.npmjs.org/cidr-regex/-/cidr-regex-1.0.7.tgz", @@ -634,6 +649,11 @@ "node": ">= 0.8" } }, + "node_modules/os": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/os/-/os-0.1.2.tgz", + "integrity": "sha512-ZoXJkvAnljwvc56MbvhtKVWmSkzV712k42Is2mA0+0KTSRakq5XXuXpjZjgAt9ctzl51ojhQWakQQpmOvXWfjQ==" + }, "node_modules/parseurl": { "version": "1.3.3", "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", @@ -1060,6 +1080,15 @@ "resolved": "https://registry.npmjs.org/any-promise/-/any-promise-1.3.0.tgz", "integrity": "sha1-q8av7tzqUugJzcA3au0845Y10X8=" }, + "arpping": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/arpping/-/arpping-2.0.0.tgz", + "integrity": "sha512-+bkS6pf3vvYRfC/zDKKP9saFw4u73i7JgD75s37LU8i/OffAFhrIvQaKTzv0x1/P0JqYho/97yHD8b8jkhT78w==", + "requires": { + "child_process": "^1.0.2", + "os": "^0.1.1" + } + }, "array-flatten": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", @@ -1122,6 +1151,11 @@ "is-regex": "^1.0.3" } }, + "child_process": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/child_process/-/child_process-1.0.2.tgz", + "integrity": "sha1-sffn/HPSXn/R1FWtyU4UODAYK1o=" + }, "cidr-regex": { "version": "1.0.7", "resolved": "https://registry.npmjs.org/cidr-regex/-/cidr-regex-1.0.7.tgz", @@ -1509,6 +1543,11 @@ "ee-first": "1.1.1" } }, + "os": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/os/-/os-0.1.2.tgz", + "integrity": "sha512-ZoXJkvAnljwvc56MbvhtKVWmSkzV712k42Is2mA0+0KTSRakq5XXuXpjZjgAt9ctzl51ojhQWakQQpmOvXWfjQ==" + }, "parseurl": { "version": "1.3.3", "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", diff --git a/package.json b/package.json index 9d7cfda..e5b492f 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "author": "", "license": "ISC", "dependencies": { + "arpping": "^2.0.0", "express": "^4.17.1", "local-devices": "^3.1.0", "ping": "^0.4.1",