function AmtManager(c,p,u){var J=function(P){c.SendCommand({action:"msg",type:"console",value:P})};var q=function(P){if(u){J("amt-manager: "+P+"
")}};var g=null,h=0;var e=null,f=0;var d=null;var F=null;var E=null;var j=null;var D=this;D.state=0;D.lmsstate=0;D.onStateChange=null;D.setDebug=function(P){u=P};D.setPolicy=function(P){if(JSON.stringify(j)!=JSON.stringify(P)){j=P;if(l==null){D.applyPolicy()}}};var I=0;D.reset=function(){++I;g=null,h=0,e=null,f=0,D.state=0,D.lmsstate=0;try{var P=require("amt-mei");g=new P();g.on("error",function(R){q("MEI error");g=null;h=-1;D.state=-1;D.onStateChange(h)});g.getVersion(function(R){if(R==null){h=-1;D.state=-1;if(D.onStateChange!=null){D.onStateChange(h)}if(I<10){setTimeout(D.reset,10000)}}else{d=R;h=2;D.state=2;I=0;if(D.onStateChange!=null){D.onStateChange(h)}D.lmsreset()}})}catch(Q){q("MEI exception: "+Q);g=null;h=-1;D.state=-1}};var i=null;D.getAmtInfo=function(Q){if((g==null)||(h<2)){if(Q!=null){Q(null)}return}try{i={Flags:0};g.getProtocolVersion(function(R){if(R!=null){i.MeiVersion=R}});g.getVersion(function(R){if(R){i.Versions={};for(var S in R.Versions){i.Versions[R.Versions[S].Description]=R.Versions[S].Version}}});g.getProvisioningMode(function(R){if(R){i.ProvisioningMode=R.mode}});g.getProvisioningState(function(R){if(R){i.ProvisioningState=R.state}});g.getEHBCState(function(R){if((R!=null)&&(R.EHBC==true)){i.Flags+=1}});g.getControlMode(function(R){if(R!=null){if(R.controlMode==1){i.Flags+=2}if(R.controlMode==2){i.Flags+=4}}});g.getLanInterfaceSettings(0,function(R){if(R){i.net0=R}});g.getLanInterfaceSettings(1,function(R){if(R){i.net1=R}});g.getUuid(function(R){if((R!=null)&&(R.uuid!=null)){i.UUID=R.uuid}});g.getDnsSuffix(function(R){if(R!=null){i.dns=R}if(Q!=null){Q(i)}})}catch(P){if(Q!=null){Q(null)}return}};var s=function(S){if((S==null)||(S.Body==null)||(S.Body.MessageID==null)||(S.Body.MessageArguments==null)){return null}var P=S.Body.MessageID,Q=S.Body.MessageArguments[0],R=null;switch(P){case"iAMT0050":if(Q=="48"){R="Intel® AMT Serial-over-LAN connected"}else{if(Q=="49"){R="Intel® AMT Serial-over-LAN disconnected"}}break;case"iAMT0052":if(Q=="1"){R="Intel® AMT KVM connected"}else{if(Q=="2"){R="Intel® AMT KVM disconnected"}}break;default:break}if(R!=null){c.SendCommand({action:"msg",type:"notify",value:R,tag:"general"})}};D.lmsreset=function(){var P=null,Q=0;D.lmsstate=0;try{var S=require("amt-lme");Q=1;D.lmsstate=1;P=new S();P.on("error",function(T){Q=0;D.lmsstate=0;P=null;q("LMS error");K(1)});P.on("connect",function(){Q=2;D.lmsstate=2;q("LMS connected");K(2)});P.on("notify",function(U,V,W,T){if(T=="iAMT0052-3"){x()}else{s(U)}})}catch(R){Q=-1;D.lmsstate=-1;P=null}};var K=function(P){if((g==null)||(h<2)||(d==null)){return}g.getLocalSystemAccount(function(W){if(W==null){return}var U=require("amt-wsman-duk");var V=require("amt-wsman");var Q=require("amt");F=new V(U,"127.0.0.1",16992,W.user,W.pass,false);E=new Q(F);h=3;D.state=3;if(D.onStateChange!=null){D.onStateChange(h)}if(l==null){D.applyPolicy()}var R=null;try{for(var T in d.Versions){if(d.Versions[T].Description=="AMT"){R=parseInt(d.Versions[T].Version.split(".")[0])}}}catch(S){}if((R!=null)&&(R>=12)){q("KVM data channel setup");x("skip");D.kvmTempTimer=setInterval(function(){x()},2000);z(JSON.stringify({action:"restart",ver:1}))}})};var x=function(P){E.IPS_KVMRedirectionSettingData_DataChannelRead(D.kvmDataGetResponse,P)};var w=function(S,Q,R,T,U){if((U!="skip")&&(T==200)&&(R.Body.ReturnValue==0)){var V=null;try{V=Buffer.from(R.Body.DataMessage,"base64").toString()}catch(P){return}if(V!=null){D.kvmProcessData(R.Body.RealmsBitmap,R.Body.MessageId,V)}}};var M=null;var y=function(S,R,U){var P=null;try{P=JSON.parse(U)}catch(Q){}if((P!=null)&&(P.action)){if(P.action=="present"){z(JSON.stringify({action:"present",ver:1,platform:process.platform}))}if(P.action=="offer"){M={};var T=require("ILibWebRTC");M.webrtc=T.createConnection();M.webrtc.on("connected",function(){});M.webrtc.on("disconnected",function(){D.webRtcCleanUp()});M.webrtc.on("dataChannel",function(V){M.rtcchannel=V;M.kvm=mesh.getRemoteDesktopStream();M.kvm.pipe(M.rtcchannel,{dataTypeSkip:1,end:false});M.rtcchannel.on("end",function(){D.webRtcCleanUp()});M.rtcchannel.on("data",function(W){D.kvmCtrlData(this,W)});M.rtcchannel.pipe(M.kvm,{dataTypeSkip:1,end:false})});z(JSON.stringify({action:"answer",ver:1,sdp:M.webrtc.setOffer(P.sdp)}))}}};var v=function(Q,S){if(S.length>0&&S.charCodeAt(0)!=123){if(D.fileupload!=null){S=Buffer.from(S,"base64");var Y=S.readUInt32BE(0);if((Y==16777216)||(Y==16777217)){fs.writeSync(D.fileupload.fp,S.slice(4));Q.write({action:"upload",sub:"ack",reqid:D.fileupload.reqid});if(Y==16777217){fs.closeSync(D.fileupload.fp);D.fileupload=null}}}return}q("KVM Ctrl Data: "+S);try{S=JSON.parse(S)}catch(V){q("Invalid JSON: "+S);return}if((S.path!=null)&&(process.platform!="win32")&&(S.path[0]!="/")){S.path="/"+S.path}switch(S.action){case"ping":Q.write({action:"pong"});break;case"lock":if(process.platform=="win32"){var R=require("child_process");R.execFile(process.env.windir+"\\system32\\cmd.exe",["/c","RunDll32.exe user32.dll,LockWorkStation"],{type:1})}break;case"ls":var ab=getDirectoryInfo(S.path);if(S.reqid!=undefined){ab.reqid=S.reqid}Q.write(ab);break;case"mkdir":fs.mkdirSync(S.path);break;case"rm":for(var Z in S.delfiles){var X=G.join(S.path,S.delfiles[Z]);try{fs.unlinkSync(X)}catch(U){q(U)}}break;case"rename":try{fs.renameSync(G.join(S.path,S.oldname),G.join(S.path,S.newname))}catch(U){q(U)}break;case"download":var ad=0;if(S.sub=="start"){if(D.filedownload!=null){Q.write({action:"download",sub:"cancel",id:D.filedownload.id});delete D.filedownload}D.filedownload={id:S.id,path:S.path,ptr:0};try{D.filedownload.f=fs.openSync(D.filedownload.path,"rbN")}catch(U){Q.write({action:"download",sub:"cancel",id:D.filedownload.id});delete D.filedownload}if(D.filedownload){Q.write({action:"download",sub:"start",id:S.id})}}else{if((D.filedownload!=null)&&(S.id==D.filedownload.id)){if(S.sub=="startack"){ad=8}else{if(S.sub=="stop"){delete D.filedownload}else{if(S.sub=="ack"){ad=1}}}}}while(ad>0){ad--;var P=Buffer.alloc(4096);var aa=fs.readSync(D.filedownload.f,P,4,4092,null);D.filedownload.ptr+=aa;if(aa<4092){P.writeInt32BE(16777217,0);fs.closeSync(D.filedownload.f);delete D.filedownload;ad=0}else{P.writeInt32BE(16777216,0)}Q.write(P.slice(0,aa+4).toString("base64"))}break;case"upload":if(S.sub=="start"){if(D.fileupload!=null){fs.closeSync(D.fileupload.fp)}if(!S.path||!S.name){break}D.fileupload={reqid:S.reqid};var W=G.join(S.path,S.name);try{D.fileupload.fp=fs.openSync(W,"wbN")}catch(U){}if(D.fileupload.fp){Q.write({action:"upload",sub:"start",reqid:D.fileupload.reqid})}else{D.fileupload=null;Q.write({action:"upload",sub:"error",reqid:D.fileupload.reqid})}}else{if(S.sub=="cancel"){if(D.fileupload!=null){fs.closeSync(D.fileupload.fp);D.fileupload=null}}}break;case"copy":for(var Z in S.names){var ac=G.join(S.scpath,S.names[Z]),T=G.join(S.dspath,S.names[Z]);if(ac!=T){try{fs.copyFileSync(ac,T)}catch(U){}}}break;case"move":for(var Z in S.names){var ac=G.join(S.scpath,S.names[Z]),T=G.join(S.dspath,S.names[Z]);if(ac!=T){try{fs.copyFileSync(ac,T);fs.unlinkSync(ac)}catch(U){}}}break;default:q("Invalid KVM command: "+S);break}};var L=function(){q("webRtcCleanUp");if(M==null){return}if(M.rtcchannel){try{M.rtcchannel.close()}catch(P){}try{M.rtcchannel.removeAllListeners("data")}catch(P){}try{M.rtcchannel.removeAllListeners("end")}catch(P){}delete M.rtcchannel}if(M.webrtc){try{M.webrtc.close()}catch(P){}try{M.webrtc.removeAllListeners("connected")}catch(P){}try{M.webrtc.removeAllListeners("disconnected")}catch(P){}try{M.webrtc.removeAllListeners("dataChannel")}catch(P){}delete M.webrtc}if(M.kvm){try{M.kvm.end()}catch(P){}delete M.kvm}M=null};var z=function(P){E.IPS_KVMRedirectionSettingData_DataChannelWrite(Buffer.from(P).toString("base64"),function(){})};var r=function(P,Q){if(fs.existsSync(P)){if(Q==true){fs.readdirSync(D.path.join(P,"*")).forEach(function(S,T){var R=D.path.join(P,S);if(fs.statSync(R).isDirectory()){r(R,true)}else{fs.unlinkSync(R)}})}fs.unlinkSync(P)}};var G={join:function(){var R=[];for(var P in arguments){var Q=arguments[P];if(Q!=null){while(Q.endsWith("/")||Q.endsWith("\\")){Q=Q.substring(0,Q.length-1)}if(P!=0){while(Q.startsWith("/")||Q.startsWith("\\")){Q=Q.substring(1)}}R.push(Q)}}if(R.length==0){return"/"}return R.join("/")}};function C(P){return require("MD5Stream").create().syncHash(P).toString("hex")}D.deactivateCCM=function(){g.unprovision(1,function(P){if(P==0){q("Success deactivating Intel AMT CCM.");c.SendCommand({action:"coreinfo",intelamt:{state:0,flags:0}});l=setTimeout(D.applyPolicy,8000)}else{q("Intel AMT CCM deactivation error: "+P)}})};function A(Q){var S="",R="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";for(var P=0;P";k.AMT_RemoteAccessService_AddRemoteAccessPolicyRule(2,0,"AAAAAAAAAAo=",[Q],null,function(U,S,T,V){if(V!=200){q("Add AddRemoteAccessPolicyRule Error "+V)}else{n(P)}})}function n(P){var R=[];if((j.ciraserver!=null)&&(j.ciraserver.home!=null)){R=j.ciraserver.home}var Q=P.AMT_EnvironmentDetectionSettingData.response;Q.DetectionStrings=B(Q.DetectionStrings);if(o(Q.DetectionStrings,R)==false){Q.DetectionStrings=R;k.Put("AMT_EnvironmentDetectionSettingData",Q,function(U,S,T,V){if(V!=200){q("Put AMT_EnvironmentDetectionSettingData Error "+V)}},0,1)}}function o(P,Q){if(P==Q){return true}if(P==null){P=[]}if(Q==null){Q=[]}if(P.length!=Q.length){return false}for(var R in P){if(Q.indexOf(P[R])==-1){return false}}return true}function B(P){if(!P||P==null||typeof P=="object"){return P}return[P]}}module.exports=AmtManager;