- Escape HTML in client list and wake-on-LAN names to prevent XSS - Log successful/failed login attempts with remote address - Fix leading-comma bug in AllowedIPs template when only extra allowed IPs are set - Add PreUp script support for server interfaces (alongside existing PostUp/PreDown/PostDown) - Fix endpoint parsing to support IPv6 addresses (upstream PR #223) Cherry-picked from upstream PRs #656, #653, #680, #673, #223. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
769 lines
34 KiB
HTML
769 lines
34 KiB
HTML
{{define "title"}}
|
|
All Servers
|
|
{{end}}
|
|
|
|
{{define "top_css"}}
|
|
{{end}}
|
|
|
|
{{define "username"}}
|
|
{{ .username }}
|
|
{{end}}
|
|
|
|
{{define "page_title"}}
|
|
All Servers
|
|
{{end}}
|
|
|
|
{{define "page_content"}}
|
|
<section class="content">
|
|
<div class="container-fluid">
|
|
<div class="row" id="servers-list">
|
|
</div>
|
|
</div>
|
|
</section>
|
|
|
|
<div class="modal fade" id="modal_new_server">
|
|
<div class="modal-dialog">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h4 class="modal-title">Add new server</h4>
|
|
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
|
|
<span aria-hidden="true">×</span>
|
|
</button>
|
|
</div>
|
|
<form name="frm_new_server" id="frm_new_server">
|
|
<div class="modal-body">
|
|
<div class="form-group">
|
|
<label for="_server_id" class="control-label">ID</label>
|
|
<input type="text" class="form-control" id="_server_id" name="_server_id"
|
|
placeholder="e.g. wg-home">
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="_server_name" class="control-label">Name</label>
|
|
<input type="text" class="form-control" id="_server_name" name="_server_name"
|
|
placeholder="e.g. WGhome">
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="_server_interface" class="control-label">Interface</label>
|
|
<input type="text" class="form-control" id="_server_interface" name="_server_interface"
|
|
placeholder="e.g. wg-home">
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="_server_addresses" class="control-label">Addresses</label>
|
|
<input type="text" class="form-control" id="_server_addresses" name="_server_addresses"
|
|
placeholder="e.g. 10.20.22.0/24, fd00::/64">
|
|
<small class="form-text text-muted">Comma-separated list of address ranges.</small>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="_server_listen_port" class="control-label">Listen Port</label>
|
|
<input type="text" class="form-control" id="_server_listen_port" name="_server_listen_port"
|
|
placeholder="e.g. 51822">
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer justify-content-between">
|
|
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
|
|
<button type="submit" class="btn btn-success">Save</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
<!-- /.modal-content -->
|
|
</div>
|
|
<!-- /.modal-dialog -->
|
|
</div>
|
|
<!-- /.modal -->
|
|
|
|
<div class="modal fade" id="modal_server_settings">
|
|
<div class="modal-dialog">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h4 class="modal-title">Server Settings</h4>
|
|
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
|
|
<span aria-hidden="true">×</span>
|
|
</button>
|
|
</div>
|
|
<form name="frm_server_settings" id="frm_server_settings">
|
|
<div class="modal-body">
|
|
<input type="hidden" id="_settings_server_id" name="_settings_server_id">
|
|
<div class="form-group">
|
|
<label for="_settings_endpoint_address" class="control-label">Endpoint Address</label>
|
|
<input type="text" class="form-control" id="_settings_endpoint_address"
|
|
placeholder="e.g. vpn.example.com or vpn.example.com:51822">
|
|
<small class="form-text text-muted">Leave empty to fall back to the app-wide default endpoint.</small>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="_settings_config_file_path" class="control-label">Config File Path</label>
|
|
<input type="text" class="form-control" id="_settings_config_file_path"
|
|
placeholder="/etc/wireguard/wg-home.conf">
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="_settings_firewall_mark" class="control-label">Firewall Mark</label>
|
|
<input type="text" class="form-control" id="_settings_firewall_mark" placeholder="0xca6c">
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="_settings_table" class="control-label">Table</label>
|
|
<input type="text" class="form-control" id="_settings_table" placeholder="auto">
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="_settings_lan_interface" class="control-label">LAN Interface</label>
|
|
<input type="text" class="form-control" id="_settings_lan_interface" placeholder="e.g. eth0, br-lan">
|
|
<small class="form-text text-muted">Optional. Used only for the Firewall Preview - lets peers forward to this interface.</small>
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer justify-content-between">
|
|
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
|
|
<button type="submit" class="btn btn-success">Save</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
<!-- /.modal-content -->
|
|
</div>
|
|
<!-- /.modal-dialog -->
|
|
</div>
|
|
<!-- /.modal -->
|
|
|
|
<div class="modal fade" id="modal_server_interface">
|
|
<div class="modal-dialog">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h4 class="modal-title">Server Interface</h4>
|
|
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
|
|
<span aria-hidden="true">×</span>
|
|
</button>
|
|
</div>
|
|
<form name="frm_server_interface" id="frm_server_interface">
|
|
<div class="modal-body">
|
|
<input type="hidden" id="_iface_server_id" name="_iface_server_id">
|
|
<div class="form-group">
|
|
<label for="_iface_addresses" class="control-label">Server Interface Addresses</label>
|
|
<input type="text" class="form-control" id="_iface_addresses"
|
|
placeholder="e.g. 10.20.22.1/24, fd00::1/64">
|
|
<small class="form-text text-muted">Comma-separated list of address ranges.</small>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="_iface_listen_port" class="control-label">Listen Port</label>
|
|
<input type="text" class="form-control" id="_iface_listen_port" name="_iface_listen_port"
|
|
placeholder="e.g. 51822">
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="_iface_pre_up" class="control-label">Pre Up Script</label>
|
|
<input type="text" class="form-control" id="_iface_pre_up" placeholder="Pre Up Script">
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="_iface_post_up" class="control-label">Post Up Script</label>
|
|
<input type="text" class="form-control" id="_iface_post_up" placeholder="Post Up Script">
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="_iface_pre_down" class="control-label">Pre Down Script</label>
|
|
<input type="text" class="form-control" id="_iface_pre_down" placeholder="Pre Down Script">
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="_iface_post_down" class="control-label">Post Down Script</label>
|
|
<input type="text" class="form-control" id="_iface_post_down" placeholder="Post Down Script">
|
|
</div>
|
|
<hr>
|
|
<div class="form-group">
|
|
<label class="control-label">Public Key</label>
|
|
<input type="text" class="form-control" id="_iface_public_key" disabled>
|
|
</div>
|
|
<button type="button" class="btn btn-outline-danger btn-sm" id="btn_regenerate_keypair">
|
|
Regenerate Keypair
|
|
</button>
|
|
</div>
|
|
<div class="modal-footer justify-content-between">
|
|
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
|
|
<button type="submit" class="btn btn-success">Save</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
<!-- /.modal-content -->
|
|
</div>
|
|
<!-- /.modal-dialog -->
|
|
</div>
|
|
<!-- /.modal -->
|
|
|
|
<div class="modal fade" id="modal_firewall">
|
|
<div class="modal-dialog modal-lg">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h4 class="modal-title">Firewall Rules (nftables) - <span id="_fw_server_label"></span></h4>
|
|
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
|
|
<span aria-hidden="true">×</span>
|
|
</button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<input type="hidden" id="_fw_server_id">
|
|
|
|
<table class="table table-sm" id="_fw_rules_table">
|
|
<thead>
|
|
<tr>
|
|
<th>On</th><th>Chain</th><th>Proto</th><th>Port</th><th>Source</th><th>Action</th><th>Comment</th><th></th>
|
|
</tr>
|
|
</thead>
|
|
<tbody id="_fw_rules_tbody"></tbody>
|
|
</table>
|
|
|
|
<form id="frm_firewall_rule" class="form-inline">
|
|
<select class="form-control form-control-sm mr-1 mb-1" id="_fw_direction">
|
|
<option value="input">input</option>
|
|
<option value="forward">forward</option>
|
|
</select>
|
|
<select class="form-control form-control-sm mr-1 mb-1" id="_fw_protocol">
|
|
<option value="">any</option>
|
|
<option value="tcp">tcp</option>
|
|
<option value="udp">udp</option>
|
|
</select>
|
|
<input type="text" class="form-control form-control-sm mr-1 mb-1" id="_fw_port" placeholder="port(s) e.g. 8080" style="width:9em">
|
|
<input type="text" class="form-control form-control-sm mr-1 mb-1" id="_fw_source" placeholder="source CIDR (optional)" style="width:11em">
|
|
<select class="form-control form-control-sm mr-1 mb-1" id="_fw_action">
|
|
<option value="accept">accept</option>
|
|
<option value="drop">drop</option>
|
|
<option value="reject">reject</option>
|
|
</select>
|
|
<input type="text" class="form-control form-control-sm mr-1 mb-1" id="_fw_comment" placeholder="comment" style="width:11em">
|
|
<button type="submit" class="btn btn-primary btn-sm mb-1">Add rule</button>
|
|
</form>
|
|
|
|
<hr>
|
|
<p class="text-muted mb-1">Ruleset preview:</p>
|
|
<pre id="_firewall_preview_text" style="max-height: 30vh; overflow:auto;"></pre>
|
|
</div>
|
|
<div class="modal-footer justify-content-between">
|
|
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
|
|
<button type="button" class="btn btn-danger" id="btn_apply_firewall">Apply now (live)</button>
|
|
</div>
|
|
</div>
|
|
<!-- /.modal-content -->
|
|
</div>
|
|
<!-- /.modal-dialog -->
|
|
</div>
|
|
<!-- /.modal -->
|
|
{{end}}
|
|
|
|
{{define "bottom_js"}}
|
|
<script>
|
|
// cache of the last-fetched server list, keyed by id, so the Interface
|
|
// modal can prefill fields without a second round trip
|
|
var serversById = {};
|
|
|
|
function renderServersList(data) {
|
|
$.each(data, function (index, obj) {
|
|
serversById[obj.id] = obj;
|
|
const addresses = (obj.Interface && obj.Interface.addresses) ? obj.Interface.addresses.join(", ") : "";
|
|
const listenPort = obj.Interface ? obj.Interface.listen_port : "";
|
|
const interfaceName = obj.Interface ? obj.Interface.name : "";
|
|
|
|
// obj.id/interfaceName are server-side validated against [a-zA-Z0-9_-],
|
|
// safe to interpolate; obj.name is free text and must be escaped.
|
|
const safeName = $('<div>').text(obj.name).html();
|
|
|
|
let html = `<div class="col-sm-6 col-md-6 col-lg-4" id="server_${obj.id}">
|
|
<div class="info-box">
|
|
<div class="info-box-content">
|
|
<div class="btn-group">
|
|
<a href="{{.basePath}}/servers/${obj.id}/clients" class="btn btn-outline-primary btn-sm">Manage clients</a>
|
|
</div>
|
|
<div class="btn-group">
|
|
<button type="button" class="btn btn-outline-secondary btn-sm" data-toggle="modal"
|
|
data-target="#modal_server_settings" data-serverid="${obj.id}">Settings</button>
|
|
</div>
|
|
<div class="btn-group">
|
|
<button type="button" class="btn btn-outline-secondary btn-sm" data-toggle="modal"
|
|
data-target="#modal_server_interface" data-serverid="${obj.id}">Interface</button>
|
|
</div>
|
|
<div class="btn-group">
|
|
<button type="button" class="btn btn-outline-info btn-sm btn-firewall-preview" data-serverid="${obj.id}" data-servername="${safeName}">Firewall</button>
|
|
</div>
|
|
<div class="btn-group">
|
|
<button type="button" class="btn btn-outline-danger btn-sm btn-delete-server" data-serverid="${obj.id}" data-servername="${safeName}">Delete</button>
|
|
</div>
|
|
<hr>
|
|
<span class="info-box-text"><i class="fas fa-server"></i> ${safeName}</span>
|
|
<span class="info-box-text"><i class="fas fa-fingerprint"></i> ID: ${obj.id}</span>
|
|
<span class="info-box-text"><i class="fas fa-ethernet"></i> Interface: ${interfaceName}</span>
|
|
<span class="info-box-text"><i class="fas fa-plug"></i> Listen Port: ${listenPort}</span>
|
|
<span class="info-box-text"><i class="fas fa-map-marker-alt"></i> Addresses: ${addresses}</span>
|
|
</div>
|
|
</div>
|
|
</div>`
|
|
|
|
$('#servers-list').append(html);
|
|
});
|
|
}
|
|
|
|
function populateServersList() {
|
|
$.ajax({
|
|
cache: false,
|
|
method: 'GET',
|
|
url: '{{.basePath}}/servers',
|
|
dataType: 'json',
|
|
contentType: "application/json",
|
|
success: function (data) {
|
|
renderServersList(data);
|
|
},
|
|
error: function (jqXHR, exception) {
|
|
const responseJson = jQuery.parseJSON(jqXHR.responseText);
|
|
toastr.error(responseJson['message']);
|
|
}
|
|
});
|
|
}
|
|
|
|
// load server list
|
|
$(document).ready(function () {
|
|
populateServersList();
|
|
let newServerHtml = '<div class="col-sm-2 offset-md-4" style=" text-align: right;">' +
|
|
'<button style="" id="btn_new_server" type="button" class="btn btn-outline-primary btn-sm" ' +
|
|
'data-toggle="modal" data-target="#modal_new_server">' +
|
|
'<i class="nav-icon fas fa-plus"></i> New Server</button></div>';
|
|
$('h1').parents(".row").append(newServerHtml);
|
|
})
|
|
|
|
// New server modal event: reset the form each time it is opened
|
|
$(document).ready(function () {
|
|
$("#modal_new_server").on('show.bs.modal', function (event) {
|
|
let modal = $(this);
|
|
modal.find("#_server_id").val("");
|
|
modal.find("#_server_name").val("");
|
|
modal.find("#_server_interface").val("");
|
|
modal.find("#_server_addresses").val("");
|
|
modal.find("#_server_listen_port").val("");
|
|
});
|
|
});
|
|
|
|
function submitNewServer() {
|
|
const id = $("#_server_id").val();
|
|
const name = $("#_server_name").val();
|
|
const iface = $("#_server_interface").val();
|
|
const addresses = $("#_server_addresses").val().split(",").map(function (a) {
|
|
return a.trim();
|
|
}).filter(function (a) {
|
|
return a !== "";
|
|
});
|
|
const listen_port = parseInt($("#_server_listen_port").val(), 10);
|
|
|
|
const data = {
|
|
"id": id,
|
|
"name": name,
|
|
"interface": iface,
|
|
"addresses": addresses,
|
|
"listen_port": listen_port
|
|
};
|
|
|
|
$.ajax({
|
|
cache: false,
|
|
method: 'POST',
|
|
url: '{{.basePath}}/servers',
|
|
dataType: 'json',
|
|
contentType: "application/json",
|
|
data: JSON.stringify(data),
|
|
success: function (data) {
|
|
$("#modal_new_server").modal('hide');
|
|
toastr.success("Created server successfully");
|
|
location.reload();
|
|
},
|
|
error: function (jqXHR, exception) {
|
|
const responseJson = jQuery.parseJSON(jqXHR.responseText);
|
|
toastr.error(responseJson['message']);
|
|
}
|
|
});
|
|
}
|
|
|
|
// Server settings modal event: load current settings for the clicked server
|
|
$("#modal_server_settings").on('show.bs.modal', function (event) {
|
|
const button = $(event.relatedTarget);
|
|
const serverId = button.data('serverid');
|
|
const modal = $(this);
|
|
modal.find("#_settings_server_id").val(serverId);
|
|
modal.find("#_settings_endpoint_address").val("");
|
|
modal.find("#_settings_config_file_path").val("");
|
|
modal.find("#_settings_firewall_mark").val("");
|
|
modal.find("#_settings_table").val("");
|
|
modal.find("#_settings_lan_interface").val("");
|
|
|
|
$.ajax({
|
|
cache: false,
|
|
method: 'GET',
|
|
url: '{{.basePath}}/servers/' + serverId + '/settings',
|
|
dataType: 'json',
|
|
contentType: "application/json",
|
|
success: function (settings) {
|
|
modal.find("#_settings_endpoint_address").val(settings.endpoint_address);
|
|
modal.find("#_settings_config_file_path").val(settings.config_file_path);
|
|
modal.find("#_settings_firewall_mark").val(settings.firewall_mark);
|
|
modal.find("#_settings_table").val(settings.table);
|
|
modal.find("#_settings_lan_interface").val(settings.lan_interface);
|
|
},
|
|
error: function (jqXHR, exception) {
|
|
const responseJson = jQuery.parseJSON(jqXHR.responseText);
|
|
toastr.error(responseJson['message']);
|
|
}
|
|
});
|
|
});
|
|
|
|
function submitServerSettings() {
|
|
const serverId = $("#_settings_server_id").val();
|
|
const data = {
|
|
"endpoint_address": $("#_settings_endpoint_address").val(),
|
|
"config_file_path": $("#_settings_config_file_path").val(),
|
|
"firewall_mark": $("#_settings_firewall_mark").val(),
|
|
"table": $("#_settings_table").val(),
|
|
"lan_interface": $("#_settings_lan_interface").val()
|
|
};
|
|
|
|
$.ajax({
|
|
cache: false,
|
|
method: 'POST',
|
|
url: '{{.basePath}}/servers/' + serverId + '/settings',
|
|
dataType: 'json',
|
|
contentType: "application/json",
|
|
data: JSON.stringify(data),
|
|
success: function (data) {
|
|
$("#modal_server_settings").modal('hide');
|
|
toastr.success("Updated server settings successfully");
|
|
},
|
|
error: function (jqXHR, exception) {
|
|
const responseJson = jQuery.parseJSON(jqXHR.responseText);
|
|
toastr.error(responseJson['message']);
|
|
}
|
|
});
|
|
}
|
|
|
|
$(document).ready(function () {
|
|
$("#frm_server_settings").on('submit', function (e) {
|
|
e.preventDefault();
|
|
submitServerSettings();
|
|
});
|
|
});
|
|
|
|
// Server interface modal event: prefill from the cached server list data
|
|
$("#modal_server_interface").on('show.bs.modal', function (event) {
|
|
const button = $(event.relatedTarget);
|
|
const serverId = button.data('serverid');
|
|
const modal = $(this);
|
|
const obj = serversById[serverId] || {};
|
|
|
|
modal.find("#_iface_server_id").val(serverId);
|
|
modal.find("#_iface_addresses").val((obj.Interface && obj.Interface.addresses) ? obj.Interface.addresses.join(", ") : "");
|
|
modal.find("#_iface_listen_port").val(obj.Interface ? obj.Interface.listen_port : "");
|
|
modal.find("#_iface_pre_up").val(obj.Interface ? obj.Interface.pre_up : "");
|
|
modal.find("#_iface_post_up").val(obj.Interface ? obj.Interface.post_up : "");
|
|
modal.find("#_iface_pre_down").val(obj.Interface ? obj.Interface.pre_down : "");
|
|
modal.find("#_iface_post_down").val(obj.Interface ? obj.Interface.post_down : "");
|
|
modal.find("#_iface_public_key").val(obj.KeyPair ? obj.KeyPair.public_key : "");
|
|
});
|
|
|
|
function submitServerInterface() {
|
|
const serverId = $("#_iface_server_id").val();
|
|
const addresses = $("#_iface_addresses").val().split(",").map(function (a) {
|
|
return a.trim();
|
|
}).filter(function (a) {
|
|
return a !== "";
|
|
});
|
|
const data = {
|
|
"addresses": addresses,
|
|
"listen_port": $("#_iface_listen_port").val(),
|
|
"pre_up": $("#_iface_pre_up").val(),
|
|
"post_up": $("#_iface_post_up").val(),
|
|
"pre_down": $("#_iface_pre_down").val(),
|
|
"post_down": $("#_iface_post_down").val()
|
|
};
|
|
|
|
$.ajax({
|
|
cache: false,
|
|
method: 'POST',
|
|
url: '{{.basePath}}/servers/' + serverId + '/interface',
|
|
dataType: 'json',
|
|
contentType: "application/json",
|
|
data: JSON.stringify(data),
|
|
success: function (data) {
|
|
$("#modal_server_interface").modal('hide');
|
|
toastr.success("Updated server interface successfully");
|
|
$('#servers-list').empty();
|
|
populateServersList();
|
|
},
|
|
error: function (jqXHR, exception) {
|
|
const responseJson = jQuery.parseJSON(jqXHR.responseText);
|
|
toastr.error(responseJson['message']);
|
|
}
|
|
});
|
|
}
|
|
|
|
$(document).ready(function () {
|
|
$("#frm_server_interface").validate({
|
|
rules: {
|
|
_iface_listen_port: {
|
|
required: true,
|
|
digits: true,
|
|
range: [1, 65535]
|
|
}
|
|
},
|
|
messages: {
|
|
_iface_listen_port: {
|
|
required: "Please enter a port",
|
|
digits: "Port must be an integer",
|
|
range: "Port must be in range 1..65535"
|
|
}
|
|
},
|
|
errorElement: 'span',
|
|
errorPlacement: function (error, element) {
|
|
error.addClass('invalid-feedback');
|
|
element.closest('.form-group').append(error);
|
|
},
|
|
highlight: function (element, errorClass, validClass) {
|
|
$(element).addClass('is-invalid');
|
|
},
|
|
unhighlight: function (element, errorClass, validClass) {
|
|
$(element).removeClass('is-invalid');
|
|
},
|
|
submitHandler: function (form) {
|
|
submitServerInterface();
|
|
}
|
|
});
|
|
});
|
|
|
|
// Regenerate keypair button: confirm, then POST and refresh the field
|
|
$(document).ready(function () {
|
|
$("#btn_regenerate_keypair").click(function () {
|
|
const serverId = $("#_iface_server_id").val();
|
|
if (!confirm("Are you sure you want to generate a new key pair for this server?\n" +
|
|
"All existing peers will lose connectivity until their configuration is updated with the new public key.")) {
|
|
return;
|
|
}
|
|
$.ajax({
|
|
cache: false,
|
|
method: 'POST',
|
|
url: '{{.basePath}}/servers/' + serverId + '/keypair',
|
|
dataType: 'json',
|
|
contentType: "application/json",
|
|
success: function (data) {
|
|
toastr.success("Generated new key pair successfully");
|
|
$("#_iface_public_key").val(data['public_key']);
|
|
$('#servers-list').empty();
|
|
populateServersList();
|
|
},
|
|
error: function (jqXHR, exception) {
|
|
const responseJson = jQuery.parseJSON(jqXHR.responseText);
|
|
toastr.error(responseJson['message']);
|
|
}
|
|
});
|
|
});
|
|
});
|
|
|
|
// Firewall management modal
|
|
function refreshFirewallPreview(serverId) {
|
|
$("#_firewall_preview_text").text("Loading...");
|
|
$.ajax({
|
|
cache: false,
|
|
method: 'GET',
|
|
url: '{{.basePath}}/servers/' + serverId + '/firewall-preview',
|
|
dataType: 'text',
|
|
success: function (data) { $("#_firewall_preview_text").text(data); },
|
|
error: function () { $("#_firewall_preview_text").text("Could not load firewall preview."); }
|
|
});
|
|
}
|
|
|
|
function renderFirewallRules(serverId, rules) {
|
|
const tbody = $("#_fw_rules_tbody");
|
|
tbody.empty();
|
|
$.each(rules, function (i, rule) {
|
|
const safeComment = $('<div>').text(rule.comment || "").html();
|
|
const row = `<tr data-ruleid="${rule.id}">
|
|
<td>${rule.enabled ? "yes" : "no"}</td>
|
|
<td>${rule.direction}</td>
|
|
<td>${rule.protocol || "any"}</td>
|
|
<td>${rule.port || "any"}</td>
|
|
<td>${rule.source || "any"}</td>
|
|
<td>${rule.action}</td>
|
|
<td>${safeComment}</td>
|
|
<td><button type="button" class="btn btn-outline-danger btn-sm btn-delete-fw-rule" data-serverid="${serverId}" data-ruleid="${rule.id}">Delete</button></td>
|
|
</tr>`;
|
|
tbody.append(row);
|
|
});
|
|
}
|
|
|
|
function loadFirewallRules(serverId) {
|
|
$.ajax({
|
|
cache: false,
|
|
method: 'GET',
|
|
url: '{{.basePath}}/servers/' + serverId + '/firewall/rules',
|
|
dataType: 'json',
|
|
success: function (rules) { renderFirewallRules(serverId, rules); },
|
|
error: function (jqXHR) {
|
|
const responseJson = jQuery.parseJSON(jqXHR.responseText);
|
|
toastr.error(responseJson['message']);
|
|
}
|
|
});
|
|
}
|
|
|
|
// Firewall button: rendered dynamically, use event delegation
|
|
$(document).ready(function () {
|
|
$('#servers-list').on('click', '.btn-firewall-preview', function () {
|
|
const serverId = $(this).data('serverid');
|
|
const serverName = $(this).data('servername');
|
|
$("#_fw_server_id").val(serverId);
|
|
$("#_fw_server_label").text(serverName + " (" + serverId + ")");
|
|
$("#modal_firewall").modal('show');
|
|
loadFirewallRules(serverId);
|
|
refreshFirewallPreview(serverId);
|
|
});
|
|
|
|
$("#frm_firewall_rule").on('submit', function (e) {
|
|
e.preventDefault();
|
|
const serverId = $("#_fw_server_id").val();
|
|
const data = {
|
|
direction: $("#_fw_direction").val(),
|
|
protocol: $("#_fw_protocol").val(),
|
|
port: $("#_fw_port").val(),
|
|
source: $("#_fw_source").val(),
|
|
action: $("#_fw_action").val(),
|
|
comment: $("#_fw_comment").val(),
|
|
enabled: true
|
|
};
|
|
$.ajax({
|
|
cache: false,
|
|
method: 'POST',
|
|
url: '{{.basePath}}/servers/' + serverId + '/firewall/rules',
|
|
dataType: 'json',
|
|
contentType: "application/json",
|
|
data: JSON.stringify(data),
|
|
success: function () {
|
|
toastr.success("Rule added");
|
|
$("#frm_firewall_rule")[0].reset();
|
|
loadFirewallRules(serverId);
|
|
refreshFirewallPreview(serverId);
|
|
},
|
|
error: function (jqXHR) {
|
|
const responseJson = jQuery.parseJSON(jqXHR.responseText);
|
|
toastr.error(responseJson['message']);
|
|
}
|
|
});
|
|
});
|
|
|
|
$("#_fw_rules_tbody").on('click', '.btn-delete-fw-rule', function () {
|
|
const serverId = $(this).data('serverid');
|
|
const ruleId = $(this).data('ruleid');
|
|
if (!confirm("Delete this firewall rule?")) return;
|
|
$.ajax({
|
|
cache: false,
|
|
method: 'POST',
|
|
url: '{{.basePath}}/servers/' + serverId + '/firewall/rules/' + ruleId + '/delete',
|
|
dataType: 'json',
|
|
contentType: "application/json",
|
|
success: function () {
|
|
toastr.success("Rule deleted");
|
|
loadFirewallRules(serverId);
|
|
refreshFirewallPreview(serverId);
|
|
},
|
|
error: function (jqXHR) {
|
|
const responseJson = jQuery.parseJSON(jqXHR.responseText);
|
|
toastr.error(responseJson['message']);
|
|
}
|
|
});
|
|
});
|
|
|
|
$("#btn_apply_firewall").click(function () {
|
|
const serverId = $("#_fw_server_id").val();
|
|
if (!confirm("Apply this ruleset to the live firewall now?\n" +
|
|
"This runs 'nft -f' on the server, scoped to this server's own nftables table only.")) {
|
|
return;
|
|
}
|
|
$.ajax({
|
|
cache: false,
|
|
method: 'POST',
|
|
url: '{{.basePath}}/servers/' + serverId + '/firewall/apply',
|
|
dataType: 'json',
|
|
contentType: "application/json",
|
|
success: function (data) {
|
|
toastr.success(data.message);
|
|
if (data.output) { $("#_firewall_preview_text").text(data.output); }
|
|
},
|
|
error: function (jqXHR) {
|
|
const responseJson = jQuery.parseJSON(jqXHR.responseText);
|
|
toastr.error(responseJson['message'] || "Failed to apply firewall rules");
|
|
if (responseJson['output']) { $("#_firewall_preview_text").text(responseJson['output']); }
|
|
}
|
|
});
|
|
});
|
|
});
|
|
|
|
// Delete server button: rendered dynamically, use event delegation
|
|
$(document).ready(function () {
|
|
$('#servers-list').on('click', '.btn-delete-server', function () {
|
|
const serverId = $(this).data('serverid');
|
|
const serverName = $(this).data('servername');
|
|
if (!confirm("Are you sure you want to delete server \"" + serverName + "\" (" + serverId + ")?\n" +
|
|
"This only works if no clients are assigned to it anymore.")) {
|
|
return;
|
|
}
|
|
$.ajax({
|
|
cache: false,
|
|
method: 'POST',
|
|
url: '{{.basePath}}/servers/' + serverId + '/delete',
|
|
dataType: 'json',
|
|
contentType: "application/json",
|
|
success: function (data) {
|
|
toastr.success("Server deleted successfully");
|
|
$('#servers-list').empty();
|
|
populateServersList();
|
|
},
|
|
error: function (jqXHR, exception) {
|
|
const responseJson = jQuery.parseJSON(jqXHR.responseText);
|
|
toastr.error(responseJson['message']);
|
|
}
|
|
});
|
|
});
|
|
});
|
|
|
|
$(document).ready(function () {
|
|
$.validator.setDefaults({
|
|
submitHandler: function (form) {
|
|
submitNewServer();
|
|
}
|
|
});
|
|
$("#frm_new_server").validate({
|
|
rules: {
|
|
_server_id: {
|
|
required: true
|
|
},
|
|
_server_name: {
|
|
required: true
|
|
},
|
|
_server_interface: {
|
|
required: true
|
|
},
|
|
_server_listen_port: {
|
|
required: true,
|
|
digits: true,
|
|
range: [1, 65535]
|
|
}
|
|
},
|
|
messages: {
|
|
_server_id: {
|
|
required: "Please enter a server ID"
|
|
},
|
|
_server_name: {
|
|
required: "Please enter a server name"
|
|
},
|
|
_server_interface: {
|
|
required: "Please enter an interface name"
|
|
},
|
|
_server_listen_port: {
|
|
required: "Please enter a port",
|
|
digits: "Port must be an integer",
|
|
range: "Port must be in range 1..65535"
|
|
}
|
|
},
|
|
errorElement: 'span',
|
|
errorPlacement: function (error, element) {
|
|
error.addClass('invalid-feedback');
|
|
element.closest('.form-group').append(error);
|
|
},
|
|
highlight: function (element, errorClass, validClass) {
|
|
$(element).addClass('is-invalid');
|
|
},
|
|
unhighlight: function (element, errorClass, validClass) {
|
|
$(element).removeClass('is-invalid');
|
|
}
|
|
});
|
|
});
|
|
</script>
|
|
{{end}}
|