-
Notifications
You must be signed in to change notification settings - Fork 127
/
filters.go
41 lines (33 loc) · 976 Bytes
/
filters.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
//
// date : 2016-05-13
// author: xjdrew
//
package kone
import (
"io"
"github.com/xjdrew/kone/tcpip"
)
type PacketFilter interface {
Filter(wr io.Writer, p tcpip.IPv4Packet)
}
type PacketFilterFunc func(wr io.Writer, p tcpip.IPv4Packet)
func (f PacketFilterFunc) Filter(wr io.Writer, p tcpip.IPv4Packet) {
f(wr, p)
}
func icmpFilterFunc(wr io.Writer, ipPacket tcpip.IPv4Packet) {
icmpPacket := tcpip.ICMPPacket(ipPacket.Payload())
if icmpPacket.Type() == tcpip.ICMPRequest && icmpPacket.Code() == 0 {
logger.Debugf("[icmp filter] ping %s > %s", ipPacket.SourceIP(), ipPacket.DestinationIP())
// forge a reply
icmpPacket.SetType(tcpip.ICMPEcho)
srcIP := ipPacket.SourceIP()
dstIP := ipPacket.DestinationIP()
ipPacket.SetSourceIP(dstIP)
ipPacket.SetDestinationIP(srcIP)
icmpPacket.ResetChecksum()
ipPacket.ResetChecksum()
wr.Write(ipPacket)
} else {
logger.Debugf("icmp: %s -> %s", ipPacket.SourceIP(), ipPacket.DestinationIP())
}
}