Add notice about snipping process, if it happened

This commit is contained in:
maride 2019-12-04 00:10:42 +01:00
parent e9ec8ad46c
commit 7dfc4cb514
3 changed files with 87 additions and 41 deletions

View File

@ -3,6 +3,7 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"git.darknebu.la/maride/pancap/output"
"log" "log"
"math/rand" "math/rand"
"time" "time"
@ -14,6 +15,7 @@ func main() {
// register flags // register flags
registerFileFlags() registerFileFlags()
output.RegisterFlags()
flag.Parse() flag.Parse()
// Open the given PCAP // Open the given PCAP
@ -29,6 +31,9 @@ func main() {
// Mh, encountered some problems while analyzing file // Mh, encountered some problems while analyzing file
log.Fatalf("Error occurred while analyzing: %s", analyzeErr.Error()) log.Fatalf("Error occurred while analyzing: %s", analyzeErr.Error())
} }
// Finalize output
output.Finalize()
} }
// Prints a simple figlet-style ASCII art and a random quote // Prints a simple figlet-style ASCII art and a random quote

View File

@ -1,52 +1,24 @@
package output package output
import ( import (
"fmt" "flag"
"github.com/fatih/color" "github.com/fatih/color"
) )
const ( var (
MaxContentLines = 50 fullOutput *bool
) )
// Prints a block of information with the given headline func RegisterFlags() {
// If content is empty, printing the headline is omitted. fullOutput = flag.Bool("full-output", false, "Show full output instead of limiting submodule output")
// If the content is longer than MaxContentLines, content is cut.
func PrintBlock(headline string, content string) {
// Print a newline to add some space between blocks
fmt.Println("")
// Check if we need to print a headline
if len(content) > 0 {
// We have content, we can print the headline
headlineColor := color.New(color.FgRed, color.Bold)
headlineColor.Println(headline)
}
// Cut to MaxContentLines if required
cutCont := cutContent(content)
fmt.Print(cutCont)
} }
// Cut content after MaxContentLines lines // Called at the very end, before terminating pancap
func cutContent(content string) string { func Finalize() {
numNewlines := 0 // Check if we snipped, to add a notice how to show the whole block
if DidSnip {
// iterate over every character // We snipped - inform user about this process
for i, c := range content { printer := color.New(color.Bold, color.BgBlack)
// check if character is newline printer.Print("\nOutput is snipped at one or more positions. Add --full-output to avoid snipping.")
if c == '\n' {
// it is, count occurrence
numNewlines++
// Check if we already hit our limit yet
if numNewlines == MaxContentLines {
// Found nth newline, return content up to this position.
return content[:i+1] // +1 to include the last newline as well
}
}
} }
// We are done before reaching the cut limit; return the whole content
return content
} }

69
output/printer.go Normal file
View File

@ -0,0 +1,69 @@
package output
import (
"fmt"
"github.com/fatih/color"
)
const (
MaxContentLines = 50
SnipMark = "----- cut at 50 entries -----\n"
)
var (
DidSnip bool
)
// Prints a block of information with the given headline
// If content is empty, printing the headline is omitted.
// If the content is longer than MaxContentLines, content is cut.
func PrintBlock(headline string, content string) {
// Print a newline to add some space between blocks
fmt.Println("")
// Check if we need to print a headline
if len(content) > 0 {
// We have content, we can print the headline
headlineColor := color.New(color.FgRed, color.Bold)
headlineColor.Println(headline)
}
// Cut to MaxContentLines if required
if !(*fullOutput) {
// User states that they don't want to see the whole output - cut content.
content = cutContent(content)
}
// And print our content.
fmt.Print(content)
}
// Cut content after MaxContentLines lines
func cutContent(content string) string {
numNewlines := 0
// iterate over every character
for i, c := range content {
// check if character is newline
if c == '\n' {
// it is, count occurrence
numNewlines++
// Check if we already hit our limit yet
if numNewlines == MaxContentLines {
// Found nth newline, return content up to this position and add a notice about it.
DidSnip = true
return addSnipMark(content[:i])
}
}
}
// We are done before reaching the cut limit; return the whole content
return content
}
// Adds a notice about the snipping process
func addSnipMark(content string) string {
printer := color.New(color.Bold)
return content + "\n" + printer.Sprint(SnipMark)
}