86 lines
1.3 KiB
Go
86 lines
1.3 KiB
Go
|
package matcher
|
||
|
|
||
|
import "fmt"
|
||
|
|
||
|
type Matcher interface {
|
||
|
Key() string
|
||
|
Name() string
|
||
|
|
||
|
Meta() Metadata
|
||
|
|
||
|
IsPrePacket() bool
|
||
|
PacketCount() int
|
||
|
Packet(index int) *Packet
|
||
|
HasResponse(index int) bool
|
||
|
|
||
|
Match(info MatchInfo, index int, packet *Packet) error
|
||
|
}
|
||
|
|
||
|
type Metadata map[string]string
|
||
|
|
||
|
func NewMetadata() Metadata {
|
||
|
return make(map[string]string)
|
||
|
}
|
||
|
|
||
|
type UDPMatcher interface {
|
||
|
Matcher
|
||
|
IsSend(port int) bool
|
||
|
}
|
||
|
|
||
|
type Matchers struct {
|
||
|
packets []*Packet
|
||
|
}
|
||
|
|
||
|
func (m *Matchers) PacketCount() int {
|
||
|
return len(m.packets)
|
||
|
}
|
||
|
|
||
|
func (m *Matchers) Packet(index int) *Packet {
|
||
|
return m.packets[index]
|
||
|
}
|
||
|
|
||
|
func (m *Matchers) HasResponse(index int) bool {
|
||
|
return len(m.packets)-1 > index
|
||
|
}
|
||
|
|
||
|
func (m *Matchers) AddPacket(packet *Packet) {
|
||
|
m.packets = append(m.packets, packet)
|
||
|
}
|
||
|
|
||
|
type MatchInfo interface {
|
||
|
IP() string
|
||
|
Port() int
|
||
|
}
|
||
|
|
||
|
type simpleMatchInfo struct {
|
||
|
ip string
|
||
|
port int
|
||
|
}
|
||
|
|
||
|
func (mi *simpleMatchInfo) IP() string {
|
||
|
return mi.ip
|
||
|
}
|
||
|
|
||
|
func (mi *simpleMatchInfo) Port() int {
|
||
|
return mi.port
|
||
|
}
|
||
|
|
||
|
func NewMatchInfo(ip string, port int) MatchInfo {
|
||
|
return &simpleMatchInfo{
|
||
|
ip: ip,
|
||
|
port: port,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
const (
|
||
|
noPacketReceived = 1000 + iota
|
||
|
notMatched
|
||
|
)
|
||
|
|
||
|
func NoPacketReceivedError() error {
|
||
|
return fmt.Errorf("[%d] %s", noPacketReceived, "No packet received.")
|
||
|
}
|
||
|
func NotMatchedError() error {
|
||
|
return fmt.Errorf("[%d] %s", notMatched, "Not matched packet.")
|
||
|
}
|