1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
// 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 convs
import (
"bytes"
"context"
"fmt"
"os/exec"
"sync"
"github.com/gcla/gowid"
"github.com/gcla/termshark/v2"
"github.com/gcla/termshark/v2/pkg/pcap"
log "github.com/sirupsen/logrus"
)
//======================================================================
var Goroutinewg *sync.WaitGroup
//======================================================================
type ILoaderCmds interface {
Convs(pcapfile string, convs []string, filter string, abs bool, resolve bool) pcap.IPcapCommand
}
type commands struct{}
func MakeCommands() commands {
return commands{}
}
var _ ILoaderCmds = commands{}
func (c commands) Convs(pcapfile string, convs []string, filter string, abs bool, resolve bool) pcap.IPcapCommand {
args := []string{"-q", "-r", pcapfile}
if abs {
args = append(args, "-t", "a")
}
if !resolve {
args = append(args, "-n")
}
for _, conv := range convs {
args = append(args, "-z", fmt.Sprintf("conv,%s", conv))
if filter != "" {
args[len(args)-1] = fmt.Sprintf("%s,%s", args[len(args)-1], filter)
}
}
return &pcap.Command{
Cmd: exec.Command(termshark.TSharkBin(), args...),
}
}
//======================================================================
type Loader struct {
cmds ILoaderCmds
SuppressErrors bool // if true, don't report process errors e.g. at shutdown
mainCtx context.Context // cancelling this cancels the dependent contexts
mainCancelFn context.CancelFunc
convsCtx context.Context
convsCancelFn context.CancelFunc
convsCmd pcap.IPcapCommand
}
func NewLoader(cmds ILoaderCmds, ctx context.Context) *Loader {
res := &Loader{
cmds: cmds,
}
res.mainCtx, res.mainCancelFn = context.WithCancel(ctx)
return res
}
func (c *Loader) StopLoad() {
if c.convsCancelFn != nil {
c.convsCancelFn()
}
}
//======================================================================
type IConvsCallbacks interface {
OnData(data string)
AfterDataEnd(success bool)
}
func (c *Loader) StartLoad(pcap string, convs []string, filter string, abs bool, resolve bool, app gowid.IApp, cb IConvsCallbacks) {
termshark.TrackedGo(func() {
c.loadConvAsync(pcap, convs, filter, abs, resolve, app, cb)
}, Goroutinewg)
}
func (c *Loader) loadConvAsync(pcapf string, convs []string, filter string, abs bool, resolve bool, app gowid.IApp, cb IConvsCallbacks) {
c.convsCtx, c.convsCancelFn = context.WithCancel(c.mainCtx)
procChan := make(chan int)
pid := 0
defer func() {
if pid == 0 {
close(procChan)
}
}()
c.convsCmd = c.cmds.Convs(pcapf, convs, filter, abs, resolve)
termChan := make(chan error)
termshark.TrackedGo(func() {
var err error
cmd := c.convsCmd
cancelledChan := c.convsCtx.Done()
procChan := procChan
state := pcap.NotStarted
kill := func() {
err := termshark.KillIfPossible(cmd)
if err != nil {
log.Infof("Did not kill tshark conv process: %v", err)
}
}
loop:
for {
select {
case err = <-termChan:
state = pcap.Terminated
if !c.SuppressErrors && err != nil {
if _, ok := err.(*exec.ExitError); ok {
pcap.HandleError(pcap.ConvCode, app, pcap.MakeUsefulError(c.convsCmd, err), cb)
}
}
case pid := <-procChan:
procChan = nil
if pid != 0 {
state = pcap.Started
if cancelledChan == nil {
kill()
}
}
case <-cancelledChan:
cancelledChan = nil
if state == pcap.Started {
kill()
}
}
if state == pcap.Terminated || (procChan == nil && state == pcap.NotStarted) {
break loop
}
}
}, Goroutinewg)
convsOut, err := c.convsCmd.StdoutReader()
if err != nil {
pcap.HandleError(pcap.ConvCode, app, err, cb)
return
}
defer func() {
cb.AfterDataEnd(true)
}()
app.Run(gowid.RunFunction(func(app gowid.IApp) {
pcap.HandleBegin(pcap.ConvCode, app, cb)
}))
defer func() {
app.Run(gowid.RunFunction(func(app gowid.IApp) {
pcap.HandleEnd(pcap.ConvCode, app, cb)
}))
}()
err = c.convsCmd.Start()
if err != nil {
err = fmt.Errorf("Error starting %v: %v", c.convsCmd, err)
pcap.HandleError(pcap.ConvCode, app, err, cb)
return
}
log.Infof("Started command %v with pid %d", c.convsCmd, c.convsCmd.Pid())
termshark.TrackedGo(func() {
termChan <- c.convsCmd.Wait()
}, Goroutinewg)
pid = c.convsCmd.Pid()
procChan <- pid
buf := new(bytes.Buffer)
buf.ReadFrom(convsOut)
cb.OnData(buf.String())
c.convsCancelFn()
}
//======================================================================
// Local Variables:
// mode: Go
// fill-column: 78
// End:
|