Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into v24docs
Browse files Browse the repository at this point in the history
  • Loading branch information
gcla committed Jul 10, 2022
2 parents 1ac3107 + 79fad3d commit 5aa42e9
Show file tree
Hide file tree
Showing 107 changed files with 258 additions and 144 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
### Changed

- Now you can build and install termshark with one command: `go install github.com/gcla/termshark/v2/cmd/termshark`
- Added a summary of standard error to the error dialogs displayed when a tshark process fails to run
correctly (has a non-zero exit code).
- Fixed a race condition that caused extcap captures (e.g. randpkt) to sporadically fail.
- Dark-mode is now the default in the absence of a specific user-setting.
- Fixed a bug that caused mouse-clicks within the hex view to not function correctly if the viewport was not
Expand Down
29 changes: 17 additions & 12 deletions cmd/termshark/termshark.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ import (
"github.com/blang/semver"
"github.com/gcla/gowid"
"github.com/gcla/termshark/v2"
"github.com/gcla/termshark/v2/capinfo"
"github.com/gcla/termshark/v2/cli"
"github.com/gcla/termshark/v2/pkg/cli"
"github.com/gcla/termshark/v2/configs/profiles"
"github.com/gcla/termshark/v2/convs"
"github.com/gcla/termshark/v2/pcap"
"github.com/gcla/termshark/v2/shark"
"github.com/gcla/termshark/v2/streams"
"github.com/gcla/termshark/v2/system"
"github.com/gcla/termshark/v2/tty"
"github.com/gcla/termshark/v2/pkg/capinfo"
"github.com/gcla/termshark/v2/pkg/confwatcher"
"github.com/gcla/termshark/v2/pkg/convs"
"github.com/gcla/termshark/v2/pkg/fields"
"github.com/gcla/termshark/v2/pkg/pcap"
"github.com/gcla/termshark/v2/pkg/shark"
"github.com/gcla/termshark/v2/pkg/streams"
"github.com/gcla/termshark/v2/pkg/summary"
"github.com/gcla/termshark/v2/pkg/system"
"github.com/gcla/termshark/v2/pkg/tailfile"
"github.com/gcla/termshark/v2/pkg/tty"
"github.com/gcla/termshark/v2/ui"
"github.com/gcla/termshark/v2/widgets/filter"
"github.com/gcla/termshark/v2/widgets/wormhole"
Expand All @@ -55,13 +59,14 @@ func main() {
// stopped. Any exception is a bug.
var ensureGoroutinesStopWG sync.WaitGroup
filter.Goroutinewg = &ensureGoroutinesStopWG
termshark.Goroutinewg = &ensureGoroutinesStopWG
pcap.Goroutinewg = &ensureGoroutinesStopWG
streams.Goroutinewg = &ensureGoroutinesStopWG
capinfo.Goroutinewg = &ensureGoroutinesStopWG
convs.Goroutinewg = &ensureGoroutinesStopWG
ui.Goroutinewg = &ensureGoroutinesStopWG
wormhole.Goroutinewg = &ensureGoroutinesStopWG
summary.Goroutinewg = &ensureGoroutinesStopWG
confwatcher.Goroutinewg = &ensureGoroutinesStopWG

res := cmain()
ensureGoroutinesStopWG.Wait()
Expand Down Expand Up @@ -179,7 +184,7 @@ func cmain() int {
// On Windows, termshark itself is used to tail the pcap generated by dumpcap, and the output
// is fed into tshark -T psml ...
if tsopts.TailFileValue() != "" {
err = termshark.TailFile(tsopts.TailFileValue())
err = tailfile.Tail(tsopts.TailFileValue())
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v", err)
return 1
Expand Down Expand Up @@ -607,7 +612,7 @@ func cmain() int {
// If the last tshark we used isn't the same as the current one, then remove the cached fields
// data structure so it can be regenerated.
if tsharkBin != profiles.ConfString("main.last-used-tshark", "") {
termshark.DeleteCachedFields()
fields.DeleteCachedFields()
}

// Write out the last-used tshark path. We do this to make the above fields cache be consistent
Expand Down Expand Up @@ -693,7 +698,7 @@ func cmain() int {
}
}

watcher, err := termshark.NewConfigWatcher()
watcher, err := confwatcher.New()
if err != nil {
fmt.Fprintf(os.Stderr, "Problem constructing config file watcher: %v", err)
return 1
Expand Down
8 changes: 2 additions & 6 deletions capinfo/loader.go → pkg/capinfo/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

"github.com/gcla/gowid"
"github.com/gcla/termshark/v2"
"github.com/gcla/termshark/v2/pcap"
"github.com/gcla/termshark/v2/pkg/pcap"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -122,11 +122,7 @@ func (c *Loader) loadCapinfoAsync(pcapf string, app gowid.IApp, cb ICapinfoCallb
state = pcap.Terminated
if !c.SuppressErrors && err != nil {
if _, ok := err.(*exec.ExitError); ok {
cerr := gowid.WithKVs(termshark.BadCommand, map[string]interface{}{
"command": c.capinfoCmd.String(),
"error": err,
})
pcap.HandleError(pcap.CapinfoCode, app, cerr, cb)
pcap.HandleError(pcap.CapinfoCode, app, pcap.MakeUsefulError(c.capinfoCmd, err), cb)
}
}

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
17 changes: 10 additions & 7 deletions confwatcher.go → pkg/confwatcher/confwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,27 @@
// code is governed by the MIT license that can be found in the LICENSE
// file.

package termshark
package confwatcher

import (
"os"
"sync"

"github.com/gcla/termshark/v2"
log "github.com/sirupsen/logrus"
fsnotify "gopkg.in/fsnotify/fsnotify.v1"
)

//======================================================================

var Goroutinewg *sync.WaitGroup

type ConfigWatcher struct {
watcher *fsnotify.Watcher
change chan struct{}
closech chan struct{}
closeWait sync.WaitGroup
}

func NewConfigWatcher() (*ConfigWatcher, error) {
func New() (*ConfigWatcher, error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
panic(err)
Expand All @@ -39,7 +38,7 @@ func NewConfigWatcher() (*ConfigWatcher, error) {

res.closeWait.Add(1)

TrackedGo(func() {
termshark.TrackedGo(func() {
defer func() {
res.watcher.Close()
close(change)
Expand All @@ -60,7 +59,7 @@ func NewConfigWatcher() (*ConfigWatcher, error) {
}
}, Goroutinewg)

if err := watcher.Add(ConfFile("termshark.toml")); err != nil && !os.IsNotExist(err) {
if err := watcher.Add(termshark.ConfFile("termshark.toml")); err != nil && !os.IsNotExist(err) {
return nil, err
}

Expand All @@ -73,7 +72,7 @@ func (c *ConfigWatcher) Close() {
// drain the change channel to ensure the goroutine above can process the close. This
// is safe because I know, at this point, there are no other readers because termshark
// has exited its select loop.
TrackedGo(func() {
termshark.TrackedGo(func() {
// This might block because the goroutine above might not be blocked sending
// to c.change. But then that means the goroutine's for loop above will terminate,
// c.change will be closed, and then this goroutine will end. If the above
Expand All @@ -90,6 +89,10 @@ func (c *ConfigWatcher) ConfigChanged() <-chan struct{} {
return c.change
}

//======================================================================

var Goroutinewg *sync.WaitGroup

//======================================================================
// Local Variables:
// mode: Go
Expand Down
8 changes: 2 additions & 6 deletions convs/loader.go → pkg/convs/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

"github.com/gcla/gowid"
"github.com/gcla/termshark/v2"
"github.com/gcla/termshark/v2/pcap"
"github.com/gcla/termshark/v2/pkg/pcap"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -134,11 +134,7 @@ func (c *Loader) loadConvAsync(pcapf string, convs []string, filter string, abs
state = pcap.Terminated
if !c.SuppressErrors && err != nil {
if _, ok := err.(*exec.ExitError); ok {
cerr := gowid.WithKVs(termshark.BadCommand, map[string]interface{}{
"command": c.convsCmd.String(),
"error": err,
})
pcap.HandleError(pcap.ConvCode, app, cerr, cb)
pcap.HandleError(pcap.ConvCode, app, pcap.MakeUsefulError(c.convsCmd, err), cb)
}
}

Expand Down
File renamed without changes.
15 changes: 8 additions & 7 deletions fields.go → pkg/fields/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// code is governed by the MIT license that can be found in the LICENSE
// file.

package termshark
package fields

import (
"bufio"
Expand All @@ -13,6 +13,7 @@ import (
"strings"
"sync"

"github.com/gcla/termshark/v2"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -160,25 +161,25 @@ type IPrefixCompleter interface {
Completions(prefix string, cb IPrefixCompleterCallback)
}

func NewFields() *TSharkFields {
func New() *TSharkFields {
return &TSharkFields{}
}

func DeleteCachedFields() error {
return os.Remove(CacheFile("tsharkfieldsv3.gob.gz"))
return os.Remove(termshark.CacheFile("tsharkfieldsv3.gob.gz"))
}

// Can be run asynchronously.
// This ought to use interfaces to make it testable.
func (w *TSharkFields) Init() error {
newer, err := FileNewerThan(CacheFile("tsharkfieldsv3.gob.gz"), DirOfPathCommandUnsafe(TSharkBin()))
newer, err := termshark.FileNewerThan(termshark.CacheFile("tsharkfieldsv3.gob.gz"), termshark.DirOfPathCommandUnsafe(termshark.TSharkBin()))
if err == nil && newer {
f := &FieldsAndProtos{
Fields: make(map[string]interface{}),
Protocols: make(map[string]struct{}),
}

err = ReadGob(CacheFile("tsharkfieldsv3.gob.gz"), f)
err = termshark.ReadGob(termshark.CacheFile("tsharkfieldsv3.gob.gz"), f)
if err == nil {
w.ser = f
log.Infof("Read cached tshark fields.")
Expand All @@ -193,7 +194,7 @@ func (w *TSharkFields) Init() error {
return err
}

err = WriteGob(CacheFile("tsharkfieldsv3.gob.gz"), w.ser)
err = termshark.WriteGob(termshark.CacheFile("tsharkfieldsv3.gob.gz"), w.ser)
if err != nil {
return err
}
Expand All @@ -202,7 +203,7 @@ func (w *TSharkFields) Init() error {
}

func (w *TSharkFields) InitNoCache() error {
cmd := exec.Command(TSharkBin(), []string{"-G", "fields"}...)
cmd := exec.Command(termshark.TSharkBin(), []string{"-G", "fields"}...)

out, err := cmd.StdoutPipe()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions fields_test.go → pkg/fields/fields_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2019-2022 Graham Clark. All rights reserved. Use of this source code is governed by the MIT license
// that can be found in the LICENSE file.

package termshark
package fields

import (
"testing"
Expand All @@ -13,7 +13,7 @@ import (

func TestFields1(t *testing.T) {

fields := NewFields()
fields := New()
err := fields.InitNoCache()
assert.NoError(t, err)

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 6 additions & 6 deletions noroot.go → pkg/noroot/noroot.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// code is governed by the MIT license that can be found in the LICENSE
// file.

package termshark
package noroot

import (
"github.com/gcla/gowid/widgets/list"
Expand All @@ -11,22 +11,22 @@ import (

//======================================================================

type NoRootWalker struct {
type Walker struct {
*tree.TreeWalker
}

func NewNoRootWalker(w *tree.TreeWalker) *NoRootWalker {
return &NoRootWalker{
func NewWalker(w *tree.TreeWalker) *Walker {
return &Walker{
TreeWalker: w,
}
}

// for omitting top level node
func (f *NoRootWalker) Next(pos list.IWalkerPosition) list.IWalkerPosition {
func (f *Walker) Next(pos list.IWalkerPosition) list.IWalkerPosition {
return tree.WalkerNext(f, pos)
}

func (f *NoRootWalker) Previous(pos list.IWalkerPosition) list.IWalkerPosition {
func (f *Walker) Previous(pos list.IWalkerPosition) list.IWalkerPosition {
fc := pos.(tree.IPos)
pp := tree.PreviousPosition(fc, f.Tree())
if pp.Equal(tree.NewPos()) {
Expand Down
23 changes: 20 additions & 3 deletions pcap/cmds.go → pkg/pcap/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import (

"github.com/gcla/termshark/v2"
"github.com/gcla/termshark/v2/configs/profiles"
"github.com/gcla/termshark/v2/shark"
"github.com/gcla/termshark/v2/pkg/summary"
"github.com/gcla/termshark/v2/pkg/shark"
"github.com/kballard/go-shellquote"
)

Expand All @@ -35,6 +36,8 @@ func (e ProcessNotStarted) Error() string {
type Command struct {
sync.Mutex
*exec.Cmd
summaryReader *summary.Reader
summaryWriter io.Closer
}

func (c *Command) String() string {
Expand All @@ -46,14 +49,21 @@ func (c *Command) String() string {
func (c *Command) Start() error {
c.Lock()
defer c.Unlock()
c.Cmd.Stderr = termshark.ErrLogger("cmd", c.Path)
pr, pw := io.Pipe()
c.summaryWriter = pw
c.summaryReader = summary.New(pr)
c.Cmd.Stderr = io.MultiWriter(pw, termshark.ErrLogger("cmd", c.Path))
c.PutInNewGroupOnUnix()
res := c.Cmd.Start()
return res
}

func (c *Command) Wait() error {
return c.Cmd.Wait()
err := c.Cmd.Wait()
c.Lock()
c.summaryWriter.Close()
c.Unlock()
return err
}

func (c *Command) StdoutReader() (io.ReadCloser, error) {
Expand All @@ -62,6 +72,13 @@ func (c *Command) StdoutReader() (io.ReadCloser, error) {
return c.Cmd.StdoutPipe()
}

func (c *Command) StderrSummary() []string {
c.Lock()
defer c.Unlock()

return c.summaryReader.Summary()
}

func (c *Command) SetStdout(w io.Writer) {
c.Lock()
defer c.Unlock()
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 5aa42e9

Please sign in to comment.