Decoding packet layers
Packets can be decoded layer by layer with the packet.Layer()
function. This program will inspect the packets, look for TCP traffic, and then output the Ethernet layer, IP layer, TCP layer, and application layer information. This is useful when you need to inspect the traffic and make a decision based on the information. When it gets to the application layer, it looks for the HTTP
keyword and prints a message if one is detected:
package main import ( "fmt" "github.com/google/gopacket" "github.com/google/gopacket/layers" "github.com/google/gopacket/pcap" "log" "strings" "time" ) var ( device = "eth0" snapshotLen int32 = 1024 promiscuous = false err error timeout = 30 * time.Second handle *pcap.Handle ) func main() { // Open device handle, err = pcap.OpenLive(device, snapshotLen, promiscuous, timeout) if err != nil { log.Fatal(err) } defer handle.Close() packetSource...