8bc927cf91
This reverts commit 7e4a2f1775
100 lines
1.9 KiB
Go
100 lines
1.9 KiB
Go
package matcher
|
|
|
|
import (
|
|
"fmt"
|
|
)
|
|
|
|
type Matcher interface {
|
|
Key() string
|
|
Type() string
|
|
Vendor(matchCtx *MatchCtx) string
|
|
Version(matchCtx *MatchCtx) string
|
|
OsType(matchCtx *MatchCtx) string
|
|
OsVersion(matchCtx *MatchCtx) string
|
|
Name(matchCtx *MatchCtx) string
|
|
|
|
IsPrePacket() bool
|
|
PacketCount(matchCtx *MatchCtx) int
|
|
Packet(matchCtx *MatchCtx, index int) *Packet
|
|
HasResponse(matchCtx *MatchCtx, index int) bool
|
|
|
|
Match(matchCtx *MatchCtx, index int, packet *Packet) error
|
|
}
|
|
|
|
type UDPMatcher interface {
|
|
Matcher
|
|
IsSend(port int) bool
|
|
}
|
|
|
|
type Matchers struct {
|
|
packets []*Packet
|
|
}
|
|
|
|
func (m *Matchers) PacketCount(matchCtx *MatchCtx) int {
|
|
return len(m.packets)
|
|
}
|
|
|
|
func (m *Matchers) Packet(matchCtx *MatchCtx, index int) *Packet {
|
|
return m.packets[index]
|
|
}
|
|
|
|
func (m *Matchers) HasResponse(matchCtx *MatchCtx, index int) bool {
|
|
return len(m.packets)-1 > index
|
|
}
|
|
|
|
func (m *Matchers) AddPacket(packet *Packet) {
|
|
m.packets = append(m.packets, packet)
|
|
}
|
|
|
|
type MatchCtx struct {
|
|
address string
|
|
port int
|
|
attributes map[string]string
|
|
}
|
|
|
|
func (mc *MatchCtx) Address() string {
|
|
return mc.address
|
|
}
|
|
|
|
func (mc *MatchCtx) Port() int {
|
|
return mc.port
|
|
}
|
|
|
|
func (mc *MatchCtx) GetAttribute(key string) (value string, ok bool) {
|
|
value, ok = mc.attributes[key]
|
|
|
|
return
|
|
}
|
|
|
|
func (mc *MatchCtx) GetAttributes() map[string]string {
|
|
return mc.attributes
|
|
}
|
|
|
|
func (mc *MatchCtx) SetAttribute(key string, value string) {
|
|
mc.attributes[key] = value
|
|
}
|
|
|
|
func (mc *MatchCtx) InitAttribute() {
|
|
mc.attributes = make(map[string]string)
|
|
}
|
|
|
|
func NewMatchCtx(address string, port int) *MatchCtx {
|
|
return &MatchCtx{
|
|
address: address,
|
|
port: port,
|
|
attributes: make(map[string]string),
|
|
}
|
|
}
|
|
|
|
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.")
|
|
}
|