2024-12-05 14:00:58 +01:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2024-12-05 15:58:55 +01:00
|
|
|
"mime/multipart"
|
2024-12-05 14:00:58 +01:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CommandStat struct {
|
|
|
|
Command string
|
|
|
|
Count int
|
|
|
|
}
|
|
|
|
|
2024-12-05 19:26:18 +01:00
|
|
|
func ProcessHistory(file multipart.File) (int, int, map[string]int, map[string]int, map[string]int, map[string]int) {
|
2024-12-05 14:00:58 +01:00
|
|
|
commandCounts := make(map[string]int)
|
2024-12-05 19:26:18 +01:00
|
|
|
totalCommands := 0
|
2024-12-05 14:00:58 +01:00
|
|
|
categories := make(map[string]int)
|
|
|
|
|
|
|
|
pipeRedirectionCounts := make(map[string]int)
|
|
|
|
commonPatterns := make(map[string]int)
|
|
|
|
re := regexp.MustCompile(`\S+\.(log|txt|conf|json)`)
|
|
|
|
|
2024-12-05 15:58:55 +01:00
|
|
|
if seeker, ok := file.(multipart.File); ok {
|
|
|
|
seeker.Seek(0, 0)
|
2024-12-05 14:00:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
|
|
|
command := scanner.Text()
|
|
|
|
if command == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-12-05 19:26:18 +01:00
|
|
|
totalCommands++
|
2024-12-05 14:00:58 +01:00
|
|
|
commandCounts[command]++
|
|
|
|
|
|
|
|
if strings.Contains(command, "git") {
|
|
|
|
categories["Git"]++
|
|
|
|
} else if strings.Contains(command, "docker") {
|
|
|
|
categories["Docker"]++
|
|
|
|
} else if strings.Contains(command, "ssh") {
|
|
|
|
categories["SSH"]++
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(command, "|") {
|
|
|
|
pipeRedirectionCounts["pipe"]++
|
|
|
|
}
|
|
|
|
if strings.Contains(command, ">") || strings.Contains(command, "<") || strings.Contains(command, ">>") {
|
|
|
|
pipeRedirectionCounts["redirection"]++
|
|
|
|
}
|
|
|
|
|
|
|
|
matches := re.FindAllString(command, -1)
|
|
|
|
for _, match := range matches {
|
|
|
|
commonPatterns[match]++
|
|
|
|
}
|
2024-12-05 19:26:18 +01:00
|
|
|
|
2024-12-05 14:00:58 +01:00
|
|
|
}
|
|
|
|
|
2024-12-05 19:26:18 +01:00
|
|
|
return totalCommands, len(commandCounts), commandCounts, categories, pipeRedirectionCounts, commonPatterns
|
2024-12-05 14:00:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func GenerateStats(commandCounts map[string]int, categories map[string]int, commonPatterns map[string]int) map[string]interface{} {
|
|
|
|
stats := map[string]interface{}{
|
|
|
|
"topCommands": commandCounts,
|
|
|
|
"categories": categories,
|
|
|
|
"commonPatterns": commonPatterns,
|
|
|
|
}
|
|
|
|
return stats
|
|
|
|
}
|