MeshCentral/agents/modules_meshcore_min/amt-scanner.min.js
2019-10-01 16:53:10 -07:00

1 line
1.9 KiB
JavaScript

function AMTScanner(){var a=require("events").inherits(this);a.createEvent("found");this.dgram=require("dgram");this.buildRmcpPing=function(c){var b=Buffer.from("06000006000011BE80000000","hex");b[9]=c;return b};this.parseRmcpPacket=function(h,b,g,d){if(b==null||b.length<20){return}var f={};if(((b[12]==0)||(b[13]!=0)||(b[14]!=1)||(b[15]!=87))&&(b[21]&32)){f.servertag=b[9];f.minorVersion=b[18]&15;f.majorVersion=(b[18]>>4)&15;f.provisioningState=b[19]&3;var e=(b[16]*256)+b[17];var c=((b[19]&4)!=0)?true:false;f.openPorts=[e];f.address=g.address;if(c==true){f.openPorts=[16992,16993]}if(d!==undefined){d(h,f)}}};this.parseIPv4Range=function(f){if(f==undefined||f==null){return null}var g=f.split("-");if(g.length==2){return{min:this.parseIpv4Addr(g[0]),max:this.parseIpv4Addr(g[1])}}g=f.split("/");if(g.length==2){var c=this.parseIpv4Addr(g[0]),e=parseInt(g[1]),d=0;if(e<=16||e>32){return null}e=32-e;for(var b=0;b<e;b++){d=(d<<1);d++}return{min:(c&(4294967295-d))+1,max:(c&(4294967295-d))+d-1}}g=this.parseIpv4Addr(f);if(g==null){return null}return{min:g,max:g}};this.parseIpv4Addr=function(b){var c=b.split(".");if(c.length==4){return(parseInt(c[0])<<24)+(parseInt(c[1])<<16)+(parseInt(c[2])<<8)+(parseInt(c[3])<<0)}return null};this.IPv4NumToStr=function(b){return((b>>24)&255)+"."+((b>>16)&255)+"."+((b>>8)&255)+"."+(b&255)};this.scan=function(e,h,b){var d=this.parseIPv4Range(e);var f=this.buildRmcpPing(0);var g=this.dgram.createSocket({type:"udp4"});g.parent=this;g.scanResults=[];g.on("error",function(j){console.log("Error:"+j)});g.on("message",function(j,k){if(k.size>4){this.parent.parseRmcpPacket(this,j,k,function(m,l){m.scanResults.push(l)})}});g.on("listening",function(){for(var j=d.min;j<=d.max;j++){g.send(f,623,g.parent.IPv4NumToStr(j))}});g.bind({address:"0.0.0.0",port:0,exclusive:true});var i=setTimeout(function c(){g.close();if(b){b(g.scanResults)}g.parent.emit("found",g.scanResults);delete g},h)}}module.exports=AMTScanner;