Skip to content

Commit

Permalink
improve stdout format
Browse files Browse the repository at this point in the history
  • Loading branch information
mozillazg committed Apr 23, 2024
1 parent c70eac3 commit 98a1f87
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ require (

replace (
github.com/gopacket/gopacket => github.com/mozillazg/gopacket v0.0.0-20240420072046-71afeafe42df
github.com/x-way/pktdump => github.com/mozillazg/pktdump v0.0.0-20240421054319-759ababbd329
github.com/x-way/pktdump => github.com/mozillazg/pktdump v0.0.0-20240422135914-a9ab652291b1
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ github.com/mdlayher/socket v0.1.1 h1:q3uOGirUPfAV2MUoaC7BavjQ154J7+JOkTWyiV+intI
github.com/mdlayher/socket v0.1.1/go.mod h1:mYV5YIZAfHh4dzDVzI8x8tWLWCliuX8Mon5Awbj+qDs=
github.com/mozillazg/gopacket v0.0.0-20240420072046-71afeafe42df h1:dP09t/Mu44X7itTtVKr0H+NCCvpROItG5FMkPkzUhT4=
github.com/mozillazg/gopacket v0.0.0-20240420072046-71afeafe42df/go.mod h1:lnXM4VDqJTe4d2NoZr8DZMtidkhss2Y82QFlamXWfXo=
github.com/mozillazg/pktdump v0.0.0-20240421054319-759ababbd329 h1:mZ5vMcl+pQG4u1lotN3Me9T99n3dNv+xWjhZy+tVmdE=
github.com/mozillazg/pktdump v0.0.0-20240421054319-759ababbd329/go.mod h1:InLCDK8kgkk26VtyPZ51e0igf15eiXDMvvQuV62Wqmw=
github.com/mozillazg/pktdump v0.0.0-20240422135914-a9ab652291b1 h1:hAP2PH4czj22vr78Dh6dPlXlxDsKK/uYNxPiE9jvRUM=
github.com/mozillazg/pktdump v0.0.0-20240422135914-a9ab652291b1/go.mod h1:InLCDK8kgkk26VtyPZ51e0igf15eiXDMvvQuV62Wqmw=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
7 changes: 4 additions & 3 deletions internal/writer/stdout.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,21 @@ func NewStdoutWriter(writer io.Writer, pcache *metadata.ProcessCache) *StdoutWri
}

func (w *StdoutWriter) Write(e *event.Packet) error {
ifName := e.Device.Name
packetType := "In"
if e.Egress() {
packetType = "Out"
}
p := w.pcache.Get(e.Pid)
pidInfo := fmt.Sprintf("Process [pid %d, cmd %s, args %s]",
pidInfo := fmt.Sprintf("Process (pid %d, cmd %s, args %s)",
e.Pid, p.FilenameStr(), p.ArgsStr())

// Decode a packet
packet := gopacket.NewPacket(e.Data, layers.LayerTypeEthernet, gopacket.NoCopy)
formated := pktdump.Format(packet)

msg := fmt.Sprintf("%s %s %s, %s\n",
e.Time.Local().Format("15:04:05.000000"),
msg := fmt.Sprintf("%s %s %s %s\n %s\n",
e.Time.Local().Format("15:04:05.000000"), ifName,
packetType, formated, pidInfo)

if _, err := w.w.Write([]byte(msg)); err != nil {
Expand Down
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ func parseExecEvent(pcache *metadata.ProcessCache, rawSample []byte) {

func newPcapWriter(w io.Writer, pcache *metadata.ProcessCache) (*writer.PcapNGWriter, error) {
devices, err := dev.GetDevices("any")
if err != nil {
return nil, err
}

var interfaces []pcapgo.NgInterface
for _, dev := range devices {
Expand Down

0 comments on commit 98a1f87

Please sign in to comment.