MeshCentral/public/scripts/amt-desktop-0.0.2-min.js
2020-11-23 16:54:28 -08:00

1 line
15 KiB
JavaScript

var CreateAmtRemoteDesktop=function(a,t){var y={};function b(a){return String.fromCharCode.apply(null,a)}function C(a){for(var t=new Uint8Array(a.length),e=0,n=a.length;e<n;++e)t[e]=a.charCodeAt(e);return t}function D(a,t,e,n,o,r,i){var s,c,h,f,v=a[t++],d={},u=0,l=0;if(0==v){if(2==y.bpp)for(f=0;f<i;f++)L(a[t++]+(a[t++]<<8),f);else for(f=0;f<i;f++)K(a[t++],f);k(y.spare,e,n)}else if(1==v){c=a[t++]+(2==y.bpp?a[t++]<<8:0),y.canvas.fillStyle="rgb("+(1==y.bpp?(224&c)+","+((28&c)<<3)+","+M((3&c)<<6):(c>>8&248)+","+(c>>3&252)+","+((31&c)<<3))+")";var p=T(e,n);n=x(e,n),e=p,y.canvas.fillRect(e,n,o,r)}else if(1<v&&v<17){var g=4,m=15;if(2==y.bpp){for(f=0;f<v;f++)d[f]=a[t++]+(a[t++]<<8);for(2==v?m=g=1:v<=4&&(g=2,m=3);u<i&&t<a.byteLength;)for(c=a[t++],f=8-g;0<=f;f-=g)L(d[c>>f&m],u++)}else{for(f=0;f<v;f++)d[f]=a[t++];for(2==v?m=g=1:v<=4&&(g=2,m=3);u<i&&t<a.byteLength;)for(c=a[t++],f=8-g;0<=f;f-=g)K(d[c>>f&m],u++)}k(y.spare,e,n)}else if(128==v){if(2==y.bpp)for(;u<i&&t<a.byteLength;){for(c=a[t++]+(a[t++]<<8),l=1;l+=h=a[t++],255==h;);if(0==y.rotation)w(c,u,l),u+=l;else for(;0<=--l;)L(c,u++)}else for(;u<i&&t<a.byteLength;){for(c=a[t++],l=1;l+=h=a[t++],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<v){if(2==y.bpp)for(f=0;f<v-128;f++)d[f]=a[t++]+(a[t++]<<8);else for(f=0;f<v-128;f++)d[f]=a[t++];for(;u<i&&t<a.byteLength;){if(l=1,c=d[(s=a[t++])%128],127<s)for(;l+=h=a[t++],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(a,t,e){if(1!=y.holding){var n=function(a,t){return 0==y.rotation?a:1==y.rotation?y.canvas.canvas.width-y.sparew2-t:2==y.rotation?y.canvas.canvas.width-y.sparew2-a:3==y.rotation?t:0}(t,e);e=function(a,t){return 0==y.rotation?t:1==y.rotation?a:2==y.rotation?y.canvas.canvas.height-y.spareh2-t:3==y.rotation?y.canvas.canvas.height-y.spareh-a:0}(t,e),t=n,y.canvas.putImageData(a,t,e)}}function K(a,t){var e=t<<2;if(0<y.rotation)if(1==y.rotation){var n=t%y.sparew,o=Math.floor(t/y.sparew);e=(t=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=t%y.sparew,o=Math.floor(t/y.sparew);e=(t=(y.sparew2-1-n)*y.sparew2+o)<<2}y.spare.data[e]=224&a,y.spare.data[e+1]=(28&a)<<3,y.spare.data[e+2]=M((3&a)<<6)}function L(a,t){var e=t<<2;if(0<y.rotation)if(1==y.rotation){var n=t%y.sparew,o=Math.floor(t/y.sparew);e=(t=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=t%y.sparew,o=Math.floor(t/y.sparew);e=(t=(y.sparew2-1-n)*y.sparew2+o)<<2}y.spare.data[e]=a>>8&248,y.spare.data[e+1]=a>>3&252,y.spare.data[e+2]=(31&a)<<3}function S(a,t,e){for(var n=t<<2,o=224&a,r=(28&a)<<3,i=M((3&a)<<6);0<=--e;)y.spare.data[n]=o,y.spare.data[n+1]=r,y.spare.data[n+2]=i,n+=4}function w(a,t,e){for(var n=t<<2,o=a>>8&248,r=a>>3&252,i=(31&a)<<3;0<=--e;)y.spare.data[n]=o,y.spare.data[n+1]=r,y.spare.data[n+2]=i,n+=4}function T(a){return 0==y.rotation?a:1==y.rotation?a:2==y.rotation?a-y.canvas.canvas.width:3==y.rotation?a-y.canvas.canvas.height:0}function x(a,t){return 0==y.rotation?t:1==y.rotation?t-y.canvas.canvas.width:2==y.rotation?t-y.canvas.canvas.height:3==y.rotation?t:0}function M(a){return 127<a?a+32:a}function A(){if(1!=y.holding)if(0<y.focusmode){var a=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(a+Math.abs(y.ox-y.mx))+ShortToStr(a+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=a,y.CanvasId=Q(a),y.scrolldiv=t,y.canvas=Q(a).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.SwapMouse=!1,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(a){0==a?(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(a){if(null==y.acc)y.acc=new Uint8Array(a);else{var t=new Uint8Array(y.acc.byteLength+a.byteLength);t.set(y.acc,0),t.set(new Uint8Array(a),y.acc.byteLength),y.acc=t}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),A(),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=I(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),v=h*f,d=n.getUint32(8);if(d<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,p=3;p<l;p+=4)y.spare.data[p]=255}}}if(4294967073==d)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==d){var g=12,m=12+v*y.bpp;if(y.acc.byteLength<m)return;if(e=m,2==y.bpp)for(p=0;p<v;p++)L(n.getUint16(g,!0),p),g+=2;else for(p=0;p<v;p++)K(y.acc[g++],p);k(y.spare,s,c)}else{if(16!=d)return y.Stop();if(y.acc.byteLength<16)return;var S=n.getUint32(12);if(y.acc.byteLength<16+S)return;g=16;if(5<S&&0==y.acc[g]&&n.getUint16(g+1,!0)==S-5)D(y.acc,g+5,s,c,h,f,v,S);else{var w=y.inflate.inflate(b(new Uint8Array(y.acc.buffer.slice(g,g+S-0))));0<w.length?D(C(w),0,s,c,h,f,v,w.length):console.log("Invalid deflate data")}e=16+S}100==--y.state&&(y.state=4,0==y.frameRateDelay?A():setTimeout(A,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(a){y.holding!=a&&(y.holding=a,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(a){for(;a<0;)a+=4;var t=a%4;if(1!=y.holding){if(t==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*t*Math.PI/180),y.rotation=t,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=t},y.Start=function(){for(var a 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[a]},y.Stop=function(){y.UnGrabMouseInput(),y.UnGrabKeyInput(),y.parent&&y.parent.Stop()},y.send=function(a){y.parent&&y.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){if((t=t||window.event).code&&0==y.localKeyMap){null!=(e=function(a){return a.code.startsWith("Key")&&4==a.code.length?a.code.charCodeAt(3)+(0==a.shiftKey?32:0):a.code.startsWith("Digit")&&6==a.code.length?a.code.charCodeAt(5):a.code.startsWith("Numpad")&&7==a.code.length?a.code.charCodeAt(6):o[a.code]}(t))&&y.sendkey(e,a)}else{var e,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),y.sendkey(n,a)}return y.haltEvent(t)}function I(a,t){if(a.byteLength<8)return 0;var e=t.getUint32(4)+8;if(a.byteLength<e)return 0;if(null!=y.onKvmData){var n=b(a.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(a,t){if("object"==typeof a){var e="";for(var n in a)e+=String.fromCharCode(4,a[n][1],0,0)+IntToStr(a[n][0]);y.send(e)}else y.send(String.fromCharCode(4,t,0,0)+IntToStr(a))},y.sendKvmData=function(a){!0!==y.onKvmDataAck?y.onKvmDataPending.push(a):(a="\0KvmDataChannel\0"+a,y.send(String.fromCharCode(6,0,0,0)+IntToStr(a.length)+a),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 a=y.canvas.canvas;a.onmouseup=y.mouseup,a.onmousedown=y.mousedown,a.onmousemove=y.mousemove,n=!0}},y.UnGrabMouseInput=function(){if(0!=n){var a=y.canvas.canvas;a.onmousemove=null,a.onmouseup=null,a.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(a){return y.haltEvent(a)},y.handleKeyUp=function(a){return e(0,a)},y.handleKeyDown=function(a){return e(1,a)},y.haltEvent=function(a){return a.preventDefault&&a.preventDefault(),a.stopPropagation&&a.stopPropagation(),!1},y.mousedblclick=function(a){},y.mousedown=function(a){var t=a.button;return y.SwapMouse&&(0==t?t=2:2==t&&(t=0)),y.buttonmask|=1<<t,y.mousemove(a,1)},y.mouseup=function(a){var t=a.button;return y.SwapMouse&&(0==t?t=2:2==t&&(t=0)),y.buttonmask&=65535-(1<<t),y.mousemove(a,1)},y.mousemove=function(a,t){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(a,t){return 0==y.rotation?a:1==y.rotation?t:2==y.rotation?y.canvas.canvas.width-a:3==y.rotation?y.canvas.canvas.height-t:0}(y.mx,y.my),y.my=function(a,t){return 0==y.rotation?t:1==y.rotation?y.canvas.canvas.width-a:2==y.rotation?y.canvas.canvas.height-t:3==y.rotation?a:0}(y.mx,y.my),y.mx=y.mx2),1==t?(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,v=QS("DeskFocus"),d=y.getPositionOfControl(Q(y.canvasid).parentElement);v.left=Math.max((r-y.focusmode)*h,0)+(pos[0]-d[0])+"px",v.top=Math.max((i-y.focusmode)*f,0)+(pos[1]-d[1])+"px",v.width=s*h-6+"px",v.height=s*h-6+"px"}return y.haltEvent(a)},y.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},y}