-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
1,526 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ | |
# vendor/ | ||
test.pcapng | ||
ptcpdump | ||
ptcpdump.pcapng |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
//go:build ignore | ||
// +build ignore | ||
|
||
#include "vmlinux.h" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
//go:build ignore | ||
// +build ignore | ||
|
||
package bpf | ||
|
||
import _ "github.com/cilium/ebpf/cmd/bpf2go" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package event | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"github.com/mozillazg/ptcpdump/bpf" | ||
"golang.org/x/xerrors" | ||
"unsafe" | ||
) | ||
|
||
type packetType int | ||
|
||
const ( | ||
packetTypeIngress packetType = 0 | ||
packetTypeEgress packetType = 1 | ||
) | ||
|
||
type Packet struct { | ||
Type packetType | ||
Pid int | ||
Comm string | ||
|
||
Data []byte | ||
} | ||
|
||
func ParsePacketEvent(rawSample []byte) (*Packet, error) { | ||
var p Packet | ||
event := bpf.BpfPacketEventT{} | ||
if err := binary.Read(bytes.NewBuffer(rawSample), binary.LittleEndian, &event.Meta); err != nil { | ||
return nil, xerrors.Errorf("parse meta: %w", err) | ||
} | ||
copy(event.Payload[:], rawSample[unsafe.Offsetof(event.Payload):]) | ||
|
||
p.Pid = int(event.Meta.Pid) | ||
p.Comm = strComm(event.Meta.Comm) | ||
if event.Meta.PacketType == 1 { | ||
p.Type = packetTypeEgress | ||
} | ||
p.Data = make([]byte, event.Meta.PayloadLen) | ||
copy(p.Data[:], event.Payload[:event.Meta.PayloadLen]) | ||
|
||
return &p, nil | ||
} | ||
|
||
func (p Packet) Ingress() bool { | ||
return p.Type == packetTypeIngress | ||
} | ||
|
||
func (p Packet) Egress() bool { | ||
return p.Type == packetTypeEgress | ||
} | ||
|
||
func strComm(comm [16]int8) string { | ||
b := make([]byte, len(comm)) | ||
for i, c := range comm { | ||
b[i] = byte(c) | ||
} | ||
return string(b) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package writer | ||
|
||
import ( | ||
"fmt" | ||
"github.com/gopacket/gopacket" | ||
"github.com/gopacket/gopacket/pcapgo" | ||
"github.com/mozillazg/ptcpdump/internal/event" | ||
"golang.org/x/xerrors" | ||
"time" | ||
) | ||
|
||
type PcapNGWriter struct { | ||
pw *pcapgo.NgWriter | ||
} | ||
|
||
func NewPcapNGWriter(pw *pcapgo.NgWriter) *PcapNGWriter { | ||
return &PcapNGWriter{pw: pw} | ||
} | ||
|
||
func (w *PcapNGWriter) Write(p *event.Packet) error { | ||
payloadLen := len(p.Data) | ||
info := gopacket.CaptureInfo{ | ||
Timestamp: time.Now(), | ||
CaptureLength: payloadLen, | ||
Length: payloadLen, | ||
InterfaceIndex: 0, | ||
} | ||
opts := pcapgo.NgPacketOptions{ | ||
Comment: fmt.Sprintf("PID: %d\nCOMMAND: %s", p.Pid, p.Comm), | ||
} | ||
|
||
if err := w.pw.WritePacketWithOptions(info, p.Data, opts); err != nil { | ||
return xerrors.Errorf("writing packet: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (w *PcapNGWriter) Flush() error { | ||
return w.pw.Flush() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package writer | ||
|
||
import ( | ||
"github.com/gopacket/gopacket" | ||
"github.com/gopacket/gopacket/layers" | ||
"github.com/mozillazg/ptcpdump/internal/event" | ||
"log" | ||
) | ||
|
||
type StdoutWriter struct { | ||
} | ||
|
||
func NewStdoutWriter() *StdoutWriter { | ||
return &StdoutWriter{} | ||
} | ||
|
||
func (w *StdoutWriter) Write(p *event.Packet) error { | ||
packetType := "=>· " | ||
if p.Egress() { | ||
packetType = " ·=>" | ||
} | ||
|
||
// Decode a packet | ||
packet := gopacket.NewPacket(p.Data, layers.LayerTypeEthernet, gopacket.Default) | ||
var ipv4 *layers.IPv4 | ||
if ipv4Layer := packet.Layer(layers.LayerTypeIPv4); ipv4Layer != nil { | ||
ipv4, _ = ipv4Layer.(*layers.IPv4) | ||
} | ||
if ipv4 == nil { | ||
return nil | ||
} | ||
if tcpLayer := packet.Layer(layers.LayerTypeTCP); tcpLayer != nil { | ||
tcp, _ := tcpLayer.(*layers.TCP) | ||
log.Printf("%s %s-%d %s:%d => %s:%d", | ||
packetType, p.Comm, p.Pid, | ||
ipv4.SrcIP.String(), tcp.SrcPort, | ||
ipv4.DstIP.String(), tcp.DstPort) | ||
} | ||
if udpLayer := packet.Layer(layers.LayerTypeUDP); udpLayer != nil { | ||
udp, _ := udpLayer.(*layers.UDP) | ||
log.Printf("%s %s-%d %s:%d => %s:%d", | ||
packetType, p.Comm, p.Pid, | ||
ipv4.SrcIP.String(), udp.SrcPort, | ||
ipv4.DstIP.String(), udp.DstPort) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package writer | ||
|
||
type PacketWriter struct { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.