1 line
15 KiB
JavaScript
1 line
15 KiB
JavaScript
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+"<br />")}};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<Q;P++){S+=R.charAt(Math.floor(Math.random()*R.length))}return S}D.activeToCCM=function(P){if((P==null)||(P=="")){P="P@0s"+A(23)}t=P;g.getLocalSystemAccount(function(T){if((T!=null)&&T.user&&T.pass){var R=require("amt-wsman-duk");var S=require("amt-wsman");var Q=require("amt");F=new S(R,"127.0.0.1",16992,T.user,T.pass,false);E=new Q(F);E.BatchEnum(null,["*AMT_GeneralSettings","*IPS_HostBasedSetupService"],a,P)}else{q("Unable to get $$OsAdmin password.")}})};var a=function(S,Q,R,T,P){if(T!=200){q("Failed to fetch activation information, status "+T)}else{if(R.IPS_HostBasedSetupService.response.AllowedControlModes.length!=2){q("Client control mode activation not allowed")}else{S.IPS_HostBasedSetupService_Setup(2,C("admin:"+R.AMT_GeneralSettings.response.DigestRealm+":"+P).substring(0,32),null,null,null,null,b)}}};var b=function(R,P,Q,S){if(S!=200){q("Failed to activate, status "+S)}else{if(Q.Body.ReturnValue!=0){q("Client control mode activation failed: "+Q.Body.ReturnValueStr)}else{q("Intel AMT CCM activation success.");p.Put("amtCCMPass",t);c.SendCommand({action:"coreinfo",intelamt:{state:2,flags:2,user:"admin",pass:t}})}}l=setTimeout(D.applyPolicy,8000)};D.start=function(){var P=null;try{P=JSON.parse(p.Get("amtPolicy"))}catch(Q){q("Exception loading amtPolicy")}try{t=p.Get("amtCCMPass")}catch(Q){}if(typeof t!="string"){t=null}D.reset()};var t,O,k,l,H=0;D.applyPolicy=function(){l=null;if((h!=3)||(j==null)||(typeof j!="object")||(typeof j.type!="number")||(j.type==0)){return}if((j.password!=null)&&(j.password!="")){t=j.password}D.getAmtInfo(function(R){if((j.type==1)&&(R.ProvisioningState==2)&&((R.Flags&2)!=0)){O=null;k=null;D.deactivateCCM()}else{if((j.type==2)&&(R.ProvisioningState==0)){O=null;k=null;if((j.password==null)||(j.password=="")){t=null}D.activeToCCM(t)}else{if((j.type==2)&&(R.ProvisioningState==2)&&(t!=null)&&((R.Flags&2)!=0)){var S=require("amt-wsman-duk");var T=require("amt-wsman");var P=require("amt");O=new T(S,"127.0.0.1",16992,"admin",t,false);k=new P(O);var U=["*AMT_GeneralSettings","*IPS_HostBasedSetupService","*AMT_RedirectionService","*CIM_KVMRedirectionSAP","AMT_PublicKeyCertificate","*AMT_EnvironmentDetectionSettingData"];if(j.cirasetup==2){U.push("AMT_ManagementPresenceRemoteSAP","AMT_RemoteAccessCredentialContext","AMT_RemoteAccessPolicyAppliesToMPS","AMT_RemoteAccessPolicyRule","*AMT_UserInitiatedConnectionService","AMT_MPSUsernamePassword")}try{k.BatchEnum(null,U,N)}catch(Q){q(Q)}}else{}}}})};function N(X,R,T,Y){if(Y!=200){if(Y==401){if(j.badpass==1){D.deactivateCCM()}}else{if(++H<20){if(H==10){q("WSMAN fault, MEI Reset");D.reset()}var Z=["*AMT_GeneralSettings","*IPS_HostBasedSetupService","*AMT_RedirectionService","*CIM_KVMRedirectionSAP","AMT_PublicKeyCertificate","*AMT_EnvironmentDetectionSettingData"];if(j.cirasetup==2){Z.push("AMT_ManagementPresenceRemoteSAP","AMT_RemoteAccessCredentialContext","AMT_RemoteAccessPolicyAppliesToMPS","AMT_RemoteAccessPolicyRule","*AMT_UserInitiatedConnectionService","AMT_MPSUsernamePassword")}try{k.BatchEnum(null,Z,N)}catch(P){q(P)}}else{q("WSMAN fault, status="+Y);H=0}}}else{H=0;var V={};V.redir=(T.AMT_RedirectionService.response.ListenerEnabled==true);V.sol=((T.AMT_RedirectionService.response.EnabledState&2)!=0);V.ider=((T.AMT_RedirectionService.response.EnabledState&1)!=0);V.kvm=(T.CIM_KVMRedirectionSAP!=null)&&((T.CIM_KVMRedirectionSAP.response.EnabledState==6&&T.CIM_KVMRedirectionSAP.response.RequestedState==2)||T.CIM_KVMRedirectionSAP.response.EnabledState==2||T.CIM_KVMRedirectionSAP.response.EnabledState==6);if((T.AMT_GeneralSettings.response.PingResponseEnabled!=true)||(T.AMT_GeneralSettings.response.RmcpPingResponseEnabled!=true)){T.AMT_GeneralSettings.response.PingResponseEnabled=true;T.AMT_GeneralSettings.response.RmcpPingResponseEnabled=true;k.Put("AMT_GeneralSettings",T.AMT_GeneralSettings.response,function(ae,ac,ad,af){if(af!=200){q("Enable PING PUT Error "+af)}},0,1)}if((V.redir==false)||(V.sol==false)||(V.ider==false)){var S=T.AMT_RedirectionService.response;S.ListenerEnabled=true;S.EnabledState=32768+1+2;k.AMT_RedirectionService_RequestStateChange(S.EnabledState,function(ae,ac,ad,af){if(af!=200){q("Enable Redirection EXEC Error "+af)}})}if((T.CIM_KVMRedirectionSAP!=null)&&(V.kvm==false)){k.CIM_KVMRedirectionSAP_RequestStateChange(2,0,function(ae,ac,ad,af){if(af!=200){messagebox("Error","KVMRedirectionSAP, RequestStateChange Error "+af);return}k.Put("AMT_RedirectionService",S,function(ai,ag,ah,aj){if(aj!=200){q("Enable KVM PUT Error "+aj)}},0,1)})}if(typeof j.rootcert=="string"){var U=false,aa=T.AMT_PublicKeyCertificate.responses;for(var Q in aa){if((aa[Q]["X509Certificate"]==j.rootcert)&&(aa[Q]["TrustedRootCertficate"]==true)){U=true}}if(U==false){k.AMT_PublicKeyManagementService_AddTrustedRootCertificate(j.rootcert,function(ae,ac,ad,af){if(af!=200){q("Add root cert EXEC Error "+af)}})}}if((j.cirasetup==2)&&(j.ciraserver!=null)){var W=false,ab=T.AMT_ManagementPresenceRemoteSAP.responses;for(var Q in ab){if((ab[Q].AccessInfo==j.ciraserver.name)&&(ab[Q].Port==j.ciraserver.port)){W=ab[Q].Name}}if(W==false){k.AMT_RemoteAccessService_AddMpServer(j.ciraserver.name,201,j.ciraserver.port,2,null,j.ciraserver.user,j.ciraserver.pass,null,function(af,ad,ae,ag){if(ag!=200){q("Add MPS server EXEC Error "+ag)}else{W=false;var ah=ae.Body.MpServer.ReferenceParameters.SelectorSet.Selector;for(var ac in ah){if(ah[ac]["@Name"]=="Name"){W=ah[ac]["Value"]}}if(W!=false){m(T,W)}}})}else{m(T,W)}}else{if(j.cirasetup==1){n(T)}}}}function m(P,R){var Q='<Address xmlns="http://schemas.xmlsoap.org/ws/2004/08/addressing">http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</Address><ReferenceParameters xmlns="http://schemas.xmlsoap.org/ws/2004/08/addressing"><ResourceURI xmlns="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd">http://intel.com/wbem/wscim/1/amt-schema/1/AMT_ManagementPresenceRemoteSAP</ResourceURI><SelectorSet xmlns="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd"><Selector Name="Name">'+R+"</Selector></SelectorSet></ReferenceParameters>";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; |