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