Skip to content

Commit

Permalink
feat: Print config as indented JSON on startup (#73)
Browse files Browse the repository at this point in the history
## Which problem is this PR solving?
Print the config as indented JSON during startup if debug flag is
enabled.

## Short description of the changes
- When creating a new config, if the debug flag is set, marshal the
config struct into json and print it

## How to verify that this has the expected result
If the debug flag is set on startup, the config is printed out
  • Loading branch information
MikeGoldsmith committed Aug 16, 2023
1 parent 28443ac commit b091a33
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 51 deletions.
87 changes: 47 additions & 40 deletions assemblers/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package assemblers

import (
"encoding/json"
"flag"
"time"
)
Expand Down Expand Up @@ -28,49 +29,55 @@ var tstype = flag.String("timestamp_type", "", "Type of timestamps to use")
var promisc = flag.Bool("promisc", true, "Set promiscuous mode")

type config struct {
maxcount int
statsevery int
lazy bool
nodefrag bool
checksum bool
nooptcheck bool
ignorefsmerr bool
allowmissinginit bool
verbose bool
debug bool
quiet bool

iface string
fname string
snaplen int
tstype string
promisc bool

closeTimeout time.Duration
timeout time.Duration
Maxcount int
Statsevery int
Lazy bool
Nodefrag bool
Checksum bool
Nooptcheck bool
Ignorefsmerr bool
Allowmissinginit bool
Verbose bool
Debug bool
Quiet bool
Interface string
FileName string
Snaplen int
TsType string
Promiscuous bool
CloseTimeout time.Duration
Timeout time.Duration
}

func NewConfig() *config {
return &config{
maxcount: *maxcount,
statsevery: *statsevery,
lazy: *lazy,
nodefrag: *nodefrag,
checksum: *checksum,
nooptcheck: *nooptcheck,
ignorefsmerr: *ignorefsmerr,
allowmissinginit: *allowmissinginit,
verbose: *verbose,
debug: *debug,
quiet: *quiet,

iface: *iface,
fname: *fname,
snaplen: *snaplen,
tstype: *tstype,
promisc: *promisc,
c := &config{
Maxcount: *maxcount,
Statsevery: *statsevery,
Lazy: *lazy,
Nodefrag: *nodefrag,
Checksum: *checksum,
Nooptcheck: *nooptcheck,
Ignorefsmerr: *ignorefsmerr,
Allowmissinginit: *allowmissinginit,
Verbose: *verbose,
Debug: *debug,
Quiet: *quiet,
Interface: *iface,
FileName: *fname,
Snaplen: *snaplen,
TsType: *tstype,
Promiscuous: *promisc,
CloseTimeout: closeTimeout,
Timeout: timeout,
}

closeTimeout: closeTimeout,
timeout: timeout,
if c.Debug {
b, err := json.MarshalIndent(c, "", " ")
if err != nil {
Debug("Failed to marshal agent config: %e", err)
} else {
Debug("Agent config: %s", string(b))
}
}
return c
}
21 changes: 10 additions & 11 deletions assemblers/tcp_assembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"flag"
"fmt"
"log"
"os"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -69,6 +68,7 @@ func NewTcpAssembler(config config, httpEvents chan HttpEvent) tcpAssembler {
} else if *quiet {
logLevel = -1
}

errorsMap = make(map[string]uint)
// Set up pcap packet capture
if *fname != "" {
Expand Down Expand Up @@ -122,7 +122,7 @@ func (h *tcpAssembler) Start() {
data := packet.Data()
bytes += int64(len(data))
// defrag the IPv4 packet if required
if !h.config.nodefrag {
if !h.config.Nodefrag {
ip4Layer := packet.Layer(layers.LayerTypeIPv4)
if ip4Layer == nil {
continue
Expand Down Expand Up @@ -153,7 +153,7 @@ func (h *tcpAssembler) Start() {
tcp := packet.Layer(layers.LayerTypeTCP)
if tcp != nil {
tcp := tcp.(*layers.TCP)
if h.config.checksum {
if h.config.Checksum {
err := tcp.SetNetworkLayerForChecksum(packet.NetworkLayer())
if err != nil {
log.Fatalf("Failed to set network layer for checksum: %s\n", err)
Expand All @@ -165,19 +165,18 @@ func (h *tcpAssembler) Start() {
stats.totalsz += len(tcp.Payload)
h.assembler.AssembleWithContext(packet.NetworkLayer().NetworkFlow(), tcp, &c)
}
if count%h.config.statsevery == 0 {
if count%h.config.Statsevery == 0 {
ref := packet.Metadata().CaptureInfo.Timestamp
flushed, closed := h.assembler.FlushWithOptions(reassembly.FlushOptions{T: ref.Add(-h.config.timeout), TC: ref.Add(-h.config.closeTimeout)})
// Debug("Forced flush: %d flushed, %d closed (%s)", flushed, closed, ref)
log.Printf("Forced flush: %d flushed, %d closed (%s)", flushed, closed, ref)
flushed, closed := h.assembler.FlushWithOptions(reassembly.FlushOptions{T: ref.Add(-h.config.Timeout), TC: ref.Add(-h.config.CloseTimeout)})
Debug("Forced flush: %d flushed, %d closed (%s)", flushed, closed, ref)
}

done := h.config.maxcount > 0 && count >= h.config.maxcount
if count%h.config.statsevery == 0 || done {
done := h.config.Maxcount > 0 && count >= h.config.Maxcount
if count%h.config.Statsevery == 0 || done {
errorsMapMutex.Lock()
errorMapLen := len(errorsMap)
errorsMapMutex.Unlock()
fmt.Fprintf(os.Stderr, "Processed %v packets (%v bytes) in %v (errors: %v, errTypes:%v)\n", count, bytes, time.Since(start), errors, errorMapLen)
Debug("Processed %v packets (%v bytes) in %v (errors: %v, errTypes:%v)\n", count, bytes, time.Since(start), errors, errorMapLen)
}
}
}
Expand All @@ -193,7 +192,7 @@ func (h *tcpAssembler) Stop() {
h.streamFactory.WaitGoRoutines()
// Debug("%s\n", h.assembler.Dump())
log.Printf("%s\n", h.assembler.Dump())
if !h.config.nodefrag {
if !h.config.Nodefrag {
fmt.Printf("IPdefrag:\t\t%d\n", stats.ipdefrag)
}
fmt.Printf("TCP stats:\n")
Expand Down

0 comments on commit b091a33

Please sign in to comment.