-
Notifications
You must be signed in to change notification settings - Fork 398
/
utils.go
1418 lines (1209 loc) · 34.2 KB
/
utils.go
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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
import (
"bufio"
"bytes"
"compress/gzip"
"encoding/binary"
"encoding/gob"
"encoding/json"
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"runtime"
"sort"
"strconv"
"strings"
"sync"
"syscall"
"text/template"
"time"
"unicode"
"github.com/adam-hanna/arrayOperations"
"github.com/blang/semver"
"github.com/gcla/gowid"
"github.com/gcla/gowid/gwutil"
"github.com/gcla/gowid/vim"
"github.com/gcla/gowid/widgets/table"
"github.com/gcla/termshark/v2/configs/profiles"
"github.com/gcla/termshark/v2/pkg/system"
"github.com/gcla/termshark/v2/widgets/resizable"
"github.com/gdamore/tcell/v2"
"github.com/gdamore/tcell/v2/terminfo"
"github.com/gdamore/tcell/v2/terminfo/dynamic"
"github.com/mattn/go-isatty"
"github.com/pkg/errors"
"github.com/shibukawa/configdir"
log "github.com/sirupsen/logrus"
"github.com/tevino/abool"
)
//======================================================================
type BadStateError struct{}
var _ error = BadStateError{}
func (e BadStateError) Error() string {
return "Bad state"
}
var BadState = BadStateError{}
//======================================================================
type BadCommandError struct{}
var _ error = BadCommandError{}
func (e BadCommandError) Error() string {
return "Error running command"
}
var BadCommand = BadCommandError{}
//======================================================================
type ConfigError struct{}
var _ error = ConfigError{}
func (e ConfigError) Error() string {
return "Configuration error"
}
var ConfigErr = ConfigError{}
//======================================================================
type InternalError struct{}
var _ error = InternalError{}
func (e InternalError) Error() string {
return "Internal error"
}
var InternalErr = InternalError{}
//======================================================================
var (
UserGuideURL string = "https://termshark.io/userguide"
FAQURL string = "https://termshark.io/faq"
BugURL string = "https://github.com/gcla/termshark/issues/new?assignees=&labels=&template=bug_report.md&title="
FeatureURL string = "https://github.com/gcla/termshark/issues/new?assignees=&labels=&template=feature_request.md&title="
OriginalEnv []string
ShouldSwitchTerminal bool
ShouldSwitchBack bool
unitsRe *regexp.Regexp = regexp.MustCompile(`^([0-9,]+)\s*(bytes|kB|MB)?`)
)
//======================================================================
func IsCommandInPath(bin string) bool {
_, err := exec.LookPath(bin)
return err == nil
}
func DirOfPathCommandUnsafe(bin string) string {
d, err := DirOfPathCommand(bin)
if err != nil {
panic(err)
}
return d
}
func DirOfPathCommand(bin string) (string, error) {
return exec.LookPath(bin)
}
//======================================================================
func ReverseStringSlice(s []string) {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
}
//======================================================================
var TSharkVersionUnknown = fmt.Errorf("Could not determine version of tshark")
func TSharkVersionFromOutput(output string) (semver.Version, error) {
var ver = regexp.MustCompile(`^TShark .*?(\d+\.\d+\.\d+)`)
res := ver.FindStringSubmatch(output)
if len(res) > 0 {
if v, err := semver.Make(res[1]); err == nil {
return v, nil
} else {
return semver.Version{}, err
}
}
return semver.Version{}, errors.WithStack(TSharkVersionUnknown)
}
func TSharkVersion(tshark string) (semver.Version, error) {
cmd := exec.Command(tshark, "--version")
cmdOutput := &bytes.Buffer{}
cmd.Stdout = cmdOutput
cmd.Run() // don't check error - older versions return error code 1. Just search output.
output := cmdOutput.Bytes()
return TSharkVersionFromOutput(string(output))
}
// Depends on empty.pcap being present
func TSharkSupportsColor(tshark string) (bool, error) {
exitCode, err := RunForExitCode(
tshark,
[]string{"-r", CacheFile("empty.pcap"), "-T", "psml", "--color"},
nil,
)
return exitCode == 0, err
}
// TSharkPath will return the full path of the tshark binary, if it's found in the path, otherwise an error
func TSharkPath() (string, *gowid.KeyValueError) {
tsharkBin := profiles.ConfString("main.tshark", "")
if tsharkBin != "" {
confirmedTshark := false
if _, err := os.Stat(tsharkBin); err == nil {
confirmedTshark = true
} else if IsCommandInPath(tsharkBin) {
confirmedTshark = true
}
// This message is for a configured tshark binary that is invalid
if !confirmedTshark {
err := gowid.WithKVs(ConfigErr, map[string]interface{}{
"msg": fmt.Sprintf("Could not run tshark binary '%s'. The tshark binary is required to run termshark.\n", tsharkBin) +
fmt.Sprintf("Check your config file %s\n", ConfFile("toml")),
})
return "", &err
}
} else {
tsharkBin = "tshark"
if !IsCommandInPath(tsharkBin) {
// This message is for an unconfigured tshark bin (via PATH) that is invalid
errstr := fmt.Sprintf("Could not find tshark in your PATH. The tshark binary is required to run termshark.\n")
if strings.Contains(os.Getenv("PREFIX"), "com.termux") {
errstr += fmt.Sprintf("Try installing with: pkg install root-repo && pkg install tshark")
} else if IsCommandInPath("apt") {
errstr += fmt.Sprintf("Try installing with: apt install tshark")
} else if IsCommandInPath("apt-get") {
errstr += fmt.Sprintf("Try installing with: apt-get install tshark")
} else if IsCommandInPath("yum") {
errstr += fmt.Sprintf("Try installing with: yum install wireshark")
} else if IsCommandInPath("brew") {
errstr += fmt.Sprintf("Try installing with: brew install wireshark")
}
errstr += "\n"
err := gowid.WithKVs(ConfigErr, map[string]interface{}{
"msg": errstr,
})
return "", &err
}
}
// Here we know it's in PATH
tsharkBin = DirOfPathCommandUnsafe(tsharkBin)
return tsharkBin, nil
}
func RunForExitCode(prog string, args []string, env []string) (int, error) {
return RunForStderr(prog, args, env, ioutil.Discard)
}
func RunForStderr(prog string, args []string, env []string, stderr io.Writer) (int, error) {
var err error
exitCode := -1 // default bad
cmd := exec.Command(prog, args...)
if env != nil {
cmd.Env = env
}
cmd.Stdout = ioutil.Discard
cmd.Stderr = stderr
err = cmd.Run()
if err != nil {
if exerr, ok := err.(*exec.ExitError); ok {
ws := exerr.Sys().(syscall.WaitStatus)
exitCode = ws.ExitStatus()
}
} else {
ws := cmd.ProcessState.Sys().(syscall.WaitStatus)
exitCode = ws.ExitStatus()
}
return exitCode, err
}
func ConfFile(file string) string {
stdConf := configdir.New("", "termshark")
dirs := stdConf.QueryFolders(configdir.Global)
return path.Join(dirs[0].Path, file)
}
func CacheFile(bin string) string {
return filepath.Join(CacheDir(), bin)
}
func CacheDir() string {
stdConf := configdir.New("", "termshark")
dirs := stdConf.QueryFolders(configdir.Cache)
return dirs[0].Path
}
// A separate dir from CacheDir because I need to use inotify under some
// circumstances for a non-existent file, meaning I need to track a directory,
// and I don't want to be constantly triggered by log file updates.
func PcapDir() string {
var res string
// If use-tshark-temp-for-cache is set, use that
if profiles.ConfBool("main.use-tshark-temp-for-pcap-cache", false) {
tmp, err := TsharkSetting("Temp")
if err == nil {
res = tmp
}
}
// Otherwise try the user's preference
if res == "" {
res = profiles.ConfString("main.pcap-cache-dir", "")
}
if res == "" {
res = DefaultPcapDir()
}
return res
}
// DefaultPcapDir returns ~/.cache/pcaps by default. Termshark will check a
// couple of user settings first before using this.
func DefaultPcapDir() string {
return path.Join(CacheDir(), "pcaps")
}
func TSharkBin() string {
return profiles.ConfString("main.tshark", "tshark")
}
func DumpcapBin() string {
return profiles.ConfString("main.dumpcap", "dumpcap")
}
func CapinfosBin() string {
return profiles.ConfString("main.capinfos", "capinfos")
}
// CaptureBin is the binary the user intends to use to capture
// packets i.e. with the -i switch. This might be distinct from
// DumpcapBin because dumpcap can't capture on extcap interfaces
// like randpkt, but while tshark can, it can drop packets more
// readily than dumpcap. This value is interpreted as the name
// of a binary, resolved against PATH. Note that the default is
// termshark - this invokes termshark in a special mode where it
// first tries DumpcapBin, then if that fails, TSharkBin - for
// the best of both worlds. To detect this, termshark will run
// CaptureBin with TERMSHARK_CAPTURE_MODE=1 in the environment,
// so when termshark itself is invoked with this in the environment,
// it switches to capture mode.
func CaptureBin() string {
if runtime.GOOS == "windows" {
return profiles.ConfString("main.capture-command", DumpcapBin())
} else {
return profiles.ConfString("main.capture-command", os.Args[0])
}
}
// PrivilegedBin returns a capture binary that may require setcap
// privileges on Linux. This is a simple UI to cover the fact that
// termshark's default capture method is to run dumpcap and tshark
// as a fallback. I don't want to tell the user the capture binary
// is termshark - that'd be confusing. We know that on Linux, termshark
// will run dumpcap first, then fall back to tshark if needed. Only
// dumpcap should need access to live interfaces; tshark is needed
// for extcap interfaces only. This is used to provide advice to
// the user if packet capture fails.
func PrivilegedBin() string {
cap := CaptureBin()
if cap == "termshark" {
return DumpcapBin()
} else {
return cap
}
}
func TailCommand() []string {
def := []string{"tail", "-f", "-c", "+0"}
if runtime.GOOS == "windows" {
def = []string{os.Args[0], "--tail"}
}
return profiles.ConfStringSlice("main.tail-command", def)
}
func KeyPressIsPrintable(key gowid.IKey) bool {
return unicode.IsPrint(key.Rune()) && key.Modifiers() & ^tcell.ModShift == 0
}
type KeyMapping struct {
From vim.KeyPress
To vim.KeySequence
}
func AddKeyMapping(km KeyMapping) {
mappings := LoadKeyMappings()
newMappings := make([]KeyMapping, 0)
for _, mapping := range mappings {
if mapping.From != km.From {
newMappings = append(newMappings, mapping)
}
}
newMappings = append(newMappings, km)
SaveKeyMappings(newMappings)
}
func RemoveKeyMapping(kp vim.KeyPress) {
mappings := LoadKeyMappings()
newMappings := make([]KeyMapping, 0)
for _, mapping := range mappings {
if mapping.From != kp {
newMappings = append(newMappings, mapping)
}
}
SaveKeyMappings(newMappings)
}
func LoadKeyMappings() []KeyMapping {
mappings := profiles.ConfStringSlice("main.key-mappings", []string{})
res := make([]KeyMapping, 0)
for _, mapping := range mappings {
pair := strings.Split(mapping, " ")
if len(pair) != 2 {
log.Warnf("Could not parse vim key mapping (missing separator?): %s", mapping)
continue
}
from := vim.VimStringToKeys(pair[0])
if len(from) != 1 {
log.Warnf("Could not parse 'source' vim keypress: %s", pair[0])
continue
}
to := vim.VimStringToKeys(pair[1])
if len(to) < 1 {
log.Warnf("Could not parse 'target' vim keypresses: %s", pair[1])
continue
}
res = append(res, KeyMapping{From: from[0], To: to})
}
return res
}
func SaveKeyMappings(mappings []KeyMapping) {
ser := make([]string, 0, len(mappings))
for _, mapping := range mappings {
ser = append(ser, fmt.Sprintf("%v %v", mapping.From, vim.KeySequence(mapping.To)))
}
profiles.SetConf("main.key-mappings", ser)
}
func RemoveFromStringSlice(pcap string, comps []string) []string {
var newcomps []string
for _, v := range comps {
if v == pcap {
continue
} else {
newcomps = append(newcomps, v)
}
}
newcomps = append([]string{pcap}, newcomps...)
return newcomps
}
const magicMicroseconds = 0xA1B2C3D4
const versionMajor = 2
const versionMinor = 4
const dlt_en10mb = 1
func WriteEmptyPcap(filename string) error {
var buf [24]byte
binary.LittleEndian.PutUint32(buf[0:4], magicMicroseconds)
binary.LittleEndian.PutUint16(buf[4:6], versionMajor)
binary.LittleEndian.PutUint16(buf[6:8], versionMinor)
// bytes 8:12 stay 0 (timezone = UTC)
// bytes 12:16 stay 0 (sigfigs is always set to zero, according to
// http://wiki.wireshark.org/Development/LibpcapFileFormat
binary.LittleEndian.PutUint32(buf[16:20], 10000)
binary.LittleEndian.PutUint32(buf[20:24], uint32(dlt_en10mb))
err := ioutil.WriteFile(filename, buf[:], 0644)
return err
}
func FileNewerThan(f1, f2 string) (bool, error) {
file1, err := os.Open(f1)
if err != nil {
return false, err
}
defer file1.Close()
file2, err := os.Open(f2)
if err != nil {
return false, err
}
defer file2.Close()
f1s, err := file1.Stat()
if err != nil {
return false, err
}
f2s, err := file2.Stat()
if err != nil {
return false, err
}
return f1s.ModTime().After(f2s.ModTime()), nil
}
func ReadGob(filePath string, object interface{}) error {
file, err := os.Open(filePath)
if err == nil {
defer file.Close()
gr, err := gzip.NewReader(file)
if err != nil {
return err
}
defer gr.Close()
decoder := gob.NewDecoder(gr)
err = decoder.Decode(object)
}
return err
}
func WriteGob(filePath string, object interface{}) error {
file, err := os.Create(filePath)
if err == nil {
defer file.Close()
gzipper := gzip.NewWriter(file)
defer gzipper.Close()
encoder := gob.NewEncoder(gzipper)
err = encoder.Encode(object)
}
return err
}
func StringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
// Must succeed - use on internal templates
func TemplateToString(tmpl *template.Template, name string, data interface{}) string {
var res bytes.Buffer
if err := tmpl.ExecuteTemplate(&res, name, data); err != nil {
log.Fatal(err)
}
return res.String()
}
func StringIsArgPrefixOf(a string, list []string) bool {
for _, b := range list {
if strings.HasPrefix(a, fmt.Sprintf("%s=", b)) {
return true
}
}
return false
}
func RunOnDoubleTicker(ch <-chan struct{}, fn func(), dur1 time.Duration, dur2 time.Duration, loops int) {
ticker := time.NewTicker(dur1)
counter := 0
Loop:
for {
select {
case <-ticker.C:
fn()
counter++
if counter == loops {
ticker.Stop()
ticker = time.NewTicker(dur2)
}
case <-ch:
ticker.Stop()
break Loop
}
}
}
func TrackedGo(fn func(), wgs ...*sync.WaitGroup) {
for _, wg := range wgs {
wg.Add(1)
}
go func() {
for _, wg := range wgs {
defer wg.Done()
}
fn()
}()
}
type IProcess interface {
Kill() error
Pid() int
}
func KillIfPossible(p IProcess) error {
if p == nil {
return nil
}
err := p.Kill()
if errProcessAlreadyFinished(err) {
return nil
} else {
return err
}
}
func errProcessAlreadyFinished(err error) bool {
if err == nil {
return false
}
// Terrible hack - but the error isn't published
return err.Error() == "os: process already finished"
}
func SafePid(p IProcess) int {
if p == nil {
return -1
}
return p.Pid()
}
func SetConvTypes(convs []string) {
profiles.SetConf("main.conv-types", convs)
}
func ConvTypes() []string {
defs := []string{"eth", "ip", "ipv6", "tcp", "udp"}
ctypes := profiles.ConfStrings("main.conv-types")
if len(ctypes) > 0 {
z, ok := arrayOperations.Intersect(defs, ctypes)
if ok {
res, ok := z.Interface().([]string)
if ok {
return res
}
}
}
return defs
}
func AddToRecentFiles(pcap string) {
comps := profiles.ConfStrings("main.recent-files")
if len(comps) == 0 || comps[0] != pcap {
comps = RemoveFromStringSlice(pcap, comps)
if len(comps) > 16 {
comps = comps[0 : 16-1]
}
profiles.SetConf("main.recent-files", comps)
}
}
func AddToRecentFilters(val string) {
addToRecent("main.recent-filters", val)
}
func addToRecent(field string, val string) {
comps := profiles.ConfStrings(field)
if (len(comps) == 0 || comps[0] != val) && strings.TrimSpace(val) != "" {
comps = RemoveFromStringSlice(val, comps)
if len(comps) > 64 {
comps = comps[0 : 64-1]
}
profiles.SetConf(field, comps)
}
}
func LoadOffsetFromConfig(name string) ([]resizable.Offset, error) {
offsStr := profiles.ConfString("main."+name, "")
if offsStr == "" {
return nil, errors.WithStack(gowid.WithKVs(ConfigErr, map[string]interface{}{
"name": name,
"msg": "No offsets found",
}))
}
res := make([]resizable.Offset, 0)
err := json.Unmarshal([]byte(offsStr), &res)
if err != nil {
return nil, errors.WithStack(gowid.WithKVs(ConfigErr, map[string]interface{}{
"name": name,
"msg": "Could not unmarshal offsets",
}))
}
return res, nil
}
func SaveOffsetToConfig(name string, offsets2 []resizable.Offset) {
offsets := make([]resizable.Offset, 0)
for _, off := range offsets2 {
if off.Adjust != 0 {
offsets = append(offsets, off)
}
}
if len(offsets) == 0 {
profiles.DeleteConf("main." + name)
} else {
offs, err := json.Marshal(offsets)
if err != nil {
log.Fatal(err)
}
profiles.SetConf("main."+name, string(offs))
}
// Hack to make viper save if I only deleted from the map
profiles.SetConf("main.lastupdate", time.Now().String())
}
//======================================================================
func ErrLogger(key string, val string) *io.PipeWriter {
l := log.StandardLogger()
return log.NewEntry(l).WithField(key, val).WriterLevel(log.ErrorLevel)
}
// KeyValueErrorString returns a string representation of
// a gowid KeyValueError intended to be suitable for displaying in
// a termshark error dialog.
func KeyValueErrorString(err gowid.KeyValueError) string {
res := fmt.Sprintf("%v\n\n", err.Cause())
kvs := make([]string, 0, len(err.KeyVals))
ks := make([]string, 0, len(err.KeyVals))
for k := range err.KeyVals {
ks = append(ks, k)
}
sort.Sort(sort.StringSlice(ks))
for _, k := range ks {
kvs = append(kvs, fmt.Sprintf("%v: %v", k, err.KeyVals[k]))
}
res = res + strings.Join(kvs, "\n\n")
return res
}
//======================================================================
// Need to publish fields for template use
type JumpPos struct {
Summary string `json:"summary"`
Pos int `json:"position"`
}
type GlobalJumpPos struct {
JumpPos
Filename string `json:"filename"`
}
// For ease of use in the template
func (g GlobalJumpPos) Base() string {
return filepath.Base(g.Filename)
}
type globalJumpPosMapping struct {
Key rune `json:"key"`
GlobalJumpPos // embedding without a field name makes the json more concise
}
func LoadGlobalMarks(m map[rune]GlobalJumpPos) error {
marksStr := profiles.ConfString("main.marks", "")
if marksStr == "" {
return nil
}
mappings := make([]globalJumpPosMapping, 0)
err := json.Unmarshal([]byte(marksStr), &mappings)
if err != nil {
return errors.WithStack(gowid.WithKVs(ConfigErr, map[string]interface{}{
"name": "marks",
"msg": "Could not unmarshal marks",
}))
}
for _, mapping := range mappings {
m[mapping.Key] = mapping.GlobalJumpPos
}
return nil
}
func SaveGlobalMarks(m map[rune]GlobalJumpPos) {
marks := make([]globalJumpPosMapping, 0)
for k, v := range m {
marks = append(marks, globalJumpPosMapping{Key: k, GlobalJumpPos: v})
}
if len(marks) == 0 {
profiles.DeleteConf("main.marks")
} else {
marksJ, err := json.Marshal(marks)
if err != nil {
log.Fatal(err)
}
profiles.SetConf("main.marks", string(marksJ))
}
// Hack to make viper save if I only deleted from the map
profiles.SetConf("main.lastupdate", time.Now().String())
}
//======================================================================
// IPCompare is a unit type that satisfies ICompare, and can be used
// for numerically comparing IP addresses.
type IPCompare struct{}
func (s IPCompare) Less(i, j string) bool {
x := net.ParseIP(i)
y := net.ParseIP(j)
if x != nil && y != nil {
if len(x) != len(y) {
return len(x) < len(y)
} else {
for i := 0; i < len(x); i++ {
switch {
case x[i] < y[i]:
return true
case y[i] < x[i]:
return false
}
}
return false
}
} else if x != nil {
return true
} else if y != nil {
return false
} else {
return i < j
}
}
var _ table.ICompare = IPCompare{}
//======================================================================
// MacCompare is a unit type that satisfies ICompare, and can be used
// for numerically comparing MAC addresses.
type MACCompare struct{}
func (s MACCompare) Less(i, j string) bool {
x, errx := net.ParseMAC(i)
y, erry := net.ParseMAC(j)
if errx == nil && erry == nil {
for i := 0; i < len(x); i++ {
switch {
case x[i] < y[i]:
return true
case y[i] < x[i]:
return false
}
}
return false
} else if errx == nil {
return true
} else if erry == nil {
return false
} else {
return i < j
}
}
var _ table.ICompare = MACCompare{}
//======================================================================
// ConvPktsCompare is a unit type that satisfies ICompare, and can be used
// for numerically comparing values emitted by the tshark -z conv,... e.g.
// "2,456 kB"
type ConvPktsCompare struct{}
func (s ConvPktsCompare) Less(i, j string) bool {
mi := unitsRe.FindStringSubmatch(i)
if len(mi) <= 2 {
return false
}
mx, err := strconv.ParseUint(strings.Replace(mi[1], ",", "", -1), 10, 64)
if err != nil {
return false
}
if mi[2] == "kB" {
mx *= 1024
} else if mi[2] == "MB" {
mx *= (1024 * 1024)
}
mj := unitsRe.FindStringSubmatch(j)
if len(mj) <= 2 {
return false
}
my, err := strconv.ParseUint(strings.Replace(mj[1], ",", "", -1), 10, 64)
if err != nil {
return false
}
if mj[2] == "kB" {
my *= 1024
} else if mj[2] == "MB" {
my *= (1024 * 1024)
}
return mx < my
}
var _ table.ICompare = ConvPktsCompare{}
//======================================================================
func PrunePcapCache() error {
// This is a new option. Best to err on the side of caution and, if not, present
// assume the cache can grow indefinitely - in case users are now relying on this
// to keep old pcaps around. I don't want to delete any files without the user's
// explicit permission.
var diskCacheSize int64 = int64(profiles.ConfInt("main.disk-cache-size-mb", -1))
if diskCacheSize == -1 {
log.Infof("No pcap disk cache size set. Skipping cache pruning.")
return nil
}
// Let user use MB as the most sensible unit of disk size. Convert to
// bytes for comparing to file sizes.
diskCacheSize = diskCacheSize * 1024 * 1024
log.Infof("Pruning termshark's pcap disk cache at %s...", PcapDir())
var totalSize int64
var fileInfos []os.FileInfo
err := filepath.Walk(PcapDir(),
func(path string, info os.FileInfo, err error) error {
if err == nil {
totalSize += info.Size()
fileInfos = append(fileInfos, info)
}
return nil
},
)
if err != nil {
return err
}
sort.Slice(fileInfos, func(i, j int) bool {
return fileInfos[i].ModTime().Before(fileInfos[j].ModTime())
})
filesRemoved := 0
curCacheSize := totalSize
for len(fileInfos) > 0 && curCacheSize > diskCacheSize {
err = os.Remove(filepath.Join(PcapDir(), fileInfos[0].Name()))
if err != nil {
log.Warnf("Could not remove pcap cache file %s while pruning - %v", fileInfos[0].Name(), err)
} else {
curCacheSize = curCacheSize - fileInfos[0].Size()
filesRemoved++
}
fileInfos = fileInfos[1:]
}
if filesRemoved > 0 {
log.Infof("Pruning complete. Removed %d old pcaps. Cache size is now %d MB",
filesRemoved, curCacheSize/(1024*1024))
} else {
log.Infof("Pruning complete. No old pcaps removed. Cache size is %d MB",
curCacheSize/(1024*1024))
}
return nil
}
//======================================================================
var cpuProfileRunning *abool.AtomicBool
func init() {
cpuProfileRunning = abool.New()
}
// Down to the second for profiling, etc
func DateStringForFilename() string {
return time.Now().Format("2006-01-02--15-04-05")
}
func ProfileCPUFor(secs int) bool {
if !cpuProfileRunning.SetToIf(false, true) {
log.Infof("CPU profile already running.")
return false
}
file := filepath.Join(CacheDir(), fmt.Sprintf("cpu-%s.prof", DateStringForFilename()))
log.Infof("Starting CPU profile for %d seconds in %s", secs, file)
gwutil.StartProfilingCPU(file)
go func() {
time.Sleep(time.Duration(secs) * time.Second)
log.Infof("Stopping CPU profile")
gwutil.StopProfilingCPU()
cpuProfileRunning.UnSet()
}()
return true
}
func ProfileHeap() {
file := filepath.Join(CacheDir(), fmt.Sprintf("mem-%s.prof", DateStringForFilename()))
log.Infof("Creating memory profile in %s", file)
gwutil.ProfileHeap(file)
}
func LocalIPs() []string {
res := make([]string, 0)
addrs, err := net.InterfaceAddrs()
if err != nil {
return res
}
for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
res = append(res, ipnet.IP.String())
}
}
return res
}
//======================================================================
// From http://blog.kamilkisiel.net/blog/2012/07/05/using-the-go-regexp-package/
//
type tsregexp struct {
*regexp.Regexp
}
func (r *tsregexp) FindStringSubmatchMap(s string) map[string]string {
captures := make(map[string]string)
match := r.FindStringSubmatch(s)
if match == nil {
return captures
}