sshconn.html 8.1 KB

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