Remove legacy default-server page, unify on multi-server registry
Deletes /wg-server (route, handlers, template) entirely and adds generic /servers/:id/interface and /servers/:id/keypair endpoints + UI in the All Servers page, so every server (including the default one) is managed through the same per-server registry. Drops the write-through dual-write hacks that kept the old single-server collection in sync - the registry is now the single source of truth. One-time legacy-install migration path is untouched. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5
parent
2212141ba3
commit
58db6839c3
@@ -114,12 +114,73 @@ All Servers
|
||||
<!-- /.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_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 -->
|
||||
{{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 : "";
|
||||
@@ -138,6 +199,10 @@ All Servers
|
||||
<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>
|
||||
<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>
|
||||
@@ -293,6 +358,118 @@ All Servers
|
||||
});
|
||||
});
|
||||
|
||||
// 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_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(),
|
||||
"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']);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
$(document).ready(function () {
|
||||
$.validator.setDefaults({
|
||||
submitHandler: function (form) {
|
||||
|
||||
Reference in New Issue
Block a user