upnp.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <h3><i class="blue exchange icon"></i> Port Forward</h3>
  2. <p>Port forward using UPnP protocol</p>
  3. <div class="ui divider"></div>
  4. <div class="ui message">
  5. <h4 id="upnpstatus"><i class="ui loading spinner icon"></i> Checking Upnp State</h4>
  6. <p><i class="ui info circle icon"></i> If you are hosting this server under a home router which you have no access to, you can try port forward your services port and expose them to the internet via Upnp protocol.
  7. Note that not all router support this function, sometime this might be disabled by your ISP or administrator.</p>
  8. </div>
  9. <div class="ui form">
  10. <div class="field">
  11. <div class="ui toggle checkbox" id="currentUpnpState" >
  12. <input type="checkbox" name="upnp">
  13. <label>Enable UPnP</label>
  14. </div>
  15. </div>
  16. <div class="field">
  17. <label>Port to Forward</label>
  18. <div class="ui input">
  19. <input type="number" min="1" max="65535" name="forwardPort" placeholder="Forward Port">
  20. </div>
  21. </div>
  22. <div class="field">
  23. <label>Rule Name</label>
  24. <div class="ui input">
  25. <input type="text" name="ruleName" placeholder="Rule Name">
  26. </div>
  27. </div>
  28. <div class="field">
  29. <button id="addForwardRuleButton" class="ui teal button" type="button" name="addRule" onclick="addForwardRule();"><i class="ui add icon"></i> Add Rule</button>
  30. </div>
  31. </div>
  32. <div class="ui basic segment">
  33. <p>Forwarded Ports</p>
  34. <table class="ui basic table">
  35. <thead>
  36. <tr>
  37. <th>Port Forwarded</th>
  38. <th>Name of Rule</th>
  39. <th></th>
  40. </tr>
  41. </thead>
  42. <tbody id="forwardPortTable">
  43. <tr>
  44. <td>80</td>
  45. <td>HTTP</td>
  46. <td><button class="ui button negative" type="button" name="deleteRule">Delete</button></td>
  47. </tr>
  48. </tbody>
  49. </table>
  50. </div>
  51. <script>
  52. let UpnpRouterFound = false;
  53. function addForwardRule() {
  54. // Get the values of the form inputs
  55. var forwardPort = $('input[name="forwardPort"]').val();
  56. var ruleName = $('input[name="ruleName"]').val();
  57. // Make an AJAX request
  58. $.ajax({
  59. type: 'POST',
  60. url: '/api/upnp/add',
  61. data: {port: forwardPort, name: ruleName},
  62. success: function(data) {
  63. if (data.error !== undefined){
  64. alert(data.error);
  65. }else{
  66. $('input[name="forwardPort"]').val("");
  67. $('input[name="ruleName"]').val("");
  68. initUPnPStatus();
  69. }
  70. }
  71. });
  72. }
  73. function removeForwardRule(portno){
  74. $.ajax({
  75. url: "/api/upnp/remove",
  76. method: "POST",
  77. data: {port: portno},
  78. success: function(data){
  79. if (data.error != undefined){
  80. alert(data.error);
  81. }else{
  82. updateFordwardTable();
  83. }
  84. }
  85. })
  86. }
  87. function initUpnpToggleState(){
  88. $.get("/api/upnp/toggle", function(data){
  89. if (data == true){
  90. //Set the current UPnP state to true
  91. $("#currentUpnpState").checkbox("set checked");
  92. }else{
  93. $("#currentUpnpState").checkbox("set unchecked");
  94. }
  95. });
  96. $("#currentUpnpState").on("change", function(){
  97. let isChecked = $("#currentUpnpState").checkbox("is checked");
  98. if (isChecked){
  99. $("#addForwardRuleButton").removeClass("disabled");
  100. }else{
  101. $("#addForwardRuleButton").addClass("disabled");
  102. }
  103. $.ajax({
  104. url: "/api/upnp/toggle",
  105. method: "POST",
  106. data: {mode: isChecked},
  107. success: function(data){
  108. console.log(data);
  109. }
  110. });
  111. })
  112. }
  113. initUpnpToggleState();
  114. function initUPnPStatus(){
  115. $.get("/api/upnp/discover", function(data){
  116. if (data.error != undefined){
  117. $("#upnpstatus").html(`<i class="red remove icon"></i> UPnP Router Not Found`);
  118. $("#currentUpnpState").addClass("disabled");
  119. }else{
  120. $("#upnpstatus").html(`<i class="green check icon"></i> Router WAN IP: ${data.ExternalIp}`);
  121. UpnpRouterFound = true;
  122. updateFordwardTable();
  123. }
  124. });
  125. }
  126. initUPnPStatus();
  127. function updateFordwardTable(){
  128. $.get("/api/upnp/list", function(data){
  129. renderForwardTable(data);
  130. console.log(data);
  131. })
  132. }
  133. function renderForwardTable(data){
  134. // Assuming the JSON data is stored in a variable called 'data'
  135. // Get the table body element
  136. var tableBody = $("#forwardPortTable")[0];
  137. // Clear any existing rows from the table
  138. tableBody.innerHTML = '';
  139. // Loop through the required ports and add a row for each
  140. for (const [port, name] of Object.entries(data)) {
  141. // Create a new row element
  142. var row = document.createElement('tr');
  143. // Create a cell for the port number
  144. var portCell = document.createElement('td');
  145. portCell.textContent = port;
  146. row.appendChild(portCell);
  147. // Create a cell for the name of the rule (you can customize this based on your needs)
  148. var nameCell = document.createElement('td');
  149. nameCell.textContent = name;
  150. row.appendChild(nameCell);
  151. // Create a cell for the delete button
  152. var deleteCell = document.createElement('td');
  153. var deleteButton = document.createElement('button');
  154. deleteButton.textContent = 'Delete';
  155. deleteButton.className = 'ui button negative';
  156. deleteButton.setAttribute('type', 'button');
  157. deleteButton.setAttribute('name', 'deleteRule');
  158. deleteButton.onclick = function() {
  159. removeForwardRule(port);
  160. };
  161. deleteCell.appendChild(deleteButton);
  162. row.appendChild(deleteCell);
  163. // Add the row to the table body
  164. tableBody.appendChild(row);
  165. }
  166. if (Object.keys(data).length == 0){
  167. $("#forwardPortTable").append(`<tr>
  168. <td colspan="3"><i class="check icon"></i> No Forwarded Port</td>
  169. </tr>`);
  170. }
  171. }
  172. </script>