Skip to content

Commit

Permalink
refactor: remove pipe close monitoring, rely solely on Wireshark send…
Browse files Browse the repository at this point in the history
…ing signals

Signed-off-by: thediveo <thediveo@gmx.eu>
  • Loading branch information
thediveo committed Dec 26, 2023
1 parent 2a3c733 commit ef6e9dc
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 265 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ coverage.*
__debug_bin
/cshargextcap
/cshargextcap.exe
*.log
2 changes: 1 addition & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ builds:
- netgo
- osusergo
ldflags:
- 's -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}} -X main.builtBy=goreleaser'
- '-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}} -X main.builtBy=goreleaser'
hooks:
post:
- cmd: packaging/windows/post.sh {{ .Path }}
Expand Down
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ SHELL:=/bin/bash
GOGEN:=go generate .
BUILDTAGS:="osusergo,netgo"

.PHONY: help clean dist pkgsite report run vuln
.PHONY: help clean cshargextcap dist pkgsite report run vuln

help: ## list available targets
@# Derived from Gomega's Makefile (github.com/onsi/gomega) under MIT License
Expand All @@ -21,6 +21,12 @@ dist: ## build snapshot cshargextcap binary packages+archives in dist/ and test
@ls -lh dist/cshargextcap_*
@echo "🏁 done"

cshargextcap: ## build local extcap binary
go build \
-tags netgo,osusergo \
-ldflags "-s -w" \
./cmd/cshargextcap

clean: ## cleans up build and testing artefacts
rm -rf dist
find . -name __debug_bin -delete
Expand Down
39 changes: 15 additions & 24 deletions capturestream.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,13 @@
package cshargextcap

import (
"context"
"os"
"os/signal"
"runtime"
"strings"

"github.com/siemens/csharg"
"github.com/siemens/cshargextcap/cli/target"
"github.com/siemens/cshargextcap/cli/wireshark"
"github.com/siemens/cshargextcap/pipe"
"golang.org/x/sys/unix"

log "github.com/sirupsen/logrus"
Expand All @@ -34,11 +31,16 @@ import (
// data arriving from the underlying websocket connected to the capture service
// into the Wireshark pipe.
func Capture(st csharg.SharkTank) int {
if runtime.GOOS != "windows" {
defer func() {
signal.Reset(unix.SIGINT, unix.SIGTERM)
}()
}
// While Wireshark (and Tshark) currently send SIGTERM (and maybe SIGINT in
// some situations, maybe when using a control pipe which we don't) only on
// unix systems, there are developer discussions to in the future send
// events to a Windows extcap. As Go maps such events to its signal API
// we're already now unconditionally handling SIGINT and SIGTERM in the hope
// that we're future-proof.
defer func() {
signal.Reset(unix.SIGINT, unix.SIGTERM)
}()

// Open packet stream pipe to Wireshark to feed it jucy packets...
log.Debugf("opening fifo to Wireshark %s", wireshark.FifoPath)
fifo, err := os.OpenFile(wireshark.FifoPath, os.O_WRONLY, 0)
Expand Down Expand Up @@ -99,23 +101,12 @@ func Capture(st csharg.SharkTank) int {
}()
}
}()
if runtime.GOOS != "windows" {
signal.Notify(sigs, unix.SIGINT, unix.SIGTERM)
}
// As mentioned above, we unconditionally handle SIGINT and SIGTERM on all
// platforms. While this is currently not needed on Windows, some day it
// might become alive.
signal.Notify(sigs, unix.SIGINT, unix.SIGTERM)

ctx, cancel := context.WithCancel(context.Background())
defer func() {
cancel()
cs.Stop() // be overly careful
}()
// Always keep an eye on the fifo getting closed by Wireshark: we then need
// to stop the capture stream. This is necessary because the capture stream
// might be idle for long times and thus we would otherwise not notice that
// Wireshark has already stopped capturing.
go func() {
pipe.WaitTillBreak(ctx, fifo)
cs.Stop()
}()
defer cs.Stop() // be overly careful
// ...and finally wait for the packet capture to terminate (or getting
// ex-term-inated).
cs.Wait()
Expand Down
63 changes: 0 additions & 63 deletions pipe/checker_notwin.go

This file was deleted.

98 changes: 0 additions & 98 deletions pipe/checker_notwin_test.go

This file was deleted.

52 changes: 0 additions & 52 deletions pipe/checker_windows.go

This file was deleted.

4 changes: 0 additions & 4 deletions pipe/doc.go

This file was deleted.

22 changes: 0 additions & 22 deletions pipe/package_test.go

This file was deleted.

0 comments on commit ef6e9dc

Please sign in to comment.