MeshCentral/public/scripts/amt-desktop-0.0.2-min.js

1 line
15 KiB
JavaScript

var CreateAmtRemoteDesktop=function(a,t){var S={};function w(a,t,e,n,o,r,c){var s,i,h,d,f=a.charCodeAt(t++),v={},l=0,u=0;if(0==f){if(2==S.bpp)for(d=0;d<c;d++)A(a.charCodeAt(t++)+(a.charCodeAt(t++)<<8),d);else for(d=0;d<c;d++)y(a.charCodeAt(t++),d);C(S.spare,e,n)}else if(1==f){i=a.charCodeAt(t++)+(2==S.bpp?a.charCodeAt(t++)<<8:0),S.canvas.fillStyle="rgb("+(1==S.bpp?(224&i)+","+((28&i)<<3)+","+T((3&i)<<6):(i>>8&248)+","+(i>>3&252)+","+((31&i)<<3))+")";var p=k(e,n);n=K(e,n),e=p,S.canvas.fillRect(e,n,o,r)}else if(1<f&&f<17){var m=4,g=15;if(2==S.bpp){for(d=0;d<f;d++)v[d]=a.charCodeAt(t++)+(a.charCodeAt(t++)<<8);for(2==f?g=m=1:f<=4&&(m=2,g=3);l<c&&t<a.length;)for(i=a.charCodeAt(t++),d=8-m;0<=d;d-=m)A(v[i>>d&g],l++)}else{for(d=0;d<f;d++)v[d]=a.charCodeAt(t++);for(2==f?g=m=1:f<=4&&(m=2,g=3);l<c&&t<a.length;)for(i=a.charCodeAt(t++),d=8-m;0<=d;d-=m)y(v[i>>d&g],l++)}C(S.spare,e,n)}else if(128==f){if(2==S.bpp)for(;l<c&&t<a.length;){for(i=a.charCodeAt(t++)+(a.charCodeAt(t++)<<8),u=1;u+=h=a.charCodeAt(t++),255==h;);if(0==S.rotation)b(i,l,u),l+=u;else for(;0<=--u;)A(i,l++)}else for(;l<c&&t<a.length;){for(i=a.charCodeAt(t++),u=1;u+=h=a.charCodeAt(t++),255==h;);if(0==S.rotation)D(i,l,u),l+=u;else for(;0<=--u;)y(i,l++)}C(S.spare,e,n)}else if(129<f){if(2==S.bpp)for(d=0;d<f-128;d++)v[d]=a.charCodeAt(t++)+(a.charCodeAt(t++)<<8);else for(d=0;d<f-128;d++)v[d]=a.charCodeAt(t++);for(;l<c&&t<a.length;){if(u=1,i=v[(s=a.charCodeAt(t++))%128],127<s)for(;u+=h=a.charCodeAt(t++),255==h;);if(0==S.rotation)2==S.bpp?b(i,l,u):D(i,l,u),l+=u;else if(2==S.bpp)for(;0<=--u;)A(i,l++);else for(;0<=--u;)y(i,l++)}C(S.spare,e,n)}}function C(a,t,e){if(1!=S.holding){var n=function(a,t){return 0==S.rotation?a:1==S.rotation?S.canvas.canvas.width-S.sparew2-t:2==S.rotation?S.canvas.canvas.width-S.sparew2-a:3==S.rotation?t:0}(t,e);e=function(a,t){return 0==S.rotation?t:1==S.rotation?a:2==S.rotation?S.canvas.canvas.height-S.spareh2-t:3==S.rotation?S.canvas.canvas.height-S.spareh-a:0}(t,e),t=n,S.canvas.putImageData(a,t,e)}}function y(a,t){var e=t<<2;if(0<S.rotation)if(1==S.rotation){var n=t%S.sparew,o=Math.floor(t/S.sparew);e=(t=n*S.sparew2+(S.sparew2-1-o))<<2}else if(2==S.rotation)e=S.sparew*S.spareh*4-4-e;else if(3==S.rotation){n=t%S.sparew,o=Math.floor(t/S.sparew);e=(t=(S.sparew2-1-n)*S.sparew2+o)<<2}S.spare.data[e]=224&a,S.spare.data[e+1]=(28&a)<<3,S.spare.data[e+2]=T((3&a)<<6)}function A(a,t){var e=t<<2;if(0<S.rotation)if(1==S.rotation){var n=t%S.sparew,o=Math.floor(t/S.sparew);e=(t=n*S.sparew2+(S.sparew2-1-o))<<2}else if(2==S.rotation)e=S.sparew*S.spareh*4-4-e;else if(3==S.rotation){n=t%S.sparew,o=Math.floor(t/S.sparew);e=(t=(S.sparew2-1-n)*S.sparew2+o)<<2}S.spare.data[e]=a>>8&248,S.spare.data[e+1]=a>>3&252,S.spare.data[e+2]=(31&a)<<3}function D(a,t,e){for(var n=t<<2,o=224&a,r=(28&a)<<3,c=T((3&a)<<6);0<=--e;)S.spare.data[n]=o,S.spare.data[n+1]=r,S.spare.data[n+2]=c,n+=4}function b(a,t,e){for(var n=t<<2,o=a>>8&248,r=a>>3&252,c=(31&a)<<3;0<=--e;)S.spare.data[n]=o,S.spare.data[n+1]=r,S.spare.data[n+2]=c,n+=4}function k(a){return 0==S.rotation?a: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 T(a){return 127<a?a+32:a}function R(){if(1!=S.holding)if(0<S.focusmode){var 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}else 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="",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.mNagleTimer=null,S.mx=0,S.my=0,S.ox=-1,S.oy=-1,S.focusmode=0,S.inflate=ZLIB.inflateInit(-15),S.Debug=function(a){console.log(a)},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.ProcessData=function(a){if(a)for(S.acc+=a;0<S.acc.length;){var t=0;if(0==S.state&&12<=S.acc.length)t=12,S.state=1,S.send("RFB 003.008\n");else if(1==S.state&&1<=S.acc.length)t=S.acc.charCodeAt(0)+1,S.send(String.fromCharCode(1)),S.state=2;else if(2==S.state&&4<=S.acc.length){if(t=4,0!=ReadInt(S.acc,0))return S.Stop();S.send(String.fromCharCode(1)),S.state=3}else if(3==S.state&&24<=S.acc.length){S.rotation=0;var e=ReadInt(S.acc,20);if(S.acc.length<24+e)return;t=24+e,S.canvas.canvas.width=S.rwidth=S.width=S.ScreenWidth=ReadShort(S.acc,0),S.canvas.canvas.height=S.rheight=S.height=S.ScreenHeight=ReadShort(S.acc,2);var n="";S.useZRLE&&(n+=IntToStr(16)),n+=IntToStr(0),n+=IntToStr(1092),S.send(String.fromCharCode(2,0)+ShortToStr(n.length/4+1)+n+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),R(),S.ox=-1,null!=S.onScreenSizeChange&&S.onScreenSizeChange(S,S.ScreenWidth,S.ScreenHeight)}else if(4==S.state)switch(S.acc.charCodeAt(0)){case 0:if(S.acc.length<4)return;S.state=100+ReadShort(S.acc,2),t=4;break;case 2:t=1;break;case 3:if(S.acc.length<8)return;var o=ReadInt(S.acc,4)+8;if(S.acc.length<o)return;t=I(S.acc)}else if(100<S.state&&12<=S.acc.length){var r=ReadShort(S.acc,0),c=ReadShort(S.acc,2),s=ReadShort(S.acc,4),i=ReadShort(S.acc,6),h=s*i,d=ReadInt(S.acc,8);if(d<17){if(s<1||64<s||i<1||64<i)return console.log("Invalid tile size ("+s+","+i+"), disconnecting."),S.Stop();if(S.sparew!=s||S.spareh!=i){S.sparew=S.sparew2=s,S.spareh=S.spareh2=i,1!=S.rotation&&3!=S.rotation||(S.sparew2=i,S.spareh2=s);var f=S.sparew2+"x"+S.spareh2;if(S.spare=S.sparecache[f],!S.spare){S.sparecache[f]=S.spare=S.canvas.createImageData(S.sparew2,S.spareh2);for(var v=S.sparew2*S.spareh2<<2,l=3;l<v;l+=4)S.spare.data[l]=255}}}if(4294967073==d)S.canvas.canvas.width=S.rwidth=S.width=s,S.canvas.canvas.height=S.rheight=S.height=i,S.send(String.fromCharCode(3,0,0,0,0,0)+ShortToStr(S.width)+ShortToStr(S.height)),t=12,null!=S.onScreenSizeChange&&S.onScreenSizeChange(S,S.ScreenWidth,S.ScreenHeight);else if(0==d){var u=12,p=12+h*S.bpp;if(S.acc.length<p)return;if(t=p,2==S.bpp)for(l=0;l<h;l++)A(S.acc.charCodeAt(u++)+(S.acc.charCodeAt(u++)<<8),l);else for(l=0;l<h;l++)y(S.acc.charCodeAt(u++),l);C(S.spare,r,c)}else{if(16!=d)return S.Debug("Unknown Encoding: "+d),S.Stop();if(S.acc.length<16)return;var m=ReadInt(S.acc,12);if(S.acc.length<16+m)return;u=16;if(5<m&&0==S.acc.charCodeAt(u)&&ReadShortX(S.acc,u+1)==m-5)w(S.acc,u+5,r,c,s,i,h,m);else{var g=S.inflate.inflate(S.acc.substring(u,u+m-0));0<g.length?w(g,0,r,c,s,i,h,g.length):S.Debug("Invalid deflate data")}t=16+m}100==--S.state&&(S.state=4,0==S.frameRateDelay?R():setTimeout(R,S.frameRateDelay))}if(0==t)return;S.acc=S.acc.substring(t)}},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="",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){if((t=t||window.event).code&&0==S.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))&&S.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),S.sendkey(n,a)}return S.haltEvent(t)}function I(a){if(a.length<8)return 0;var t=ReadInt(S.acc,4)+8;if(a.length<t)return 0;if(null!=S.onKvmData){var e=a.substring(8,t);16<=e.length&&"\0KvmDataChannel"==e.substring(0,15)&&(0==S.kvmDataSupported&&(S.kvmDataSupported=!0,console.log("KVM Data Channel Supported.")),(-1==S.onKvmDataAck&&16==e.length||0!=e.charCodeAt(15))&&(S.onKvmDataAck=!0),16<=e.length&&S.onKvmData(e.substring(16)),1==S.onKvmDataAck&&0<S.onKvmDataPending.length&&S.sendKvmData(S.onKvmDataPending.shift()))}return t}S.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]);S.send(e)}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(){if(1!=n){var a=S.canvas.canvas;a.onmouseup=S.mouseup,a.onmousedown=S.mousedown,a.onmousemove=S.mousemove,n=!0}},S.UnGrabMouseInput=function(){if(0!=n){var a=S.canvas.canvas;a.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){return S.buttonmask|=1<<a.button,S.mousemove(a,1)},S.mouseup=function(a){return S.buttonmask&=65535-(1<<a.button),S.mousemove(a,1)},S.mousemove=function(a,t){if(S.state<4)return!0;var e=S.canvas.canvas.height/Q(S.canvasid).offsetHeight,n=S.canvas.canvas.width/Q(S.canvasid).offsetWidth,o=S.getPositionOfControl(Q(S.canvasid));if(S.mx=(event.pageX-o[0])*n,S.my=(event.pageY-o[1])*e,event.addx&&(S.mx+=event.addx),event.addy&&(S.my+=event.addy),1!=S.noMouseRotate&&(S.mx2=function(a,t){return 0==S.rotation?a:1==S.rotation?t:2==S.rotation?S.canvas.canvas.width-a:3==S.rotation?S.canvas.canvas.height-t:0}(S.mx,S.my),S.my=function(a,t){return 0==S.rotation?t:1==S.rotation?S.canvas.canvas.width-a:2==S.rotation?S.canvas.canvas.height-t:3==S.rotation?a:0}(S.mx,S.my),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){var r=Math.min(S.mx,S.canvas.canvas.width-S.focusmode),c=Math.min(S.my,S.canvas.canvas.height-S.focusmode),s=2*S.focusmode,i=Q(S.canvasid),h=i.offsetHeight/S.canvas.canvas.height,d=i.offsetWidth/S.canvas.canvas.width,f=QS("DeskFocus"),v=S.getPositionOfControl(Q(S.canvasid).parentElement);f.left=Math.max((r-S.focusmode)*h,0)+(pos[0]-v[0])+"px",f.top=Math.max((c-S.focusmode)*d,0)+(pos[1]-v[1])+"px",f.width=s*h-6+"px",f.height=s*h-6+"px"}return 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}