|
@@ -3,15 +3,13 @@
|
|
|
<p>Port forward using UPnP protocol</p>
|
|
|
<div class="ui divider"></div>
|
|
|
<div class="ui message">
|
|
|
- <h4><i class="ui loading spinner icon"></i> Checking Upnp State</h4>
|
|
|
+ <h4 id="upnpstatus"><i class="ui loading spinner icon"></i> Checking Upnp State</h4>
|
|
|
<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.
|
|
|
Note that not all router support this function, sometime this might be disabled by your ISP or administrator.</p>
|
|
|
-
|
|
|
- <button style="position: absolute; right: 0.6em; top: 0.6em;" class="ui circular basic icon button"><i class="ui green refresh icon"></i></button>
|
|
|
</div>
|
|
|
<div class="ui form">
|
|
|
<div class="field">
|
|
|
- <div class="ui toggle checkbox">
|
|
|
+ <div class="ui toggle checkbox" id="currentUpnpState" >
|
|
|
<input type="checkbox" name="upnp">
|
|
|
<label>Enable UPnP</label>
|
|
|
</div>
|
|
@@ -19,7 +17,7 @@
|
|
|
<div class="field">
|
|
|
<label>Port to Forward</label>
|
|
|
<div class="ui input">
|
|
|
- <input type="number" min="1" max="65535" name="forwardPort" placeholder="Forwardable Port">
|
|
|
+ <input type="number" min="1" max="65535" name="forwardPort" placeholder="Forward Port">
|
|
|
</div>
|
|
|
</div>
|
|
|
<div class="field">
|
|
@@ -29,7 +27,7 @@
|
|
|
</div>
|
|
|
</div>
|
|
|
<div class="field">
|
|
|
- <button class="ui teal button" type="button" name="addRule"><i class="ui add icon"></i> Add Rule</button>
|
|
|
+ <button id="addForwardRuleButton" class="ui teal button" type="button" name="addRule" onclick="addForwardRule();"><i class="ui add icon"></i> Add Rule</button>
|
|
|
</div>
|
|
|
</div>
|
|
|
|
|
@@ -43,29 +41,153 @@
|
|
|
<th></th>
|
|
|
</tr>
|
|
|
</thead>
|
|
|
- <tbody>
|
|
|
+ <tbody id="forwardPortTable">
|
|
|
<tr>
|
|
|
<td>80</td>
|
|
|
<td>HTTP</td>
|
|
|
<td><button class="ui button negative" type="button" name="deleteRule">Delete</button></td>
|
|
|
</tr>
|
|
|
- <tr>
|
|
|
- <td>22</td>
|
|
|
- <td>SSH</td>
|
|
|
- <td><button class="ui button negative" type="button" name="deleteRule">Delete</button></td>
|
|
|
- </tr>
|
|
|
+
|
|
|
</tbody>
|
|
|
</table>
|
|
|
</div>
|
|
|
|
|
|
<script>
|
|
|
- //Get value of the form
|
|
|
- function getFormValues() {
|
|
|
- var formValues = {};
|
|
|
- formValues.upnp = $('input[name="upnp"]').prop('checked');
|
|
|
- formValues.forwardPort = $('input[name="forwardPort"]').val();
|
|
|
- formValues.ruleName = $('input[name="ruleName"]').val();
|
|
|
- return formValues;
|
|
|
+ let UpnpRouterFound = false;
|
|
|
+
|
|
|
+ function addForwardRule() {
|
|
|
+ // Get the values of the form inputs
|
|
|
+ var forwardPort = $('input[name="forwardPort"]').val();
|
|
|
+ var ruleName = $('input[name="ruleName"]').val();
|
|
|
+
|
|
|
+ // Make an AJAX request
|
|
|
+ $.ajax({
|
|
|
+ type: 'POST',
|
|
|
+ url: '/api/upnp/add',
|
|
|
+ data: {port: forwardPort, name: ruleName},
|
|
|
+ success: function(data) {
|
|
|
+ if (data.error !== undefined){
|
|
|
+ alert(data.error);
|
|
|
+ }else{
|
|
|
+ $('input[name="forwardPort"]').val("");
|
|
|
+ $('input[name="ruleName"]').val("");
|
|
|
+ initUPnPStatus();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ function removeForwardRule(portno){
|
|
|
+ $.ajax({
|
|
|
+ url: "/api/upnp/remove",
|
|
|
+ method: "POST",
|
|
|
+ data: {port: portno},
|
|
|
+ success: function(data){
|
|
|
+ if (data.error != undefined){
|
|
|
+ alert(data.error);
|
|
|
+ }else{
|
|
|
+ updateFordwardTable();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ function initUpnpToggleState(){
|
|
|
+ $.get("/api/upnp/toggle", function(data){
|
|
|
+ if (data == true){
|
|
|
+ //Set the current UPnP state to true
|
|
|
+ $("#currentUpnpState").checkbox("set checked");
|
|
|
+ }else{
|
|
|
+ $("#currentUpnpState").checkbox("set unchecked");
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ $("#currentUpnpState").on("change", function(){
|
|
|
+ let isChecked = $("#currentUpnpState").checkbox("is checked");
|
|
|
+ if (isChecked){
|
|
|
+ $("#addForwardRuleButton").removeClass("disabled");
|
|
|
+ }else{
|
|
|
+ $("#addForwardRuleButton").addClass("disabled");
|
|
|
+ }
|
|
|
+ $.ajax({
|
|
|
+ url: "/api/upnp/toggle",
|
|
|
+ method: "POST",
|
|
|
+ data: {mode: isChecked},
|
|
|
+ success: function(data){
|
|
|
+ console.log(data);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ })
|
|
|
}
|
|
|
+ initUpnpToggleState();
|
|
|
|
|
|
+ function initUPnPStatus(){
|
|
|
+ $.get("/api/upnp/discover", function(data){
|
|
|
+ if (data.error != undefined){
|
|
|
+ $("#upnpstatus").html(`<i class="red remove icon"></i> UPnP Router Not Found`);
|
|
|
+ $("#currentUpnpState").addClass("disabled");
|
|
|
+ }else{
|
|
|
+ $("#upnpstatus").html(`<i class="green check icon"></i> Router WAN IP: ${data.ExternalIp}`);
|
|
|
+ UpnpRouterFound = true;
|
|
|
+ updateFordwardTable();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ initUPnPStatus();
|
|
|
+
|
|
|
+ function updateFordwardTable(){
|
|
|
+ $.get("/api/upnp/list", function(data){
|
|
|
+ renderForwardTable(data);
|
|
|
+ console.log(data);
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ function renderForwardTable(data){
|
|
|
+ // Assuming the JSON data is stored in a variable called 'data'
|
|
|
+
|
|
|
+ // Get the table body element
|
|
|
+ var tableBody = $("#forwardPortTable")[0];
|
|
|
+
|
|
|
+ // Clear any existing rows from the table
|
|
|
+ tableBody.innerHTML = '';
|
|
|
+
|
|
|
+ // Loop through the required ports and add a row for each
|
|
|
+ for (const [port, name] of Object.entries(data)) {
|
|
|
+ // Create a new row element
|
|
|
+ var row = document.createElement('tr');
|
|
|
+
|
|
|
+ // Create a cell for the port number
|
|
|
+ var portCell = document.createElement('td');
|
|
|
+ portCell.textContent = port;
|
|
|
+ row.appendChild(portCell);
|
|
|
+
|
|
|
+ // Create a cell for the name of the rule (you can customize this based on your needs)
|
|
|
+ var nameCell = document.createElement('td');
|
|
|
+ nameCell.textContent = name;
|
|
|
+ row.appendChild(nameCell);
|
|
|
+
|
|
|
+ // Create a cell for the delete button
|
|
|
+ var deleteCell = document.createElement('td');
|
|
|
+ var deleteButton = document.createElement('button');
|
|
|
+ deleteButton.textContent = 'Delete';
|
|
|
+ deleteButton.className = 'ui button negative';
|
|
|
+ deleteButton.setAttribute('type', 'button');
|
|
|
+ deleteButton.setAttribute('name', 'deleteRule');
|
|
|
+ deleteButton.onclick = function() {
|
|
|
+ removeForwardRule(port);
|
|
|
+ };
|
|
|
+ deleteCell.appendChild(deleteButton);
|
|
|
+ row.appendChild(deleteCell);
|
|
|
+
|
|
|
+ // Add the row to the table body
|
|
|
+ tableBody.appendChild(row);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (Object.keys(data).length == 0){
|
|
|
+ $("#forwardPortTable").append(`<tr>
|
|
|
+ <td colspan="3"><i class="check icon"></i> No Forwarded Port</td>
|
|
|
+ </tr>`);
|
|
|
+ }
|
|
|
+ }
|
|
|
</script>
|