UNPKG

arpping

Version:

Discover and search for internet-connected devices (locally) using ping and arp

1 lines 11.1 kB
(()=>{var t={951:function(t,n){(function(){var t,e,i;i=function(t){return[(t&255<<24)>>>24,(t&255<<16)>>>16,(65280&t)>>>8,255&t].join(".")},e=function(t){var n,e,i,r,o;if(0===(n=(t+"").split(".")).length||n.length>4)throw new Error("Invalid IP");for(i=r=0,o=n.length;r<o;i=++r){if(e=n[i],isNaN(parseInt(e,10)))throw new Error("Invalid byte: "+e);if(e<0||e>255)throw new Error("Invalid byte: "+e)}return((n[0]||0)<<24|(n[1]||0)<<16|(n[2]||0)<<8|(n[3]||0))>>>0},t=function(){function t(t,n){var r,o,s;if("string"!=typeof t)throw new Error("Missing `net' parameter");if(n||(s=t.split("/",2),t=s[0],n=s[1]),!n)switch(t.split(".").length){case 1:n=8;break;case 2:n=16;break;case 3:n=24;break;case 4:n=32;break;default:throw new Error("Invalid net address: "+t)}if("string"==typeof n&&n.indexOf(".")>-1){try{this.maskLong=e(n)}catch(t){throw new Error("Invalid mask: "+n)}for(r=o=32;o>=0;r=--o)if(this.maskLong===4294967295<<32-r>>>0){this.bitmask=r;break}}else{if(!n)throw new Error("Invalid mask: empty");this.bitmask=parseInt(n,10),this.maskLong=0,this.bitmask>0&&(this.maskLong=4294967295<<32-this.bitmask>>>0)}try{this.netLong=(e(t)&this.maskLong)>>>0}catch(n){throw new Error("Invalid net address: "+t)}if(!(this.bitmask<=32))throw new Error("Invalid mask for ip4: "+n);this.size=Math.pow(2,32-this.bitmask),this.base=i(this.netLong),this.mask=i(this.maskLong),this.hostmask=i(~this.maskLong),this.first=this.bitmask<=30?i(this.netLong+1):this.base,this.last=this.bitmask<=30?i(this.netLong+this.size-2):i(this.netLong+this.size-1),this.broadcast=this.bitmask<=30?i(this.netLong+this.size-1):void 0}return t.prototype.contains=function(n){return"string"==typeof n&&(n.indexOf("/")>0||4!==n.split(".").length)&&(n=new t(n)),n instanceof t?this.contains(n.base)&&this.contains(n.broadcast||n.last):(e(n)&this.maskLong)>>>0==(this.netLong&this.maskLong)>>>0},t.prototype.next=function(n){return null==n&&(n=1),new t(i(this.netLong+this.size*n),this.mask)},t.prototype.forEach=function(t){var n,r,o,s,a,c,u,h;for(h=[],n=r=0,o=(a=function(){u=[];for(var t=c=e(this.first),n=e(this.last);c<=n?t<=n:t>=n;c<=n?t++:t--)u.push(t);return u}.apply(this)).length;r<o;n=++r)s=a[n],h.push(t(i(s),s,n));return h},t.prototype.toString=function(){return this.base+"/"+this.bitmask},t}(),n.ip2long=e,n.long2ip=i,n.Netmask=t}).call(this)},970:(t,n,e)=>{t.exports=e(970)},607:function(t,n,e){"use strict";var i=this&&this.__assign||function(){return(i=Object.assign||function(t){for(var n,e=1,i=arguments.length;e<i;e++)for(var r in n=arguments[e])Object.prototype.hasOwnProperty.call(n,r)&&(t[r]=n[r]);return t}).apply(this,arguments)},r=this&&this.__awaiter||function(t,n,e,i){return new(e||(e=Promise))((function(r,o){function s(t){try{c(i.next(t))}catch(t){o(t)}}function a(t){try{c(i.throw(t))}catch(t){o(t)}}function c(t){var n;t.done?r(t.value):(n=t.value,n instanceof e?n:new e((function(t){t(n)}))).then(s,a)}c((i=i.apply(t,n||[])).next())}))},o=this&&this.__generator||function(t,n){var e,i,r,o,s={label:0,sent:function(){if(1&r[0])throw r[1];return r[1]},trys:[],ops:[]};return o={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function a(o){return function(a){return function(o){if(e)throw new TypeError("Generator is already executing.");for(;s;)try{if(e=1,i&&(r=2&o[0]?i.return:o[0]?i.throw||((r=i.return)&&r.call(i),0):i.next)&&!(r=r.call(i,o[1])).done)return r;switch(i=0,r&&(o=[2&o[0],r.value]),o[0]){case 0:case 1:r=o;break;case 4:return s.label++,{value:o[1],done:!1};case 5:s.label++,i=o[1],o=[0];continue;case 7:o=s.ops.pop(),s.trys.pop();continue;default:if(!((r=(r=s.trys).length>0&&r[r.length-1])||6!==o[0]&&2!==o[0])){s=0;continue}if(3===o[0]&&(!r||o[1]>r[0]&&o[1]<r[3])){s.label=o[1];break}if(6===o[0]&&s.label<r[1]){s.label=r[1],r=o;break}if(r&&s.label<r[2]){s.label=r[2],s.ops.push(o);break}r[2]&&s.ops.pop(),s.trys.pop();continue}o=n.call(t,s)}catch(t){o=[6,t],i=0}finally{e=r=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,a])}}},s=this&&this.__spreadArray||function(t,n,e){if(e||2===arguments.length)for(var i,r=0,o=n.length;r<o;r++)!i&&r in n||(i||(i=Array.prototype.slice.call(n,0,r)),i[r]=n[r]);return t.concat(i||Array.prototype.slice.call(n))},a=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(n,"__esModule",{value:!0});var c,u=e(970),h=e(951).Netmask,l=e(654).execFile,f=a(e(260)),d=u.type(),p="Windows_NT"===d?["-a"]:[];switch(d){case"Windows_NT":case"Linux":c="-w";break;case"Darwin":c="-t";break;default:throw new Error("Unsupported OS: ".concat(d))}var v=function(){function t(t){var n=void 0===t?{}:t,e=n.timeout,i=void 0===e?3:e,r=n.includeEndpoints,o=void 0!==r&&r,s=n.useCache,a=void 0===s||s,c=n.cacheTimeout,u=void 0===c?3600:c,h=n.interfaceFilters,l=void 0===h?{}:h,f=n.connectionInterval,p=void 0===f?600:f,v=n.onConnect,m=void 0===v?[]:v,g=n.onDisconnect,y=void 0===g?[]:g,w=n.debug,b=void 0!==w&&w,k=this;if(i<1||i>60)throw new Error("Invalid timeout parameter: ".concat(i,". Timeout should be between 1 and 60."));this.timeout=parseInt(i.toString()),this.debug=b,this.includeEndpoints=o,this.useCache=a,this.cache=[],this.cacheTimeout=u,this.cacheUpdate=0,this.interfaceFilters=Object.assign({interface:["lo0","en0","en1","wlan0"],internal:[!1],family:["IPv4"]},l),this.myDevice={os:d,connection:null},this.onConnect=m,this.onDisconnect=y,this.getConnection(this.interfaceFilters),p&&(this.interval=setInterval((function(){return k.getConnection(k.interfaceFilters)}),1e3*p))}return t.getNetworkInterfaces=function(){return u.networkInterfaces()},t.prototype.getConnection=function(n){for(var e=this,r=void 0===n?{}:n,o=r.interface,s=void 0===o?["lo0","en0","eth0","wlan0"]:o,a=r.internal,c=void 0===a?[!1]:a,u=r.family,h=void 0===u?["IPv4"]:u,l=!!this.myDevice.connection,d=t.getNetworkInterfaces(),p=0,v=Object.entries(d);p<v.length;p++){var m=v[p],g=m[0],y=m[1];if(!s.length||s.includes(g))for(var w=0,b=y;w<b.length;w++){var k=b[w];if((!c.length||c.includes(k.internal))&&(!h.length||h.includes(k.family)))return this.myDevice.connection=i({name:g},k),this.myDevice.type=(0,f.default)(k.mac),l||(this.debug&&console.log("Interface ".concat(g," connected")),this.onConnect.forEach((function(t){return t(e.myDevice.connection)}))),this.myDevice.connection}}return l&&(this.debug&&console.log("Interface ".concat(this.myDevice.connection.name," disconnected")),this.onDisconnect.forEach((function(t){return t()}))),this.myDevice.connection=null,null},t.prototype._getFullRange=function(t){if(!this.myDevice.connection)return this.debug&&console.log("No connection available"),[];var n=this.myDevice.connection,e=n.address,i=n.netmask,r=new h(e,t||i),o=[];return this.includeEndpoints?(o.push(r.base),r.forEach((function(t){return o.push(t)})),o.push(r.broadcast)):r.forEach((function(t){return o.push(t)})),o},t.prototype.ping=function(t){return r(this,void 0,void 0,(function(){var n,e=this;return o(this,(function(i){switch(i.label){case 0:if(!this.myDevice.connection)throw new Error("No connection!");if(!t&&!(t=this._getFullRange()).length)throw new Error("No connection!");return n=t.map((function(t){return new Promise((function(n,i){l("ping",[c,e.timeout,t],(function(e,r){return e||r.match(/100(\.0)?% packet loss/g)?i(t):n(t)}))}))})),[4,Promise.allSettled(n)];case 1:return[2,i.sent().reduce((function(t,n){var e=n.status,i=n.value,r=void 0===i?null:i,o=n.reason,s=void 0===o?null:o;return"fulfilled"===e?t.hosts.push(r):t.missing.push(s),t}),{hosts:[],missing:[]})]}}))}))},t.prototype.arp=function(t){return void 0===t&&(t=[]),r(this,void 0,void 0,(function(){var n,e=this;return o(this,(function(i){switch(i.label){case 0:if(!this.myDevice.connection)throw new Error("No connection!");return n=t.map((function(t){return new Promise((function(n,i){l("arp",s(s([],p,!0),[t],!1),(function(r,o){if(r||o.includes("no entry")||o.includes("(incomplete)"))return i(t);var s=(o.match(/([0-9A-Fa-f]{1,2}[:-]){5}([0-9A-Fa-f]{1,2})/gi)||[])[0],a=void 0===s?null:s;if(!a)return i(t);var c={ip:t,mac:a,type:(0,f.default)(a)};t===e.myDevice.connection.address&&(c.isHostDevice=!0),l("nslookup",[t],(function(t,e){if(!t){var i=(e.match(/ = .*/)||[])[0],r=void 0===i?null:i;r&&(c.name=r.substr(3,r.length-4))}n(c)}))}))}))})),[4,Promise.allSettled(n)];case 1:return[2,i.sent().reduce((function(t,n){var e=n.status,i=n.value,r=void 0===i?null:i,o=n.reason,s=void 0===o?null:o;return"fulfilled"===e?t.hosts.push(r):t.missing.push(s),t}),{hosts:[],missing:[]})]}}))}))},t.prototype.discover=function(t){return r(this,void 0,void 0,(function(){var n,e=this;return o(this,(function(i){switch(i.label){case 0:return this.useCache&&this.cache.length&&Date.now()-this.cacheUpdate<1e3*this.cacheTimeout?[2,t?this.cache.filter((function(n){var e=n.ip;return t.includes(e)})):this.cache]:[4,this.ping(t).then((function(t){var n=t.hosts;return e.arp(n)}))];case 1:return n=i.sent().hosts,this.cache=n.slice(0),this.cacheUpdate=Date.now(),[2,n]}}))}))},t.prototype.searchByIpAddress=function(t){return r(this,void 0,void 0,(function(){var n,e;return o(this,(function(i){switch(i.label){case 0:if(!Array.isArray(t)||!t.length)throw new Error("Invalid ipArray: ".concat(t,". Search input should be an array of one or more ip strings."));return[4,this.discover()];case 1:return n=i.sent(),e=n.map((function(t){return t.ip})),[2,{hosts:n.filter((function(n){return t.includes(n.ip)})),missing:t.filter((function(t){return!e.includes(t)}))}]}}))}))},t.prototype.searchByMacAddress=function(t,n){return r(this,void 0,void 0,(function(){var e,i;return o(this,(function(r){switch(r.label){case 0:if(!Array.isArray(t)||!t.length)throw new Error("Invalid macArray: ".concat(t,". Search input should be an array of one or more mac address strings."));return[4,this.discover(n)];case 1:return e=r.sent(),i=JSON.stringify(e),[2,{hosts:e.filter((function(n){n.matched=[];for(var e=0,i=t;e<i.length;e++){var r=i[e];n.mac.toLowerCase().includes(r.toLowerCase())&&n.matched.push(r)}return n.matched.length})),missing:t.filter((function(t){return!i.includes(t)}))}]}}))}))},t.prototype.searchByMacType=function(t,n){return r(this,void 0,void 0,(function(){return o(this,(function(e){switch(e.label){case 0:return t=t.toLowerCase(),[4,this.discover(n)];case 1:return[2,e.sent().filter((function(n){return n.type&&n.type.toLowerCase()===t}))]}}))}))},t.osType=d,t}();t.exports=v,n.default=v},260:(t,n)=>{"use strict";Object.defineProperty(n,"__esModule",{value:!0});var e={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"]},i=JSON.stringify(e);n.default=function(t,n){var r=t.split(":").slice(0,3).join(":");if(n&&e[n]&&e[n].includes(r))return n;if(!i.includes(r))return null;for(var o in e)if(e[o].includes(r))return o;return null}},654:()=>{}},n={};!function e(i){var r=n[i];if(void 0!==r)return r.exports;var o=n[i]={exports:{}};return t[i].call(o.exports,o,o.exports,e),o.exports}(607)})();