Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Jorisjean/aggressive mode #36

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
wip
jorisjean committed Jan 11, 2022
commit fe6bb70bfba7e0c8f36b6a46048ad5eb2b976b58
29 changes: 20 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
@@ -17,6 +17,8 @@ func main() {
// Read config file and generate mDNS forwarding maps
configPath := flag.String("config", "", "Config file in TOML format")
debug := flag.Bool("debug", false, "Enable pprof server on /debug/pprof/")
aggressiveMode := flag.Bool("aggressivemode", false, "Also sends mdns response packet to last query packet MAC address")
verbose := flag.Bool("verbose", false, "Show bonjour packets")
flag.Parse()

// Start debug server
@@ -37,11 +39,12 @@ func main() {
}

// Parse IP use to relay queries to chromecasts
spoofAddr := net.ParseIP(cfg.SpoofAddr)
if spoofAddr == nil {
ccSubnetIP := net.ParseIP(cfg.SpoofAddr)
if ccSubnetIP == nil {
log.Fatalf("Could not parse cc_subnet_ip")
}


// Get the local MAC address, to filter out Bonjour packet generated locally
intf, err := net.InterfaceByName(cfg.NetInterface)
if err != nil {
@@ -66,20 +69,28 @@ func main() {

// Process Bonjours packets
for bonjourPacket := range bonjourPackets {
//fmt.Println(bonjourPacket.packet.String())
if *verbose {
fmt.Println(bonjourPacket.packet.String())
}

// Forward the mDNS query or response to appropriate VLANs
if bonjourPacket.isDNSQuery {
// We store the MAC of the last client that sent a query so we can send the response directly to it
lastquery[*bonjourPacket.vlanTag]=*bonjourPacket.srcMAC
fmt.Printf("Storing MAC %v for vlan %v \n", *bonjourPacket.srcMAC, *bonjourPacket.vlanTag)

if *aggressiveMode {
// We store the MAC of the last client that sent a query so we can send the response directly to it
if clientMAC, ok := lastquery[*bonjourPacket.vlanTag]; !ok || clientMAC != *bonjourPacket.srcMAC {
fmt.Printf("Storing new MAC %v for vlan %v \n", *bonjourPacket.srcMAC, *bonjourPacket.vlanTag)
lastquery[*bonjourPacket.vlanTag]=*bonjourPacket.srcMAC
}
}

tags, ok := poolsMap[*bonjourPacket.vlanTag]
if !ok {
continue
}

for _, tag := range tags {
sendBonjourPacket(rawTraffic, &bonjourPacket, tag, brMACAddress, spoofAddr, true, *bonjourPacket.dstMAC, false)
sendBonjourPacket(rawTraffic, &bonjourPacket, tag, brMACAddress, ccSubnetIP, true, *bonjourPacket.dstMAC, false)
}
} else {
device, ok := cfg.Devices[macAddress(bonjourPacket.srcMAC.String())]
@@ -90,10 +101,10 @@ func main() {
// if we have a MAC stored for this vlan we also send the response packet directly to it
if clientMAC, ok := lastquery[tag]; ok {
fmt.Printf("Sending direct packet to MAC %v \n", clientMAC)
sendBonjourPacket(rawTraffic, &bonjourPacket, tag, brMACAddress, spoofAddr, false, clientMAC, true)
sendBonjourPacket(rawTraffic, &bonjourPacket, tag, brMACAddress, *bonjourPacket.srcIP, false, clientMAC, true)
}
// we always forward the multicast answer
sendBonjourPacket(rawTraffic, &bonjourPacket, tag, brMACAddress, spoofAddr, false, *bonjourPacket.dstMAC, false)
sendBonjourPacket(rawTraffic, &bonjourPacket, tag, brMACAddress, *bonjourPacket.srcIP, false, *bonjourPacket.dstMAC, false)
}
}
}
9 changes: 4 additions & 5 deletions packet.go
Original file line number Diff line number Diff line change
@@ -114,14 +114,13 @@ func sendBonjourPacket(
spoofdstMAC bool) {
*bonjourPacket.vlanTag = tag
*bonjourPacket.srcMAC = brMACAddress




// Network devices may set dstMAC to the local MAC address
// Rewrite dstMAC to ensure that it is set to the appropriate multicast MAC address
// or Rewrite dstMAC to the specified one
if bonjourPacket.isIPv6 {
*bonjourPacket.dstMAC = net.HardwareAddr{0x33, 0x33, 0x00, 0x00, 0x00, 0xFB}
} else if spoofdstMAC && bonjourPacket.isIPv6 == false {
} else if spoofdstMAC && !bonjourPacket.isIPv6{
*bonjourPacket.dstMAC = dstMACAddress
} else {
*bonjourPacket.dstMAC = net.HardwareAddr{0x01, 0x00, 0x5E, 0x00, 0x00, 0xFB}
@@ -133,7 +132,7 @@ func sendBonjourPacket(

// We change the Source IP address of the mDNS query since Chromecasts ignore
// packets coming from outside their subnet.
if spoofsrcIP && bonjourPacket.isIPv6 == false {
if spoofsrcIP && !bonjourPacket.isIPv6{
serializeOptions = gopacket.SerializeOptions{ComputeChecksums: true}
*bonjourPacket.srcIP = srcIPAddress
// We recalculate the checksum since the IP was modified