1
0

sshconn.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. //Start the SSH connection process
  67. function connectSSH() {
  68. const serverValue = $('input[name="server"]').val().trim();
  69. var portString = $('input[name="port"]').val().trim();
  70. if (portString.trim() == ""){
  71. portString = "22";
  72. }
  73. const portValue = parseInt(portString, 10);
  74. const username = $('input[name="username"]').val().trim();
  75. if (username == ""){
  76. username = "root";
  77. }
  78. // Validate server name or IP address
  79. const validServer = isValidServerNameOrIPAddress(serverValue);
  80. // Validate port number
  81. const validPort = (portValue >= 1 && portValue <= 65535);
  82. if (validServer && validPort) {
  83. // Call doSomething function if both inputs are valid
  84. createSSHProxy(serverValue, portValue, username);
  85. } else {
  86. // Display an error message if either input is invalid
  87. const errorLabel = $('<div>').addClass('ui red basic label');
  88. if (!validServer) {
  89. errorLabel.text('Invalid server name or IP address');
  90. $('input[name="server"]').addClass('error');
  91. } else if (!validPort) {
  92. errorLabel.text('Invalid port number');
  93. $('input[name="port"]').addClass('error');
  94. }
  95. const field = $('input[name="server"]').closest('.field');
  96. field.append(errorLabel);
  97. }
  98. }
  99. function redirectWithCountdown(url) {
  100. $("#loadingUI").show();
  101. $("#connectionSettings").hide();
  102. var count = 3;
  103. var interval = setInterval(function() {
  104. $("#counter").html("ETA " + count + " seconds");
  105. count--;
  106. if (count === 0) {
  107. clearInterval(interval);
  108. window.location.href = url;
  109. }
  110. }, 1000);
  111. }
  112. //Try to ask the server side to create a ssh proxy object
  113. function createSSHProxy(remoteAddr, remotePort, username){
  114. //Request to create a ssh session instance
  115. $.ajax({
  116. url: "/api/tools/webssh",
  117. data: {ipaddr: remoteAddr, port: remotePort, username:username},
  118. method: "POST",
  119. success: function(sessionToken){
  120. if (sessionToken.error != undefined){
  121. alert(sessionToken.error);
  122. }else{
  123. //Session created. Redirect to ssh terminal
  124. redirectWithCountdown("/web.ssh/" + sessionToken + "/");
  125. }
  126. },
  127. error: function(){
  128. }
  129. })
  130. }
  131. function isValidServerNameOrIPAddress(str) {
  132. // First, check if the string is a valid IP address
  133. const ipAddressRegex = /^(\d{1,3}\.){3}\d{1,3}$/;
  134. if (ipAddressRegex.test(str)) {
  135. return true;
  136. }
  137. // If the string is not an IP address, check if it is a valid domain name or server name
  138. const serverNameRegex = /^[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z]{2,})+$/;
  139. if (serverNameRegex.test(str)) {
  140. return true;
  141. }
  142. // If the string is neither an IP address nor a server name, return false
  143. return false;
  144. }
  145. </script>
  146. </body>
  147. </html>