mirror of
https://github.com/eduardogsilva/wireguard_webadmin.git
synced 2025-06-28 01:07:03 +00:00
peer Throughput display
This commit is contained in:
parent
8b8566cdb1
commit
ec65a5c0d2
@ -173,7 +173,6 @@
|
|||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
function openPeerModal(uuid) {
|
function openPeerModal(uuid) {
|
||||||
$(".qr-code-content").hide();
|
$(".qr-code-content").hide();
|
||||||
@ -184,7 +183,7 @@
|
|||||||
var peerElem = document.querySelector('[data-uuid="' + uuid + '"]');
|
var peerElem = document.querySelector('[data-uuid="' + uuid + '"]');
|
||||||
if (peerElem) {
|
if (peerElem) {
|
||||||
var peerNameFromCard = peerElem.querySelector('h5').innerText;
|
var peerNameFromCard = peerElem.querySelector('h5').innerText;
|
||||||
var peerThroughput = peerElem.querySelector('[id^="peer-throughput-"]').innerText;
|
var peerThroughput = peerElem.querySelector('[id^="peer-throughput-"]').innerHTML;
|
||||||
var peerTransfer = peerElem.querySelector('[id^="peer-transfer-"]').innerText;
|
var peerTransfer = peerElem.querySelector('[id^="peer-transfer-"]').innerText;
|
||||||
var peerHandshake = peerElem.querySelector('[id^="peer-latest-handshake-"]').innerText;
|
var peerHandshake = peerElem.querySelector('[id^="peer-latest-handshake-"]').innerText;
|
||||||
var peerEndpoints = peerElem.querySelector('[id^="peer-endpoints-"]').innerText;
|
var peerEndpoints = peerElem.querySelector('[id^="peer-endpoints-"]').innerText;
|
||||||
@ -192,7 +191,7 @@
|
|||||||
|
|
||||||
// Update the modal fields with the card values
|
// Update the modal fields with the card values
|
||||||
$('#peerPreviewModalLabel').text(peerNameFromCard);
|
$('#peerPreviewModalLabel').text(peerNameFromCard);
|
||||||
$('#peerThroughput').text(peerThroughput);
|
$('#peerThroughput').html(peerThroughput);
|
||||||
$('#peerTransfer').text(peerTransfer);
|
$('#peerTransfer').text(peerTransfer);
|
||||||
$('#peerHandshake').text(peerHandshake);
|
$('#peerHandshake').text(peerHandshake);
|
||||||
$('#peerEndpoints').text(peerEndpoints);
|
$('#peerEndpoints').text(peerEndpoints);
|
||||||
@ -234,106 +233,195 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
document.addEventListener('DOMContentLoaded', function() {
|
var previousMeasurements = {};
|
||||||
const fetchWireguardStatus = async () => {
|
var toastShownThisCycle = false;
|
||||||
try {
|
|
||||||
const response = await fetch('/api/wireguard_status/');
|
|
||||||
let data = await response.json();
|
|
||||||
|
|
||||||
// if latest-handshakes is 0, use the stored value
|
const updateThroughput = (peerId, peerInfo) => {
|
||||||
for (const [interfaceName, peers] of Object.entries(data)) {
|
const throughputElement = document.getElementById(`peer-throughput-${peerId}`);
|
||||||
for (const [peerId, peerInfo] of Object.entries(peers)) {
|
const currentTime = Date.now() / 1000; // current timestamp in seconds
|
||||||
const peerElementId = `peer-stored-latest-handshake-${peerId}`;
|
let formattedThroughput = '';
|
||||||
const storedHandshakeElement = document.getElementById(peerElementId);
|
|
||||||
if (peerInfo['latest-handshakes'] === '0' && storedHandshakeElement) {
|
if (previousMeasurements[peerId]) {
|
||||||
peerInfo['latest-handshakes'] = storedHandshakeElement.textContent;
|
const prev = previousMeasurements[peerId];
|
||||||
}
|
const timeDiff = currentTime - prev.timestamp; // time difference in seconds
|
||||||
|
|
||||||
|
// For peer perspective: download corresponds to tx and upload to rx
|
||||||
|
let downloadDiff = peerInfo.transfer.tx - prev.transfer.tx;
|
||||||
|
let uploadDiff = peerInfo.transfer.rx - prev.transfer.rx;
|
||||||
|
|
||||||
|
// If counters have been reset (current < previous), show toast (only once per cycle)
|
||||||
|
if (downloadDiff < 0 || uploadDiff < 0) {
|
||||||
|
if (!toastShownThisCycle) {
|
||||||
|
$(document).Toasts('create', {
|
||||||
|
class: 'bg-info',
|
||||||
|
title: 'info',
|
||||||
|
body: 'Throughput discarded due to counter reset',
|
||||||
|
delay: 10000,
|
||||||
|
autohide: true
|
||||||
|
});
|
||||||
|
toastShownThisCycle = true;
|
||||||
|
}
|
||||||
|
downloadDiff = 0;
|
||||||
|
uploadDiff = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate throughput in bytes per second
|
||||||
|
const downloadThroughput = downloadDiff / timeDiff;
|
||||||
|
const uploadThroughput = uploadDiff / timeDiff;
|
||||||
|
|
||||||
|
// Format throughput values (using convertBytes function)
|
||||||
|
let downloadDisplay = convertBytes(downloadThroughput) + '/s';
|
||||||
|
let uploadDisplay = convertBytes(uploadThroughput) + '/s';
|
||||||
|
// Threshold: 1mb (1 megabit/s = 125000 bytes per second)
|
||||||
|
const threshold = 125000;
|
||||||
|
|
||||||
|
if (downloadThroughput > threshold) {
|
||||||
|
downloadDisplay = `<strong>${downloadDisplay}</strong>`;
|
||||||
|
}
|
||||||
|
if (uploadThroughput > threshold) {
|
||||||
|
uploadDisplay = `<strong>${uploadDisplay}</strong>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
formattedThroughput = `<i class="fas fa-arrow-down"></i> ${downloadDisplay}, <i class="fas fa-arrow-up"></i> ${uploadDisplay}`;
|
||||||
|
throughputElement.innerHTML = formattedThroughput;
|
||||||
|
} else {
|
||||||
|
// First cycle: no previous measurement available.
|
||||||
|
formattedThroughput = `<i class="fas fa-arrow-down"></i> -.- B/s, <i class="fas fa-arrow-up"></i> -.- B/s`;
|
||||||
|
throughputElement.innerHTML = formattedThroughput;
|
||||||
|
}
|
||||||
|
|
||||||
|
previousMeasurements[peerId] = {
|
||||||
|
timestamp: currentTime,
|
||||||
|
transfer: {
|
||||||
|
tx: peerInfo.transfer.tx,
|
||||||
|
rx: peerInfo.transfer.rx
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return formattedThroughput;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Convert bytes to human-readable format with abbreviated units
|
||||||
|
const convertBytes = (bytes) => {
|
||||||
|
if (bytes === 0) return '0 B';
|
||||||
|
const k = 1024;
|
||||||
|
const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
|
||||||
|
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||||
|
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
|
||||||
|
};
|
||||||
|
|
||||||
|
// Fetch Wireguard status and update UI
|
||||||
|
document.addEventListener('DOMContentLoaded', function() {
|
||||||
|
const fetchWireguardStatus = async () => {
|
||||||
|
try {
|
||||||
|
const response = await fetch('/api/wireguard_status/');
|
||||||
|
let data = await response.json();
|
||||||
|
|
||||||
|
// If latest-handshakes is 0, use the stored value
|
||||||
|
for (const [interfaceName, peers] of Object.entries(data)) {
|
||||||
|
for (const [peerId, peerInfo] of Object.entries(peers)) {
|
||||||
|
const peerElementId = `peer-stored-latest-handshake-${peerId}`;
|
||||||
|
const storedHandshakeElement = document.getElementById(peerElementId);
|
||||||
|
if (peerInfo['latest-handshakes'] === '0' && storedHandshakeElement) {
|
||||||
|
peerInfo['latest-handshakes'] = storedHandshakeElement.textContent;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
updateUI(data);
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error fetching Wireguard status:', error);
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
fetchWireguardStatus();
|
updateUI(data);
|
||||||
setInterval(fetchWireguardStatus, {{ current_instance.peer_list_refresh_interval }} * 1000);
|
} catch (error) {
|
||||||
});
|
console.error('Error fetching Wireguard status:', error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const updateUI = (data) => {
|
fetchWireguardStatus();
|
||||||
for (const [interfaceName, peers] of Object.entries(data)) {
|
setInterval(fetchWireguardStatus, {{ current_instance.peer_list_refresh_interval }} * 1000);
|
||||||
for (const [peerId, peerInfo] of Object.entries(peers)) {
|
});
|
||||||
const peerDiv = document.getElementById(`peer-${peerId}`);
|
|
||||||
if (peerDiv) {
|
const updateUI = (data) => {
|
||||||
updatePeerInfo(peerDiv, peerId, peerInfo);
|
// Reset the toast flag for this update cycle
|
||||||
updateCalloutClass(peerDiv, peerInfo['latest-handshakes']);
|
toastShownThisCycle = false;
|
||||||
|
|
||||||
|
for (const [interfaceName, peers] of Object.entries(data)) {
|
||||||
|
for (const [peerId, peerInfo] of Object.entries(peers)) {
|
||||||
|
const peerDiv = document.getElementById(`peer-${peerId}`);
|
||||||
|
if (peerDiv) {
|
||||||
|
updatePeerInfo(peerDiv, peerId, peerInfo);
|
||||||
|
updateCalloutClass(peerDiv, peerInfo['latest-handshakes']);
|
||||||
|
// Calculate throughput and update the card
|
||||||
|
const throughputHTML = updateThroughput(peerId, peerInfo);
|
||||||
|
|
||||||
|
// If the modal is active for this peer, update its fields as well
|
||||||
|
const peerUuid = peerDiv.getAttribute("data-uuid");
|
||||||
|
if ($('#peerPreviewModal').is(':visible') && $('#peerPreviewModal').data('peer-uuid') === peerUuid) {
|
||||||
|
$('#peerThroughput').html(throughputHTML);
|
||||||
|
$('#peerTransfer').text(`${convertBytes(peerInfo.transfer.tx)} TX, ${convertBytes(peerInfo.transfer.rx)} RX`);
|
||||||
|
$('#peerHandshake').text(
|
||||||
|
peerInfo['latest-handshakes'] !== '0'
|
||||||
|
? new Date(parseInt(peerInfo['latest-handshakes']) * 1000).toLocaleString()
|
||||||
|
: '0'
|
||||||
|
);
|
||||||
|
$('#peerEndpoints').text(peerInfo.endpoints);
|
||||||
|
const allowedIpsModalElement = document.getElementById('peerAllowedIPs');
|
||||||
|
checkAllowedIps(allowedIpsModalElement, peerInfo['allowed-ips']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const updatePeerInfo = (peerDiv, peerId, peerInfo) => {
|
const updatePeerInfo = (peerDiv, peerId, peerInfo) => {
|
||||||
const escapedPeerId = peerId.replace(/([!"#$%&'()*+,.\/:;<=>?@[\]^`{|}~])/g, '\\$1');
|
const escapedPeerId = peerId.replace(/([!"#$%&'()*+,.\/:;<=>?@[\]^`{|}~])/g, '\\$1');
|
||||||
const transfer = peerDiv.querySelector(`#peer-transfer-${escapedPeerId}`);
|
const transfer = peerDiv.querySelector(`#peer-transfer-${escapedPeerId}`);
|
||||||
const latestHandshake = peerDiv.querySelector(`#peer-latest-handshake-${escapedPeerId}`);
|
const latestHandshake = peerDiv.querySelector(`#peer-latest-handshake-${escapedPeerId}`);
|
||||||
const endpoints = peerDiv.querySelector(`#peer-endpoints-${escapedPeerId}`);
|
const endpoints = peerDiv.querySelector(`#peer-endpoints-${escapedPeerId}`);
|
||||||
const allowedIps = peerDiv.querySelector(`#peer-allowed-ips-${escapedPeerId}`);
|
const allowedIps = peerDiv.querySelector(`#peer-allowed-ips-${escapedPeerId}`);
|
||||||
|
|
||||||
transfer.textContent = `${convertBytes(peerInfo.transfer.tx)} TX, ${convertBytes(peerInfo.transfer.rx)} RX`;
|
transfer.textContent = `${convertBytes(peerInfo.transfer.tx)} TX, ${convertBytes(peerInfo.transfer.rx)} RX`;
|
||||||
latestHandshake.textContent = `${peerInfo['latest-handshakes'] !== '0' ? new Date(parseInt(peerInfo['latest-handshakes']) * 1000).toLocaleString() : '0'}`;
|
latestHandshake.textContent = `${peerInfo['latest-handshakes'] !== '0' ? new Date(parseInt(peerInfo['latest-handshakes']) * 1000).toLocaleString() : '0'}`;
|
||||||
endpoints.textContent = `${peerInfo.endpoints}`;
|
endpoints.textContent = `${peerInfo.endpoints}`;
|
||||||
checkAllowedIps(allowedIps, peerInfo['allowed-ips']);
|
checkAllowedIps(allowedIps, peerInfo['allowed-ips']);
|
||||||
};
|
};
|
||||||
|
|
||||||
const convertBytes = (bytes) => {
|
const checkAllowedIps = (allowedIpsElement, allowedIpsApiResponse) => {
|
||||||
if (bytes === 0) return '0 Bytes';
|
const apiIps = allowedIpsApiResponse[0].split(' ');
|
||||||
const k = 1024;
|
const htmlIpsText = allowedIpsElement.textContent.trim();
|
||||||
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
const htmlIpsArray = htmlIpsText.match(/\b(?:\d{1,3}\.){3}\d{1,3}\/\d{1,2}\b/g) || [];
|
||||||
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
||||||
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
|
|
||||||
};
|
|
||||||
|
|
||||||
const checkAllowedIps = (allowedIpsElement, allowedIpsApiResponse) => {
|
allowedIpsElement.innerHTML = '';
|
||||||
const apiIps = allowedIpsApiResponse[0].split(' ');
|
htmlIpsArray.forEach((ip, index, array) => {
|
||||||
const htmlIpsText = allowedIpsElement.textContent.trim();
|
const ipSpan = document.createElement('span');
|
||||||
const htmlIpsArray = htmlIpsText.match(/\b(?:\d{1,3}\.){3}\d{1,3}\/\d{1,2}\b/g);
|
ipSpan.textContent = ip;
|
||||||
|
allowedIpsElement.appendChild(ipSpan);
|
||||||
|
|
||||||
allowedIpsElement.innerHTML = '';
|
if (!apiIps.includes(ip)) {
|
||||||
htmlIpsArray.forEach((ip, index, array) => {
|
ipSpan.style.color = 'red';
|
||||||
const ipSpan = document.createElement('span');
|
ipSpan.style.textDecoration = 'underline';
|
||||||
ipSpan.textContent = ip;
|
ipSpan.title = 'This address does not appear in the wg show command output, likely indicating that another peer has an IP overlapping this network or that the configuration file is outdated.';
|
||||||
allowedIpsElement.appendChild(ipSpan);
|
|
||||||
|
|
||||||
if (!apiIps.includes(ip)) {
|
|
||||||
ipSpan.style.color = 'red';
|
|
||||||
ipSpan.style.textDecoration = 'underline';
|
|
||||||
ipSpan.title = 'This address does not appear in the wg show command output, likely indicating that another peer has an IP overlapping this network or that the configuration file is outdated.';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (index < array.length - 1) {
|
|
||||||
allowedIpsElement.appendChild(document.createTextNode(', '));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const updateCalloutClass = (peerDiv, latestHandshake) => {
|
|
||||||
const calloutDiv = peerDiv.querySelector('.callout');
|
|
||||||
calloutDiv.classList.remove('callout-success', 'callout-info', 'callout-warning', 'callout-danger');
|
|
||||||
const handshakeAge = Date.now() / 1000 - parseInt(latestHandshake);
|
|
||||||
|
|
||||||
if (latestHandshake === '0') {
|
|
||||||
calloutDiv.classList.add('callout-danger');
|
|
||||||
} else if (handshakeAge < 600) {
|
|
||||||
calloutDiv.classList.add('callout-success');
|
|
||||||
} else if (handshakeAge < 1800) {
|
|
||||||
calloutDiv.classList.add('callout-info');
|
|
||||||
} else if (handshakeAge < 21600) {
|
|
||||||
calloutDiv.classList.add('callout-warning');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
calloutDiv.style.transition = 'all 5s';
|
if (index < array.length - 1) {
|
||||||
};
|
allowedIpsElement.appendChild(document.createTextNode(', '));
|
||||||
</script>
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const updateCalloutClass = (peerDiv, latestHandshake) => {
|
||||||
|
const calloutDiv = peerDiv.querySelector('.callout');
|
||||||
|
calloutDiv.classList.remove('callout-success', 'callout-info', 'callout-warning', 'callout-danger');
|
||||||
|
const handshakeAge = Date.now() / 1000 - parseInt(latestHandshake);
|
||||||
|
|
||||||
|
if (latestHandshake === '0') {
|
||||||
|
calloutDiv.classList.add('callout-danger');
|
||||||
|
} else if (handshakeAge < 600) {
|
||||||
|
calloutDiv.classList.add('callout-success');
|
||||||
|
} else if (handshakeAge < 1800) {
|
||||||
|
calloutDiv.classList.add('callout-info');
|
||||||
|
} else if (handshakeAge < 21600) {
|
||||||
|
calloutDiv.classList.add('callout-warning');
|
||||||
|
}
|
||||||
|
calloutDiv.style.transition = 'all 5s';
|
||||||
|
};
|
||||||
|
</script>
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
Loading…
x
Reference in New Issue
Block a user