-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
52 lines (44 loc) · 1.48 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"flag"
"fmt"
"gohttpcapture/capture"
"os"
"github.com/google/gopacket"
"github.com/google/gopacket/pcap"
)
func main() {
config := new(capture.CaptureConfig)
flag.StringVar(&config.Interface, "i", "", "The interface from the data are captured")
flag.StringVar(&config.CaptureFile, "f", "", "Read from capture file (tcpdump)")
flag.StringVar(&config.Pattern, "p", "", "Capture paths matching to this regexp")
flag.Parse()
fmt.Printf("%#v\n", config)
if config.Interface != "" {
capture.Capture(config, NewInterfaceSource(config))
} else if config.CaptureFile != "" {
capture.Capture(config, NewFileSource(config))
} else {
fmt.Fprintln(os.Stderr, "Neither -i interface nor -f capture file specified")
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
}
}
func NewInterfaceSource(config *capture.CaptureConfig) *gopacket.PacketSource {
if handle, err := pcap.OpenLive(config.Interface, 1600, true, pcap.BlockForever); err != nil {
panic(err)
} else if err := handle.SetBPFFilter("tcp and port 80"); err != nil {
panic(err)
} else {
return gopacket.NewPacketSource(handle, handle.LinkType())
}
}
func NewFileSource(config *capture.CaptureConfig) *gopacket.PacketSource {
if handle, err := pcap.OpenOffline(config.CaptureFile); err != nil {
panic(err)
} else if err := handle.SetBPFFilter("tcp and port 80"); err != nil {
panic(err)
} else {
return gopacket.NewPacketSource(handle, handle.LinkType())
}
}