Reimplement Logpath Tester with fail2ban variable resolution and real-path joining

This commit is contained in:
2025-12-05 23:21:08 +01:00
parent fe51f29d6b
commit 284ba26888
8 changed files with 652 additions and 10 deletions

View File

@@ -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,
})
}

View File

@@ -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);
});
}