sshconn.html 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 style="float: right;">
  59. <button class="ui basic button" onclick="connectSSH()"><i class="ui blue exchange icon"></i> Connect</button>
  60. <button class="ui basic button" onclick="window.open('', '_self', ''); window.close();"><i class="ui red remove icon"></i> Close</button>
  61. </div>
  62. </div>
  63. <br><br><br>
  64. </div>
  65. <script>
  66. /*
  67. SSHCONN
  68. This interface is designed to start a ssh connection instance
  69. and brige the virtual terminal to the web front-end
  70. Example Usage:
  71. let settingPayload = {
  72. server: "192.168.1.100",
  73. port: 2022,
  74. username: "pi"
  75. }
  76. let settings = encodeURIComponent(JSON.stringify(settingPayload));
  77. window.open("sshconn.html#" + settings)
  78. */
  79. if (window.location.hash.length > 1){
  80. try{
  81. var c = JSON.parse(decodeURIComponent((window.location.hash.substr(1))));
  82. $('input[name="server"]').val(c.server);
  83. $('input[name="port"]').val(c.port);
  84. $('input[name="username"]').val(c.username);
  85. connectSSH();
  86. }catch(ex){
  87. alert("invalid usage")
  88. }
  89. }
  90. //Start the SSH connection process
  91. function connectSSH() {
  92. const serverValue = $('input[name="server"]').val().trim();
  93. var portString = $('input[name="port"]').val().trim();
  94. if (portString.trim() == ""){
  95. portString = "22";
  96. }
  97. const portValue = parseInt(portString, 10);
  98. const username = $('input[name="username"]').val().trim();
  99. if (username == ""){
  100. username = "root";
  101. }
  102. // Validate server name or IP address
  103. const validServer = isValidServerNameOrIPAddress(serverValue);
  104. // Validate port number
  105. const validPort = (portValue >= 1 && portValue <= 65535);
  106. if (validServer && validPort) {
  107. // Call doSomething function if both inputs are valid
  108. createSSHProxy(serverValue, portValue, username);
  109. } else {
  110. // Display an error message if either input is invalid
  111. const errorLabel = $('<div>').addClass('ui red basic label');
  112. if (!validServer) {
  113. errorLabel.text('Invalid server name or IP address');
  114. $('input[name="server"]').addClass('error');
  115. } else if (!validPort) {
  116. errorLabel.text('Invalid port number');
  117. $('input[name="port"]').addClass('error');
  118. }
  119. const field = $('input[name="server"]').closest('.field');
  120. field.append(errorLabel);
  121. }
  122. }
  123. function redirectWithCountdown(url) {
  124. $("#loadingUI").show();
  125. $("#connectionSettings").hide();
  126. var count = 3;
  127. var interval = setInterval(function() {
  128. $("#counter").html("ETA " + count + " seconds");
  129. count--;
  130. if (count === 0) {
  131. clearInterval(interval);
  132. window.location.href = url;
  133. }
  134. }, 1000);
  135. }
  136. //Try to ask the server side to create a ssh proxy object
  137. function createSSHProxy(remoteAddr, remotePort, username){
  138. //Request to create a ssh session instance
  139. $.ajax({
  140. url: "/api/tools/webssh",
  141. data: {ipaddr: remoteAddr, port: remotePort, username:username},
  142. method: "POST",
  143. success: function(sessionToken){
  144. if (sessionToken.error != undefined){
  145. alert(sessionToken.error);
  146. }else{
  147. //Session created. Redirect to ssh terminal
  148. redirectWithCountdown("/web.ssh/" + sessionToken + "/");
  149. }
  150. },
  151. error: function(){
  152. }
  153. })
  154. }
  155. function isValidServerNameOrIPAddress(str) {
  156. // First, check if the string is a valid IP address
  157. const ipAddressRegex = /^(\d{1,3}\.){3}\d{1,3}$/;
  158. if (ipAddressRegex.test(str)) {
  159. return true;
  160. }
  161. // If the string is not an IP address, check if it is a valid domain name or server name
  162. const serverNameRegex = /^[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z]{2,})+$/;
  163. if (serverNameRegex.test(str)) {
  164. return true;
  165. }
  166. // If the string is neither an IP address nor a server name, return false
  167. return false;
  168. }
  169. </script>
  170. </body>
  171. </html>