Last active
May 4, 2021 04:41
-
-
Save jeanqasaur/0593fc9ede687b5c19eab38ee7498fed to your computer and use it in GitHub Desktop.
Example use of pcap filters
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
package main | |
import ( | |
"github.com/google/gopacket" | |
_ "github.com/google/gopacket/layers" | |
"github.com/google/gopacket/pcap" | |
) | |
const ( | |
// The same default as tcpdump. | |
defaultSnapLen = 262144 | |
) | |
func main() { | |
handle, err := pcap.OpenLive("lo", defaultSnapLen, true, | |
pcap.BlockForever) | |
if err != nil { | |
panic(err) | |
} | |
defer handle.Close() | |
if err := handle.SetBPFFilter("port 3030"); err != nil { | |
panic(err) | |
} | |
packets := gopacket.NewPacketSource( | |
handle, handle.LinkType()).Packets() | |
for pkt := range packets { | |
// Your analysis here! | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment