From c92c17c46511464f74a73d020378ba4b02e32931 Mon Sep 17 00:00:00 2001 From: Master3395 Date: Tue, 23 Sep 2025 21:23:54 +0200 Subject: [PATCH] Add IP blocking functionality to homePage.html: Implemented a script to block IP addresses with confirmation prompts, CSRF token handling, and user notifications for success or failure. Enhanced user experience with loading states and automatic page refresh after blocking an IP. --- .../templates/baseTemplate/homePage.html | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/baseTemplate/templates/baseTemplate/homePage.html b/baseTemplate/templates/baseTemplate/homePage.html index 53d11204c..8ca4f22c1 100644 --- a/baseTemplate/templates/baseTemplate/homePage.html +++ b/baseTemplate/templates/baseTemplate/homePage.html @@ -833,5 +833,85 @@ }, 100); } } + + // IP Blocking functionality + function blockIPAddress(ipAddress) { + if (!confirm(`Are you sure you want to block IP address ${ipAddress}?`)) { + return; + } + + // Set loading state + if (typeof angular !== 'undefined' && angular.element(document.body).scope()) { + var scope = angular.element(document.body).scope(); + scope.$apply(function() { + scope.blockingIP = ipAddress; + }); + } + + const formData = { + 'csrfmiddlewaretoken': getCookie('csrftoken'), + 'ip_address': ipAddress, + 'reason': 'Brute force attack detected from dashboard' + }; + + $.post('/base/blockIPAddress', formData, function(data) { + if (data.status === 1) { + showNotification('success', data.message); + // Refresh the page to update the blocked IPs list + setTimeout(() => { + location.reload(); + }, 1000); + } else { + showNotification('error', data.message); + } + }).fail(function() { + showNotification('error', 'Failed to block IP address. Please try again.'); + }).always(function() { + // Clear loading state + if (typeof angular !== 'undefined' && angular.element(document.body).scope()) { + var scope = angular.element(document.body).scope(); + scope.$apply(function() { + scope.blockingIP = null; + }); + } + }); + } + + // Helper function to get CSRF token + function getCookie(name) { + var cookieValue = null; + if (document.cookie && document.cookie !== '') { + var cookies = document.cookie.split(';'); + for (var i = 0; i < cookies.length; i++) { + var cookie = cookies[i].trim(); + if (cookie.substring(0, name.length + 1) === (name + '=')) { + cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); + break; + } + } + } + return cookieValue; + } + + // Notification function + function showNotification(type, message) { + const alertClass = type === 'success' ? 'alert-success' : 'alert-danger'; + const icon = type === 'success' ? 'fa-check-circle' : 'fa-exclamation-circle'; + + const notification = ` + + `; + + $('body').append(notification); + + setTimeout(() => { + $('.alert').fadeOut(); + }, 5000); + } {% endblock %} \ No newline at end of file