Skip to content

Commit

Permalink
At startup, determine if tshark supports colorized packets.
Browse files Browse the repository at this point in the history
The result is cached to avoid the penalty of doing this check every
startup.
  • Loading branch information
gcla committed Nov 16, 2019
1 parent adab295 commit ff449d1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 16 deletions.
52 changes: 37 additions & 15 deletions cmd/termshark/termshark.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,26 @@ func cmain() int {
}()
}

for _, dir := range []string{termshark.CacheDir(), termshark.PcapDir()} {
if _, err = os.Stat(dir); os.IsNotExist(err) {
err = os.Mkdir(dir, 0777)
if err != nil {
fmt.Fprintf(os.Stderr, "Unexpected error making dir %s: %v", dir, err)
return 1
}
}
}

// Write this pcap out here because the color validation code later depends on empty.pcap
emptyPcap := termshark.CacheFile("empty.pcap")
if _, err := os.Stat(emptyPcap); os.IsNotExist(err) {
err = termshark.WriteEmptyPcap(emptyPcap)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not create dummy pcap %s: %v", emptyPcap, err)
return 1
}
}

tsharkBin, kverr := termshark.TSharkPath()
if kverr != nil {
fmt.Fprintf(os.Stderr, kverr.KeyVals["msg"].(string))
Expand Down Expand Up @@ -480,22 +500,19 @@ func cmain() int {
// with the tshark binary we're using.
termshark.SetConf("main.last-used-tshark", tsharkBin)

for _, dir := range []string{termshark.CacheDir(), termshark.PcapDir()} {
if _, err = os.Stat(dir); os.IsNotExist(err) {
err = os.Mkdir(dir, 0777)
if err != nil {
fmt.Fprintf(os.Stderr, "Unexpected error making dir %s: %v", dir, err)
return 1
}
}
}
// Determine if the current binary supports color. Tshark will fail with an error if it's too old
// and you supply the --color flag. Assume true, and check if our current binary is not in the
// validate list.
binSupportsColor := true
colorTsharks := termshark.ConfStrings("main.color-tsharks")

emptyPcap := termshark.CacheFile("empty.pcap")
if _, err := os.Stat(emptyPcap); os.IsNotExist(err) {
err = termshark.WriteEmptyPcap(emptyPcap)
if !termshark.StringInSlice(tsharkBin, colorTsharks) {
binSupportsColor, err = termshark.TSharkSupportsColor(tsharkBin)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not create dummy pcap %s: %v", emptyPcap, err)
return 1
binSupportsColor = false
} else {
colorTsharks = append(colorTsharks, tsharkBin)
termshark.SetConf("main.color-tsharks", colorTsharks)
}
}

Expand Down Expand Up @@ -600,6 +617,11 @@ func cmain() int {
pdmlArgs := termshark.ConfStringSlice("main.pdml-args", []string{})
psmlArgs := termshark.ConfStringSlice("main.psml-args", []string{})
tsharkArgs := termshark.ConfStringSlice("main.tshark-args", []string{})
enableColor := termshark.ConfBool("main.packet-colors", true)
if enableColor && !binSupportsColor {
log.Warnf("Packet coloring is enabled, but %s does not support --color", tsharkBin)
enableColor = false
}
cacheSize := termshark.ConfInt("main.pcap-cache-size", 64)
bundleSize := termshark.ConfInt("main.pcap-bundle-size", 1000)
if bundleSize <= 0 {
Expand All @@ -608,7 +630,7 @@ func cmain() int {
bundleSize = maxBundleSize
}
ui.PcapScheduler = pcap.NewScheduler(
pcap.MakeCommands(opts.DecodeAs, tsharkArgs, pdmlArgs, psmlArgs),
pcap.MakeCommands(opts.DecodeAs, tsharkArgs, pdmlArgs, psmlArgs, enableColor),
pcap.Options{
CacheSize: cacheSize,
PacketsPerLoad: bundleSize,
Expand Down
10 changes: 9 additions & 1 deletion pcap/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,16 @@ type Commands struct {
Args []string
PdmlArgs []string
PsmlArgs []string
Color bool
}

func MakeCommands(decodeAs []string, args []string, pdml []string, psml []string) Commands {
func MakeCommands(decodeAs []string, args []string, pdml []string, psml []string, color bool) Commands {
return Commands{
DecodeAs: decodeAs,
Args: args,
PdmlArgs: pdml,
PsmlArgs: psml,
Color: color,
}
}

Expand Down Expand Up @@ -166,6 +168,9 @@ func (c Commands) Psml(pcap interface{}, displayFilter string) IPcapCommand {
for _, arg := range c.DecodeAs {
args = append(args, "-d", arg)
}
if c.Color {
args = append(args, "--color")
}
args = append(args, c.PsmlArgs...)
args = append(args, c.Args...)

Expand All @@ -190,6 +195,9 @@ func (c Commands) Pcap(pcap string, displayFilter string) IPcapCommand {

func (c Commands) Pdml(pcap string, displayFilter string) IPcapCommand {
args := []string{"-T", "pdml", "-r", pcap}
if c.Color {
args = append(args, "--color")
}
if displayFilter != "" {
args = append(args, "-Y", displayFilter)
}
Expand Down

0 comments on commit ff449d1

Please sign in to comment.