sshconn.html 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. $('input[name="server"]').parent().addClass('error');
  116. } else{
  117. $('input[name="server"]').parent().removeClass('error');
  118. }
  119. if (!validPort) {
  120. $('input[name="port"]').parent().addClass('error');
  121. }else{
  122. $('input[name="port"]').parent().removeClass('error');
  123. }
  124. }
  125. }
  126. function redirectWithCountdown(url) {
  127. $("#loadingUI").show();
  128. $("#connectionSettings").hide();
  129. var count = 3;
  130. var interval = setInterval(function() {
  131. $("#counter").html("ETA " + count + " seconds");
  132. count--;
  133. if (count === 0) {
  134. clearInterval(interval);
  135. window.location.href = url;
  136. }
  137. }, 1000);
  138. }
  139. //Try to ask the server side to create a ssh proxy object
  140. function createSSHProxy(remoteAddr, remotePort, username){
  141. //Request to create a ssh session instance
  142. $.ajax({
  143. url: "/api/tools/webssh",
  144. data: {ipaddr: remoteAddr, port: remotePort, username:username},
  145. method: "POST",
  146. success: function(sessionToken){
  147. if (sessionToken.error != undefined){
  148. alert(sessionToken.error);
  149. }else{
  150. //Session created. Redirect to ssh terminal
  151. redirectWithCountdown("/web.ssh/" + sessionToken + "/");
  152. }
  153. },
  154. error: function(){
  155. }
  156. })
  157. }
  158. function isValidServerNameOrIPAddress(str) {
  159. //loopback
  160. if (str == "localhost"){
  161. return true;
  162. }
  163. // First, check if the string is a valid IP address
  164. const ipAddressRegex = /^(\d{1,3}\.){3}\d{1,3}$/;
  165. if (ipAddressRegex.test(str)) {
  166. return true;
  167. }
  168. // If the string is not an IP address, check if it is a valid domain name or server name
  169. const serverNameRegex = /^[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z]{2,})+$/;
  170. if (serverNameRegex.test(str)) {
  171. return true;
  172. }
  173. // If the string is neither an IP address nor a server name, return false
  174. return false;
  175. }
  176. </script>
  177. </body>
  178. </html>