126962b6a764c6d49082a1a41d50a2eb9e4d8a17
[arvados-workbench2.git] / public / webshell / index.html
1 <!DOCTYPE html>
2     <head>
3     <title></title>
4     <link rel="stylesheet" href="styles.css" type="text/css">
5     <style type="text/css">
6       body {
7         margin: 0px;
8       }
9       #notoken {
10         position: absolute;
11         top: 0;
12         left: 0;
13         right: 0;
14         bottom: 0;
15         text-align: center;
16         vertical-align: middle;
17         line-height: 100vh;
18         z-index: 100;
19         font-family: sans;
20       }
21     </style>
22     <script type="text/javascript"><!--
23       (function() {
24         // We would like to hide overflowing lines as this can lead to
25         // visually jarring results if the browser substitutes oversized
26         // Unicode characters from different fonts. Unfortunately, a bug
27         // in Firefox prevents it from allowing multi-line text
28         // selections whenever we change the "overflow" style. So, only
29         // do so for non-Netscape browsers.
30         if (typeof navigator.appName == 'undefined' ||
31             navigator.appName != 'Netscape') {
32           document.write('<style type="text/css">' +
33                          '#vt100 #console div, #vt100 #alt_console div {' +
34                          '  overflow: hidden;' +
35                          '}' +
36                          '</style>');
37         }
38       })();
39       var sh;
40       var urlParams = new URLSearchParams(window.location.search);
41       var token = urlParams.get('token');
42       var user = urlParams.get('login');
43       var host = urlParams.get('host');
44       var timeout = urlParams.get('timeout');
45       urlParams = null;
46
47       var idleTimeoutMs = timeout * 1000;
48
49       function updateIdleTimer() {
50         var currentTime = Date.now();
51         var lastTime = localStorage.getItem('lastActiveTimestamp');
52         if (currentTime - lastTime > 1000) {
53           localStorage.setItem('lastActiveTimestamp', currentTime);
54         }
55       }
56
57       function checkIdleTimer() {
58         var currentTime = Date.now();
59         var lastTime = localStorage.getItem('lastActiveTimestamp');
60         if (currentTime - lastTime > idleTimeoutMs) {
61           //logout
62           sh.sendKeys('03'); // Ctrl + c
63           sh.sendKeys('04'); // Ctrl + d
64         } else {
65           setTimeout(checkIdleTimer, 1000);
66         }
67       }
68
69       function login() {
70         sh = new ShellInABox(host);
71
72         var findText = function(txt) {
73           var a = document.querySelectorAll("span.ansi0");
74           for (var i = 0; i < a.length; i++) {
75             if (a[i].textContent.indexOf(txt) > -1) {
76               return true;
77             }
78           }
79           return false;
80         }
81
82         var trySendToken = function() {
83           // change this text when PAM is reconfigured to present a
84           // password prompt that we can wait for.
85           if (findText("assword:")) {
86              sh.keysPressed(token + "\n");
87              sh.vt100('(sent authentication token)\n');
88              token = null;
89              updateIdleTimer();
90              document.body.onmousemove = updateIdleTimer;
91              document.body.onkeydown = updateIdleTimer;
92              setTimeout(checkIdleTimer, 1000);
93           } else {
94             setTimeout(trySendToken, 200);
95           }
96         };
97
98         var trySendLogin = function() {
99           if (findText("login:")) {
100             sh.keysPressed(user + "\n");
101             // Make this wait shorter when PAM is reconfigured to
102             // present a password prompt that we can wait for.
103             setTimeout(trySendToken, 200);
104           } else {
105             setTimeout(trySendLogin, 200);
106           }
107         };
108
109         trySendLogin();
110       }
111
112       function init() {
113         if (token) {
114           history.replaceState(null, "", `/webshell/?host=${encodeURIComponent(host)}&timeout=${timeout}&login=${encodeURIComponent(user)}`);
115         } else if (localStorage.getItem('apiToken')) {
116           token = localStorage.getItem('apiToken');
117         } else {
118           document.getElementById("notoken").style.display = "block";
119           return;
120         }
121         login();
122       }
123     // -->
124 </script>
125     <script type="text/javascript" src="shell_in_a_box.js"></script>
126   </head>
127   <!-- Load ShellInABox from a timer as Konqueror sometimes fails to
128        correctly deal with the enclosing frameset (if any), if we do not
129        do this
130    -->
131 <body onload="setTimeout(init, 1000)"
132     scroll="no"><noscript>JavaScript must be enabled for ShellInABox</noscript>
133     <div id="notoken" style="display: none;">
134       Error: No token found. Please return to <a href="/virtual-machines-user">Virtual Machines</a> and try again.
135     </div>
136 </body>
137 </html>