mirror of
https://github.com/Ylianst/MeshCentral.git
synced 2024-12-26 23:25:53 -05:00
1 line
15 KiB
JavaScript
1 line
15 KiB
JavaScript
var CreateAmtRemoteDesktop=function(a,t){var y={};function b(a){return String.fromCharCode.apply(null,a)}function C(a,t,e,n,o,r,s){var i,c,h,d,v=a[t++],f={},l=0,u=0;if(0==v){if(2==y.bpp)for(d=0;d<s;d++)K(a[t++]+(a[t++]<<8),d);else for(d=0;d<s;d++)k(a[t++],d);D(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)+","+x((3&c)<<6):(c>>8&248)+","+(c>>3&252)+","+((31&c)<<3))+")";var g=L(e);n=T(0,n),e=g,y.canvas.fillRect(e,n,o,r)}else if(1<v&&v<17){var p=4,m=15;if(2==y.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);l<s&&t<a.byteLength;)for(c=a[t++],d=8-p;0<=d;d-=p)K(f[c>>d&m],l++)}else{for(d=0;d<v;d++)f[d]=a[t++];for(2==v?m=p=1:v<=4&&(p=2,m=3);l<s&&t<a.byteLength;)for(c=a[t++],d=8-p;0<=d;d-=p)k(f[c>>d&m],l++)}D(y.spare,e,n)}else if(128==v){if(2==y.bpp)for(;l<s&&t<a.byteLength;){for(c=a[t++]+(a[t++]<<8),u=1;u+=h=a[t++],255==h;);if(0==y.rotation)w(c,l,u),l+=u;else for(;0<=--u;)K(c,l++)}else for(;l<s&&t<a.byteLength;){for(c=a[t++],u=1;u+=h=a[t++],255==h;);if(0==y.rotation)S(c,l,u),l+=u;else for(;0<=--u;)k(c,l++)}D(y.spare,e,n)}else if(129<v){if(2==y.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(;l<s&&t<a.byteLength;){if(u=1,c=f[(i=a[t++])%128],127<i)for(;u+=h=a[t++],255==h;);if(0==y.rotation)(2==y.bpp?w:S)(c,l,u),l+=u;else if(2==y.bpp)for(;0<=--u;)K(c,l++);else for(;0<=--u;)k(c,l++)}D(y.spare,e,n)}}function D(a,t,e){var n,o,r,s,i;1!=y.holding&&(o=t,r=e,n=0==y.rotation?o:1==y.rotation?y.canvas.canvas.width-y.sparew2-r:2==y.rotation?y.canvas.canvas.width-y.sparew2-o:3==y.rotation?r:0,s=t,i=e,e=0==y.rotation?i:1==y.rotation?s:2==y.rotation?y.canvas.canvas.height-y.spareh2-i:3==y.rotation?y.canvas.canvas.height-y.spareh-s:0,t=n,y.canvas.putImageData(a,t,e))}function k(a,t){var e,n,o=t<<2;0<y.rotation&&(1==y.rotation?(e=t%y.sparew,n=Math.floor(t/y.sparew),o=(t=e*y.sparew2+(y.sparew2-1-n))<<2):2==y.rotation?o=y.sparew*y.spareh*4-4-o:3==y.rotation&&(e=t%y.sparew,n=Math.floor(t/y.sparew),o=(t=(y.sparew2-1-e)*y.sparew2+n)<<2)),y.spare.data[o]=224&a,y.spare.data[o+1]=(28&a)<<3,y.spare.data[o+2]=x((3&a)<<6)}function K(a,t){var e,n,o=t<<2;0<y.rotation&&(1==y.rotation?(e=t%y.sparew,n=Math.floor(t/y.sparew),o=(t=e*y.sparew2+(y.sparew2-1-n))<<2):2==y.rotation?o=y.sparew*y.spareh*4-4-o:3==y.rotation&&(e=t%y.sparew,n=Math.floor(t/y.sparew),o=(t=(y.sparew2-1-e)*y.sparew2+n)<<2)),y.spare.data[o]=a>>8&248,y.spare.data[o+1]=a>>3&252,y.spare.data[o+2]=(31&a)<<3}function S(a,t,e){for(var n=t<<2,o=224&a,r=(28&a)<<3,s=x((3&a)<<6);0<=--e;)y.spare.data[n]=o,y.spare.data[n+1]=r,y.spare.data[n+2]=s,n+=4}function w(a,t,e){for(var n=t<<2,o=a>>8&248,r=a>>3&252,s=(31&a)<<3;0<=--e;)y.spare.data[n]=o,y.spare.data[n+1]=r,y.spare.data[n+2]=s,n+=4}function L(a){return 0==y.rotation||1==y.rotation?a:2==y.rotation?a-y.canvas.canvas.width:3==y.rotation?a-y.canvas.canvas.height:0}function T(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 x(a){return 127<a?a+32:a}function A(){var a;1!=y.holding&&(0<y.focusmode?(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):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.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){var t;for(null==y.acc?y.acc=new Uint8Array(a):((t=new Uint8Array(y.acc.byteLength+a.byteLength)).set(y.acc,0),t.set(new Uint8Array(a),y.acc.byteLength),y.acc=t);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 s=n.getUint32(4)+8;if(y.acc.byteLength<s)return;e=function(a,t){if(a.byteLength<8)return 0;var e=t.getUint32(4)+8;if(a.byteLength<e)return 0;{var n;null==y.onKvmData||16<=(n=b(a.slice(8,e))).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.acc,n)}else if(100<y.state&&12<=y.acc.byteLength){var i=n.getUint16(0),c=n.getUint16(2),h=n.getUint16(4),d=n.getUint16(6),v=h*d,f=n.getUint32(8);if(f<17){if(h<1||64<h||d<1||64<d)return console.log("Invalid tile size ("+h+","+d+"), disconnecting."),y.Stop();if(y.sparew!=h||y.spareh!=d){y.sparew=y.sparew2=h,y.spareh=y.spareh2=d,1!=y.rotation&&3!=y.rotation||(y.sparew2=d,y.spareh2=h);var l=y.sparew2+"x"+y.spareh2;if(y.spare=y.sparecache[l],!y.spare){y.sparecache[l]=y.spare=y.canvas.createImageData(y.sparew2,y.spareh2);for(var u=y.sparew2*y.spareh2<<2,g=3;g<u;g+=4)y.spare.data[g]=255}}}if(4294967073==f)y.canvas.canvas.width=y.rwidth=y.width=h,y.canvas.canvas.height=y.rheight=y.height=d,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==f){var p=12,m=12+v*y.bpp;if(y.acc.byteLength<m)return;if(e=m,2==y.bpp)for(g=0;g<v;g++)K(n.getUint16(p,!0),g),p+=2;else for(g=0;g<v;g++)k(y.acc[p++],g);D(y.spare,i,c)}else{if(16!=f)return y.Stop();if(y.acc.byteLength<16)return;var S=n.getUint32(12);if(y.acc.byteLength<16+S)return;var w,p=16;5<S&&0==y.acc[p]&&n.getUint16(p+1,!0)==S-5?C(y.acc,p+5,i,c,h,d,v):0<(w=y.inflate.inflate(b(new Uint8Array(y.acc.buffer.slice(p,+(p+S)))))).length?C(function(a){for(var t=new Uint8Array(a.length),e=0,n=a.length;e<n;++e)t[e]=a.charCodeAt(e);return t}(w),0,i,c,h,d,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,L(0),T(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 r={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,o;return(t=t||window.event).code&&0==y.localKeyMap?null!=(e=(o=t).code.startsWith("Key")&&4==o.code.length?o.code.charCodeAt(3)+(0==o.shiftKey?32:0):o.code.startsWith("Digit")&&6==o.code.length?o.code.charCodeAt(5):o.code.startsWith("Numpad")&&7==o.code.length?o.code.charCodeAt(6):r[o.code])&&y.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),y.sendkey(n,a)),y.haltEvent(t)}y.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]);y.send(n)}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]])}),o=!1;return y.GrabMouseInput=function(){var a;1!=n&&((a=y.canvas.canvas).onmouseup=y.mouseup,a.onmousedown=y.mousedown,a.onmousemove=y.mousemove,n=!0)},y.UnGrabMouseInput=function(){var a;0!=n&&((a=y.canvas.canvas).onmousemove=null,a.onmouseup=null,a.onmousedown=null,n=!1)},y.GrabKeyInput=function(){1!=o&&(document.onkeyup=y.handleKeyUp,document.onkeydown=y.handleKeyDown,document.onkeypress=y.handleKeys,o=!0)},y.UnGrabKeyInput=function(){0!=o&&(document.onkeyup=null,document.onkeydown=null,document.onkeypress=null,o=!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){return y.buttonmask|=1<<a.button,y.mousemove(a,1)},y.mouseup=function(a){return y.buttonmask&=65535-(1<<a.button),y.mousemove(a,1)},y.mousemove=function(a,t){if(y.state<4)return!0;var e,n,o,r,s,i,c,h,d,v,f,l,u=y.canvas.canvas.height/Q(y.canvasid).offsetHeight,g=y.canvas.canvas.width/Q(y.canvasid).offsetWidth,p=y.getPositionOfControl(Q(y.canvasid));return y.mx=(event.pageX-p[0])*g,y.my=(event.pageY-p[1])*u,event.addx&&(y.mx+=event.addx),event.addy&&(y.my+=event.addy),1!=y.noMouseRotate&&(y.mx2=(o=y.mx,r=y.my,0==y.rotation?o:1==y.rotation?r:2==y.rotation?y.canvas.canvas.width-o:3==y.rotation?y.canvas.canvas.height-r:0),y.my=(e=y.mx,n=y.my,0==y.rotation?n:1==y.rotation?y.canvas.canvas.width-e:2==y.rotation?y.canvas.canvas.height-n:3==y.rotation?e:0),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&&(s=Math.min(y.mx,y.canvas.canvas.width-y.focusmode),i=Math.min(y.my,y.canvas.canvas.height-y.focusmode),c=2*y.focusmode,d=(h=Q(y.canvasid)).offsetHeight/y.canvas.canvas.height,v=h.offsetWidth/y.canvas.canvas.width,f=QS("DeskFocus"),l=y.getPositionOfControl(Q(y.canvasid).parentElement),f.left=Math.max((s-y.focusmode)*d,0)+(pos[0]-l[0])+"px",f.top=Math.max((i-y.focusmode)*v,0)+(pos[1]-l[1])+"px",f.width=c*d-6+"px",f.height=c*d-6+"px"),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} |