sshconn.html 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="zoraxy.csrf.Token" content="{{.csrfToken}}">
  5. <meta name="apple-mobile-web-app-capable" content="yes" />
  6. <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1"/>
  7. <meta charset="UTF-8">
  8. <meta name="theme-color" content="#4b75ff">
  9. <link rel="icon" type="image/png" href="./favicon.png" />
  10. <title>Web SSH | Zoraxy</title>
  11. <link rel="stylesheet" href="../script/semantic/semantic.min.css">
  12. <script src="../script/jquery-3.6.0.min.js"></script>
  13. <script src="../../script/ao_module.js"></script>
  14. <script src="../script/semantic/semantic.min.js"></script>
  15. <script src="../script/tablesort.js"></script>
  16. <link rel="stylesheet" href="../main.css">
  17. <script src="../script/utils.js"></script>
  18. <style>
  19. #loadingUI{
  20. width: 100%;
  21. height: 100%;
  22. background-color: black;
  23. position: fixed;
  24. top: 0;
  25. left: 0;
  26. display:none;
  27. }
  28. #loadingUI div{
  29. color: white;
  30. font-family: monospace;
  31. }
  32. </style>
  33. </head>
  34. <body>
  35. <div id="loadingUI">
  36. <div style="margin-top: 2em; margin-left: 2em; color: white;">
  37. <i class="ui loading spinner icon"></i> <b>Creating virtual terminal session</b><br>
  38. <div id="counter" style="color: white; margin-left: 1.9em;">Setting Up Environment</div>
  39. </div>
  40. </div>
  41. <div id="connectionSettings">
  42. <div class="ui container">
  43. <br>
  44. <div class="ui form">
  45. <div class="two fields">
  46. <div class="field">
  47. <label>Server Name or IP Address</label>
  48. <input type="text" name="server" placeholder="e.g. example.com or 192.168.1.1">
  49. </div>
  50. <div class="field">
  51. <label>Port Number</label>
  52. <input type="number" name="port" placeholder="e.g. 22 or 2022">
  53. </div>
  54. <div class="field">
  55. <label>Username</label>
  56. <input type="text" name="username" placeholder="root">
  57. </div>
  58. </div>
  59. </div>
  60. <div id="ui error message">
  61. </div>
  62. <div style="float: right;">
  63. <button class="ui basic button" onclick="connectSSH()"><i class="ui blue exchange icon"></i> Connect</button>
  64. <button class="ui basic button" onclick="window.open('', '_self', ''); window.close();"><i class="ui red remove icon"></i> Close</button>
  65. </div>
  66. </div>
  67. <br><br><br>
  68. </div>
  69. <script>
  70. /*
  71. SSHCONN
  72. This interface is designed to start a ssh connection instance
  73. and brige the virtual terminal to the web front-end
  74. Example Usage:
  75. let settingPayload = {
  76. server: "192.168.1.100",
  77. port: 2022,
  78. username: "pi"
  79. }
  80. let settings = encodeURIComponent(JSON.stringify(settingPayload));
  81. window.open("sshconn.html#" + settings)
  82. */
  83. if (window.location.hash.length > 1){
  84. try{
  85. var c = JSON.parse(decodeURIComponent((window.location.hash.substr(1))));
  86. $('input[name="server"]').val(c.server);
  87. $('input[name="port"]').val(c.port);
  88. $('input[name="username"]').val(c.username);
  89. connectSSH();
  90. }catch(ex){
  91. alert("invalid usage")
  92. }
  93. }
  94. //Start the SSH connection process
  95. function connectSSH() {
  96. const serverValue = $('input[name="server"]').val().trim();
  97. var portString = $('input[name="port"]').val().trim();
  98. if (portString.trim() == ""){
  99. portString = "22";
  100. }
  101. const portValue = parseInt(portString, 10);
  102. const username = $('input[name="username"]').val().trim();
  103. if (username == ""){
  104. username = "root";
  105. }
  106. // Validate server name or IP address
  107. const validServer = isValidServerNameOrIPAddress(serverValue);
  108. // Validate port number
  109. const validPort = (portValue >= 1 && portValue <= 65535);
  110. if (validServer && validPort) {
  111. // Call doSomething function if both inputs are valid
  112. createSSHProxy(serverValue, portValue, username);
  113. } else {
  114. // Display an error message if either input is invalid
  115. const errorLabel = $('<div>').addClass('ui red basic label');
  116. if (!validServer) {
  117. $('input[name="server"]').parent().addClass('error');
  118. } else{
  119. $('input[name="server"]').parent().removeClass('error');
  120. }
  121. if (!validPort) {
  122. $('input[name="port"]').parent().addClass('error');
  123. }else{
  124. $('input[name="port"]').parent().removeClass('error');
  125. }
  126. }
  127. }
  128. function redirectWithCountdown(url) {
  129. $("#loadingUI").show();
  130. $("#connectionSettings").hide();
  131. var count = 3;
  132. var interval = setInterval(function() {
  133. $("#counter").html("ETA " + count + " seconds");
  134. count--;
  135. if (count === 0) {
  136. clearInterval(interval);
  137. window.location.href = url;
  138. }
  139. }, 1000);
  140. }
  141. //Try to ask the server side to create a ssh proxy object
  142. function createSSHProxy(remoteAddr, remotePort, username){
  143. //Request to create a ssh session instance
  144. $.cjax({
  145. url: "/api/tools/webssh",
  146. data: {ipaddr: remoteAddr, port: remotePort, username:username},
  147. method: "POST",
  148. success: function(sessionToken){
  149. if (sessionToken.error != undefined){
  150. alert(sessionToken.error);
  151. }else{
  152. //Session created. Redirect to ssh terminal
  153. redirectWithCountdown("/web.ssh/" + sessionToken + "/");
  154. }
  155. },
  156. error: function(){
  157. }
  158. })
  159. }
  160. function isValidServerNameOrIPAddress(str) {
  161. //loopback
  162. if (str == "localhost"){
  163. return true;
  164. }
  165. // First, check if the string is a valid IP address
  166. const ipAddressRegex = /^(\d{1,3}\.){3}\d{1,3}$/;
  167. if (ipAddressRegex.test(str)) {
  168. return true;
  169. }
  170. // If the string is not an IP address, check if it is a valid domain name or server name
  171. const serverNameRegex = /^[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z]{2,})+$/;
  172. if (serverNameRegex.test(str)) {
  173. return true;
  174. }
  175. // If the string is neither an IP address nor a server name, return false
  176. return false;
  177. }
  178. </script>
  179. </body>
  180. </html>