From 7c28b957d470dc8c6d0431c499183561cf466080 Mon Sep 17 00:00:00 2001 From: Simon Moser Date: Wed, 3 Nov 2021 19:47:28 +0100 Subject: [PATCH] Changed filter method to variable types [Close #13] --- proxy/main.go | 76 ++++++++++++++------------------------------------- 1 file changed, 21 insertions(+), 55 deletions(-) diff --git a/proxy/main.go b/proxy/main.go index 29e03b7..b4575c6 100644 --- a/proxy/main.go +++ b/proxy/main.go @@ -113,8 +113,8 @@ func pipeForward(reader io.Reader, writer io.Writer, prefix string, passthrough if frame.EthernetType == layers.EthernetTypeARP { arpPacket := packet.Layer(layers.LayerTypeARP).(*layers.ARP) log.Debug(prefix, "ARP Type ", arpPacket.Operation) - filterIPb(prefix, &arpPacket.DstProtAddress, &arpPacket.SourceProtAddress, arpPacket.LayerType()) - filterMACb(prefix, &arpPacket.DstHwAddress, &arpPacket.SourceHwAddress, arpPacket.LayerType()) + filterIP(prefix, &arpPacket.DstProtAddress, &arpPacket.SourceProtAddress, arpPacket.LayerType()) + filterMAC(prefix, &arpPacket.DstHwAddress, &arpPacket.SourceHwAddress, arpPacket.LayerType()) } log.Debug("End packet") @@ -150,9 +150,9 @@ func pipeForward(reader io.Reader, writer io.Writer, prefix string, passthrough } } -// filterIP checks whether an IP target in net.IP format equals a given value. If yes, it is changed -func filterIP(prefix string, dst *net.IP, src *net.IP, context gopacket.LayerType) { - var target *net.IP +// filterIP checks whether an IP target selected from src and dst equals a given value. If yes, it is changed +func filterIP(prefix string, dst interface{}, src interface{}, context gopacket.LayerType) { + var target interface{} var condVal net.IP var newVal net.IP var which string @@ -167,38 +167,21 @@ func filterIP(prefix string, dst *net.IP, src *net.IP, context gopacket.LayerTyp condVal = OldIP newVal = NewIP } - if bytes.Equal(*target, condVal) { - *target = newVal + ip, isIp := target.(*net.IP) + bs, isBs := target.(*[]byte) + if isIp && bytes.Equal(*ip, condVal) { + *ip = newVal log.Debugf("%s%s %s IP %s changed to %s", prefix, context, which, condVal, newVal) } -} - -// filterIPb checks whether an IP target in []byte format equals a given value. If yes, it is changed -func filterIPb(prefix string, dst *[]byte, src *[]byte, context gopacket.LayerType) { - var target *[]byte - var condVal net.IP - var newVal net.IP - var which string - if prefix == cmd.In { - target = dst - which = "dst" - condVal = NewIP - newVal = OldIP - } else if prefix == cmd.Out { - target = src - which = "src" - condVal = OldIP - newVal = NewIP - } - if bytes.Equal(*target, condVal) { - *target = newVal + if isBs && bytes.Equal(*bs, condVal) { + *bs = newVal log.Debugf("%s%s %s IP %s changed to %s", prefix, context, which, condVal, newVal) } } -// filterMAC checks whether a MAC target in net.HardwareAddr format equals a given value. If yes, it is changed -func filterMAC(prefix string, dst *net.HardwareAddr, src *net.HardwareAddr, context gopacket.LayerType) { - var target *net.HardwareAddr +// filterMAC checks whether a MAC target selected from src and dst equals a given value. If yes, it is changed +func filterMAC(prefix string, dst interface{}, src interface{}, context gopacket.LayerType) { + var target interface{} var condVal net.HardwareAddr var newVal net.HardwareAddr var which string @@ -213,31 +196,14 @@ func filterMAC(prefix string, dst *net.HardwareAddr, src *net.HardwareAddr, cont condVal = OldMac newVal = NewMac } - if bytes.Equal(*target, condVal) { - *target = newVal + mac, isMac := target.(*net.HardwareAddr) + bs, isBs := target.(*[]byte) + if isMac && bytes.Equal(*mac, condVal) { + *mac = newVal log.Debugf("%s%s %s MAC %s changed to %s", prefix, context, which, condVal, newVal) } -} - -// filterMACb checks whether a MAC target in []byte format equals a given value. If yes, it is changed -func filterMACb(prefix string, dst *[]byte, src *[]byte, context gopacket.LayerType) { - var target *[]byte - var condVal net.HardwareAddr - var newVal net.HardwareAddr - var which string - if prefix == cmd.In { - target = dst - which = "dst" - condVal = NewMac - newVal = OldMac - } else if prefix == cmd.Out { - target = src - which = "src" - condVal = OldMac - newVal = NewMac - } - if bytes.Equal(*target, condVal) { - *target = newVal + if isBs && bytes.Equal(*bs, condVal) { + *bs = newVal log.Debugf("%s%s %s MAC %s changed to %s", prefix, context, which, condVal, newVal) } -} +} \ No newline at end of file