ssh.handlebars 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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>SSH</title>
  22. </head>
  23. <body style="overflow:hidden;background-color:black" onload="start()">
  24. <div id=p11 class="noselect" style="overflow:hidden">
  25. <div id=deskarea0 style="position:relative">
  26. <div id=deskarea1 class="areaHead">
  27. <div class="toright2">
  28. </div>
  29. <div>
  30. <input id="ConnectButton" style="display:none" type=button value="Connect" onclick="connectButton()">
  31. <input id="DisconnectButton" type=button value="Disconnect" onclick="connectButton()">
  32. <span><b id="computerName"></b></span> - <span id="termstatus"></span>
  33. </div>
  34. </div>
  35. <div id=deskarea2 style="">
  36. <div class="areaProgress"><div id="progressbar" style=""></div></div>
  37. </div>
  38. <div id=deskarea3x style="max-height:calc(100vh - 54px);height:calc(100vh - 54px);">
  39. <div id="bigok" style="display:none;left:calc((100vh / 2))"><b>&checkmark;</b></div>
  40. <div id="bigfail" style="display:none;left:calc((100vh / 2))"><b>&#10007;</b></div>
  41. <div id="metadatadiv" style="padding:20px;color:lightgrey;text-align:left;display:none"></div>
  42. <div id=terminal style="max-height:calc(100vh - 54px);height:calc(100vh - 54px);text-align:left"></div>
  43. <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>
  44. </div>
  45. <div id=deskarea4 class="areaHead">
  46. <div class="toright2"></div>
  47. <div style="height:21px;max-height:21px"></div>
  48. </div>
  49. </div>
  50. <div id=dialog class="noselect" style="display:none">
  51. <div id=dialogHeader>
  52. <div tabindex=0 id=id_dialogclose onclick=setDialogMode() onkeypress="if (event.key == 'Enter') setDialogMode()">&#x2716;</div>
  53. <div id=id_dialogtitle></div>
  54. </div>
  55. <div id=dialogBody>
  56. <div id=dialog1>
  57. <div id=id_dialogMessage style=""></div>
  58. </div>
  59. <div id=dialog2 style="">
  60. <div id=id_dialogOptions></div>
  61. </div>
  62. </div>
  63. <div id="idx_dlgButtonBar">
  64. <input id="idx_dlgCancelButton" type="button" value="Cancel" style="" onclick="dialogclose(0)">
  65. <input id="idx_dlgOkButton" type="button" value="OK" style="" onclick="dialogclose(1)">
  66. <div><input id="idx_dlgDeleteButton" type="button" value="Delete" style="display:none" onclick="dialogclose(2)"></div>
  67. </div>
  68. </div>
  69. </div>
  70. <script>
  71. var random = '{{{randomlength}}}' // Random length string for BREACH mitigation
  72. var term = null;
  73. var termfit = null;
  74. var resizeTimer = null;
  75. var urlargs = parseUriArgs();
  76. if (urlargs.key && (isAlphaNumeric(urlargs.key) == false)) { delete urlargs.key; }
  77. var cookie = '{{{cookie}}}';
  78. var domainurl = '{{{domainurl}}}';
  79. var features = parseInt('{{{features}}}');
  80. var name = decodeURIComponent('{{{name}}}');
  81. if (name != '') { document.title = name + ' - ' + document.title; }
  82. var StatusStrs = ["Disconnected", "Connecting...", "Setup...", "Connected"];
  83. var state = 0;
  84. var socket = null;
  85. var user = '';
  86. var pass = '';
  87. function start() {
  88. // Set the computer name
  89. QH('computerName', name);
  90. // When the user resizes the window, re-fit
  91. window.onresize = function () { if (termfit != null) { termfit.fit(); } }
  92. // Update the terminal status and buttons
  93. updateState();
  94. resetTerminal();
  95. connectButton();
  96. }
  97. function resetTerminal() {
  98. // Setup the terminal with auto-fit
  99. if (term != null) { term.dispose(); }
  100. if (urlargs.fixsize != 1) { termfit = new FitAddon.FitAddon(); }
  101. term = new Terminal();
  102. if (termfit) { term.loadAddon(termfit); }
  103. term.open(Q('terminal'));
  104. term.onData(function (data) { if (state == 3) { socket.send('~' + data); } });
  105. if (termfit) { termfit.fit(); }
  106. term.onResize(function (size) {
  107. // Despam resize
  108. if (resizeTimer) clearTimeout(resizeTimer);
  109. resizeTimer = setTimeout(sendResize, 200);
  110. });
  111. //term.setOption('convertEol', true); // Consider \n to be \r\n, this should be taken care of by "termios"
  112. }
  113. // Send the new terminal size to the agent
  114. function sendResize() {
  115. resizeTimer = null;
  116. if (socket != null) { socket.send(JSON.stringify({ action: 'resize', cols: term.cols, rows: term.rows, width: Q('terminal').offsetWidth, height: Q('terminal').offsetHeight })); }
  117. }
  118. function connectButton() {
  119. if (state == 0) {
  120. connectEx2({ action: 'connect', cols: term.cols, rows: term.rows, width: Q('terminal').offsetWidth, height: Q('terminal').offsetHeight, useexisting: true });
  121. } else {
  122. disconnect();
  123. }
  124. }
  125. function sshAuthUpdate(e) {
  126. QV('d2userauth', Q('dp2authmethod').value != 3);
  127. QV('d2passauth', Q('dp2authmethod').value == 1);
  128. QV('d2keyauth', Q('dp2authmethod').value == 2);
  129. QV('d2keyauth2', Q('dp2authmethod').value == 3);
  130. if (Q('dp2authmethod').value == 1) {
  131. QE('idx_dlgOkButton', (Q('dp2user').value.length > 0) && (Q('dp2pass').value.length > 0));
  132. } else if (Q('dp2authmethod').value == 3) {
  133. QE('idx_dlgOkButton', Q('dp2keypass2').value.length > 0);
  134. } else {
  135. QE('idx_dlgOkButton', false);
  136. if ((features & 1) == 0) { QE('dp2keep2', Q('dp2keep1').checked); }
  137. var ok = (Q('dp2user').value.length > 0) && (Q('dp2key').files != null) && (Q('dp2key').files.length == 1) && (Q('dp2key').files[0].size < 8000);
  138. if (ok == true) {
  139. var reader = new FileReader();
  140. reader.onload = function (e) {
  141. var validkey =
  142. ((e.target.result.indexOf('-----BEGIN OPENSSH PRIVATE KEY-----') >= 0) && (e.target.result.indexOf('-----END OPENSSH PRIVATE KEY-----') >= 0)) ||
  143. ((e.target.result.indexOf('-----BEGIN RSA PRIVATE KEY-----') >= 0) && (e.target.result.indexOf('-----END RSA PRIVATE KEY-----') >= 0));
  144. QE('idx_dlgOkButton', validkey);
  145. QS('d2badkey')['color'] = validkey ? '#000' : '#F00';
  146. }
  147. reader.readAsText(Q('dp2key').files[0]);
  148. }
  149. }
  150. // When the enter key is pressed, move to the next field
  151. if (e && (e.keyCode == 13) && (e.target) && (Q('dp2authmethod').value == 1)) {
  152. if (e.target.id == 'dp2user') { Q('dp2pass').focus(); }
  153. if (e.target.id == 'dp2pass') { dialogclose(1); }
  154. }
  155. }
  156. function connectEx() {
  157. var cmd = { action: 'connect', cols: term.cols, rows: term.rows, width: Q('terminal').offsetWidth, height: Q('terminal').offsetHeight, username: Q('dp2user').value, keep: 0 };
  158. if (Q('dp2authmethod').value == 1) {
  159. cmd.password = Q('dp2pass').value;
  160. if ((features & 1) == 0) { cmd.keep = Q('dp2keep').checked ? 1 : 0; }
  161. connectEx2(cmd);
  162. } else if (Q('dp2authmethod').value == 3) {
  163. cmd.action = 'connectKeyPass';
  164. cmd.keypass = Q('dp2keypass2').value;
  165. connectEx2(cmd);
  166. } else {
  167. if ((features & 1) == 0) { cmd.keep = (Q('dp2keep1').checked ? 1 : 0); if (cmd.keep == 1) { cmd.keep += (Q('dp2keep2').checked ? 1 : 0); } } // Keep: 1 = user & key, 2 = User, key and password
  168. cmd.keypass = Q('dp2keypass').value;
  169. var reader = new FileReader();
  170. reader.onload = function (e) { cmd.key = e.target.result; connectEx2(cmd); }
  171. reader.readAsText(Q('dp2key').files[0]);
  172. }
  173. }
  174. function sshTunnelAuthDialog(j, func) {
  175. var x = '';
  176. if (j.askkeypass) {
  177. x += addHtmlValue("Authentication", '<select id=dp2authmethod style=width:230px onchange=sshAuthUpdate(event)><option value=3 selected>' + "Stored Key" + '</option><option value=1>' + "Username & Password" + '</option><option value=2>' + "Username and Key" + '</option></select>');
  178. } else {
  179. x += addHtmlValue("Authentication", '<select id=dp2authmethod style=width:230px onchange=sshAuthUpdate(event)><option value=1 selected>' + "Username & Password" + '</option><option value=2>' + "Username and Key" + '</option></select>');
  180. }
  181. x += '<div id=d2userauth style=display:none>';
  182. x += addHtmlValue("Username", '<input id=dp2user style=width:230px maxlength=64 autocomplete=off onkeyup=sshAuthUpdate(event) />');
  183. x += '</div>';
  184. x += '<div id=d2passauth style=display:none>';
  185. x += addHtmlValue("Password", '<input type=password id=dp2pass style=width:230px maxlength=64 autocomplete=off onkeyup=sshAuthUpdate(event) />');
  186. if ((features & 1) == 0) { x += addHtmlValue('', '<label><input id=dp2keep type=checkbox>' + "Remember credentials" + '</label>'); }
  187. x += '</div><div id=d2keyauth style=display:none>';
  188. x += addHtmlValue("Key File", '<input type=file id=dp2key style=width:230px maxlength=64 autocomplete=off onchange=sshAuthUpdate(event) />' + '<div id=d2badkey style=font-size:x-small>' + "Key file must be in OpenSSH format." + '</div>');
  189. x += addHtmlValue("Key Password", '<input type=password id=dp2keypass style=width:230px maxlength=64 autocomplete=off onkeyup=sshAuthUpdate(event) />');
  190. if ((features & 1) == 0) {
  191. x += addHtmlValue('', '<label><input id=dp2keep1 type=checkbox onchange=sshAuthUpdate(event)>' + "Remember user & key" + '</label>');
  192. x += addHtmlValue('', '<label><input id=dp2keep2 type=checkbox>' + "Remember password" + '</label>');
  193. }
  194. x += '</div>';
  195. if (j.askkeypass) {
  196. x += '<div id=d2keyauth2 style=display:none>';
  197. x += addHtmlValue("Password", '<input type=password id=dp2keypass2 style=width:230px maxlength=64 autocomplete=off onkeyup=sshAuthUpdate(event) />');
  198. x += '</div>';
  199. }
  200. setDialogMode(2, "Authentication", 11, func, x, 'ssh');
  201. Q('dp2user').focus();
  202. sshAuthUpdate();
  203. setTimeout(sshAuthUpdate, 50);
  204. }
  205. function connectEx2(cmd) {
  206. state = 1;
  207. var url = window.location.protocol.replace('http', 'ws') + '//' + window.location.host + domainurl + 'sshrelay.ashx?auth=' + cookie + (urlargs.key ? ('&key=' + urlargs.key) : '');
  208. socket = new WebSocket(url);
  209. socket.onopen = function (e) {
  210. state = 2;
  211. updateState();
  212. term.reset();
  213. // Send username and terminal width and height
  214. socket.send(JSON.stringify(cmd));
  215. pass = '';
  216. }
  217. socket.onmessage = function (data) {
  218. if (typeof data.data != 'string') return;
  219. if (data.data[0] == '{') {
  220. var json = null;
  221. try { json = JSON.parse(data.data); } catch (ex) { }
  222. if ((json == null) || (typeof json != 'object')) return;
  223. if ((json.ctrlChannel == 102938) && (json.type == 'ping')) { socket.send('{"ctrlChannel":"102938","type":"pong"}'); return; }
  224. switch (json.action) {
  225. case 'connected': { state = 3; updateState(); term.focus(); break; }
  226. case 'sshauth': { sshTunnelAuthDialog(json, connectEx); break; }
  227. case 'autherror': { setDialogMode(2, "Authentication", 1, null, "Unable to authenticate."); break; }
  228. case 'sessionerror': { setDialogMode(2, "Session", 1, null, "Session expired."); break; }
  229. case 'sessiontimeout': { setDialogMode(2, "Session", 1, null, "Session timeout."); break; }
  230. }
  231. } else if (data.data[0] == '~') {
  232. if (term.writeUtf8) { term.writeUtf8(data.data.substring(1)); } else { term.write(data.data.substring(1)); }
  233. }
  234. }
  235. socket.onclose = function (e) { disconnect(); }
  236. socket.onerror = function (e) { disconnect(); }
  237. updateState();
  238. }
  239. function disconnect() {
  240. if (socket != null) { socket.close(); socket = null; }
  241. state = 0;
  242. updateState();
  243. resetTerminal();
  244. }
  245. function updateState() {
  246. QV('ConnectButton', state == 0);
  247. QV('DisconnectButton', state != 0);
  248. QH('termstatus', StatusStrs[state]);
  249. }
  250. //
  251. // POPUP DIALOG
  252. //
  253. // null = Hidden, 1 = Generic Message
  254. var xxdialogMode;
  255. var xxdialogFunc;
  256. var xxdialogButtons;
  257. var xxdialogTag;
  258. var xxcurrentView = -1;
  259. // Display a dialog box
  260. // 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)
  261. function setDialogMode(x, y, b, f, c, tag) {
  262. xxdialogMode = x;
  263. xxdialogFunc = f;
  264. xxdialogButtons = b;
  265. xxdialogTag = tag;
  266. QE('idx_dlgOkButton', true);
  267. QV('idx_dlgOkButton', b & 1);
  268. QV('idx_dlgCancelButton', b & 2);
  269. QV('id_dialogclose', (b & 2) || (b & 8));
  270. QV('idx_dlgDeleteButton', b & 4);
  271. QV('idx_dlgButtonBar', b & 7);
  272. if (y) QH('id_dialogtitle', y);
  273. for (var i = 1; i < 3; i++) { QV('dialog' + i, i == x); } // Edit this line when more dialogs are added
  274. QV('dialog', x);
  275. if (c) { if (x == 2) { QH('id_dialogOptions', c); } else { QH('id_dialogMessage', c); } }
  276. }
  277. // Called when the dialog box must be closed
  278. function dialogclose(x) {
  279. var f = xxdialogFunc, b = xxdialogButtons, t = xxdialogTag;
  280. setDialogMode();
  281. if (((b & 8) || x) && f) f(x, t);
  282. }
  283. function messagebox(t, m) { setSessionActivity(); QH('id_dialogMessage', m); setDialogMode(1, t, 1); }
  284. function statusbox(t, m) { setSessionActivity(); QH('id_dialogMessage', m); setDialogMode(1, t); }
  285. function haltEvent(e) { if (e.preventDefault) e.preventDefault(); if (e.stopPropagation) e.stopPropagation(); return false; }
  286. function pad2(num) { var s = '00' + num; return s.substr(s.length - 2); }
  287. 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; }); };
  288. function isAlphaNumeric(str) { return (str.match(/^[A-Za-z0-9]+$/) != null); };
  289. 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)) };
  290. function addHtmlValue(t, v) { return '<table><td style=width:120px>' + t + '<td><b>' + v + '</b></table>'; }
  291. // Parse URL arguments, only keep safe values
  292. function parseUriArgs() {
  293. var href = window.document.location.href;
  294. if (href.endsWith('#')) { href = href.substring(0, href.length - 1); }
  295. var name, r = {}, parsedUri = href.split(/[\?&|\=]/);
  296. parsedUri.splice(0, 1);
  297. for (x in parsedUri) {
  298. switch (x % 2) {
  299. case 0: { name = decodeURIComponent(parsedUri[x]); break; }
  300. case 1: {
  301. r[name] = decodeURIComponent(parsedUri[x]);
  302. if (!isSafeString(r[name])) { delete r[name]; } else { var x = parseInt(r[name]); if (x == r[name]) { r[name] = x; } }
  303. break;
  304. } default: { break; }
  305. }
  306. }
  307. return r;
  308. }
  309. start();
  310. </script>
  311. </body>
  312. </html>