xterm.handlebars 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <!DOCTYPE html>
  2. <html dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
  3. <head lang="en">
  4. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  5. <meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
  6. <meta name="viewport" content="user-scalable=1.0,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0" />
  7. <meta name="apple-mobile-web-app-capable" content="yes" />
  8. <meta name="format-detection" content="telephone=no" />
  9. <meta name="robots" content="noindex,nofollow">
  10. <link type="text/css" href="styles/style.css" media="screen" rel="stylesheet" title="CSS" />
  11. <link type="text/css" href="styles/xterm.css" media="screen" rel="stylesheet" title="CSS" />
  12. {{{customCSSTags}}}
  13. <link rel="apple-touch-icon" href="/favicon-303x303.png" />
  14. <script type="text/javascript" src="scripts/common-0.0.1{{min}}.js"></script>
  15. <script type="text/javascript" src="scripts/meshcentral{{min}}.js"></script>
  16. <script type="text/javascript" src="scripts/agent-redir-ws-0.1.1{{min}}.js"></script>
  17. <script type="text/javascript" src="scripts/agent-redir-rtc-0.1.0{{min}}.js"></script>
  18. <script type="text/javascript" src="scripts/xterm-min.js"></script>
  19. <script type="text/javascript" src="scripts/xterm-addon-fit-min.js"></script>
  20. {{{customJSTags}}}
  21. <title>{{{name}}}</title>
  22. </head>
  23. <body style="overflow:hidden;background-color:black" oncontextmenu="handleContextMenu(event)">
  24. <!-- right click menu -->
  25. <div id="termShellContextMenu" class="contextMenu noselect" style="display:none;min-width:0px">
  26. <div id="cxtermnorm" class="cmtext" onclick="cmtermaction(1,event)"><b>Admin Shell</b></div>
  27. <div id="cxtermps" class="cmtext" onclick="cmtermaction(6,event)">Admin PowerShell</div>
  28. <div id="cxtermunorm" class="cmtext" onclick="cmtermaction(8,event)">User Shell</div>
  29. <div id="cxtermups" class="cmtext" onclick="cmtermaction(9,event)">User PowerShell</div>
  30. </div>
  31. <div id="termShellContextMenuLinux" class="contextMenu noselect" style="display:none;min-width:0px">
  32. <div id="cxtermnorm" class="cmtext" onclick="cmtermaction(1,event)"><b>Root Shell</b></div>
  33. <div id="cxtermps" class="cmtext" onclick="cmtermaction(8,event)">User Shell</div>
  34. <div id="cxtermps" class="cmtext" onclick="cmtermaction(100,event)">Login Shell</div>
  35. </div>
  36. <div id=p11 class="noselect" style="overflow:hidden">
  37. <div id=deskarea0 style="position:relative">
  38. <div id=deskarea1 class="areaHead">
  39. <div class="toright2">
  40. </div>
  41. <div>
  42. <input id="ConnectButton" style="display:none" type=button value="Connect" onclick="connectButton()">
  43. <input id="DisconnectButton" type=button value="Disconnect" onclick="connectButton()">
  44. <span><b>{{{name}}}</b></span> - <span id="termstatus"></span>
  45. </div>
  46. </div>
  47. <div id=deskarea2 style="">
  48. <div class="areaProgress"><div id="progressbar" style=""></div></div>
  49. </div>
  50. <div id=deskarea3x style="max-height:calc(100vh - 54px);height:calc(100vh - 54px);">
  51. <div id="bigok" style="display:none;left:calc((100vh / 2))"><b>&checkmark;</b></div>
  52. <div id="bigfail" style="display:none;left:calc((100vh / 2))"><b>&#10007;</b></div>
  53. <div id="metadatadiv" style="padding:20px;color:lightgrey;text-align:left;display:none"></div>
  54. <div id=terminal style="max-height:calc(100vh - 54px);height:calc(100vh - 54px);"></div>
  55. <div id=TermConsoleMsg style="display:none;cursor:pointer;z-index:10;position:absolute;left:30px;top:17px;color:yellow;background-color:rgba(0,0,0,0.6);padding:10px;border-radius:5px" onclick=clearConsoleMsg()></div>
  56. </div>
  57. <div id=deskarea4 class="areaHead">
  58. <div class="toright2">
  59. </div>
  60. <div style="height:21px;max-height:21px">
  61. </div>
  62. </div>
  63. </div>
  64. <div id=dialog class="noselect" style="display:none">
  65. <div id=dialogHeader>
  66. <div tabindex=0 id=id_dialogclose onclick=setDialogMode() onkeypress="if (event.key == 'Enter') setDialogMode()">&#x2716;</div>
  67. <div id=id_dialogtitle></div>
  68. </div>
  69. <div id=dialogBody>
  70. <div id=dialog1>
  71. <div id=id_dialogMessage style=""></div>
  72. </div>
  73. <div id=dialog2 style="">
  74. <div id=id_dialogOptions></div>
  75. </div>
  76. </div>
  77. <div id="idx_dlgButtonBar">
  78. <input id="idx_dlgCancelButton" type="button" value="Cancel" style="" onclick="dialogclose(0)">
  79. <input id="idx_dlgOkButton" type="button" value="OK" style="" onclick="dialogclose(1)">
  80. <div><input id="idx_dlgDeleteButton" type="button" value="Delete" style="display:none" onclick="dialogclose(2)"></div>
  81. </div>
  82. </div>
  83. </div>
  84. <script>
  85. var random = '{{{randomlength}}}' // Random length string for BREACH mitigation
  86. var term = null;
  87. var termfit = null;
  88. var tunnel = null;
  89. var domain = '{{{domain}}}';
  90. var domainUrl = '{{{domainurl}}}';
  91. var authCookie = '{{{authCookie}}}';
  92. var authRelayCookie = '{{{authRelayCookie}}}';
  93. var serverPublicNamePort = '{{{serverDnsName}}}:{{{serverPublicPort}}}';
  94. var StatusStrs = ["Disconnected", "Connecting...", "Setup...", "Connected", "Intel&reg; AMT Connected"];
  95. var resizeTimer = null;
  96. function start() {
  97. // Parse any URL arguments
  98. args = parseUriArgs();
  99. if (args.key && (isAlphaNumeric(args.key) == false)) { delete args.key; }
  100. // Connect to the mesh server
  101. meshserver = MeshServerCreateControl(domainUrl, authCookie);
  102. meshserver.onStateChanged = onStateChanged;
  103. meshserver.onMessage = onMessage;
  104. meshserver.trace = (args.trace == 1);
  105. meshserver.Start();
  106. // When the user resizes the window, re-fit
  107. window.onresize = function () { if (termfit != null) { termfit.fit(); } }
  108. // Hide the context menu
  109. document.onclick = function (e) { hideContextMenu(); }
  110. // Update the terminal status and buttons
  111. QH('termstatus', StatusStrs[0]);
  112. updateButtons();
  113. }
  114. // Show the correct button state
  115. function updateButtons() {
  116. var tunnelState = ((tunnel != null) && (tunnel.state != 0) && (meshserver.State == 2));
  117. QE('ConnectButton', meshserver.State == 2);
  118. QV('ConnectButton', !tunnelState);
  119. QV('DisconnectButton', tunnelState);
  120. }
  121. // MeshServer - Change State
  122. function onStateChanged(server, state, prevState, errorCode) {
  123. QE('ConnectButton', (state == 2));
  124. if ((state == 2) && (args.auto == 1)) { delete args.auto; connectButton(); }
  125. updateButtons();
  126. }
  127. // MeshServer - Handle messages
  128. function onMessage(server, message) { }
  129. // Handles a tunnel to a remote shell
  130. function CreateRemoteTunnel(onTunnelUpdate, options) {
  131. var obj = { protocol: 1 };
  132. if ((options != null) && (typeof options.protocol == 'number')) { obj.protocol = options.protocol; }
  133. obj.onTunnelUpdate = onTunnelUpdate;
  134. obj.xxStateChange = function (state) { }
  135. obj.ProcessBinaryData = function (data) { obj.onTunnelUpdate(data); }
  136. obj.ProcessData = function (data) { obj.onTunnelUpdate(data); }
  137. return obj;
  138. }
  139. // Send the new terminal size to the agent
  140. function sendResize() {
  141. resizeTimer = null;
  142. if ((term != null) && (tunnel != null)) { tunnel.sendCtrlMsg(JSON.stringify({ ctrlChannel: '102938', type: 'termsize', cols: term.cols, rows: term.rows })); }
  143. }
  144. // Called when the connect/disconnect button is pressed
  145. function connectButton(options) {
  146. if (!tunnel) {
  147. // Setup the terminal with auto-fit
  148. if (term != null) { term.dispose(); }
  149. if (args.fixsize != 1) { termfit = new FitAddon.FitAddon(); }
  150. term = new Terminal();
  151. if (termfit) { term.loadAddon(termfit); }
  152. term.open(Q('terminal'));
  153. term.onData(function (data) { if (tunnel != null) { tunnel.sendText(data); } })
  154. if (termfit) { termfit.fit(); }
  155. term.onResize(function (size) {
  156. // Despam resize
  157. if (resizeTimer) clearTimeout(resizeTimer);
  158. resizeTimer = setTimeout(sendResize, 200);
  159. });
  160. //term.setOption('convertEol', true); // Consider \n to be \r\n, this should be taken care of by "termios"
  161. // Setup a terminal tunnel to the agent
  162. tunnel = CreateAgentRedirect(meshserver, CreateRemoteTunnel(tunnelUpdate, options), serverPublicNamePort, authCookie, authRelayCookie, domainUrl);
  163. tunnel.options = { cols: term.cols, rows: term.rows };
  164. if (options && options.requireLogin) { tunnel.options.requireLogin = true; }
  165. tunnel.Start(args.nodeid);
  166. tunnel.onStateChanged = onTunnelStateChange;
  167. tunnel.onConsoleMessageChange = function (server, msg) { setConsoleMsg(msg); };
  168. } else {
  169. tunnel.Stop();
  170. }
  171. }
  172. function tunnelUpdate(data) {
  173. if (term.writeUtf8) {
  174. if (typeof data == 'string') { term.writeUtf8(data); } else { term.writeUtf8(new Uint8Array(data)); }
  175. } else {
  176. if (typeof data == 'string') { term.write(data); } else { term.write(new Uint8Array(data)); }
  177. }
  178. }
  179. // Called when the terminal state changes
  180. function onTunnelStateChange(xterminal, state) {
  181. var xstate = state;
  182. if ((xstate == 3) && (xterminal.contype == 2)) { xstate++; }
  183. var str = StatusStrs[xstate];
  184. if (tunnel.webRtcActive == true) { str += ", WebRTC"; }
  185. QH('termstatus', str);
  186. switch (state) {
  187. case 0:
  188. // Disconnected, clear the terminal
  189. term.dispose();
  190. term = termfit = tunnel = null;
  191. break;
  192. case 3:
  193. // Connected
  194. term.focus();
  195. break;
  196. default:
  197. // Other
  198. break;
  199. }
  200. updateButtons();
  201. }
  202. // Console messages
  203. var termConsoleMsgTimer = null;
  204. function clearConsoleMsg() { QV('TermConsoleMsg', false); if (termConsoleMsgTimer) { clearTimeout(termConsoleMsgTimer); termConsoleMsgTimer = null; } }
  205. function setConsoleMsg(msg) { QH('TermConsoleMsg', EscapeHtml(msg).split('\n').join('<br />')); QV('TermConsoleMsg', true); termConsoleMsgTimer = setTimeout(clearConsoleMsg, 8000); }
  206. //
  207. // CONTEXT MENU
  208. //
  209. var contextelement = null;
  210. function handleContextMenu(event) {
  211. hideContextMenu();
  212. var scrollLeft = (window.pageXOffset !== null) ? window.pageXOffset : (document.documentElement || document.body.parentNode || document.body).scrollLeft;
  213. var scrollTop = (window.pageYOffset !== null) ? window.pageYOffset : (document.documentElement || document.body.parentNode || document.body).scrollTop;
  214. var elem = document.elementFromPoint(event.pageX - scrollLeft, event.pageY - scrollTop);
  215. if (elem && elem != null && elem.id == 'ConnectButton') {
  216. contextelement = elem;
  217. var contextmenudiv;
  218. if (args.os == 'win') {
  219. contextmenudiv = document.getElementById('termShellContextMenu');
  220. } else {
  221. contextmenudiv = document.getElementById('termShellContextMenuLinux');
  222. }
  223. showContextMenuDiv(contextmenudiv, event.pageX, event.pageY);
  224. return haltEvent(event);
  225. }
  226. }
  227. function showContextMenuDiv(element, x, y) {
  228. var clientRect = document.documentElement.getBoundingClientRect();
  229. var docHeight = clientRect.height;
  230. var docWidth = clientRect.width;
  231. element.style.left = element.style.right = element.style.top = element.style.bottom = null;
  232. if (x > (docWidth / 2)) { element.style.right = (docWidth - event.pageX) + 'px'; } else { element.style.left = event.pageX + 'px'; }
  233. if (y > (docHeight / 2)) { element.style.bottom = (docHeight - event.pageY) + 'px'; } else { element.style.top = event.pageY + 'px'; }
  234. element.style.display = 'block';
  235. }
  236. function cmtermaction(action) {
  237. if (action < 100) {
  238. connectButton({ protocol: action })
  239. } else if (action == 100) {
  240. connectButton({ protocol: 1, requireLogin: true })
  241. }
  242. }
  243. function hideContextMenu() {
  244. QV('contextMenu', false);
  245. QV('termShellContextMenu', false);
  246. QV('termShellContextMenuLinux', false);
  247. contextelement = null;
  248. }
  249. //
  250. // POPUP DIALOG
  251. //
  252. // null = Hidden, 1 = Generic Message
  253. var xxdialogMode;
  254. var xxdialogFunc;
  255. var xxdialogButtons;
  256. var xxdialogTag;
  257. var xxcurrentView = -1;
  258. // Display a dialog box
  259. // Parameters: Dialog Mode (0 = none), Dialog Title, Buttons (1 = OK, 2 = Cancel, 3 = OK & Cancel), Call back function(0 = Cancel, 1 = OK), Dialog Content (Mode 2 only)
  260. function setDialogMode(x, y, b, f, c, tag) {
  261. xxdialogMode = x;
  262. xxdialogFunc = f;
  263. xxdialogButtons = b;
  264. xxdialogTag = tag;
  265. QE('idx_dlgOkButton', true);
  266. QV('idx_dlgOkButton', b & 1);
  267. QV('idx_dlgCancelButton', b & 2);
  268. QV('id_dialogclose', (b & 2) || (b & 8));
  269. QV('idx_dlgDeleteButton', b & 4);
  270. QV('idx_dlgButtonBar', b & 7);
  271. if (y) QH('id_dialogtitle', y);
  272. for (var i = 1; i < 3; i++) { QV('dialog' + i, i == x); } // Edit this line when more dialogs are added
  273. QV('dialog', x);
  274. if (c) { if (x == 2) { QH('id_dialogOptions', c); } else { QH('id_dialogMessage', c); } }
  275. }
  276. // Called when the dialog box must be closed
  277. function dialogclose(x) {
  278. var f = xxdialogFunc, b = xxdialogButtons, t = xxdialogTag;
  279. setDialogMode();
  280. if (((b & 8) || x) && f) f(x, t);
  281. }
  282. function messagebox(t, m) { setSessionActivity(); QH('id_dialogMessage', m); setDialogMode(1, t, 1); }
  283. function statusbox(t, m) { setSessionActivity(); QH('id_dialogMessage', m); setDialogMode(1, t); }
  284. function haltEvent(e) { if (e.preventDefault) e.preventDefault(); if (e.stopPropagation) e.stopPropagation(); return false; }
  285. function pad2(num) { var s = '00' + num; return s.substr(s.length - 2); }
  286. function format(format) { var args = Array.prototype.slice.call(arguments, 1); return format.replace(/{(\d+)}/g, function (match, number) { return typeof args[number] != 'undefined' ? args[number] : match; }); };
  287. function isAlphaNumeric(str) { return (str.match(/^[A-Za-z0-9]+$/) != null); };
  288. function isSafeString(str) { return ((typeof str == 'string') && (str.indexOf('<') == -1) && (str.indexOf('>') == -1) && (str.indexOf('&') == -1) && (str.indexOf('"') == -1) && (str.indexOf('\'') == -1) && (str.indexOf('+') == -1) && (str.indexOf('(') == -1) && (str.indexOf(')') == -1) && (str.indexOf('#') == -1) && (str.indexOf('%') == -1) && (str.indexOf(':') == -1)) };
  289. // Parse URL arguments, only keep safe values
  290. function parseUriArgs() {
  291. var href = window.document.location.href;
  292. if (href.endsWith('#')) { href = href.substring(0, href.length - 1); }
  293. var name, r = {}, parsedUri = href.split(/[\?&|\=]/);
  294. parsedUri.splice(0, 1);
  295. for (x in parsedUri) {
  296. switch (x % 2) {
  297. case 0: { name = decodeURIComponent(parsedUri[x]); break; }
  298. case 1: {
  299. r[name] = decodeURIComponent(parsedUri[x]);
  300. if (!isSafeString(r[name])) { delete r[name]; } else { var x = parseInt(r[name]); if (x == r[name]) { r[name] = x; } }
  301. break;
  302. } default: { break; }
  303. }
  304. }
  305. return r;
  306. }
  307. start();
  308. </script>
  309. </body>
  310. </html>