|
| 1 | +//go:build !e2e |
| 2 | + |
| 3 | +package integrationtests |
| 4 | + |
| 5 | +import ( |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "os" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/gopacket/gopacket" |
| 13 | + "github.com/gopacket/gopacket/layers" |
| 14 | + "github.com/gopacket/gopacket/pcapgo" |
| 15 | +) |
| 16 | + |
| 17 | +// PacketInfo holds information about a captured packet |
| 18 | +type PacketInfo struct { |
| 19 | + Timestamp int64 |
| 20 | + Length int |
| 21 | + SrcIP string |
| 22 | + DstIP string |
| 23 | + SrcPort uint16 |
| 24 | + DstPort uint16 |
| 25 | + Protocol string |
| 26 | + Comments []string |
| 27 | + HasTCP bool |
| 28 | + HasUDP bool |
| 29 | + HasIPv4 bool |
| 30 | + HasIPv6 bool |
| 31 | + K8sMetadata map[string]string |
| 32 | +} |
| 33 | + |
| 34 | +// PacketFilter defines filtering criteria for packets |
| 35 | +type PacketFilter struct { |
| 36 | + Port *uint16 // Filter by port (source or destination) |
| 37 | + SrcPort *uint16 // Filter by source port |
| 38 | + DstPort *uint16 // Filter by destination port |
| 39 | + Protocol string // Filter by protocol (TCP, UDP, ICMP, etc.) |
| 40 | + SrcIP string // Filter by source IP |
| 41 | + DstIP string // Filter by destination IP |
| 42 | + MinLength int // Minimum packet length |
| 43 | + MaxLength int // Maximum packet length |
| 44 | +} |
| 45 | + |
| 46 | +// ReadPcapngFile reads a pcapng file and returns all packets |
| 47 | +func ReadPcapngFile(filepath string) ([]PacketInfo, error) { |
| 48 | + return ReadPcapngFileWithFilter(filepath, nil) |
| 49 | +} |
| 50 | + |
| 51 | +// ReadPcapngFileWithFilter reads a pcapng file and returns filtered packets |
| 52 | +func ReadPcapngFileWithFilter(filepath string, filter *PacketFilter) ([]PacketInfo, error) { |
| 53 | + f, err := os.Open(filepath) |
| 54 | + if err != nil { |
| 55 | + return nil, fmt.Errorf("failed to open pcapng file: %w", err) |
| 56 | + } |
| 57 | + defer f.Close() |
| 58 | + |
| 59 | + ngReader, err := pcapgo.NewNgReader(f, pcapgo.DefaultNgReaderOptions) |
| 60 | + if err != nil { |
| 61 | + return nil, fmt.Errorf("failed to create pcapng reader: %w", err) |
| 62 | + } |
| 63 | + |
| 64 | + var packets []PacketInfo |
| 65 | + |
| 66 | + for { |
| 67 | + data, ci, opts, err := ngReader.ReadPacketDataWithOptions() |
| 68 | + if err != nil { |
| 69 | + if errors.Is(err, io.EOF) { |
| 70 | + break |
| 71 | + } |
| 72 | + return nil, fmt.Errorf("error reading packet data: %w", err) |
| 73 | + } |
| 74 | + |
| 75 | + packet := gopacket.NewPacket(data, layers.LayerTypeEthernet, gopacket.Default) |
| 76 | + packetInfo := extractPacketInfo(packet, ci, &opts) |
| 77 | + |
| 78 | + // Apply filter if provided |
| 79 | + if filter != nil && !matchesFilter(&packetInfo, filter) { |
| 80 | + continue |
| 81 | + } |
| 82 | + |
| 83 | + packets = append(packets, packetInfo) |
| 84 | + } |
| 85 | + |
| 86 | + return packets, nil |
| 87 | +} |
| 88 | + |
| 89 | +// extractPacketInfo extracts information from a packet |
| 90 | +func extractPacketInfo(packet gopacket.Packet, ci gopacket.CaptureInfo, opts *pcapgo.NgPacketOptions) PacketInfo { |
| 91 | + info := PacketInfo{ |
| 92 | + Timestamp: ci.Timestamp.Unix(), |
| 93 | + Length: ci.Length, |
| 94 | + K8sMetadata: make(map[string]string), |
| 95 | + } |
| 96 | + |
| 97 | + // Extract comments from NgPacketOptions (contains k8s metadata) |
| 98 | + if len(opts.Comments) > 0 { |
| 99 | + info.Comments = opts.Comments |
| 100 | + extractK8sMetadata(&info) |
| 101 | + } |
| 102 | + |
| 103 | + // Check for IPv4 layer |
| 104 | + if ipv4Layer := packet.Layer(layers.LayerTypeIPv4); ipv4Layer != nil { |
| 105 | + ipv4, _ := ipv4Layer.(*layers.IPv4) |
| 106 | + info.SrcIP = ipv4.SrcIP.String() |
| 107 | + info.DstIP = ipv4.DstIP.String() |
| 108 | + info.HasIPv4 = true |
| 109 | + info.Protocol = ipv4.Protocol.String() |
| 110 | + } |
| 111 | + |
| 112 | + // Check for IPv6 layer |
| 113 | + if ipv6Layer := packet.Layer(layers.LayerTypeIPv6); ipv6Layer != nil { |
| 114 | + ipv6, _ := ipv6Layer.(*layers.IPv6) |
| 115 | + info.SrcIP = ipv6.SrcIP.String() |
| 116 | + info.DstIP = ipv6.DstIP.String() |
| 117 | + info.HasIPv6 = true |
| 118 | + info.Protocol = ipv6.NextHeader.String() |
| 119 | + } |
| 120 | + |
| 121 | + // Check for TCP layer |
| 122 | + if tcpLayer := packet.Layer(layers.LayerTypeTCP); tcpLayer != nil { |
| 123 | + tcp, _ := tcpLayer.(*layers.TCP) |
| 124 | + info.SrcPort = uint16(tcp.SrcPort) |
| 125 | + info.DstPort = uint16(tcp.DstPort) |
| 126 | + info.HasTCP = true |
| 127 | + if info.Protocol == "" { |
| 128 | + info.Protocol = "TCP" |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + // Check for UDP layer |
| 133 | + if udpLayer := packet.Layer(layers.LayerTypeUDP); udpLayer != nil { |
| 134 | + udp, _ := udpLayer.(*layers.UDP) |
| 135 | + info.SrcPort = uint16(udp.SrcPort) |
| 136 | + info.DstPort = uint16(udp.DstPort) |
| 137 | + info.HasUDP = true |
| 138 | + if info.Protocol == "" { |
| 139 | + info.Protocol = "UDP" |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + // Check for ICMP layers |
| 144 | + if packet.Layer(layers.LayerTypeICMPv4) != nil { |
| 145 | + info.Protocol = "ICMPv4" |
| 146 | + } |
| 147 | + if packet.Layer(layers.LayerTypeICMPv6) != nil { |
| 148 | + info.Protocol = "ICMPv6" |
| 149 | + } |
| 150 | + |
| 151 | + return info |
| 152 | +} |
| 153 | + |
| 154 | +// extractK8sMetadata parses k8s metadata from packet comments |
| 155 | +func extractK8sMetadata(info *PacketInfo) { |
| 156 | + for _, comment := range info.Comments { |
| 157 | + lines := strings.Split(comment, "\n") |
| 158 | + for _, line := range lines { |
| 159 | + if strings.Contains(line, ":") { |
| 160 | + parts := strings.SplitN(line, ":", 2) |
| 161 | + if len(parts) == 2 { |
| 162 | + key := strings.TrimSpace(parts[0]) |
| 163 | + value := strings.TrimSpace(parts[1]) |
| 164 | + info.K8sMetadata[key] = value |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +// matchesFilter checks if a packet matches the filter criteria |
| 172 | +func matchesFilter(info *PacketInfo, filter *PacketFilter) bool { |
| 173 | + // Filter by port (either source or destination) |
| 174 | + if filter.Port != nil { |
| 175 | + if info.SrcPort != *filter.Port && info.DstPort != *filter.Port { |
| 176 | + return false |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + // Filter by source port |
| 181 | + if filter.SrcPort != nil && info.SrcPort != *filter.SrcPort { |
| 182 | + return false |
| 183 | + } |
| 184 | + |
| 185 | + // Filter by destination port |
| 186 | + if filter.DstPort != nil && info.DstPort != *filter.DstPort { |
| 187 | + return false |
| 188 | + } |
| 189 | + |
| 190 | + // Filter by protocol |
| 191 | + if filter.Protocol != "" && !strings.EqualFold(info.Protocol, filter.Protocol) { |
| 192 | + return false |
| 193 | + } |
| 194 | + |
| 195 | + // Filter by source IP |
| 196 | + if filter.SrcIP != "" && info.SrcIP != filter.SrcIP { |
| 197 | + return false |
| 198 | + } |
| 199 | + |
| 200 | + // Filter by destination IP |
| 201 | + if filter.DstIP != "" && info.DstIP != filter.DstIP { |
| 202 | + return false |
| 203 | + } |
| 204 | + |
| 205 | + // Filter by minimum length |
| 206 | + if filter.MinLength > 0 && info.Length < filter.MinLength { |
| 207 | + return false |
| 208 | + } |
| 209 | + |
| 210 | + // Filter by maximum length |
| 211 | + if filter.MaxLength > 0 && info.Length > filter.MaxLength { |
| 212 | + return false |
| 213 | + } |
| 214 | + |
| 215 | + return true |
| 216 | +} |
| 217 | + |
| 218 | +// FilterPacketsByPort filters packets by port (source or destination) |
| 219 | +func FilterPacketsByPort(packets []PacketInfo, port uint16) []PacketInfo { |
| 220 | + var filtered []PacketInfo |
| 221 | + for _, p := range packets { |
| 222 | + if p.SrcPort == port || p.DstPort == port { |
| 223 | + filtered = append(filtered, p) |
| 224 | + } |
| 225 | + } |
| 226 | + return filtered |
| 227 | +} |
0 commit comments