sshconn.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. </style>
  18. </head>
  19. <body>
  20. <br>
  21. <div class="ui container">
  22. <div class="ui form">
  23. <div class="two fields">
  24. <div class="field">
  25. <label>Server Name or IP Address</label>
  26. <input type="text" name="server" placeholder="e.g. example.com or 192.168.1.1">
  27. </div>
  28. <div class="field">
  29. <label>Port Number</label>
  30. <input type="number" name="port" placeholder="e.g. 22 or 2022">
  31. </div>
  32. <div class="field">
  33. <label>Username</label>
  34. <input type="text" name="username" placeholder="root">
  35. </div>
  36. </div>
  37. </div>
  38. </div>
  39. <div style="float: right;">
  40. <button class="ui basic button" onclick="connectSSH()"><i class="ui blue exchange icon"></i> Connect</button>
  41. <button class="ui basic button" style="margin-right: 1em;" onclick="window.open('', '_self', ''); window.close();"><i class="ui red remove icon"></i> Close</button>
  42. </div>
  43. <br><br><br>
  44. <script>
  45. //Start the SSH connection process
  46. function connectSSH() {
  47. const serverValue = $('input[name="server"]').val().trim();
  48. var portString = $('input[name="port"]').val().trim();
  49. if (portString.trim() == ""){
  50. portString = "22";
  51. }
  52. const portValue = parseInt(portString, 10);
  53. const username = $('input[name="username"]').val().trim();
  54. if (username == ""){
  55. username = "root";
  56. }
  57. // Validate server name or IP address
  58. const validServer = isValidServerNameOrIPAddress(serverValue);
  59. // Validate port number
  60. const validPort = (portValue >= 1 && portValue <= 65535);
  61. if (validServer && validPort) {
  62. // Call doSomething function if both inputs are valid
  63. createSSHProxy(serverValue, portValue, username);
  64. } else {
  65. // Display an error message if either input is invalid
  66. const errorLabel = $('<div>').addClass('ui red basic label');
  67. if (!validServer) {
  68. errorLabel.text('Invalid server name or IP address');
  69. $('input[name="server"]').addClass('error');
  70. } else if (!validPort) {
  71. errorLabel.text('Invalid port number');
  72. $('input[name="port"]').addClass('error');
  73. }
  74. const field = $('input[name="server"]').closest('.field');
  75. field.append(errorLabel);
  76. }
  77. }
  78. //Try to ask the server side to create a ssh proxy object
  79. function createSSHProxy(remoteAddr, remotePort, username){
  80. //Request to create a ssh session instance
  81. $.ajax({
  82. url: "/api/tools/webssh",
  83. data: {ipaddr: remoteAddr, port: remotePort, username:username},
  84. method: "POST",
  85. success: function(sessionToken){
  86. if (sessionToken.error != undefined){
  87. alert(sessionToken.error);
  88. }else{
  89. //Session created. Redirect to ssh terminal
  90. window.location.href = "/web.ssh/" + sessionToken + "/";
  91. }
  92. },
  93. error: function(){
  94. }
  95. })
  96. }
  97. function isValidServerNameOrIPAddress(str) {
  98. // First, check if the string is a valid IP address
  99. const ipAddressRegex = /^(\d{1,3}\.){3}\d{1,3}$/;
  100. if (ipAddressRegex.test(str)) {
  101. return true;
  102. }
  103. // If the string is not an IP address, check if it is a valid domain name or server name
  104. const serverNameRegex = /^[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z]{2,})+$/;
  105. if (serverNameRegex.test(str)) {
  106. return true;
  107. }
  108. // If the string is neither an IP address nor a server name, return false
  109. return false;
  110. }
  111. </script>
  112. </body>
  113. </html>