forked from Gandem/bonjour-reflector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
51 lines (43 loc) · 1.34 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
package main
import (
"fmt"
"log"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
)
func main() {
cfg, err := readConfig("./config.toml")
if err != nil {
log.Fatalf("Could not read configuration: %v", err)
}
src, err := pcap.OpenLive(cfg.NetInterface, 65536, true, time.Second)
if err != nil {
log.Fatalf("Could not find network interface: %v", cfg.NetInterface)
}
var dec gopacket.Decoder
var ok bool
if dec, ok = gopacket.DecodersByLayerName["Ethernet"]; !ok {
log.Fatalln("No decoder named Ethernet")
}
source := gopacket.NewPacketSource(src, dec)
var eth layers.Ethernet
var ip4 layers.IPv4
var ip6 layers.IPv6
var udp layers.UDP
var tag layers.Dot1Q
parser := gopacket.NewDecodingLayerParser(layers.LayerTypeEthernet, &tag, ð, &ip4, &ip6, &udp)
decoded := []gopacket.LayerType{}
for packet := range source.Packets() {
parser.DecodeLayers(packet.Data(), &decoded)
// Detect Bonjour packets
if ip4.DstIP.String() == "224.0.0.251" || ip6.DstIP.String() == "ff02::fb" {
if udp.DstPort == 5353 {
// Print time for logging / debugging purposes
fmt.Printf("[%v] New Bonjour packet detected from %v\n",
time.Now().Format("02/01/2006 15:04:05"), ip4.SrcIP) // Custom time layouts must use the reference time: Mon Jan 2 15:04:05 MST 2006
}
}
}
}