mirror of
https://github.com/Ylianst/MeshCentral.git
synced 2024-12-24 14:32:58 +03:00
1 line
15 KiB
JavaScript
1 line
15 KiB
JavaScript
var CreateAmtRemoteDesktop=function(t,a){var y={};function b(t){return String.fromCharCode.apply(null,t)}function C(t){for(var a=new Uint8Array(t.length),e=0,n=t.length;e<n;++e)a[e]=t.charCodeAt(e);return a}function D(t,a,e,n,o,r,i){var s,c,h,f,d=t[a++],v={},u=0,l=0;if(0==d){if(2==y.bpp)for(f=0;f<i;f++)L(t[a++]+(t[a++]<<8),f);else for(f=0;f<i;f++)K(t[a++],f);k(y.spare,e,n)}else if(1==d){c=t[a++]+(2==y.bpp?t[a++]<<8:0),y.canvas.fillStyle="rgb("+(1==y.bpp?(224&c)+","+((28&c)<<3)+","+A((3&c)<<6):(c>>8&248)+","+(c>>3&252)+","+((31&c)<<3))+")";var g=T(e,n);n=x(e,n),e=g,y.canvas.fillRect(e,n,o,r)}else if(1<d&&d<17){var p=4,m=15;if(2==y.bpp){for(f=0;f<d;f++)v[f]=t[a++]+(t[a++]<<8);for(2==d?m=p=1:d<=4&&(p=2,m=3);u<i&&a<t.byteLength;)for(c=t[a++],f=8-p;0<=f;f-=p)L(v[c>>f&m],u++)}else{for(f=0;f<d;f++)v[f]=t[a++];for(2==d?m=p=1:d<=4&&(p=2,m=3);u<i&&a<t.byteLength;)for(c=t[a++],f=8-p;0<=f;f-=p)K(v[c>>f&m],u++)}k(y.spare,e,n)}else if(128==d){if(2==y.bpp)for(;u<i&&a<t.byteLength;){for(c=t[a++]+(t[a++]<<8),l=1;l+=h=t[a++],255==h;);if(0==y.rotation)w(c,u,l),u+=l;else for(;0<=--l;)L(c,u++)}else for(;u<i&&a<t.byteLength;){for(c=t[a++],l=1;l+=h=t[a++],255==h;);if(0==y.rotation)S(c,u,l),u+=l;else for(;0<=--l;)K(c,u++)}k(y.spare,e,n)}else if(129<d){if(2==y.bpp)for(f=0;f<d-128;f++)v[f]=t[a++]+(t[a++]<<8);else for(f=0;f<d-128;f++)v[f]=t[a++];for(;u<i&&a<t.byteLength;){if(l=1,c=v[(s=t[a++])%128],127<s)for(;l+=h=t[a++],255==h;);if(0==y.rotation)2==y.bpp?w(c,u,l):S(c,u,l),u+=l;else if(2==y.bpp)for(;0<=--l;)L(c,u++);else for(;0<=--l;)K(c,u++)}k(y.spare,e,n)}}function k(t,a,e){if(1!=y.holding){var n=function(t,a){return 0==y.rotation?t:1==y.rotation?y.canvas.canvas.width-y.sparew2-a:2==y.rotation?y.canvas.canvas.width-y.sparew2-t:3==y.rotation?a:0}(a,e);e=function(t,a){return 0==y.rotation?a:1==y.rotation?t:2==y.rotation?y.canvas.canvas.height-y.spareh2-a:3==y.rotation?y.canvas.canvas.height-y.spareh-t:0}(a,e),a=n,y.canvas.putImageData(t,a,e)}}function K(t,a){var e=a<<2;if(0<y.rotation)if(1==y.rotation){var n=a%y.sparew,o=Math.floor(a/y.sparew);e=(a=n*y.sparew2+(y.sparew2-1-o))<<2}else if(2==y.rotation)e=y.sparew*y.spareh*4-4-e;else if(3==y.rotation){n=a%y.sparew,o=Math.floor(a/y.sparew);e=(a=(y.sparew2-1-n)*y.sparew2+o)<<2}y.spare.data[e]=224&t,y.spare.data[e+1]=(28&t)<<3,y.spare.data[e+2]=A((3&t)<<6)}function L(t,a){var e=a<<2;if(0<y.rotation)if(1==y.rotation){var n=a%y.sparew,o=Math.floor(a/y.sparew);e=(a=n*y.sparew2+(y.sparew2-1-o))<<2}else if(2==y.rotation)e=y.sparew*y.spareh*4-4-e;else if(3==y.rotation){n=a%y.sparew,o=Math.floor(a/y.sparew);e=(a=(y.sparew2-1-n)*y.sparew2+o)<<2}y.spare.data[e]=t>>8&248,y.spare.data[e+1]=t>>3&252,y.spare.data[e+2]=(31&t)<<3}function S(t,a,e){for(var n=a<<2,o=224&t,r=(28&t)<<3,i=A((3&t)<<6);0<=--e;)y.spare.data[n]=o,y.spare.data[n+1]=r,y.spare.data[n+2]=i,n+=4}function w(t,a,e){for(var n=a<<2,o=t>>8&248,r=t>>3&252,i=(31&t)<<3;0<=--e;)y.spare.data[n]=o,y.spare.data[n+1]=r,y.spare.data[n+2]=i,n+=4}function T(t){return 0==y.rotation?t:1==y.rotation?t:2==y.rotation?t-y.canvas.canvas.width:3==y.rotation?t-y.canvas.canvas.height:0}function x(t,a){return 0==y.rotation?a:1==y.rotation?a-y.canvas.canvas.width:2==y.rotation?a-y.canvas.canvas.height:3==y.rotation?a:0}function A(t){return 127<t?t+32:t}function I(){if(1!=y.holding)if(0<y.focusmode){var t=2*y.focusmode;y.send(String.fromCharCode(3,1)+ShortToStr(Math.max(Math.min(y.ox,y.mx)-y.focusmode,0))+ShortToStr(Math.max(Math.min(y.oy,y.my)-y.focusmode,0))+ShortToStr(t+Math.abs(y.ox-y.mx))+ShortToStr(t+Math.abs(y.oy-y.my))),y.ox=y.mx,y.oy=y.my}else y.send(String.fromCharCode(3,1,0,0,0,0)+ShortToStr(y.rwidth)+ShortToStr(y.rheight))}y.canvasid=t,y.CanvasId=Q(t),y.scrolldiv=a,y.canvas=Q(t).getContext("2d"),y.protocol=2,y.state=0,y.acc=null,y.ScreenWidth=960,y.ScreenHeight=700,y.width=0,y.height=0,y.rwidth=0,y.rheight=0,y.bpp=2,y.useZRLE=!0,y.showmouse=!0,y.buttonmask=0,y.localKeyMap=!0,y.spare=null,y.sparew=0,y.spareh=0,y.sparew2=0,y.spareh2=0,y.sparecache={},y.ZRLEfirst=1,y.onScreenSizeChange=null,y.frameRateDelay=0,y.noMouseRotate=!1,y.rotation=0,y.kvmDataSupported=!1,y.onKvmData=null,y.onKvmDataPending=[],y.onKvmDataAck=-1,y.holding=!1,y.lastKeepAlive=Date.now(),y.mNagleTimer=null,y.mx=0,y.my=0,y.ox=-1,y.oy=-1,y.focusmode=0,y.inflate=ZLIB.inflateInit(-15),y.xxStateChange=function(t){0==t?(y.canvas.fillStyle="#000000",y.canvas.fillRect(0,0,y.width,y.height),y.canvas.canvas.width=y.rwidth=y.width=640,y.canvas.canvas.height=y.rheight=y.height=400,QS(y.canvasid).cursor="default"):QS(y.canvasid).cursor=y.showmouse?"default":"none"},y.ProcessBinaryData=function(t){if(null==y.acc)y.acc=new Uint8Array(t);else{var a=new Uint8Array(y.acc.byteLength+t.byteLength);a.set(y.acc,0),a.set(new Uint8Array(t),y.acc.byteLength),y.acc=a}for(;null!=y.acc&&0<y.acc.byteLength;){var e=0,n=new DataView(y.acc.buffer);if(0==y.state&&12<=y.acc.byteLength)e=12,y.state=1,y.send("RFB 003.008\n");else if(1==y.state&&1<=y.acc.byteLength)e=y.acc[0]+1,y.send(String.fromCharCode(1)),y.state=2;else if(2==y.state&&4<=y.acc.byteLength){if(e=4,0!=n.getUint32(0))return y.Stop();y.send(String.fromCharCode(1)),y.state=3}else if(3==y.state&&24<=y.acc.byteLength){y.rotation=0;var o=n.getUint32(20);if(y.acc.byteLength<24+o)return;e=24+o,y.canvas.canvas.width=y.rwidth=y.width=y.ScreenWidth=n.getUint16(0),y.canvas.canvas.height=y.rheight=y.height=y.ScreenHeight=n.getUint16(2);var r="";y.useZRLE&&(r+=IntToStr(16)),r+=IntToStr(0),r+=IntToStr(1092),y.send(String.fromCharCode(2,0)+ShortToStr(r.length/4+1)+r+IntToStr(-223)),1==y.bpp&&y.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)),y.state=4,y.parent&&y.parent.xxStateChange(3),I(),y.ox=-1,null!=y.onScreenSizeChange&&y.onScreenSizeChange(y,y.ScreenWidth,y.ScreenHeight)}else if(4==y.state)switch(y.acc[0]){case 0:if(y.acc.byteLength<4)return;y.state=100+n.getUint16(2),e=4;break;case 2:e=1;break;case 3:if(y.acc.byteLength<8)return;var i=n.getUint32(4)+8;if(y.acc.byteLength<i)return;e=M(y.acc,n)}else if(100<y.state&&12<=y.acc.byteLength){var s=n.getUint16(0),c=n.getUint16(2),h=n.getUint16(4),f=n.getUint16(6),d=h*f,v=n.getUint32(8);if(v<17){if(h<1||64<h||f<1||64<f)return console.log("Invalid tile size ("+h+","+f+"), disconnecting."),y.Stop();if(y.sparew!=h||y.spareh!=f){y.sparew=y.sparew2=h,y.spareh=y.spareh2=f,1!=y.rotation&&3!=y.rotation||(y.sparew2=f,y.spareh2=h);var u=y.sparew2+"x"+y.spareh2;if(y.spare=y.sparecache[u],!y.spare){y.sparecache[u]=y.spare=y.canvas.createImageData(y.sparew2,y.spareh2);for(var l=y.sparew2*y.spareh2<<2,g=3;g<l;g+=4)y.spare.data[g]=255}}}if(4294967073==v)y.canvas.canvas.width=y.rwidth=y.width=h,y.canvas.canvas.height=y.rheight=y.height=f,y.send(String.fromCharCode(3,0,0,0,0,0)+ShortToStr(y.width)+ShortToStr(y.height)),e=12,null!=y.onScreenSizeChange&&y.onScreenSizeChange(y,y.ScreenWidth,y.ScreenHeight);else if(0==v){var p=12,m=12+d*y.bpp;if(y.acc.byteLength<m)return;if(e=m,2==y.bpp)for(g=0;g<d;g++)L(n.getUint16(p,!0),g),p+=2;else for(g=0;g<d;g++)K(y.acc[p++],g);k(y.spare,s,c)}else{if(16!=v)return y.Stop();if(y.acc.byteLength<16)return;var S=n.getUint32(12);if(y.acc.byteLength<16+S)return;p=16;if(5<S&&0==y.acc[p]&&n.getUint16(p+1,!0)==S-5)D(y.acc,p+5,s,c,h,f,d,S);else{var w=y.inflate.inflate(b(new Uint8Array(y.acc.buffer.slice(p,p+S-0))));0<w.length?D(C(w),0,s,c,h,f,d,w.length):console.log("Invalid deflate data")}e=16+S}100==--y.state&&(y.state=4,0==y.frameRateDelay?I():setTimeout(I,y.frameRateDelay))}if(0==e)return;e!=y.acc.byteLength?y.acc=new Uint8Array(y.acc.buffer.slice(e)):y.acc=null}},y.hold=function(t){y.holding!=t&&(y.holding=t,y.canvas.fillStyle="#000000",y.canvas.fillRect(0,0,y.width,y.height),0==y.holding?(y.canvas.canvas.width==y.width&&y.canvas.canvas.height==y.height||(y.canvas.canvas.width=y.width,y.canvas.canvas.height=y.height,null!=y.onScreenSizeChange&&y.onScreenSizeChange(y,y.ScreenWidth,y.ScreenHeight)),y.Send(String.fromCharCode(3,0,0,0,0,0)+ShortToStr(y.width)+ShortToStr(y.height))):(y.UnGrabMouseInput(),y.UnGrabKeyInput()))},y.tcanvas=null,y.setRotation=function(t){for(;t<0;)t+=4;var a=t%4;if(1!=y.holding){if(a==y.rotation)return!0;var e=y.canvas.canvas.width,n=y.canvas.canvas.height;1!=y.rotation&&3!=y.rotation||(e=y.canvas.canvas.height,n=y.canvas.canvas.width),null==y.tcanvas&&(y.tcanvas=document.createElement("canvas"));var o=y.tcanvas.getContext("2d");return o.setTransform(1,0,0,1,0,0),o.canvas.width=e,o.canvas.height=n,o.rotate(-90*y.rotation*Math.PI/180),0==y.rotation&&o.drawImage(y.canvas.canvas,0,0),1==y.rotation&&o.drawImage(y.canvas.canvas,-y.canvas.canvas.width,0),2==y.rotation&&o.drawImage(y.canvas.canvas,-y.canvas.canvas.width,-y.canvas.canvas.height),3==y.rotation&&o.drawImage(y.canvas.canvas,0,-y.canvas.canvas.height),0!=y.rotation&&2!=y.rotation||(y.canvas.canvas.height=e,y.canvas.canvas.width=n),1!=y.rotation&&3!=y.rotation||(y.canvas.canvas.height=n,y.canvas.canvas.width=e),y.canvas.setTransform(1,0,0,1,0,0),y.canvas.rotate(90*a*Math.PI/180),y.rotation=a,y.canvas.drawImage(y.tcanvas,T(0),x(0,0)),y.width=y.canvas.canvas.width,y.height=y.canvas.canvas.height,null!=y.onScreenResize&&y.onScreenResize(y,y.width,y.height,y.CanvasId),!0}y.rotation=a},y.Start=function(){for(var t in y.state=0,y.acc=null,y.ZRLEfirst=1,y.inflate.inflateReset(),y.onKvmDataPending=[],y.onKvmDataAck=-1,y.kvmDataSupported=!1,y.sparecache)delete y.sparecache[t]},y.Stop=function(){y.UnGrabMouseInput(),y.UnGrabKeyInput(),y.parent&&y.parent.Stop()},y.send=function(t){y.parent&&y.parent.send(t)};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(t,a){if((a=a||window.event).code&&0==y.localKeyMap){null!=(e=function(t){return t.code.startsWith("Key")&&4==t.code.length?t.code.charCodeAt(3)+(0==t.shiftKey?32:0):t.code.startsWith("Digit")&&6==t.code.length?t.code.charCodeAt(5):t.code.startsWith("Numpad")&&7==t.code.length?t.code.charCodeAt(6):o[t.code]}(a))&&y.sendkey(e,t)}else{var e,n=e=a.keyCode;0==a.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),y.sendkey(n,t)}return y.haltEvent(a)}function M(t,a){if(t.byteLength<8)return 0;var e=a.getUint32(4)+8;if(t.byteLength<e)return 0;if(null!=y.onKvmData){var n=b(t.slice(8,e));16<=n.length&&"\0KvmDataChannel"==n.substring(0,15)&&(0==y.kvmDataSupported&&(y.kvmDataSupported=!0,console.log("KVM Data Channel Supported.")),(-1==y.onKvmDataAck&&16==n.length||0!=n.charCodeAt(15))&&(y.onKvmDataAck=!0),16<=n.length&&y.onKvmData(n.substring(16)),1==y.onKvmDataAck&&0<y.onKvmDataPending.length&&y.sendKvmData(y.onKvmDataPending.shift()))}return e}y.sendkey=function(t,a){if("object"==typeof t){var e="";for(var n in t)e+=String.fromCharCode(4,t[n][1],0,0)+IntToStr(t[n][0]);y.send(e)}else y.send(String.fromCharCode(4,a,0,0)+IntToStr(t))},y.sendKvmData=function(t){!0!==y.onKvmDataAck?y.onKvmDataPending.push(t):(t="\0KvmDataChannel\0"+t,y.send(String.fromCharCode(6,0,0,0)+IntToStr(t.length)+t),y.onKvmDataAck=!1)},y.sendKeepAlive=function(){y.lastKeepAlive<Date.now()-5e3&&(y.lastKeepAlive=Date.now(),y.send(String.fromCharCode(6,0,0,0)+IntToStr(16)+"\0KvmDataChannel\0"))},y.SendCtrlAltDelMsg=function(){y.sendcad()};var n=!(y.sendcad=function(){y.sendkey([[65507,1],[65513,1],[65535,1],[65535,0],[65513,0],[65507,0]])}),r=!1;return y.GrabMouseInput=function(){if(1!=n){var t=y.canvas.canvas;t.onmouseup=y.mouseup,t.onmousedown=y.mousedown,t.onmousemove=y.mousemove,n=!0}},y.UnGrabMouseInput=function(){if(0!=n){var t=y.canvas.canvas;t.onmousemove=null,t.onmouseup=null,t.onmousedown=null,n=!1}},y.GrabKeyInput=function(){1!=r&&(document.onkeyup=y.handleKeyUp,document.onkeydown=y.handleKeyDown,document.onkeypress=y.handleKeys,r=!0)},y.UnGrabKeyInput=function(){0!=r&&(document.onkeyup=null,document.onkeydown=null,document.onkeypress=null,r=!1)},y.handleKeys=function(t){return y.haltEvent(t)},y.handleKeyUp=function(t){return e(0,t)},y.handleKeyDown=function(t){return e(1,t)},y.haltEvent=function(t){return t.preventDefault&&t.preventDefault(),t.stopPropagation&&t.stopPropagation(),!1},y.mousedblclick=function(t){},y.mousedown=function(t){return y.buttonmask|=1<<t.button,y.mousemove(t,1)},y.mouseup=function(t){return y.buttonmask&=65535-(1<<t.button),y.mousemove(t,1)},y.mousemove=function(t,a){if(y.state<4)return!0;var e=y.canvas.canvas.height/Q(y.canvasid).offsetHeight,n=y.canvas.canvas.width/Q(y.canvasid).offsetWidth,o=y.getPositionOfControl(Q(y.canvasid));if(y.mx=(event.pageX-o[0])*n,y.my=(event.pageY-o[1])*e,event.addx&&(y.mx+=event.addx),event.addy&&(y.my+=event.addy),1!=y.noMouseRotate&&(y.mx2=function(t,a){return 0==y.rotation?t:1==y.rotation?a:2==y.rotation?y.canvas.canvas.width-t:3==y.rotation?y.canvas.canvas.height-a:0}(y.mx,y.my),y.my=function(t,a){return 0==y.rotation?a:1==y.rotation?y.canvas.canvas.width-t:2==y.rotation?y.canvas.canvas.height-a:3==y.rotation?t:0}(y.mx,y.my),y.mx=y.mx2),1==a?(y.send(String.fromCharCode(5,y.buttonmask)+ShortToStr(y.mx)+ShortToStr(y.my)),null!=y.mNagleTimer&&(clearTimeout(y.mNagleTimer),y.mNagleTimer=null)):null==y.mNagleTimer&&(y.mNagleTimer=setTimeout(function(){y.send(String.fromCharCode(5,y.buttonmask)+ShortToStr(y.mx)+ShortToStr(y.my)),y.mNagleTimer=null},50)),QV("DeskFocus",y.focusmode),0!=y.focusmode){var r=Math.min(y.mx,y.canvas.canvas.width-y.focusmode),i=Math.min(y.my,y.canvas.canvas.height-y.focusmode),s=2*y.focusmode,c=Q(y.canvasid),h=c.offsetHeight/y.canvas.canvas.height,f=c.offsetWidth/y.canvas.canvas.width,d=QS("DeskFocus"),v=y.getPositionOfControl(Q(y.canvasid).parentElement);d.left=Math.max((r-y.focusmode)*h,0)+(pos[0]-v[0])+"px",d.top=Math.max((i-y.focusmode)*f,0)+(pos[1]-v[1])+"px",d.width=s*h-6+"px",d.height=s*h-6+"px"}return y.haltEvent(t)},y.getPositionOfControl=function(t){var a=Array(2);for(a[0]=a[1]=0;t;)a[0]+=t.offsetLeft,a[1]+=t.offsetTop,t=t.offsetParent;return a},y} |