sshconn.html 8.3 KB

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