mirror of
https://github.com/Ylianst/MeshCentral.git
synced 2024-12-26 15:15:53 -05:00
1 line
15 KiB
JavaScript
1 line
15 KiB
JavaScript
var CreateAmtRemoteDesktop=function(a,t){var S={};function p(a){return String.fromCharCode.apply(null,a)}function m(a,t,e,n,o,r,s){var i,c,h,d,v=a[t++],f={},u=0,l=0;if(0==v){if(2==S.bpp)for(d=0;d<s;d++)b(a[t++]+(a[t++]<<8),d);else for(d=0;d<s;d++)y(a[t++],d);w(S.spare,e,n)}else if(1==v){c=a[t++]+(2==S.bpp?a[t++]<<8:0),S.canvas.fillStyle="rgb("+(1==S.bpp?(224&c)+","+((28&c)<<3)+","+L((3&c)<<6):(c>>8&248)+","+(c>>3&252)+","+((31&c)<<3))+")";var g=k(e);n=K(0,n),e=g,S.canvas.fillRect(e,n,o,r)}else if(1<v&&v<17){var p=4,m=15;if(2==S.bpp){for(d=0;d<v;d++)f[d]=a[t++]+(a[t++]<<8);for(2==v?m=p=1:v<=4&&(p=2,m=3);u<s&&t<a.byteLength;)for(c=a[t++],d=8-p;0<=d;d-=p)b(f[c>>d&m],u++)}else{for(d=0;d<v;d++)f[d]=a[t++];for(2==v?m=p=1:v<=4&&(p=2,m=3);u<s&&t<a.byteLength;)for(c=a[t++],d=8-p;0<=d;d-=p)y(f[c>>d&m],u++)}w(S.spare,e,n)}else if(128==v){if(2==S.bpp)for(;u<s&&t<a.byteLength;){for(c=a[t++]+(a[t++]<<8),l=1;l+=h=a[t++],255==h;);if(0==S.rotation)D(c,u,l),u+=l;else for(;0<=--l;)b(c,u++)}else for(;u<s&&t<a.byteLength;){for(c=a[t++],l=1;l+=h=a[t++],255==h;);if(0==S.rotation)C(c,u,l),u+=l;else for(;0<=--l;)y(c,u++)}w(S.spare,e,n)}else if(129<v){if(2==S.bpp)for(d=0;d<v-128;d++)f[d]=a[t++]+(a[t++]<<8);else for(d=0;d<v-128;d++)f[d]=a[t++];for(;u<s&&t<a.byteLength;){if(l=1,c=f[(i=a[t++])%128],127<i)for(;l+=h=a[t++],255==h;);if(0==S.rotation)(2==S.bpp?D:C)(c,u,l),u+=l;else if(2==S.bpp)for(;0<=--l;)b(c,u++);else for(;0<=--l;)y(c,u++)}w(S.spare,e,n)}}function w(a,t,e){var n,o,r;1!=S.holding&&(o=t,r=e,n=0==S.rotation?o:1==S.rotation?S.canvas.canvas.width-S.sparew2-r:2==S.rotation?S.canvas.canvas.width-S.sparew2-o:3==S.rotation?r:0,o=t,r=e,e=0==S.rotation?r:1==S.rotation?o:2==S.rotation?S.canvas.canvas.height-S.spareh2-r:3==S.rotation?S.canvas.canvas.height-S.spareh-o:0,t=n,S.canvas.putImageData(a,t,e))}function y(a,t){var e,n,o=t<<2;0<S.rotation&&(1==S.rotation?(e=t%S.sparew,n=Math.floor(t/S.sparew),o=(t=e*S.sparew2+(S.sparew2-1-n))<<2):2==S.rotation?o=S.sparew*S.spareh*4-4-o:3==S.rotation&&(e=t%S.sparew,n=Math.floor(t/S.sparew),o=(t=(S.sparew2-1-e)*S.sparew2+n)<<2)),S.spare.data[o]=224&a,S.spare.data[o+1]=(28&a)<<3,S.spare.data[o+2]=L((3&a)<<6)}function b(a,t){var e,n,o=t<<2;0<S.rotation&&(1==S.rotation?(e=t%S.sparew,n=Math.floor(t/S.sparew),o=(t=e*S.sparew2+(S.sparew2-1-n))<<2):2==S.rotation?o=S.sparew*S.spareh*4-4-o:3==S.rotation&&(e=t%S.sparew,n=Math.floor(t/S.sparew),o=(t=(S.sparew2-1-e)*S.sparew2+n)<<2)),S.spare.data[o]=a>>8&248,S.spare.data[o+1]=a>>3&252,S.spare.data[o+2]=(31&a)<<3}function C(a,t,e){for(var n=t<<2,o=224&a,r=(28&a)<<3,s=L((3&a)<<6);0<=--e;)S.spare.data[n]=o,S.spare.data[n+1]=r,S.spare.data[n+2]=s,n+=4}function D(a,t,e){for(var n=t<<2,o=a>>8&248,r=a>>3&252,s=(31&a)<<3;0<=--e;)S.spare.data[n]=o,S.spare.data[n+1]=r,S.spare.data[n+2]=s,n+=4}function k(a){return 0==S.rotation||1==S.rotation?a:2==S.rotation?a-S.canvas.canvas.width:3==S.rotation?a-S.canvas.canvas.height:0}function K(a,t){return 0==S.rotation?t:1==S.rotation?t-S.canvas.canvas.width:2==S.rotation?t-S.canvas.canvas.height:3==S.rotation?t:0}function L(a){return 127<a?a+32:a}function T(){var a;1!=S.holding&&(0<S.focusmode?(a=2*S.focusmode,S.send(String.fromCharCode(3,1)+ShortToStr(Math.max(Math.min(S.ox,S.mx)-S.focusmode,0))+ShortToStr(Math.max(Math.min(S.oy,S.my)-S.focusmode,0))+ShortToStr(a+Math.abs(S.ox-S.mx))+ShortToStr(a+Math.abs(S.oy-S.my))),S.ox=S.mx,S.oy=S.my):S.send(String.fromCharCode(3,1,0,0,0,0)+ShortToStr(S.rwidth)+ShortToStr(S.rheight)))}S.canvasid=a,S.CanvasId=Q(a),S.scrolldiv=t,S.canvas=Q(a).getContext("2d"),S.protocol=2,S.state=0,S.acc=null,S.ScreenWidth=960,S.ScreenHeight=700,S.width=0,S.height=0,S.rwidth=0,S.rheight=0,S.bpp=2,S.useZRLE=!0,S.showmouse=!0,S.buttonmask=0,S.localKeyMap=!0,S.spare=null,S.sparew=0,S.spareh=0,S.sparew2=0,S.spareh2=0,S.sparecache={},S.ZRLEfirst=1,S.onScreenSizeChange=null,S.frameRateDelay=0,S.noMouseRotate=!1,S.rotation=0,S.kvmDataSupported=!1,S.onKvmData=null,S.onKvmDataPending=[],S.onKvmDataAck=-1,S.holding=!1,S.lastKeepAlive=Date.now(),S.SwapMouse=!1,S.mNagleTimer=null,S.mx=0,S.my=0,S.ox=-1,S.oy=-1,S.focusmode=0,S.inflate=ZLIB.inflateInit(-15),S.xxStateChange=function(a){0==a?(S.canvas.fillStyle="#000000",S.canvas.fillRect(0,0,S.width,S.height),S.canvas.canvas.width=S.rwidth=S.width=640,S.canvas.canvas.height=S.rheight=S.height=400,QS(S.canvasid).cursor="default"):QS(S.canvasid).cursor=S.showmouse?"default":"none"},S.ProcessBinaryData=function(a){var t;for(null==S.acc?S.acc=new Uint8Array(a):((t=new Uint8Array(S.acc.byteLength+a.byteLength)).set(S.acc,0),t.set(new Uint8Array(a),S.acc.byteLength),S.acc=t);null!=S.acc&&0<S.acc.byteLength;){var e=0,n=new DataView(S.acc.buffer);if(0==S.state&&12<=S.acc.byteLength)e=12,S.state=1,S.send("RFB 003.008\n");else if(1==S.state&&1<=S.acc.byteLength)e=S.acc[0]+1,S.send(String.fromCharCode(1)),S.state=2;else if(2==S.state&&4<=S.acc.byteLength){if(e=4,0!=n.getUint32(0))return S.Stop();S.send(String.fromCharCode(1)),S.state=3}else if(3==S.state&&24<=S.acc.byteLength){S.rotation=0;var o=n.getUint32(20);if(S.acc.byteLength<24+o)return;e=24+o,S.canvas.canvas.width=S.rwidth=S.width=S.ScreenWidth=n.getUint16(0),S.canvas.canvas.height=S.rheight=S.height=S.ScreenHeight=n.getUint16(2);var r="";S.useZRLE&&(r+=IntToStr(16)),r+=IntToStr(0),r+=IntToStr(1092),S.send(String.fromCharCode(2,0)+ShortToStr(r.length/4+1)+r+IntToStr(-223)),1==S.bpp&&S.send(String.fromCharCode(0,0,0,0,8,8,0,1)+ShortToStr(7)+ShortToStr(7)+ShortToStr(3)+String.fromCharCode(5,2,0,0,0,0)),S.state=4,S.parent&&S.parent.xxStateChange(3),T(),S.ox=-1,null!=S.onScreenSizeChange&&S.onScreenSizeChange(S,S.ScreenWidth,S.ScreenHeight)}else if(4==S.state)switch(S.acc[0]){case 0:if(S.acc.byteLength<4)return;S.state=100+n.getUint16(2),e=4,null!=S.onPreDrawImage&&S.onPreDrawImage();break;case 2:e=1;break;case 3:if(S.acc.byteLength<8)return;var s=n.getUint32(4)+8;if(S.acc.byteLength<s)return;e=function(a,t){if(a.byteLength<8)return 0;t=t.getUint32(4)+8;if(a.byteLength<t)return 0;null==S.onKvmData||16<=(a=p(a.slice(8,t))).length&&"\0KvmDataChannel"==a.substring(0,15)&&(0==S.kvmDataSupported&&(S.kvmDataSupported=!0,console.log("KVM Data Channel Supported.")),(-1==S.onKvmDataAck&&16==a.length||0!=a.charCodeAt(15))&&(S.onKvmDataAck=!0),16<=a.length&&S.onKvmData(a.substring(16)),1==S.onKvmDataAck&&0<S.onKvmDataPending.length&&S.sendKvmData(S.onKvmDataPending.shift()));return t}(S.acc,n)}else if(100<S.state&&12<=S.acc.byteLength){var i=n.getUint16(0),c=n.getUint16(2),h=n.getUint16(4),d=n.getUint16(6),v=h*d,o=n.getUint32(8);if(o<17){if(h<1||64<h||d<1||64<d)return console.log("Invalid tile size ("+h+","+d+"), disconnecting."),S.Stop();if(S.sparew!=h||S.spareh!=d){S.sparew=S.sparew2=h,S.spareh=S.spareh2=d,1!=S.rotation&&3!=S.rotation||(S.sparew2=d,S.spareh2=h);r=S.sparew2+"x"+S.spareh2;if(S.spare=S.sparecache[r],!S.spare){S.sparecache[r]=S.spare=S.canvas.createImageData(S.sparew2,S.spareh2);for(var f=S.sparew2*S.spareh2<<2,u=3;u<f;u+=4)S.spare.data[u]=255}}}if(4294967073==o)S.canvas.canvas.width=S.rwidth=S.width=h,S.canvas.canvas.height=S.rheight=S.height=d,S.send(String.fromCharCode(3,0,0,0,0,0)+ShortToStr(S.width)+ShortToStr(S.height)),e=12,null!=S.onScreenSizeChange&&S.onScreenSizeChange(S,S.ScreenWidth,S.ScreenHeight);else if(0==o){var l=12,g=12+v*S.bpp;if(S.acc.byteLength<g)return;if(e=g,2==S.bpp)for(u=0;u<v;u++)b(n.getUint16(l,!0),u),l+=2;else for(u=0;u<v;u++)y(S.acc[l++],u);w(S.spare,i,c)}else{if(16!=o)return S.Stop();if(S.acc.byteLength<16)return;g=n.getUint32(12);if(S.acc.byteLength<16+g)return;l=16;5<g&&0==S.acc[l]&&n.getUint16(l+1,!0)==g-5?m(S.acc,l+5,i,c,h,d,v):0<(o=S.inflate.inflate(p(new Uint8Array(S.acc.buffer.slice(l,+(l+g)))))).length?m(function(a){for(var t=new Uint8Array(a.length),e=0,n=a.length;e<n;++e)t[e]=a.charCodeAt(e);return t}(o),0,i,c,h,d,v,o.length):console.log("Invalid deflate data"),e=16+g}100==--S.state&&(S.state=4,0==S.frameRateDelay?T():setTimeout(T,S.frameRateDelay))}if(0==e)return;e!=S.acc.byteLength?S.acc=new Uint8Array(S.acc.buffer.slice(e)):S.acc=null}},S.hold=function(a){S.holding!=a&&(S.holding=a,S.canvas.fillStyle="#000000",S.canvas.fillRect(0,0,S.width,S.height),0==S.holding?(S.canvas.canvas.width==S.width&&S.canvas.canvas.height==S.height||(S.canvas.canvas.width=S.width,S.canvas.canvas.height=S.height,null!=S.onScreenSizeChange&&S.onScreenSizeChange(S,S.ScreenWidth,S.ScreenHeight)),S.Send(String.fromCharCode(3,0,0,0,0,0)+ShortToStr(S.width)+ShortToStr(S.height))):(S.UnGrabMouseInput(),S.UnGrabKeyInput()))},S.tcanvas=null,S.setRotation=function(a){for(;a<0;)a+=4;var t=a%4;if(1!=S.holding){if(t==S.rotation)return!0;var e=S.canvas.canvas.width,n=S.canvas.canvas.height;1!=S.rotation&&3!=S.rotation||(e=S.canvas.canvas.height,n=S.canvas.canvas.width),null==S.tcanvas&&(S.tcanvas=document.createElement("canvas"));var o=S.tcanvas.getContext("2d");return o.setTransform(1,0,0,1,0,0),o.canvas.width=e,o.canvas.height=n,o.rotate(-90*S.rotation*Math.PI/180),0==S.rotation&&o.drawImage(S.canvas.canvas,0,0),1==S.rotation&&o.drawImage(S.canvas.canvas,-S.canvas.canvas.width,0),2==S.rotation&&o.drawImage(S.canvas.canvas,-S.canvas.canvas.width,-S.canvas.canvas.height),3==S.rotation&&o.drawImage(S.canvas.canvas,0,-S.canvas.canvas.height),0!=S.rotation&&2!=S.rotation||(S.canvas.canvas.height=e,S.canvas.canvas.width=n),1!=S.rotation&&3!=S.rotation||(S.canvas.canvas.height=n,S.canvas.canvas.width=e),S.canvas.setTransform(1,0,0,1,0,0),S.canvas.rotate(90*t*Math.PI/180),S.rotation=t,S.canvas.drawImage(S.tcanvas,k(0),K(0,0)),S.width=S.canvas.canvas.width,S.height=S.canvas.canvas.height,null!=S.onScreenResize&&S.onScreenResize(S,S.width,S.height,S.CanvasId),!0}S.rotation=t},S.Start=function(){for(var a in S.state=0,S.acc=null,S.ZRLEfirst=1,S.inflate.inflateReset(),S.onKvmDataPending=[],S.onKvmDataAck=-1,S.kvmDataSupported=!1,S.sparecache)delete S.sparecache[a]},S.Stop=function(){S.UnGrabMouseInput(),S.UnGrabKeyInput(),S.parent&&S.parent.Stop()},S.send=function(a){S.parent&&S.parent.send(a)};var o={Pause:19,CapsLock:20,Space:32,Quote:39,Minus:45,NumpadMultiply:42,NumpadAdd:43,PrintScreen:44,Comma:44,NumpadSubtract:45,NumpadDecimal:46,Period:46,Slash:47,NumpadDivide:47,Semicolon:59,Equal:61,OSLeft:91,BracketLeft:91,OSRight:91,Backslash:92,BracketRight:93,ContextMenu:93,Backquote:96,NumLock:144,ScrollLock:145,Backspace:65288,Tab:65289,Enter:65293,NumpadEnter:65293,Escape:65307,Delete:65535,Home:65360,PageUp:65365,PageDown:65366,ArrowLeft:65361,ArrowUp:65362,ArrowRight:65363,ArrowDown:65364,End:65367,Insert:65379,F1:65470,F2:65471,F3:65472,F4:65473,F5:65474,F6:65475,F7:65476,F8:65477,F9:65478,F10:65479,F11:65480,F12:65481,ShiftLeft:65505,ShiftRight:65506,ControlLeft:65507,ControlRight:65508,AltLeft:65513,AltRight:65514,MetaLeft:65511,MetaRight:65512};function e(a,t){var e,n;return(t=t||window.event).code&&0==S.localKeyMap?null!=(e=(n=t).code.startsWith("Key")&&4==n.code.length?n.code.charCodeAt(3)+(0==n.shiftKey?32:0):n.code.startsWith("Digit")&&6==n.code.length?n.code.charCodeAt(5):n.code.startsWith("Numpad")&&7==n.code.length?n.code.charCodeAt(6):o[n.code])&&S.sendkey(e,a):(n=e=t.keyCode,0==t.shiftKey&&65<=e&&e<=90&&(n=e+32),112<=e&&e<=124&&(n=e+65358),8==e&&(n=65288),9==e&&(n=65289),13==e&&(n=65293),16==e&&(n=65505),17==e&&(n=65507),18==e&&(n=65513),27==e&&(n=65307),33==e&&(n=65365),34==e&&(n=65366),35==e&&(n=65367),36==e&&(n=65360),37==e&&(n=65361),38==e&&(n=65362),39==e&&(n=65363),40==e&&(n=65364),45==e&&(n=65379),46==e&&(n=65535),96<=e&&e<=105&&(n=e-48),106==e&&(n=42),107==e&&(n=43),109==e&&(n=45),110==e&&(n=46),111==e&&(n=47),186==e&&(n=59),187==e&&(n=61),188==e&&(n=44),189==e&&(n=45),190==e&&(n=46),191==e&&(n=47),192==e&&(n=96),219==e&&(n=91),220==e&&(n=92),221==e&&(n=93),222==e&&(n=39),S.sendkey(n,a)),S.haltEvent(t)}S.sendkey=function(a,t){if("object"==typeof a){var e,n="";for(e in a)n+=String.fromCharCode(4,a[e][1],0,0)+IntToStr(a[e][0]);S.send(n)}else S.send(String.fromCharCode(4,t,0,0)+IntToStr(a))},S.sendKvmData=function(a){!0!==S.onKvmDataAck?S.onKvmDataPending.push(a):(a="\0KvmDataChannel\0"+a,S.send(String.fromCharCode(6,0,0,0)+IntToStr(a.length)+a),S.onKvmDataAck=!1)},S.sendKeepAlive=function(){S.lastKeepAlive<Date.now()-5e3&&(S.lastKeepAlive=Date.now(),S.send(String.fromCharCode(6,0,0,0)+IntToStr(16)+"\0KvmDataChannel\0"))},S.SendCtrlAltDelMsg=function(){S.sendcad()};var n=!(S.sendcad=function(){S.sendkey([[65507,1],[65513,1],[65535,1],[65535,0],[65513,0],[65507,0]])}),r=!1;return S.GrabMouseInput=function(){var a;1!=n&&((a=S.canvas.canvas).onmouseup=S.mouseup,a.onmousedown=S.mousedown,a.onmousemove=S.mousemove,n=!0)},S.UnGrabMouseInput=function(){var a;0!=n&&((a=S.canvas.canvas).onmousemove=null,a.onmouseup=null,a.onmousedown=null,n=!1)},S.GrabKeyInput=function(){1!=r&&(document.onkeyup=S.handleKeyUp,document.onkeydown=S.handleKeyDown,document.onkeypress=S.handleKeys,r=!0)},S.UnGrabKeyInput=function(){0!=r&&(document.onkeyup=null,document.onkeydown=null,document.onkeypress=null,r=!1)},S.handleKeys=function(a){return S.haltEvent(a)},S.handleKeyUp=function(a){return e(0,a)},S.handleKeyDown=function(a){return e(1,a)},S.haltEvent=function(a){return a.preventDefault&&a.preventDefault(),a.stopPropagation&&a.stopPropagation(),!1},S.mousedblclick=function(a){},S.mousedown=function(a){var t=a.button;return S.SwapMouse&&(0==t?t=2:2==t&&(t=0)),S.buttonmask|=1<<t,S.mousemove(a,1)},S.mouseup=function(a){var t=a.button;return S.SwapMouse&&(0==t?t=2:2==t&&(t=0)),S.buttonmask&=65535-(1<<t),S.mousemove(a,1)},S.mousemove=function(a,t){if(S.state<4)return!0;var e,n,o,r,s=S.canvas.canvas.height/Q(S.canvasid).offsetHeight,i=S.canvas.canvas.width/Q(S.canvasid).offsetWidth,c=S.getPositionOfControl(Q(S.canvasid));return S.mx=(event.pageX-c[0])*i,S.my=(event.pageY-c[1])*s,event.addx&&(S.mx+=event.addx),event.addy&&(S.my+=event.addy),1!=S.noMouseRotate&&(S.mx2=(e=S.mx,r=S.my,0==S.rotation?e:1==S.rotation?r:2==S.rotation?S.canvas.canvas.width-e:3==S.rotation?S.canvas.canvas.height-r:0),S.my=(o=S.mx,n=S.my,0==S.rotation?n:1==S.rotation?S.canvas.canvas.width-o:2==S.rotation?S.canvas.canvas.height-n:3==S.rotation?o:0),S.mx=S.mx2),1==t?(S.send(String.fromCharCode(5,S.buttonmask)+ShortToStr(S.mx)+ShortToStr(S.my)),null!=S.mNagleTimer&&(clearTimeout(S.mNagleTimer),S.mNagleTimer=null)):null==S.mNagleTimer&&(S.mNagleTimer=setTimeout(function(){S.send(String.fromCharCode(5,S.buttonmask)+ShortToStr(S.mx)+ShortToStr(S.my)),S.mNagleTimer=null},50)),QV("DeskFocus",S.focusmode),0!=S.focusmode&&(c=Math.min(S.mx,S.canvas.canvas.width-S.focusmode),s=Math.min(S.my,S.canvas.canvas.height-S.focusmode),e=2*S.focusmode,n=(r=Q(S.canvasid)).offsetHeight/S.canvas.canvas.height,o=r.offsetWidth/S.canvas.canvas.width,t=QS("DeskFocus"),r=S.getPositionOfControl(Q(S.canvasid).parentElement),t.left=Math.max((c-S.focusmode)*n,0)+(pos[0]-r[0])+"px",t.top=Math.max((s-S.focusmode)*o,0)+(pos[1]-r[1])+"px",t.width=e*n-6+"px",t.height=e*n-6+"px"),S.haltEvent(a)},S.getPositionOfControl=function(a){var t=Array(2);for(t[0]=t[1]=0;a;)t[0]+=a.offsetLeft,t[1]+=a.offsetTop,a=a.offsetParent;return t},S} |