diff --git a/baseTemplate/static/baseTemplate/custom-js/system-status.js b/baseTemplate/static/baseTemplate/custom-js/system-status.js index 10e10ace2..2eee2704f 100644 --- a/baseTemplate/static/baseTemplate/custom-js/system-status.js +++ b/baseTemplate/static/baseTemplate/custom-js/system-status.js @@ -1071,6 +1071,99 @@ app.controller('dashboardStatsController', function ($scope, $http, $timeout) { }); } }; + + // Ban IP from SSH Logs + $scope.banIPFromSSHLog = function(ipAddress) { + if (!ipAddress) { + new PNotify({ + title: 'Error', + text: 'No IP address provided', + type: 'error', + delay: 5000 + }); + return; + } + + if ($scope.blockingIP === ipAddress) { + return; // Already processing + } + + if ($scope.blockedIPs[ipAddress]) { + new PNotify({ + title: 'Info', + text: `IP address ${ipAddress} is already banned`, + type: 'info', + delay: 3000 + }); + return; + } + + $scope.blockingIP = ipAddress; + + // Use the Banned IPs system + var data = { + ip: ipAddress, + reason: 'Suspicious activity detected from SSH logs', + duration: 'permanent' + }; + + var config = { + headers: { + 'X-CSRFToken': getCookie('csrftoken') + } + }; + + $http.post('/firewall/addBannedIP', data, config).then(function (response) { + $scope.blockingIP = null; + if (response.data && response.data.status === 1) { + // Mark IP as blocked + $scope.blockedIPs[ipAddress] = true; + + // Show success notification + new PNotify({ + title: 'IP Address Banned', + text: `IP address ${ipAddress} has been permanently banned and added to the firewall. You can manage it in the Firewall > Banned IPs section.`, + type: 'success', + delay: 5000 + }); + + // Refresh SSH logs to update the UI + $scope.refreshSSHLogs(); + } else { + // Show error notification + var errorMsg = 'Failed to ban IP address'; + if (response.data && response.data.error_message) { + errorMsg = response.data.error_message; + } else if (response.data && response.data.error) { + errorMsg = response.data.error; + } + + new PNotify({ + title: 'Error', + text: errorMsg, + type: 'error', + delay: 5000 + }); + } + }, function (err) { + $scope.blockingIP = null; + var errorMessage = 'Failed to ban IP address'; + if (err.data && err.data.error_message) { + errorMessage = err.data.error_message; + } else if (err.data && err.data.error) { + errorMessage = err.data.error; + } else if (err.data && err.data.message) { + errorMessage = err.data.message; + } + + new PNotify({ + title: 'Error', + text: errorMessage, + type: 'error', + delay: 5000 + }); + }); + }; // Initial fetch $scope.refreshTopProcesses(); diff --git a/baseTemplate/templates/baseTemplate/homePage.html b/baseTemplate/templates/baseTemplate/homePage.html index c09e43619..6f4f8545a 100644 --- a/baseTemplate/templates/baseTemplate/homePage.html +++ b/baseTemplate/templates/baseTemplate/homePage.html @@ -972,22 +972,22 @@ Recommendation:

{$ alert.recommendation $}

- -
- Manage in Firewall - Blocked @@ -1015,12 +1015,43 @@ TIMESTAMP MESSAGE + IP ADDRESS + ACTIONS {$ log.timestamp $} {$ log.message $} + + + {$ log.ip_address $} + + - + + + + + + + Banned + + - + @@ -1292,31 +1323,67 @@ }); } - 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; - }); + $.ajax({ + url: '/base/blockIPAddress', + type: 'POST', + contentType: 'application/json', + headers: { + 'X-CSRFToken': getCookie('csrftoken') + }, + data: JSON.stringify({ + 'ip_address': ipAddress, + 'reason': 'Security alert detected from dashboard' + }), + success: function(data) { + // Handle both success and error responses + if (data.status === 1) { + showNotification('success', data.message || 'IP address blocked successfully'); + // Refresh the page to update the blocked IPs list + setTimeout(() => { + location.reload(); + }, 1000); + } else { + // Handle error response - check for both 'error' and 'error_message' fields + var errorMsg = data.error || data.error_message || data.message || 'Failed to block IP address'; + showNotification('error', errorMsg); + } + }, + error: function(xhr, status, error) { + // Handle network errors and parse JSON errors + console.error('Ban IP error:', xhr, status, error); + var errorMsg = 'Failed to block IP address. Please try again.'; + + // Log full response for debugging + console.log('Response status:', xhr.status); + console.log('Response text:', xhr.responseText); + + if (xhr.responseJSON) { + errorMsg = xhr.responseJSON.error || xhr.responseJSON.error_message || xhr.responseJSON.message || errorMsg; + console.log('Parsed error from JSON:', errorMsg); + } else if (xhr.responseText) { + try { + var errorData = JSON.parse(xhr.responseText); + errorMsg = errorData.error || errorData.error_message || errorData.message || errorMsg; + console.log('Parsed error from text:', errorMsg); + } catch(e) { + console.error('Failed to parse error response:', e); + // If parsing fails, try to extract error from response text + if (xhr.responseText.includes('error')) { + errorMsg = xhr.responseText.substring(0, 200); + } + } + } + + showNotification('error', errorMsg); + }, + complete: 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; + }); + } } }); } diff --git a/baseTemplate/templates/baseTemplate/index.html b/baseTemplate/templates/baseTemplate/index.html index e62ba6d4f..e7276f0fd 100644 --- a/baseTemplate/templates/baseTemplate/index.html +++ b/baseTemplate/templates/baseTemplate/index.html @@ -26,15 +26,15 @@ - - - + + + - + @@ -46,6 +46,10 @@ + + + +