Skip to content

Commit

Permalink
bugfix: fix header render (#6)
Browse files Browse the repository at this point in the history
* bugfix: fix header render bug

* chore: remove parsePort func
  • Loading branch information
chenjiandongx authored Mar 2, 2024
1 parent 686f001 commit 095d9b0
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 38 deletions.
2 changes: 1 addition & 1 deletion cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/spf13/cobra"
)

const version = "v0.6.1"
const version = "v0.6.2"

func NewApp() *cobra.Command {
defaultOpts := DefaultOptions()
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ require (
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/nsf/termbox-go v1.1.1 // indirect
github.com/pkg/errors v0.8.1
github.com/rs/dnscache v0.0.0-20211102005908-e0241e321417
github.com/shirou/gopsutil v3.21.10+incompatible
github.com/spf13/cobra v1.2.1
github.com/stretchr/testify v1.7.0
github.com/tklauser/go-sysconf v0.3.9 // indirect
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4
golang.org/x/sys v0.0.0-20211123173158-ef496fb156ab
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ github.com/nsf/termbox-go v1.1.1 h1:nksUPLCb73Q++DwbYUBEglYBRPZyoXJdrj5L+TkjyZY=
github.com/nsf/termbox-go v1.1.1/go.mod h1:T0cTdVuOwf7pHQNtfhnEbzHbcNyCEcVU4YPpouCbVxo=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pelletier/go-toml v1.9.3/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
12 changes: 0 additions & 12 deletions pcap.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"strconv"
"strings"
"sync"

Expand Down Expand Up @@ -138,14 +137,3 @@ func listPrefixDevices(prefix []string, allowAll bool) ([]pcap.Interface, error)

return devs, nil
}

func parsePort(s string) uint16 {
idx := strings.Index(s, "(")
if idx == -1 {
i, _ := strconv.Atoi(s)
return uint16(i)
}

i, _ := strconv.Atoi(s[:idx])
return uint16(i)
}
31 changes: 18 additions & 13 deletions pcap_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ package main

import (
"context"
"errors"
"sync"

"github.com/pkg/errors"

"github.com/google/gopacket"
"github.com/google/gopacket/afpacket"
"github.com/google/gopacket/layers"
Expand Down Expand Up @@ -66,12 +67,12 @@ func (c *PcapClient) getAvailableDevices() error {
for _, device := range devs {
handler, err := c.getHandler(device.Name)
if err != nil {
continue
return errors.Wrapf(err, "get device(%s) name failed", device.Name)
}

if c.bpfFilter != "" {
if err = c.setBPFFilter(handler, c.bpfFilter); err != nil {
continue
return errors.Wrapf(err, "set bpf-filter(%s) failed", c.bpfFilter)
}
}

Expand Down Expand Up @@ -133,14 +134,14 @@ func (c *PcapClient) parsePacket(ph *pcapHandler, decoded []gopacket.Layer) *Seg

case *layers.TCP:
protocol = ProtoTCP
srcPort = parsePort(lyr.SrcPort.String())
dstPort = parsePort(lyr.DstPort.String())
srcPort = uint16(lyr.SrcPort)
dstPort = uint16(lyr.DstPort)
dataLen = len(lyr.Contents) + len(lyr.Payload)

case *layers.UDP:
protocol = ProtoUDP
srcPort = parsePort(lyr.SrcPort.String())
dstPort = parsePort(lyr.DstPort.String())
srcPort = uint16(lyr.SrcPort)
dstPort = uint16(lyr.DstPort)
dataLen = len(lyr.Contents) + len(lyr.Payload)
}
}
Expand Down Expand Up @@ -195,6 +196,10 @@ func (c *PcapClient) listen(ph *pcapHandler) {
case <-c.ctx.Done():
return

// decode packets followed by layers
// 1) Ethernet Layer
// 2) IP Layer
// 3) TCP/UDP Layer
default:
decoded = decoded[:0]
payload = payload[:0]
Expand All @@ -208,7 +213,9 @@ func (c *PcapClient) listen(ph *pcapHandler) {
continue
}

if ether.EthernetType != layers.EthernetTypeIPv4 {
switch ether.EthernetType {
case layers.EthernetTypeIPv4, layers.EthernetTypeIPv6:
default:
continue
}

Expand All @@ -223,15 +230,14 @@ func (c *PcapClient) listen(ph *pcapHandler) {
}
}

if len(decoded) == 0 {
if len(decoded) == 0 || len(payload) == 0 {
continue
}

var tcpPkg layers.TCP
if err = tcpPkg.DecodeFromBytes(payload, gopacket.NilDecodeFeedback); err == nil {
decoded = append(decoded, &tcpPkg)
seg := c.parsePacket(ph, decoded)
if seg != nil {
if seg := c.parsePacket(ph, decoded); seg != nil {
c.sinker.Fetch(*seg)
}
continue
Expand All @@ -240,8 +246,7 @@ func (c *PcapClient) listen(ph *pcapHandler) {
var udpPkg layers.UDP
if err = udpPkg.DecodeFromBytes(payload, gopacket.NilDecodeFeedback); err == nil {
decoded = append(decoded, &udpPkg)
seg := c.parsePacket(ph, decoded)
if seg != nil {
if seg := c.parsePacket(ph, decoded); seg != nil {
c.sinker.Fetch(*seg)
}
}
Expand Down
8 changes: 4 additions & 4 deletions pcap_others.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ func (c *PcapClient) parsePacket(device string, packet gopacket.Packet) *Segment
tcpLayer := packet.Layer(layers.LayerTypeTCP)
tcpPkg, ok := tcpLayer.(*layers.TCP)
if ok {
srcPort = parsePort(tcpPkg.SrcPort.String())
dstPort = parsePort(tcpPkg.DstPort.String())
srcPort = uint16(tcpPkg.SrcPort)
dstPort = uint16(tcpPkg.DstPort)
protocol = ProtoTCP
dataLen = len(tcpPkg.Contents) + len(tcpPkg.Payload)
}
Expand All @@ -130,8 +130,8 @@ func (c *PcapClient) parsePacket(device string, packet gopacket.Packet) *Segment
udpLayer := packet.Layer(layers.LayerTypeUDP)
udpPkg, ok := udpLayer.(*layers.UDP)
if ok {
srcPort = parsePort(udpPkg.SrcPort.String())
dstPort = parsePort(udpPkg.DstPort.String())
srcPort = uint16(udpPkg.SrcPort)
dstPort = uint16(udpPkg.DstPort)
protocol = ProtoUDP
dataLen = len(udpPkg.Contents) + len(udpPkg.Payload)
}
Expand Down
10 changes: 6 additions & 4 deletions sniffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func exit(s string) {
fmt.Println("Start sniffer failed:", s)
fmt.Fprintln(os.Stderr, "Start sniffer failed:", s)
os.Exit(1)
}

Expand Down Expand Up @@ -51,7 +51,7 @@ func (o Options) Validate() error {
func DefaultOptions() Options {
return Options{
BPFFilter: "tcp or udp",
Interval: 1,
Interval: 2,
ViewMode: ModeTableBytes,
Unit: UnitKB,
DevicesPrefix: []string{"en", "lo", "eth", "em", "bond"},
Expand Down Expand Up @@ -99,7 +99,9 @@ func (s *Sniffer) Start() {
s.Refresh()
var paused bool

ticker := time.Tick(time.Duration(s.opts.Interval) * time.Second)
ticker := time.NewTicker(time.Duration(s.opts.Interval) * time.Second)
defer ticker.Stop()

for {
select {
case e := <-events:
Expand All @@ -117,7 +119,7 @@ func (s *Sniffer) Start() {
return
}

case <-ticker:
case <-ticker.C:
if !paused {
s.Refresh()
}
Expand Down
8 changes: 4 additions & 4 deletions stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,10 @@ func (s *StatsManager) getSnapshot() *Snapshot {
Processes: processes,
RemoteAddrs: remoteAddr,
Connections: connections,
TotalUploadBytes: totalUploadBytes,
TotalDownloadBytes: totalDownloadBytes,
TotalUploadPackets: totalUploadPackets,
TotalDownloadPackets: totalDownloadPackets,
TotalUploadBytes: totalUploadBytes / s.ratio,
TotalDownloadBytes: totalDownloadBytes / s.ratio,
TotalUploadPackets: totalUploadPackets / s.ratio,
TotalDownloadPackets: totalDownloadPackets / s.ratio,
TotalConnections: totalConnections,
}
}
1 change: 1 addition & 0 deletions ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ func NewUIComponent(opt Options) *UIComponent {
}

func (ui *UIComponent) Close() {
termui.Clear()
termui.Close()
}

Expand Down

0 comments on commit 095d9b0

Please sign in to comment.