MeshCentral/public/scripts/agent-rdp-0.0.1-min.js
2022-05-16 13:48:15 -07:00

1 line
5.2 KiB
JavaScript

var CreateRDPDesktop=function(e){var s={};function n(e){return(!0===s.m.SwapMouse?[2,0,1,0,0]:[1,0,2,0,0])[e]}function i(e){s.State!=e&&(s.State=e,null!=s.onStateChanged&&s.onStateChanged(s,s.State))}function a(e){var t=s.Canvas.canvas.height/s.CanvasId.clientHeight,n=s.Canvas.canvas.width/s.CanvasId.clientWidth,a=function(e){var t=Array(2);for(t[0]=t[1]=0;e;)t[0]+=e.offsetLeft,t[1]+=e.offsetTop,e=e.offsetParent;return t}(s.Canvas.canvas),n=(e.pageX-a[0])*n,t=(e.pageY-a[1])*t;return e.addx&&(n+=e.addx),e.addy&&(t+=e.addy),{x:n,y:t}}s.m={KeyAction:{NONE:0,DOWN:1,UP:2,SCROLL:3,EXUP:4,EXDOWN:5,DBLCLICK:6}},s.State=0,s.canvas=Q(e),"string"==typeof(s.CanvasId=e)&&(s.CanvasId=Q(e)),s.Canvas=s.CanvasId.getContext("2d"),s.ScreenWidth=s.width=1280,s.ScreenHeight=s.height=1024,s.m.onClipboardChanged=null,s.Start=function(e,t,n){i(1),s.nodeid=e,s.port=t;var a={savepass:(s.credentials=n).savecred,useServerCreds:n.servercred,width:n.width,height:n.height,flags:n.flags,workingDir:n.workdir,alternateShell:n.altshell};n.width&&n.height&&(a.width=s.ScreenWidth=s.width=n.width,a.height=s.ScreenHeight=s.height=n.height,delete n.width,delete n.height),s.render=new Mstsc.Canvas.create(s.canvas),s.socket=new WebSocket("wss://"+window.location.host+"/mstscrelay.ashx"),s.socket.binaryType="arraybuffer",s.socket.onopen=function(){i(2),s.socket.send(JSON.stringify(["infos",{ip:s.nodeid,port:s.port,screen:{width:s.width,height:s.height},domain:n.domain,username:n.username,password:n.password,options:a,locale:Mstsc.locale()}]))},s.socket.onmessage=function(e){if("string"==typeof e.data){var t=JSON.parse(e.data);switch(t[0]){case"rdp-connect":i(3),s.rotation=0,s.Canvas.setTransform(1,0,0,1,0,0),s.Canvas.canvas.width=s.ScreenWidth,s.Canvas.canvas.height=s.ScreenHeight,s.Canvas.fillRect(0,0,s.ScreenWidth,s.ScreenHeight),null!=s.m.onScreenSizeChange&&s.m.onScreenSizeChange(s,s.ScreenWidth,s.ScreenHeight,s.CanvasId);break;case"rdp-bitmap":if(null==s.bitmapData)break;var n=t[1];n.data=s.bitmapData,delete s.bitmapData,s.render.update(n);break;case"rdp-close":s.Stop();break;case"rdp-error":n=t[1];console.log("[mstsc.js] error : "+n.code+"("+n.message+")"),s.Stop();break;case"rdp-clipboard":s.lastClipboardContent=t[1],s.m.onClipboardChanged&&s.m.onClipboardChanged(t[1]);break;case"ping":s.socket.send('["pong"]')}}else s.bitmapData=e.data},s.socket.onclose=function(){i(0)},i(1)},s.Stop=function(){s.Canvas.fillRect(0,0,s.ScreenWidth,s.ScreenHeight),s.socket&&s.socket.close()},s.m.setClipboard=function(e){s.socket&&s.socket.send(JSON.stringify(["clipboard",e]))},s.m.getClipboard=function(){return s.lastClipboardContent},s.m.mousemove=function(e){if(s.socket&&3==s.State){var t=a(e);if(!(t.x<0||t.y<0||t.x>s.ScreenWidth||t.y>s.ScreenHeight))return s.mouseNagleData=["mouse",t.x,t.y,0,!1],null==s.mouseNagleTimer&&(s.mouseNagleTimer=setTimeout(function(){s.socket.send(JSON.stringify(s.mouseNagleData)),s.mouseNagleTimer=null},50)),e.preventDefault(),!1}},s.m.mouseup=function(e){if(s.socket&&3==s.State){var t=a(e);if(!(t.x<0||t.y<0||t.x>s.ScreenWidth||t.y>s.ScreenHeight))return null!=s.mouseNagleTimer&&(clearTimeout(s.mouseNagleTimer),s.mouseNagleTimer=null),s.socket.send(JSON.stringify(["mouse",t.x,t.y,n(e.button),!1])),e.preventDefault(),!1}},s.m.mousedown=function(e){if(s.socket&&3==s.State){var t=a(e);if(!(t.x<0||t.y<0||t.x>s.ScreenWidth||t.y>s.ScreenHeight))return null!=s.mouseNagleTimer&&(clearTimeout(s.mouseNagleTimer),s.mouseNagleTimer=null),s.socket.send(JSON.stringify(["mouse",t.x,t.y,n(e.button),!0])),e.preventDefault(),!1}},s.m.handleKeyUp=function(e){if(s.socket&&3==s.State)return s.socket.send(JSON.stringify(["scancode",Mstsc.scancode(e),!1])),e.preventDefault(),!1},s.m.handleKeyDown=function(e){if(s.socket&&3==s.State)return s.socket.send(JSON.stringify(["scancode",Mstsc.scancode(e),!0])),e.preventDefault(),!1},s.m.mousewheel=function(e){if(s.socket&&3==s.State){var t=a(e);if(!(t.x<0||t.y<0||t.x>s.ScreenWidth||t.y>s.ScreenHeight)){null!=s.mouseNagleTimer&&(clearTimeout(s.mouseNagleTimer),s.mouseNagleTimer=null);var n=0;return e.detail?n=120*e.detail:e.wheelDelta&&(n=3*e.wheelDelta),0!=n&&s.socket.send(JSON.stringify(["wheel",t.x,t.y,n,!1,!1])),e.preventDefault(),!1}}},s.m.SendStringUnicode=function(e){s.socket&&3==s.State&&s.socket.send(JSON.stringify(["utype",e]))},s.m.SendKeyMsgKC=function(e,t,n){if(3==s.State)if("object"==typeof e)for(var a in e)s.m.SendKeyMsgKC(e[a][0],e[a][1],e[a][2]);else{t=o[t];null!=t&&s.socket.send(JSON.stringify(["scancode",t,0!=(1&e)]))}},s.m.mousedblclick=function(){},s.m.handleKeyPress=function(){},s.m.setRotation=function(){},s.m.sendcad=function(){s.socket.send(JSON.stringify(["scancode",29,!0])),s.socket.send(JSON.stringify(["scancode",56,!0])),s.socket.send(JSON.stringify(["scancode",57427,!0])),s.socket.send(JSON.stringify(["scancode",57427,!1])),s.socket.send(JSON.stringify(["scancode",56,!1])),s.socket.send(JSON.stringify(["scancode",29,!1]))};var o={9:15,16:42,17:29,18:56,27:1,33:57417,34:57425,35:57423,36:57415,37:57419,38:57416,39:57421,40:57424,44:57399,45:57426,46:57427,65:30,66:48,67:46,68:32,69:18,70:33,71:34,72:35,73:23,74:36,75:37,76:38,77:50,78:49,79:24,80:25,81:16,82:19,83:31,84:20,85:22,86:47,87:17,88:45,89:21,90:44,91:57435,112:59,113:60,114:61,115:62,116:63,117:64,118:65,119:66,120:67,121:68,122:87,123:88};return s}