mirror of
https://github.com/swissmakers/fail2ban-ui.git
synced 2026-04-11 13:47:05 +02:00
Reimplement Logpath Tester with fail2ban variable resolution and real-path joining
This commit is contained in:
@@ -860,6 +860,7 @@ func equalStringSlices(a, b []string) bool {
|
||||
}
|
||||
|
||||
// TestLogpathHandler tests a logpath and returns matching files
|
||||
// Resolves Fail2Ban variables before testing
|
||||
func TestLogpathHandler(c *gin.Context) {
|
||||
config.DebugLog("----------------------------")
|
||||
config.DebugLog("TestLogpathHandler called (handlers.go)") // entry point
|
||||
@@ -878,22 +879,28 @@ func TestLogpathHandler(c *gin.Context) {
|
||||
}
|
||||
|
||||
// Extract logpath from jail config
|
||||
logpath := fail2ban.ExtractLogpathFromJailConfig(jailCfg)
|
||||
if logpath == "" {
|
||||
c.JSON(http.StatusOK, gin.H{"files": []string{}, "message": "No logpath configured for this jail"})
|
||||
originalLogpath := fail2ban.ExtractLogpathFromJailConfig(jailCfg)
|
||||
if originalLogpath == "" {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"original_logpath": "",
|
||||
"resolved_logpath": "",
|
||||
"files": []string{},
|
||||
"message": "No logpath configured for this jail",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Test the logpath
|
||||
files, err := conn.TestLogpath(c.Request.Context(), logpath)
|
||||
// Test the logpath with variable resolution
|
||||
originalPath, resolvedPath, files, err := conn.TestLogpathWithResolution(c.Request.Context(), originalLogpath)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to test logpath: " + err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"logpath": logpath,
|
||||
"files": files,
|
||||
"original_logpath": originalPath,
|
||||
"resolved_logpath": resolvedPath,
|
||||
"files": files,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -152,23 +152,54 @@ function testLogpath() {
|
||||
if (data.error) {
|
||||
resultsDiv.textContent = 'Error: ' + data.error;
|
||||
resultsDiv.classList.add('text-red-600');
|
||||
// Auto-scroll to results
|
||||
setTimeout(function() {
|
||||
resultsDiv.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
|
||||
}, 100);
|
||||
return;
|
||||
}
|
||||
|
||||
var originalLogpath = data.original_logpath || '';
|
||||
var resolvedLogpath = data.resolved_logpath || '';
|
||||
var files = data.files || [];
|
||||
|
||||
// Build output message
|
||||
var output = '';
|
||||
|
||||
// Show resolved logpath if different from original
|
||||
if (resolvedLogpath && resolvedLogpath !== originalLogpath) {
|
||||
output += 'Resolved logpath: ' + resolvedLogpath + '\n\n';
|
||||
} else if (resolvedLogpath) {
|
||||
output += 'Logpath: ' + resolvedLogpath + '\n\n';
|
||||
} else if (originalLogpath) {
|
||||
output += 'Logpath: ' + originalLogpath + '\n\n';
|
||||
}
|
||||
|
||||
// Show files found
|
||||
if (files.length === 0) {
|
||||
resultsDiv.textContent = 'No files found for logpath: ' + (data.logpath || 'N/A');
|
||||
output += 'No files found matching the logpath pattern.';
|
||||
resultsDiv.classList.remove('text-red-600');
|
||||
resultsDiv.classList.add('text-yellow-600');
|
||||
} else {
|
||||
resultsDiv.textContent = 'Found ' + files.length + ' file(s):\n' + files.join('\n');
|
||||
output += 'Found ' + files.length + ' file(s):\n' + files.join('\n');
|
||||
resultsDiv.classList.remove('text-red-600', 'text-yellow-600');
|
||||
}
|
||||
|
||||
resultsDiv.textContent = output;
|
||||
|
||||
// Auto-scroll to results
|
||||
setTimeout(function() {
|
||||
resultsDiv.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
|
||||
}, 100);
|
||||
})
|
||||
.catch(function(err) {
|
||||
showLoading(false);
|
||||
resultsDiv.textContent = 'Error: ' + err;
|
||||
resultsDiv.classList.add('text-red-600');
|
||||
// Auto-scroll to results
|
||||
setTimeout(function() {
|
||||
resultsDiv.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
|
||||
}, 100);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user