mirror of
https://github.com/Ylianst/MeshCentral.git
synced 2024-11-27 11:57:45 +03:00
1 line
44 KiB
JavaScript
1 line
44 KiB
JavaScript
process.on("uncaughtException",function(a){require("MeshAgent").SendCommand({action:"msg",type:"console",value:"uncaughtException1: "+a})});var MESHRIGHT_EDITMESH=1;var MESHRIGHT_MANAGEUSERS=2;var MESHRIGHT_MANAGECOMPUTERS=4;var MESHRIGHT_REMOTECONTROL=8;var MESHRIGHT_AGENTCONSOLE=16;var MESHRIGHT_SERVERFILES=32;var MESHRIGHT_WAKEDEVICE=64;var MESHRIGHT_SETNOTES=128;var MESHRIGHT_REMOTEVIEW=256;var MESHRIGHT_NOTERMINAL=512;var MESHRIGHT_NOFILES=1024;var MESHRIGHT_NOAMT=2048;var MESHRIGHT_LIMITEDINPUT=4096;function createMeshCore(agent){var obj={};if(process.platform=="darwin"&&!process.versions){var child=require("child_process").execFile("/bin/sh",["sh"]);child.stdout.str="";child.stdout.on("data",function(chunk){this.str+=chunk.toString()});child.stdin.write('cat /Library/LaunchDaemons/meshagent_osx64_LaunchDaemon.plist | tr \'\n\' \'.\' | awk \'{split($0, a, "<key>KeepAlive</key>"); split(a[2], b, "<"); split(b[2], c, ">"); ');child.stdin.write(' if(c[1]=="dict"){ split(a[2], d, "</dict>"); if(split(d[1], truval, "<true/>")>1) { split(truval[1], kn1, "<key>"); split(kn1[2], kn2, "</key>"); print kn2[1]; } }');child.stdin.write(' else { split(c[1], ka, "/"); if(ka[1]=="true") {print "ALWAYS";} } }\'\nexit\n');child.waitExit();if(child.stdout.str.trim()=="Crashed"){child=require("child_process").execFile("/bin/sh",["sh"]);child.stdout.str="";child.stdout.on("data",function(chunk){this.str+=chunk.toString()});child.stdin.write("launchctl list | grep 'meshagent' | awk '{ if($3==\"meshagent\"){print $1;}}'\nexit\n");child.waitExit();if(parseInt(child.stdout.str.trim())==process.pid){var plist=require("fs").readFileSync("/Library/LaunchDaemons/meshagent_osx64_LaunchDaemon.plist").toString();var tokens=plist.split("<key>KeepAlive</key>");if(tokens[1].split(">")[0].split("<")[1]=="dict"){var tmp=tokens[1].split("</dict>");tmp.shift();tokens[1]="\n <true/>"+tmp.join("</dict>");tokens=tokens.join("<key>KeepAlive</key>");require("fs").writeFileSync("/Library/LaunchDaemons/meshagent_osx64_LaunchDaemon.plist",tokens);var fix="";fix+=("function macosRepair()\n");fix+=("{\n");fix+=(" var child = require('child_process').execFile('/bin/sh', ['sh']);\n");fix+=(" child.stdout.str = '';\n");fix+=(" child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });\n");fix+=(" child.stderr.on('data', function (chunk) { });\n");fix+=(" child.stdin.write('launchctl unload /Library/LaunchDaemons/meshagent_osx64_LaunchDaemon.plist\\n');\n");fix+=(" child.stdin.write('launchctl load /Library/LaunchDaemons/meshagent_osx64_LaunchDaemon.plist\\n');\n");fix+=(" child.stdin.write('rm /Library/LaunchDaemons/meshagentRepair.plist\\n');\n");fix+=(" child.stdin.write('rm "+process.cwd()+"/macosRepair.js\\n');\n");fix+=(" child.stdin.write('launchctl stop meshagentRepair\\nexit\\n');\n");fix+=(" child.waitExit();\n");fix+=("}\n");fix+=("macosRepair();\n");fix+=("process.exit();\n");require("fs").writeFileSync(process.cwd()+"/macosRepair.js",fix);var plist='<?xml version="1.0" encoding="UTF-8"?>\n';plist+='<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">\n';plist+='<plist version="1.0">\n';plist+=" <dict>\n";plist+=" <key>Label</key>\n";plist+=(" <string>meshagentRepair</string>\n");plist+=" <key>ProgramArguments</key>\n";plist+=" <array>\n";plist+=(" <string>"+process.execPath+"</string>\n");plist+=" <string>macosRepair.js</string>\n";plist+=" </array>\n";plist+=" <key>WorkingDirectory</key>\n";plist+=(" <string>"+process.cwd()+"</string>\n");plist+=" <key>RunAtLoad</key>\n";plist+=" <true/>\n";plist+=" </dict>\n";plist+="</plist>";require("fs").writeFileSync("/Library/LaunchDaemons/meshagentRepair.plist",plist);child=require("child_process").execFile("/bin/sh",["sh"]);child.stdout.str="";child.stdout.on("data",function(chunk){this.str+=chunk.toString()});child.stdin.write("launchctl load /Library/LaunchDaemons/meshagentRepair.plist\nexit\n");child.waitExit()}}}}var meshCoreObj={action:"coreinfo",value:"MeshCore v6",caps:14};try{require("os").name().then(function(v){meshCoreObj.osdesc=v})}catch(ex){}var meshServerConnectionState=0;var tunnels={};var lastMeInfo=null;var lastNetworkInfo=null;var lastPublicLocationInfo=null;var selfInfoUpdateTimer=null;var http=require("http");var net=require("net");var fs=require("fs");var rtc=require("ILibWebRTC");var amt=null;var processManager=require("process-manager");var wifiScannerLib=null;var wifiScanner=null;var networkMonitor=null;var amtscanner=null;var nextTunnelIndex=1;var amtPolicy=null;if(agent==null){db=require("SimpleDataStore").Shared();sha=require("SHA256Stream");mesh=require("MeshAgent");childProcess=require("child_process");if(mesh.hasKVM==1){try{if((process.platform=="win32")||(process.platform=="darwin")||(require("monitor-info").kvm_x11_support)){meshCoreObj.caps|=1}}catch(ex){}}}else{meshCoreObj.value+="-NodeJS";meshCoreObj.caps=8;mesh=agent.getMeshApi()}try{var AMTScannerModule=require("amt-scanner");amtscanner=new AMTScannerModule()}catch(ex){amtscanner=null}var SMBiosTables=null;var SMBiosTablesRaw=null;try{var SMBiosModule=null;try{SMBiosModule=require("smbios")}catch(ex){}if(SMBiosModule!=null){SMBiosModule.get(function(data){if(data!=null){SMBiosTablesRaw=data;SMBiosTables=require("smbios").parse(data);if(mesh.isControlChannelConnected){mesh.SendCommand({action:"smbios",value:SMBiosTablesRaw})}if(SMBiosTables.amtInfo&&(SMBiosTables.amtInfo.AMT==true)){var amtmodule=require("amt-manage");amt=new amtmodule(mesh,db,true);amt.onStateChange=function(state){if(state==2){sendPeriodicServerUpdate(1)}};if(amtPolicy!=null){amt.setPolicy(amtPolicy)}amt.start()}}})}}catch(ex){sendConsoleText("ex1: "+ex)}try{var wifiScannerLib=require("wifi-scanner");wifiScanner=new wifiScannerLib();wifiScanner.on("accessPoint",function(data){sendConsoleText("wifiScanner: "+data)})}catch(ex){wifiScannerLib=null;wifiScanner=null}var getIpLocationDataExInProgress=false;var getIpLocationDataExCounts=[0,0];function getIpLocationDataEx(func){if(getIpLocationDataExInProgress==true){return false}try{getIpLocationDataExInProgress=true;getIpLocationDataExCounts[0]++;var options=http.parseUri("http://ipinfo.io/json");options.method="GET";http.request(options,function(resp){if(resp.statusCode==200){var geoData="";resp.data=function(geoipdata){geoData+=geoipdata};resp.end=function(){var location=null;try{if(typeof geoData=="string"){var result=JSON.parse(geoData);if(result.ip&&result.loc){location=result}}}catch(e){}if(func){getIpLocationDataExCounts[1]++;func(location)}}}else{func(null)}getIpLocationDataExInProgress=false}).end();return true}catch(e){return false}}function clearGatewayMac(str){if(str==null){return null}var x=JSON.parse(str);for(var i in x.netif){if(x.netif[i].gatewaymac){delete x.netif[i].gatewaymac}}return JSON.stringify(x)}function getIpLocationData(func){var publicLocationInfo=db.Get("publicLocationInfo");if(publicLocationInfo!=null){publicLocationInfo=JSON.parse(publicLocationInfo)}if(publicLocationInfo==null){getIpLocationDataEx(function(locationData){if(locationData!=null){publicLocationInfo={};publicLocationInfo.netInfoStr=lastNetworkInfo;publicLocationInfo.locationData=locationData;var x=db.Put("publicLocationInfo",JSON.stringify(publicLocationInfo));if(func){func(locationData)}}else{if(func){func(null)}}})}else{if(clearGatewayMac(publicLocationInfo.netInfoStr)==clearGatewayMac(lastNetworkInfo)){if(func){func(publicLocationInfo.locationData)}}else{getIpLocationDataEx(function(locationData){if(locationData!=null){publicLocationInfo={};publicLocationInfo.netInfoStr=lastNetworkInfo;publicLocationInfo.locationData=locationData;var x=db.Put("publicLocationInfo",JSON.stringify(publicLocationInfo));if(func){func(locationData)}}else{if(func){func(publicLocationInfo.locationData)}}})}}}if(!String.prototype.endsWith){String.prototype.endsWith=function(searchString,position){var subjectString=this.toString();if(typeof position!=="number"||!isFinite(position)||Math.floor(position)!==position||position>subjectString.length){position=subjectString.length}position-=searchString.length;var lastIndex=subjectString.lastIndexOf(searchString,position);return lastIndex!==-1&&lastIndex===position}}obj.path={join:function(){var x=[];for(var i in arguments){var w=arguments[i];if(w!=null){while(w.endsWith("/")||w.endsWith("\\")){w=w.substring(0,w.length-1)}if(i!=0){while(w.startsWith("/")||w.startsWith("\\")){w=w.substring(1)}}x.push(w)}}if(x.length==0){return"/"}return x.join("/")}};function toNumberIfNumber(x){if((typeof x=="string")&&(+parseInt(x)===x)){x=parseInt(x)}return x}function char2hex(i){return(i+256).toString(16).substr(-2).toUpperCase()}function rstr2hex(input){var r="",i;for(i=0;i<input.length;i++){r+=char2hex(input.charCodeAt(i))}return r}function buf2rstr(buf){var r="";for(var i=0;i<buf.length;i++){r+=String.fromCharCode(buf[i])}return r}function hex2rstr(d){if(typeof d!="string"||d.length==0){return""}var r="",m=(""+d).match(/../g),t;while(t=m.shift()){r+=String.fromCharCode("0x"+t)}return r}function objToString(x,p,pad,ret){if(ret==undefined){ret=""}if(p==undefined){p=0}if(x==null){return"[null]"}if(p>8){return"[...]"}if(x==undefined){return"[undefined]"}if(typeof x=="string"){if(p==0){return x}return'"'+x+'"'}if(typeof x=="buffer"){return"[buffer]"}if(typeof x!="object"){return x}var r="{"+(ret?"\r\n":" ");for(var i in x){if(i!="_ObjectID"){r+=(addPad(p+2,pad)+i+": "+objToString(x[i],p+2,pad,ret)+(ret?"\r\n":" "))}}return r+addPad(p,pad)+"}"}function addPad(p,ret){var r="";for(var i=0;i<p;i++){r+=ret}return r}function splitArgs(str){var myArray=[],myRegexp=/[^\s"]+|"([^"]*)"/gi;do{var match=myRegexp.exec(str);if(match!=null){myArray.push(match[1]?match[1]:match[0])}}while(match!=null);return myArray}function parseArgs(argv){var results={_:[]},current=null;for(var i=1,len=argv.length;i<len;i++){var x=argv[i];if(x.length>2&&x[0]=="-"&&x[1]=="-"){if(current!=null){results[current]=true}current=x.substring(2)}else{if(current!=null){results[current]=toNumberIfNumber(x);current=null}else{results._.push(toNumberIfNumber(x))}}}if(current!=null){results[current]=true}return results}function getServerTargetUrl(path){var x=mesh.ServerUrl;if(x==null){return null}if(path==null){path=""}x=http.parseUri(x);if(x==null){return null}return x.protocol+"//"+x.host+":"+x.port+"/"+path}function getServerTargetUrlEx(url){if(url.substring(0,2)=="*/"){return getServerTargetUrl(url.substring(2))}return url}function sendWakeOnLan(hexMac){var count=0;try{var interfaces=require("os").networkInterfaces();var magic="FFFFFFFFFFFF";for(var x=1;x<=16;++x){magic+=hexMac}var magicbin=Buffer.from(magic,"hex");for(var adapter in interfaces){if(interfaces.hasOwnProperty(adapter)){for(var i=0;i<interfaces[adapter].length;++i){var addr=interfaces[adapter][i];if((addr.family=="IPv4")&&(addr.mac!="00:00:00:00:00:00")){var socket=require("dgram").createSocket({type:"udp4"});socket.bind({address:addr.address});socket.setBroadcast(true);socket.send(magicbin,7,"255.255.255.255");count++}}}}}catch(e){}return count}function handleServerCommand(data){if(typeof data=="object"){switch(data.action){case"msg":switch(data.type){case"console":if(data.value&&data.sessionid){var args=splitArgs(data.value);processConsoleCommand(args[0].toLowerCase(),parseArgs(args),data.rights,data.sessionid)}break;case"tunnel":if(data.value!=null){var xurl=getServerTargetUrlEx(data.value);if(xurl!=null){var woptions=http.parseUri(xurl);woptions.rejectUnauthorized=0;var tunnel=http.request(woptions);tunnel.upgrade=onTunnelUpgrade;tunnel.on("error",function(e){sendConsoleText("ERROR: "+JSON.stringify(e))});tunnel.sessionid=data.sessionid;tunnel.rights=data.rights;tunnel.consent=data.consent;tunnel.username=data.username;tunnel.state=0;tunnel.url=xurl;tunnel.protocol=0;tunnel.tcpaddr=data.tcpaddr;tunnel.tcpport=data.tcpport;tunnel.udpaddr=data.udpaddr;tunnel.udpport=data.udpport;tunnel.end();var index=nextTunnelIndex++;tunnel.index=index;tunnels[index]=tunnel}}break;case"ps":if(data.sessionid){processManager.getProcesses(function(plist){mesh.SendCommand({action:"msg",type:"ps",value:JSON.stringify(plist),sessionid:data.sessionid})})}break;case"pskill":if(data.value){try{process.kill(data.value)}catch(e){sendConsoleText("pskill: "+JSON.stringify(e))}}break;case"openUrl":sendConsoleText("OpenURL: "+data.url);if(data.url){mesh.SendCommand({action:"msg",type:"openUrl",url:data.url,sessionid:data.sessionid,success:(openUserDesktopUrl(data.url)!=null)})}break;case"getclip":if(require("MeshAgent").isService){require("clipboard").dispatchRead().then(function(str){mesh.SendCommand({action:"msg",type:"getclip",sessionid:data.sessionid,data:str})})}else{require("clipboard").read().then(function(str){mesh.SendCommand({action:"msg",type:"getclip",sessionid:data.sessionid,data:str})})}break;case"setclip":if(typeof data.data=="string"){if(require("MeshAgent").isService){require("clipboard").dispatchWrite(data.data)}else{require("clipboard")(data.data)}mesh.SendCommand({action:"msg",type:"setclip",sessionid:data.sessionid,success:true})}break;default:break}break;case"wakeonlan":sendConsoleText("Server requesting wake-on-lan for: "+data.macs.join(", "));for(var i in data.macs){sendWakeOnLan(data.macs[i])}break;case"poweraction":if((mesh.ExecPowerState!=undefined)&&(data.actiontype)){var forced=0;if(data.forced==1){forced=1}data.actiontype=parseInt(data.actiontype);sendConsoleText("Performing power action="+data.actiontype+", forced="+forced+".");var r=mesh.ExecPowerState(data.actiontype,forced);sendConsoleText("ExecPowerState returned code: "+r)}break;case"iplocation":getIpLocationData(function(location){mesh.SendCommand({action:"iplocation",type:"publicip",value:location})});break;case"toast":if(data.title&&data.msg){try{require("toaster").Toast(data.title,data.msg)}catch(ex){}}break;case"openUrl":sendConsoleText("OpenURL: "+data.url);if(data.url){mesh.SendCommand({action:"openUrl",url:data.url,sessionid:data.sessionid,success:(openUserDesktopUrl(data.url)!=null)})}break;case"amtPolicy":amtPolicy=data.amtPolicy;if(data.amtPolicy!=null){db.Put("amtPolicy",JSON.stringify(data.amtPolicy))}else{db.Put("amtPolicy",null)}if(amt!=null){amt.setPolicy(amtPolicy)}break;case"getScript":sendConsoleText("getScript: "+JSON.stringify(data));break;case"ping":mesh.SendCommand('{"action":"pong"}');break;case"pong":break;default:break}}}function getDirectoryInfo(reqpath){var response={path:reqpath,dir:[]};if(((reqpath==undefined)||(reqpath==""))&&(process.platform=="win32")){var results=null;try{results=fs.readDrivesSync()}catch(e){}if(results!=null){for(var i=0;i<results.length;++i){var drive={n:results[i].name,t:1};if(results[i].type=="REMOVABLE"){drive.dt="removable"}response.dir.push(drive)}}}else{if(reqpath==""){reqpath="/"}var results=null,xpath=obj.path.join(reqpath,"*");try{results=fs.readdirSync(xpath)}catch(e){}if(results!=null){for(var i=0;i<results.length;++i){if((results[i]!=".")&&(results[i]!="..")){var stat=null,p=obj.path.join(reqpath,results[i]);try{stat=fs.statSync(p)}catch(e){}if((stat!=null)&&(stat!=undefined)){if(stat.isDirectory()==true){response.dir.push({n:results[i],t:2,d:stat.mtime})}else{response.dir.push({n:results[i],t:3,s:stat.size,d:stat.mtime})}}}}}}return response}function onTunnelUpgrade(response,s,head){this.s=s;s.httprequest=this;s.end=onTunnelClosed;s.tunnel=this;if(this.tcpport!=null){s.pause();s.data=onTcpRelayServerTunnelData;var connectionOptions={port:parseInt(this.tcpport)};if(this.tcpaddr!=null){connectionOptions.host=this.tcpaddr}else{connectionOptions.host="127.0.0.1"}s.tcprelay=net.createConnection(connectionOptions,onTcpRelayTargetTunnelConnect);s.tcprelay.peerindex=this.index}if(this.udpport!=null){s.data=onUdpRelayServerTunnelData;s.udprelay=require("dgram").createSocket({type:"udp4"});s.udprelay.bind({port:0});s.udprelay.peerindex=this.index;s.udprelay.on("message",onUdpRelayTargetTunnelConnect);s.udprelay.udpport=this.udpport;s.udprelay.udpaddr=this.udpaddr;s.udprelay.first=true}else{s.data=onTunnelData}}function onUdpRelayTargetTunnelConnect(data){var peerTunnel=tunnels[this.peerindex];peerTunnel.s.write(data)}function onUdpRelayServerTunnelData(data){if(this.udprelay.first===true){delete this.udprelay.first}else{this.udprelay.send(data,parseInt(this.udprelay.udpport),this.udprelay.udpaddr?this.udprelay.udpaddr:"127.0.0.1")}}function onTcpRelayTargetTunnelConnect(){var peerTunnel=tunnels[this.peerindex];this.pipe(peerTunnel.s);peerTunnel.s.first=true;peerTunnel.s.resume()}function onTcpRelayServerTunnelData(data){if(this.first==true){this.first=false;this.pipe(this.tcprelay)}}function onTunnelClosed(){if(tunnels[this.httprequest.index]==null){return}delete tunnels[this.httprequest.index];if(this.httprequest.uploadFile){fs.closeSync(this.httprequest.uploadFile);this.httprequest.uploadFile=undefined}if(this.httprequest.downloadFile){fs.closeSync(this.httprequest.downloadFile);this.httprequest.downloadFile=undefined}if(this.webrtc!=null){if(this.webrtc.rtcchannel){try{this.webrtc.rtcchannel.close()}catch(e){}this.webrtc.rtcchannel.removeAllListeners("data");this.webrtc.rtcchannel.removeAllListeners("end");delete this.webrtc.rtcchannel}if(this.webrtc.websocket){delete this.webrtc.websocket}try{this.webrtc.close()}catch(e){}this.webrtc.removeAllListeners("connected");this.webrtc.removeAllListeners("disconnected");this.webrtc.removeAllListeners("dataChannel");delete this.webrtc}this.removeAllListeners("data")}function onTunnelSendOk(){}function onTunnelData(data){if(this.httprequest.uploadFile){try{fs.writeSync(this.httprequest.uploadFile,data)}catch(e){this.write(new Buffer(JSON.stringify({action:"uploaderror"})));return}this.write(new Buffer(JSON.stringify({action:"uploadack",reqid:this.httprequest.uploadFileid})));return}if(this.httprequest.state==0){if(data=="c"){this.httprequest.state=1}}else{if(this.httprequest.protocol==0){this.httprequest.protocol=parseInt(data);if(typeof this.httprequest.protocol!="number"){this.httprequest.protocol=0}if(this.httprequest.protocol==1){if(((this.httprequest.rights&MESHRIGHT_REMOTECONTROL)==0)||((this.httprequest.rights!=4294967295)&&((this.httprequest.rights&MESHRIGHT_NOTERMINAL)!=0))){this.httprequest.protocol=999999;this.httprequest.s.end();sendConsoleText("Error: No Terminal Control Rights.");return}if(this.httprequest.consent&&(this.httprequest.consent&2)){try{require("toaster").Toast("MeshCentral",this.httprequest.username+" started a remote terminal session.")}catch(ex){}}if(process.platform=="win32"){this.httprequest._term=require("win-terminal").Start(80,25);this.httprequest._term.pipe(this,{dataTypeSkip:1});this.pipe(this.httprequest._term,{dataTypeSkip:1,end:false});this.prependListener("end",function(){this.httprequest._term.end(function(){console.log("Terminal was closed")})})}else{if(fs.existsSync("/bin/bash")){this.httprequest.process=childProcess.execFile("/bin/bash",["bash","-i"],{type:childProcess.SpawnTypes.TERM});if(process.platform=="linux"){this.httprequest.process.stdin.write("stty erase ^H\nalias ls='ls --color=auto'\nPS1='\\u@\\h:\\w\\$ '\nclear\n")}}else{this.httprequest.process=childProcess.execFile("/bin/sh",["sh"],{type:childProcess.SpawnTypes.TERM})}this.httprequest.process.tunnel=this;this.httprequest.process.on("exit",function(ecode,sig){this.tunnel.end()});this.httprequest.process.stderr.on("data",function(chunk){this.parent.tunnel.write(chunk)});this.httprequest.process.stdout.pipe(this,{dataTypeSkip:1});this.pipe(this.httprequest.process.stdin,{dataTypeSkip:1,end:false});this.prependListener("end",function(){this.httprequest.process.kill()})}this.end=function(){if(process.platform=="win32"){this.unpipe(this.httprequest._term);this.httprequest._term.unpipe(this);if(this.rtcchannel){this.rtcchannel.unpipe(this.httprequest._term);this.httprequest._term.unpipe(this.rtcchannel)}this.httprequest._term.end();this.httprequest._term=null}else{}};this.removeAllListeners("data");this.on("data",onTunnelControlData)}else{if(this.httprequest.protocol==2){if(((this.httprequest.rights&MESHRIGHT_REMOTECONTROL)==0)&&((this.httprequest.rights&MESHRIGHT_REMOTEVIEW)==0)){this.httprequest.protocol=999999;this.httprequest.s.end();sendConsoleText("Error: No Desktop Control Rights.");return}if(this.httprequest.consent&&(this.httprequest.consent&1)){try{require("toaster").Toast("MeshCentral",this.httprequest.username+" started a remote desktop session.")}catch(ex){}}this.httprequest.desktop={state:0,kvm:mesh.getRemoteDesktopStream(),tunnel:this};this.httprequest.desktop.kvm.parent=this.httprequest.desktop;this.desktop=this.httprequest.desktop;this.end=function(){--this.desktop.kvm.connectionCount;this.unpipe(this.httprequest.desktop.kvm);this.httprequest.desktop.kvm.unpipe(this);if(this.rtcchannel){this.rtcchannel.unpipe(this.httprequest.desktop.kvm);this.httprequest.desktop.kvm.unpipe(this.rtcchannel)}if(this.desktop.kvm.connectionCount==0){this.httprequest.desktop.kvm.end()}};if(this.httprequest.desktop.kvm.hasOwnProperty("connectionCount")){this.httprequest.desktop.kvm.connectionCount++}else{this.httprequest.desktop.kvm.connectionCount=1}if((this.httprequest.rights==4294967295)||(((this.httprequest.rights&MESHRIGHT_REMOTECONTROL)!=0)&&((this.httprequest.rights&MESHRIGHT_REMOTEVIEW)==0))){this.pipe(this.httprequest.desktop.kvm,{dataTypeSkip:1,end:false})}else{}this.httprequest.desktop.kvm.pipe(this,{dataTypeSkip:1});this.removeAllListeners("data");this.on("data",onTunnelControlData)}else{if(this.httprequest.protocol==5){if(((this.httprequest.rights&MESHRIGHT_REMOTECONTROL)==0)||((this.httprequest.rights!=4294967295)&&((this.httprequest.rights&MESHRIGHT_NOFILES)!=0))){this.httprequest.protocol=999999;this.httprequest.s.end();sendConsoleText("Error: No Files Control Rights.");return}if(this.httprequest.consent&&(this.httprequest.consent&4)){try{require("toaster").Toast("MeshCentral",this.httprequest.username+" started a remote file access.")}catch(ex){}}}}}}else{if(this.httprequest.protocol==1){this.httprequest.process.write(data)}else{if(this.httprequest.protocol==2){if(this.httprequest.desktop.state==0){this.write(new Buffer(String.fromCharCode(17,254,0,0,77,69,83,72,0,0,0,0,2)));this.httprequest.desktop.state=1}else{this.httprequest.desktop.write(data)}}else{if(this.httprequest.protocol==5){var cmd=null;try{cmd=JSON.parse(data)}catch(e){}if(cmd==null){return}if((cmd.ctrlChannel=="102938")||((cmd.type=="offer")&&(cmd.sdp!=null))){onTunnelControlData(cmd,this);return}if(cmd.action==undefined){return}if((cmd.path!=null)&&(process.platform!="win32")&&(cmd.path[0]!="/")){cmd.path="/"+cmd.path}switch(cmd.action){case"ls":var response=getDirectoryInfo(cmd.path);if(cmd.reqid!=undefined){response.reqid=cmd.reqid}this.write(new Buffer(JSON.stringify(response)));break;case"mkdir":fs.mkdirSync(cmd.path);break;case"rm":for(var i in cmd.delfiles){try{deleteFolderRecursive(obj.path.join(cmd.path,cmd.delfiles[i]),cmd.rec)}catch(e){}}break;case"rename":var oldfullpath=obj.path.join(cmd.path,cmd.oldname);var newfullpath=obj.path.join(cmd.path,cmd.newname);try{fs.renameSync(oldfullpath,newfullpath)}catch(e){console.log(e)}break;case"download":var sendNextBlock=0;if(cmd.sub=="start"){if(this.filedownload!=null){this.write({action:"download",sub:"cancel",id:this.filedownload.id});delete this.filedownload}this.filedownload={id:cmd.id,path:cmd.path,ptr:0};try{this.filedownload.f=fs.openSync(this.filedownload.path,"rbN")}catch(e){this.write({action:"download",sub:"cancel",id:this.filedownload.id});delete this.filedownload}if(this.filedownload){this.write({action:"download",sub:"start",id:cmd.id})}}else{if((this.filedownload!=null)&&(cmd.id==this.filedownload.id)){if(cmd.sub=="startack"){sendNextBlock=8}else{if(cmd.sub=="stop"){delete this.filedownload}else{if(cmd.sub=="ack"){sendNextBlock=1}}}}}while(sendNextBlock>0){sendNextBlock--;var buf=new Buffer(4096);var len=fs.readSync(this.filedownload.f,buf,4,4092,null);this.filedownload.ptr+=len;if(len<4092){buf.writeInt32BE(16777217,0);fs.closeSync(this.filedownload.f);delete this.filedownload;sendNextBlock=0}else{buf.writeInt32BE(16777216,0)}this.write(buf.slice(0,len+4))}break;case"upload":if(this.httprequest.uploadFile!=undefined){fs.closeSync(this.httprequest.uploadFile);this.httprequest.uploadFile=undefined}if(cmd.path==undefined){break}var filepath=cmd.name?obj.path.join(cmd.path,cmd.name):cmd.path;try{this.httprequest.uploadFile=fs.openSync(filepath,"wbN")}catch(e){this.write(new Buffer(JSON.stringify({action:"uploaderror",reqid:cmd.reqid})));break}this.httprequest.uploadFileid=cmd.reqid;if(this.httprequest.uploadFile){this.write(new Buffer(JSON.stringify({action:"uploadstart",reqid:this.httprequest.uploadFileid})))}break;case"copy":for(var i in cmd.names){var sc=obj.path.join(cmd.scpath,cmd.names[i]),ds=obj.path.join(cmd.dspath,cmd.names[i]);if(sc!=ds){try{fs.copyFileSync(sc,ds)}catch(e){}}}break;case"move":for(var i in cmd.names){var sc=obj.path.join(cmd.scpath,cmd.names[i]),ds=obj.path.join(cmd.dspath,cmd.names[i]);if(sc!=ds){try{fs.copyFileSync(sc,ds);fs.unlinkSync(sc)}catch(e){}}}break;default:break}}}}}}}function deleteFolderRecursive(path,rec){if(fs.existsSync(path)){if(rec==true){fs.readdirSync(obj.path.join(path,"*")).forEach(function(file,index){var curPath=obj.path.join(path,file);if(fs.statSync(curPath).isDirectory()){deleteFolderRecursive(curPath,true)}else{fs.unlinkSync(curPath)}})}fs.unlinkSync(path)}}function onTunnelWebRTCControlData(data){if(typeof data!="string"){return}var obj;try{obj=JSON.parse(data)}catch(e){sendConsoleText("Invalid control JSON on WebRTC: "+data);return}if(obj.type=="close"){try{this.close()}catch(e){}try{this.xrtc.close()}catch(e){}}}function onTunnelControlData(data,ws){var obj;if(ws==null){ws=this}if(typeof data=="string"){try{obj=JSON.parse(data)}catch(e){sendConsoleText("Invalid control JSON: "+data);return}}else{if(typeof data=="object"){obj=data}else{return}}if(obj.action){switch(obj.action){case"lock":try{if(process.platform=="win32"){var child=require("child_process");child.execFile(process.env.windir+"\\system32\\cmd.exe",["/c","RunDll32.exe user32.dll,LockWorkStation"],{type:1})}}catch(e){}break;default:break}return}if(obj.type=="close"){try{ws.close()}catch(e){}}else{if(obj.type=="webrtc0"){if(ws.httprequest.protocol==1){if(process.platform=="win32"){ws.httprequest._term.unpipe(ws)}else{ws.httprequest.process.stdout.unpipe(ws);ws.httprequest.process.stderr.unpipe(ws)}}else{if(ws.httprequest.protocol==2){ws.httprequest.desktop.kvm.unpipe(ws)}else{ws.rtcchannel.httprequest=ws.httprequest;ws.rtcchannel.removeAllListeners("data");ws.rtcchannel.on("data",onTunnelData)}}ws.write('{"ctrlChannel":"102938","type":"webrtc1"}')}else{if(obj.type=="webrtc1"){if(ws.httprequest.protocol==1){if(process.platform=="win32"){ws.unpipe(ws.httprequest._term);ws.rtcchannel.pipe(ws.httprequest._term,{dataTypeSkip:1})}else{ws.unpipe(ws.httprequest.process.stdin);ws.rtcchannel.pipe(ws.httprequest.process.stdin,{dataTypeSkip:1})}ws.resume()}else{if(ws.httprequest.protocol==2){ws.unpipe(ws.httprequest.desktop.kvm);try{ws.webrtc.rtcchannel.pipe(ws.httprequest.desktop.kvm,{dataTypeSkip:1,end:false})}catch(e){sendConsoleText("EX2")}ws.resume()}}ws.write('{"ctrlChannel":"102938","type":"webrtc2"}')}else{if(obj.type=="webrtc2"){if(ws.httprequest.protocol==1){if(process.platform=="win32"){ws.httprequest._term.pipe(ws.webrtc.rtcchannel,{dataTypeSkip:1,end:false})}else{ws.httprequest.process.stdout.pipe(ws.webrtc.rtcchannel,{dataTypeSkip:1,end:false});ws.httprequest.process.stderr.pipe(ws.webrtc.rtcchannel,{dataTypeSkip:1,end:false})}}else{if(ws.httprequest.protocol==2){ws.httprequest.desktop.kvm.pipe(ws.webrtc.rtcchannel,{dataTypeSkip:1})}}}else{if(obj.type=="offer"){if(ws.httprequest.protocol==1){return}ws.webrtc=rtc.createConnection();ws.webrtc.websocket=ws;ws.webrtc.on("connected",function(){});ws.webrtc.on("disconnected",function(){});ws.webrtc.on("dataChannel",function(rtcchannel){rtcchannel.xrtc=this;rtcchannel.websocket=this.websocket;this.rtcchannel=rtcchannel;this.websocket.rtcchannel=rtcchannel;this.websocket.rtcchannel.on("data",onTunnelWebRTCControlData);this.websocket.rtcchannel.on("end",function(){if(this.websocket.desktop&&this.websocket.desktop.kvm){this.unpipe(this.websocket.desktop.kvm);this.websocket.httprequest.desktop.kvm.unpipe(this)}});this.websocket.write('{"ctrlChannel":"102938","type":"webrtc0"}')});var sdp=null;try{sdp=ws.webrtc.setOffer(obj.sdp)}catch(ex){}if(sdp!=null){ws.write({type:"answer",ctrlChannel:"102938",sdp:sdp})}}}}}}}var consoleWebSockets={};var consoleHttpRequest=null;function consoleHttpResponse(response){response.data=function(data){sendConsoleText(rstr2hex(buf2rstr(data)),this.sessionid);consoleHttpRequest=null};response.close=function(){sendConsoleText("httprequest.response.close",this.sessionid);consoleHttpRequest=null}}function openUserDesktopUrl(url){var child=null;try{switch(process.platform){case"win32":child=require("child_process").execFile(process.env.windir+"\\system32\\cmd.exe",["/c","start",url],{type:childProcess.SpawnTypes.USER});break;case"linux":child=require("child_process").execFile("/usr/bin/xdg-open",["xdg-open",url],{uid:require("user-sessions").consoleUid()});break;case"darwin":child=require("child_process").execFile("/usr/bin/open",["open",url],{uid:require("user-sessions").consoleUid()});break;default:break}}catch(ex){}return child}function processConsoleCommand(cmd,args,rights,sessionid){try{var response=null;switch(cmd){case"help":response="Available commands: help, info, osinfo,args, print, type, dbget, dbset, dbcompact, eval, parseuri, httpget,\r\nwslist, wsconnect, wssend, wsclose, notify, ls, ps, kill, amt, netinfo, location, power, wakeonlan, scanwifi,\r\nscanamt, setdebug, smbios, rawsmbios, toast, lock, users, sendcaps, openurl, amtreset, amtccm, amtdeactivate,\r\namtpolicy, getscript, getclip, setclip.";break;case"getclip":if(require("MeshAgent").isService){require("clipboard").dispatchRead().then(function(str){sendConsoleText(str,sessionid)})}else{require("clipboard").read().then(function(str){sendConsoleText(str,sessionid)})}break;case"setclip":if(args._.length!=1){response='Proper usage: setclip "sample text"'}else{if(require("MeshAgent").isService){require("clipboard").dispatchWrite(args._[0]);response='Setting clipboard to: "'+args._[0]+'"'}else{require("clipboard")(args._[0]);response='Setting clipboard to: "'+args._[0]+'"'}}break;case"amtreset":if(amt!=null){amt.reset();response="Done."}break;case"amtlmsreset":if(amt!=null){amt.lmsreset();response="Done."}break;case"amtccm":if(amt==null){response="Intel AMT not supported."}else{if(args._.length!=1){response="Proper usage: amtccm (adminPassword)"}else{amt.setPolicy({type:0});amt.activeToCCM(args._[0])}}break;case"amtdeactivate":if(amt==null){response="Intel AMT not supported."}else{amt.setPolicy({type:0});amt.deactivateCCM()}break;case"amtpolicy":if(amtPolicy==null){response="No Intel(R) AMT policy."}else{response=JSON.stringify(amtPolicy)}break;case"openurl":if(args._.length!=1){response="Proper usage: openurl (url)"}else{if(openUserDesktopUrl(args._[0])==null){response="Failed."}else{response="Success."}}break;case"users":if(meshCoreObj.users==null){response="Active users are unknown."}else{response="Active Users: "+meshCoreObj.users.join(", ")+"."}require("user-sessions").enumerateUsers().then(function(u){for(var i in u){sendConsoleText(u[i])}});break;case"toast":if(process.platform=="win32"){if(args._.length<1){response='Proper usage: toast "message"'}else{try{require("toaster").Toast("MeshCentral",args._[0]);response="ok"}catch(ex){response=ex}}}else{response="Only supported on Windows."}break;case"setdebug":if(args._.length<1){response="Proper usage: setdebug (target), 0 = Disabled, 1 = StdOut, 2 = This Console, * = All Consoles, 4 = WebLog, 8 = Logfile"}else{if(args._[0]=="*"){console.setDestination(2)}else{console.setDestination(parseInt(args._[0]),sessionid)}}break;case"ps":processManager.getProcesses(function(plist){var x="";for(var i in plist){x+=i+", "+plist[i].cmd+((plist[i].user)?(", "+plist[i].user):"")+"\r\n"}sendConsoleText(x,sessionid)});break;case"kill":if((args._.length<1)){response="Proper usage: kill [pid]"}else{process.kill(parseInt(args._[0]));response="Killed process "+args._[0]+"."}break;case"smbios":if(SMBiosTables==null){response="SMBios tables not available."}else{response=objToString(SMBiosTables,0," ",true)}break;case"rawsmbios":if(SMBiosTablesRaw==null){response="SMBios tables not available."}else{response="";for(var i in SMBiosTablesRaw){var header=false;for(var j in SMBiosTablesRaw[i]){if(SMBiosTablesRaw[i][j].length>0){if(header==false){response+=("Table type #"+i+((require("smbios").smTableTypes[i]==null)?"":(", "+require("smbios").smTableTypes[i])))+"\r\n";header=true}response+=(" "+SMBiosTablesRaw[i][j].toString("hex"))+"\r\n"}}}}break;case"eval":if(args._.length<1){response='Proper usage: eval "JavaScript code"'}else{response=JSON.stringify(mesh.eval(args._[0]))}break;case"notify":if(args._.length!=1){response='Proper usage: notify "message" [--session]'}else{var notification={action:"msg",type:"notify",value:args._[0],tag:"console"};if(args.session){notification.sessionid=sessionid}mesh.SendCommand(notification);response="ok"}break;case"info":response="Current Core: "+meshCoreObj.value+".\r\nAgent Time: "+Date()+".\r\nUser Rights: 0x"+rights.toString(16)+".\r\nPlatform: "+process.platform+".\r\nCapabilities: "+meshCoreObj.caps+".\r\nServer URL: "+mesh.ServerUrl+".";if(amt!=null){response+="\r\nBuilt-in LMS: "+["Disabled","Connecting..","Connected"][amt.lmsstate]+"."}if(meshCoreObj.osdesc){response+="\r\nOS: "+meshCoreObj.osdesc+"."}response+="\r\nModules: "+addedModules.join(", ")+".";response+="\r\nServer Connection: "+mesh.isControlChannelConnected+", State: "+meshServerConnectionState+".";response+="\rlastMeInfo: "+lastMeInfo+".";var oldNodeId=db.Get("OldNodeId");if(oldNodeId!=null){response+="\r\nOldNodeID: "+oldNodeId+"."}if(process.platform!="win32"){response+="\r\nX11 support: "+require("monitor-info").kvm_x11_support+"."}break;case"osinfo":var i=1;if(args._.length>0){i=parseInt(args._[0]);if(i>8){i=8}response="Calling "+i+" times."}for(var j=0;j<i;j++){var pr=require("os").name();pr.sessionid=sessionid;pr.then(function(v){sendConsoleText("OS: "+v,this.sessionid)})}break;case"sendcaps":if(args._.length==0){response="Proper usage: sendcaps (number)"}else{meshCoreObj.caps=parseInt(args._[0]);mesh.SendCommand(meshCoreObj);response=JSON.stringify(meshCoreObj)}break;case"sendosdesc":if(args._.length>0){meshCoreObj.osdesc=args._[0];mesh.SendCommand(meshCoreObj);response=JSON.stringify(meshCoreObj)}else{response="Proper usage: sendosdesc [os description]"}break;case"args":response="args "+objToString(args,0," ",true);break;case"print":var r=[];for(var i in args._){r.push(args._[i])}console.log(r.join(" "));response="Message printed on agent console.";break;case"type":if(args._.length==0){response="Proper usage: type (filepath) [maxlength]"}else{var max=4096;if((args._.length>1)&&(typeof args._[1]=="number")){max=args._[1]}if(max>4096){max=4096}var buf=Buffer.alloc(max),fd=fs.openSync(args._[0],"r"),r=fs.readSync(fd,buf,0,max);response=buf.toString();var i=response.indexOf("\n");if((i>0)&&(response[i-1]!="\r")){response=response.split("\n").join("\r\n")}if(r==max){response+="..."}fs.closeSync(fd)}break;case"dbkeys":response=JSON.stringify(db.Keys);break;case"dbget":if(db==null){response="Database not accessible.";break}if(args._.length!=1){response="Proper usage: dbget (key)"}else{response=db.Get(args._[0])}break;case"dbset":if(db==null){response="Database not accessible.";break}if(args._.length!=2){response="Proper usage: dbset (key) (value)"}else{var r=db.Put(args._[0],args._[1]);response="Key set: "+r}break;case"dbcompact":if(db==null){response="Database not accessible.";break}var r=db.Compact();response="Database compacted: "+r;break;case"httpget":if(consoleHttpRequest!=null){response="HTTP operation already in progress."}else{if(args._.length!=1){response="Proper usage: httpget (url)"}else{var options=http.parseUri(args._[0]);options.method="GET";if(options==null){response="Invalid url."}else{try{consoleHttpRequest=http.request(options,consoleHttpResponse)}catch(e){response="Invalid HTTP GET request"}consoleHttpRequest.sessionid=sessionid;if(consoleHttpRequest!=null){consoleHttpRequest.end();response="HTTPGET "+options.protocol+"//"+options.host+":"+options.port+options.path}}}}break;case"wslist":response="";for(var i in consoleWebSockets){var httprequest=consoleWebSockets[i];response+="Websocket #"+i+", "+httprequest.url+"\r\n"}if(response==""){response="no websocket sessions."}break;case"wsconnect":if(args._.length==0){response="Proper usage: wsconnect (url)\r\nFor example: wsconnect wss://localhost:443/meshrelay.ashx?id=abc"}else{var httprequest=null;try{var options=http.parseUri(args._[0]);options.rejectUnauthorized=0;httprequest=http.request(options)}catch(e){response="Invalid HTTP websocket request"}if(httprequest!=null){httprequest.upgrade=onWebSocketUpgrade;httprequest.on("error",function(e){sendConsoleText("ERROR: "+JSON.stringify(e))});var index=1;while(consoleWebSockets[index]){index++}httprequest.sessionid=sessionid;httprequest.index=index;httprequest.url=args._[0];consoleWebSockets[index]=httprequest;response="New websocket session #"+index}}break;case"wssend":if(args._.length==0){response="Proper usage: wssend (socketnumber)\r\n";for(var i in consoleWebSockets){var httprequest=consoleWebSockets[i];response+="Websocket #"+i+", "+httprequest.url+"\r\n"}}else{var i=parseInt(args._[0]);var httprequest=consoleWebSockets[i];if(httprequest!=undefined){httprequest.s.write(args._[1]);response="ok"}else{response="Invalid web socket number"}}break;case"wsclose":if(args._.length==0){response="Proper usage: wsclose (socketnumber)"}else{var i=parseInt(args._[0]);var httprequest=consoleWebSockets[i];if(httprequest!=undefined){if(httprequest.s!=null){httprequest.s.end()}else{httprequest.end()}response="ok"}else{response="Invalid web socket number"}}break;case"tunnels":response="";for(var i in tunnels){response+="Tunnel #"+i+", "+tunnels[i].url+"\r\n"}if(response==""){response="No websocket sessions."}break;case"ls":response="";var xpath="*";if(args._.length>0){xpath=obj.path.join(args._[0],"*")}response="List of "+xpath+"\r\n";var results=fs.readdirSync(xpath);for(var i=0;i<results.length;++i){var stat=null,p=obj.path.join(args._[0],results[i]);try{stat=fs.statSync(p)}catch(e){}if((stat==null)||(stat==undefined)){response+=(results[i]+"\r\n")}else{response+=(results[i]+" "+((stat.isDirectory())?"(Folder)":"(File)")+"\r\n")}}break;case"lsx":response=objToString(getDirectoryInfo(args._[0]),0," ",true);break;case"lock":if(process.platform=="win32"){var child=require("child_process");child.execFile(process.env.windir+"\\system32\\cmd.exe",["/c","RunDll32.exe user32.dll,LockWorkStation"],{type:1});response="Ok"}else{response="Not supported on the platform"}break;case"amt":if(amt!=null){amt.getAmtInfo(function(state){var resp="Intel AMT not detected.";if(state!=null){resp=objToString(state,0," ",true)}sendConsoleText(resp,sessionid)})}break;case"netinfo":var interfaces=require("os").networkInterfaces();response=objToString(interfaces,0," ",true);break;case"netinfo2":response=objToString(mesh.NetInfo,0," ",true);break;case"wakeonlan":if((args._.length!=1)||(args._[0].length!=12)){response='Proper usage: wakeonlan [mac], for example "wakeonlan 010203040506".'}else{var count=sendWakeOnLan(args._[0]);response="Sent wake-on-lan on "+count+" interface(s)."}break;case"sendall":sendConsoleText(args._.join(" "));break;case"power":if(mesh.ExecPowerState==undefined){response="Power command not supported on this agent."}else{if((args._.length==0)||(typeof args._[0]!="number")){response="Proper usage: power (actionNumber), where actionNumber is:\r\n LOGOFF = 1\r\n SHUTDOWN = 2\r\n REBOOT = 3\r\n SLEEP = 4\r\n HIBERNATE = 5\r\n DISPLAYON = 6\r\n KEEPAWAKE = 7\r\n BEEP = 8\r\n CTRLALTDEL = 9\r\n VIBRATE = 13\r\n FLASH = 14"}else{var r=mesh.ExecPowerState(args._[0],args._[1]);response="Power action executed with return code: "+r+"."}}break;case"location":getIpLocationData(function(location){sendConsoleText(objToString({action:"iplocation",type:"publicip",value:location},0," "))});break;case"parseuri":response=JSON.stringify(http.parseUri(args._[0]));break;case"scanwifi":if(wifiScanner!=null){var wifiPresent=wifiScanner.hasWireless;if(wifiPresent){response="Perfoming Wifi scan...";wifiScanner.Scan()}else{response="Wifi absent."}}else{response="Wifi module not present."}break;case"scanamt":if(amtscanner!=null){if(args._.length!=1){response="Usage examples:\r\n scanamt 1.2.3.4\r\n scanamt 1.2.3.0-1.2.3.255\r\n scanamt 1.2.3.0/24\r\n"}else{response="Scanning: "+args._[0]+"...";amtscanner.scan(args._[0],2000,function(data){if(data.length>0){var r="",pstates=["NotActivated","InActivation","Activated"];for(var i in data){var x=data[i];if(r!=""){r+="\r\n"}r+=x.address+" - Intel AMT v"+x.majorVersion+"."+x.minorVersion;if(x.provisioningState<3){r+=(", "+pstates[x.provisioningState])}if(x.provisioningState==2){r+=(", "+x.openPorts.join(", "))}r+="."}}else{r="No Intel AMT found."}sendConsoleText(r)})}}else{response="Intel AMT scanner module not present."}break;case"modules":response=JSON.stringify(addedModules);break;case"getscript":if(args._.length!=1){response="Proper usage: getscript [scriptNumber]."}else{mesh.SendCommand({action:"getScript",type:args._[0]})}break;default:response='Unknown command "'+cmd+'", type "help" for list of avaialble commands.';break}}catch(e){response="Command returned an exception error: "+e;console.log(e)}if(response!=null){sendConsoleText(response,sessionid)}}function sendConsoleText(text,sessionid){if(typeof text=="object"){text=JSON.stringify(text)}mesh.SendCommand({action:"msg",type:"console",value:text,sessionid:sessionid})}function handleServerConnection(state){meshServerConnectionState=state;if(meshServerConnectionState==0){if(selfInfoUpdateTimer!=null){clearInterval(selfInfoUpdateTimer);selfInfoUpdateTimer=null}lastSelfInfo=null}else{var oldNodeId=db.Get("OldNodeId");if(oldNodeId!=null){mesh.SendCommand({action:"mc1migration",oldnodeid:oldNodeId})}mesh.SendCommand(meshCoreObj);if(SMBiosTablesRaw!=null){mesh.SendCommand({action:"smbios",value:SMBiosTablesRaw})}meInfoStr=null;sendPeriodicServerUpdate()}}var sendNetworkUpdateNagleTimer=null;function sendNetworkUpdateNagle(){if(sendNetworkUpdateNagleTimer!=null){clearTimeout(sendNetworkUpdateNagleTimer);sendNetworkUpdateNagleTimer=null}sendNetworkUpdateNagleTimer=setTimeout(sendNetworkUpdate,5000)}function sendNetworkUpdate(force){sendNetworkUpdateNagleTimer=null;var netInfo=mesh.NetInfo;if(netInfo){netInfo.action="netinfo";var netInfoStr=JSON.stringify(netInfo);if((force==true)||(clearGatewayMac(netInfoStr)!=clearGatewayMac(lastNetworkInfo))){mesh.SendCommand(netInfo);lastNetworkInfo=netInfoStr}}}function sendPeriodicServerUpdate(flags){if(meshServerConnectionState==0){return}if(!flags){flags=4294967295}if((flags&1)&&(amt!=null)){amt.getAmtInfo(function(meinfo){try{if(meinfo==null){return}var intelamt={},p=false;if(meinfo.Versions&&meinfo.Versions.AMT){intelamt.ver=meinfo.Versions.AMT;p=true}if(meinfo.ProvisioningState){intelamt.state=meinfo.ProvisioningState;p=true}if(meinfo.Flags){intelamt.flags=meinfo.Flags;p=true}if(meinfo.OsHostname){intelamt.host=meinfo.OsHostname;p=true}if(meinfo.UUID){intelamt.uuid=meinfo.UUID;p=true}if(p==true){var meInfoStr=JSON.stringify(intelamt);if(meInfoStr!=lastMeInfo){meshCoreObj.intelamt=intelamt;mesh.SendCommand(meshCoreObj);lastMeInfo=meInfoStr}}}catch(ex){}})}if(flags&2){sendNetworkUpdateNagle(false)}}obj.start=function(){mesh.AddCommandHandler(handleServerCommand);mesh.AddConnectHandler(handleServerConnection);try{var userSession=require("user-sessions");userSession.on("changed",function onUserSessionChanged(){userSession.enumerateUsers().then(function(users){var u=[],a=users.Active;for(var i=0;i<a.length;i++){var un=a[i].Domain?(a[i].Domain+"\\"+a[i].Username):(a[i].Username);if(u.indexOf(un)==-1){u.push(un)}}meshCoreObj.users=u;mesh.SendCommand(meshCoreObj)})});userSession.emit("changed")}catch(ex){}};obj.stop=function(){mesh.AddCommandHandler(null);mesh.AddConnectHandler(null)};function onWebSocketClosed(){sendConsoleText("WebSocket #"+this.httprequest.index+" closed.",this.httprequest.sessionid);delete consoleWebSockets[this.httprequest.index]}function onWebSocketData(data){sendConsoleText("Got WebSocket #"+this.httprequest.index+" data: "+data,this.httprequest.sessionid)}function onWebSocketSendOk(){sendConsoleText("WebSocket #"+this.index+" SendOK.",this.sessionid)}function onWebSocketUpgrade(response,s,head){sendConsoleText("WebSocket #"+this.index+" connected.",this.sessionid);this.s=s;s.httprequest=this;s.end=onWebSocketClosed;s.data=onWebSocketData}return obj}try{var xexports=null,mainMeshCore=null;try{xexports=module.exports}catch(e){}if(xexports!=null){module.exports.createMeshCore=createMeshCore}else{mainMeshCore=createMeshCore();mainMeshCore.start(null)}}catch(ex){require("MeshAgent").SendCommand({action:"msg",type:"console",value:"uncaughtException2: "+ex})}; |