From 486fd190d9d7568d4d7d6de5863b7e2abd161802 Mon Sep 17 00:00:00 2001 From: Owen Garrett Date: Fri, 8 Apr 2022 10:40:51 +0100 Subject: [PATCH] Add reserved name 'stdout' for file output, referring to stdout. Note - cannot use the more common '-' because of yaml syntax. Fix permissions on output file --- README.md | 6 +++--- contrib/config/receiver-stdout.yaml | 2 +- pkg/streamer/common.go | 10 +++++++--- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 38f89cb..4211784 100644 --- a/README.md +++ b/README.md @@ -64,8 +64,8 @@ tail -c +1 -f /tmp/dump_file | tcpdump -r - ``` ```bash -# Edit the configuration to write to /dev/stdout, and pipe output to tcpdump: -./packet-streamer receiver --config contrib/receiver-stdout.yaml | tcpdump -r - +# Edit the configuration to write to the special name 'stdout', and pipe output to tcpdump: +./packet-streamer receiver --config ./contrib/config/receiver-stdout.yaml | tcpdump -r - ``` ## Run a PacketStreamer sensor @@ -238,7 +238,7 @@ output: address: _ip-address_ port: _listen-port_ file: # required in 'receiver' mode - path: _filename_ + path: _filename_|stdout # 'stdout' is a reserved name. Receiver will write to stdout tls: # optional enable: _true_|_false_ certfile: _filename_ diff --git a/contrib/config/receiver-stdout.yaml b/contrib/config/receiver-stdout.yaml index 20145fc..6493c68 100644 --- a/contrib/config/receiver-stdout.yaml +++ b/contrib/config/receiver-stdout.yaml @@ -3,4 +3,4 @@ input: port: 8081 output: file: - path: /dev/stdout + path: stdout diff --git a/pkg/streamer/common.go b/pkg/streamer/common.go index a9c9ba0..e6df965 100644 --- a/pkg/streamer/common.go +++ b/pkg/streamer/common.go @@ -71,9 +71,13 @@ func InitOutput(config *config.Config, proto string) error { if config.Output.File != nil { var pcapBuffer bytes.Buffer pcapWriter := pcapgo.NewWriter(&pcapBuffer) - fileFd, err := os.OpenFile(config.Output.File.Path, os.O_CREATE|os.O_RDWR, 0666) - if err != nil { - return err + fileFd := os.Stdout + if config.Output.File.Path != "stdout" { + var err error + fileFd, err = os.OpenFile(config.Output.File.Path, os.O_CREATE|os.O_RDWR, 0644) + if err != nil { + return err + } } pcapWriter.WriteFileHeader(uint32(config.InputPacketLen), layers.LinkTypeEthernet) fileFd.Write(pcapBuffer.Bytes())