mirror of
https://github.com/Ylianst/MeshCentral.git
synced 2024-12-27 23:55:54 -05:00
1 line
16 KiB
JavaScript
1 line
16 KiB
JavaScript
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+"<br />")}};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<S;R++){U+=T.charAt(Math.floor(Math.random()*T.length))}return U}F.activeToCCM=function(R){if((R==null)||(R=="")){R="P@0s"+B(23)}u=R;if(G!=null){G.BatchEnum(null,["*AMT_GeneralSettings","*IPS_HostBasedSetupService"],b,R)}else{h.getLocalSystemAccount(function(V){if((V!=null)&&V.user&&V.pass){var T=require("amt-wsman-duk");var U=require("amt-wsman");var S=require("amt");H=new U(T,"127.0.0.1",16992,V.user,V.pass,false);G=new S(H);G.BatchEnum(null,["*AMT_GeneralSettings","*IPS_HostBasedSetupService"],b,R)}else{}})}};var b=function(U,S,T,V,R){if(V!=200){r("Failed to fetch activation information, status "+V)}else{if(T.IPS_HostBasedSetupService.response.AllowedControlModes.length!=2){r("Client control mode activation not allowed")}else{U.IPS_HostBasedSetupService_Setup(2,D("admin:"+T.AMT_GeneralSettings.response.DigestRealm+":"+R).substring(0,32),null,null,null,null,c)}}};var c=function(T,R,S,U){if(U!=200){r("Failed to activate, status "+U)}else{if(S.Body.ReturnValue!=0){r("Client control mode activation failed: "+S.Body.ReturnValueStr)}else{r("Intel AMT CCM activation success.");q.Put("amtCCMPass",u);d.SendCommand({action:"coreinfo",intelamt:{state:2,flags:2,user:"admin",pass:u}})}}m=setTimeout(F.applyPolicy,8000)};F.start=function(){var R=null;try{R=JSON.parse(q.Get("amtPolicy"))}catch(S){r("Exception loading amtPolicy")}try{u=q.Get("amtCCMPass")}catch(S){}if(typeof u!="string"){u=null}F.reset()};var u,Q,l,m,J=0;F.applyPolicy=function(){m=null;if((i!=3)||(k==null)||(typeof k!="object")||(typeof k.type!="number")||(k.type==0)){return}if((k.password!=null)&&(k.password!="")){u=k.password}F.getAmtInfo(function(T){if((k.type==1)&&(T.ProvisioningState==2)&&((T.Flags&2)!=0)){Q=null;l=null;F.deactivateCCM()}else{if((k.type==2)&&(T.ProvisioningState==0)){Q=null;l=null;if((k.password==null)||(k.password=="")){u=null}F.activeToCCM(u)}else{if((k.type==2)&&(T.ProvisioningState==2)&&(u!=null)&&((T.Flags&2)!=0)){var U=require("amt-wsman-duk");var V=require("amt-wsman");var R=require("amt");Q=new V(U,"127.0.0.1",16992,"admin",u,false);l=new R(Q);var W=["*AMT_GeneralSettings","*IPS_HostBasedSetupService","*AMT_RedirectionService","*CIM_KVMRedirectionSAP","AMT_PublicKeyCertificate","*AMT_EnvironmentDetectionSettingData"];if(k.cirasetup==2){W.push("AMT_ManagementPresenceRemoteSAP","AMT_RemoteAccessCredentialContext","AMT_RemoteAccessPolicyAppliesToMPS","AMT_RemoteAccessPolicyRule","*AMT_UserInitiatedConnectionService","AMT_MPSUsernamePassword")}try{l.BatchEnum(null,W,P)}catch(S){r(S)}}else{if((k.type==3)&&(T.ProvisioningState==0)){F.activeToACM(T)}else{}}}}})};function P(Z,T,V,aa){if(aa!=200){if(aa==401){if(k.badpass==1){F.deactivateCCM()}}else{if(++J<20){if(J==10){r("WSMAN fault, MEI Reset");F.reset()}var ab=["*AMT_GeneralSettings","*IPS_HostBasedSetupService","*AMT_RedirectionService","*CIM_KVMRedirectionSAP","AMT_PublicKeyCertificate","*AMT_EnvironmentDetectionSettingData"];if(k.cirasetup==2){ab.push("AMT_ManagementPresenceRemoteSAP","AMT_RemoteAccessCredentialContext","AMT_RemoteAccessPolicyAppliesToMPS","AMT_RemoteAccessPolicyRule","*AMT_UserInitiatedConnectionService","AMT_MPSUsernamePassword")}try{l.BatchEnum(null,ab,P)}catch(R){r(R)}}else{r("WSMAN fault, status="+aa);J=0}}}else{J=0;var X={};X.redir=(V.AMT_RedirectionService.response.ListenerEnabled==true);X.sol=((V.AMT_RedirectionService.response.EnabledState&2)!=0);X.ider=((V.AMT_RedirectionService.response.EnabledState&1)!=0);X.kvm=(V.CIM_KVMRedirectionSAP!=null)&&((V.CIM_KVMRedirectionSAP.response.EnabledState==6&&V.CIM_KVMRedirectionSAP.response.RequestedState==2)||V.CIM_KVMRedirectionSAP.response.EnabledState==2||V.CIM_KVMRedirectionSAP.response.EnabledState==6);if((V.AMT_GeneralSettings.response.PingResponseEnabled!=true)||(V.AMT_GeneralSettings.response.RmcpPingResponseEnabled!=true)){V.AMT_GeneralSettings.response.PingResponseEnabled=true;V.AMT_GeneralSettings.response.RmcpPingResponseEnabled=true;l.Put("AMT_GeneralSettings",V.AMT_GeneralSettings.response,function(ag,ae,af,ah){if(ah!=200){r("Enable PING PUT Error "+ah)}},0,1)}if((X.redir==false)||(X.sol==false)||(X.ider==false)){var U=V.AMT_RedirectionService.response;U.ListenerEnabled=true;U.EnabledState=32768+1+2;l.AMT_RedirectionService_RequestStateChange(U.EnabledState,function(ag,ae,af,ah){if(ah!=200){r("Enable Redirection EXEC Error "+ah)}})}if((V.CIM_KVMRedirectionSAP!=null)&&(X.kvm==false)){l.CIM_KVMRedirectionSAP_RequestStateChange(2,0,function(ag,ae,af,ah){if(ah!=200){messagebox("Error","KVMRedirectionSAP, RequestStateChange Error "+ah);return}l.Put("AMT_RedirectionService",U,function(ak,ai,aj,al){if(al!=200){r("Enable KVM PUT Error "+al)}},0,1)})}if(typeof k.rootcert=="string"){var W=false,ac=V.AMT_PublicKeyCertificate.responses;for(var S in ac){if((ac[S]["X509Certificate"]==k.rootcert)&&(ac[S]["TrustedRootCertficate"]==true)){W=true}}if(W==false){l.AMT_PublicKeyManagementService_AddTrustedRootCertificate(k.rootcert,function(ag,ae,af,ah){if(ah!=200){r("Add root cert EXEC Error "+ah)}})}}if((k.cirasetup==2)&&(k.ciraserver!=null)){var Y=false,ad=V.AMT_ManagementPresenceRemoteSAP.responses;for(var S in ad){if((ad[S].AccessInfo==k.ciraserver.name)&&(ad[S].Port==k.ciraserver.port)){Y=ad[S].Name}}if(Y==false){l.AMT_RemoteAccessService_AddMpServer(k.ciraserver.name,201,k.ciraserver.port,2,null,k.ciraserver.user,k.ciraserver.pass,null,function(ah,af,ag,ai){if(ai!=200){r("Add MPS server EXEC Error "+ai)}else{Y=false;var aj=ag.Body.MpServer.ReferenceParameters.SelectorSet.Selector;for(var ae in aj){if(aj[ae]["@Name"]=="Name"){Y=aj[ae]["Value"]}}if(Y!=false){n(V,Y)}}})}else{n(V,Y)}}else{if(k.cirasetup==1){o(V)}}}}function n(R,T){var S='<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">'+T+"</Selector></SelectorSet></ReferenceParameters>";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; |