service_matcher-go/cassandra/cassandra.go

93 lines
1.6 KiB
Go
Raw Normal View History

2018-08-13 07:48:32 +00:00
package cassandra
import (
"bytes"
"encoding/binary"
csm "git.loafle.net/commons/service_matcher-go"
)
type cassandra struct {
Version uint8
Flags uint8
Stream uint16
Opcode uint8
Length uint32
}
type CassandraMatcher struct {
csm.Matchers
}
func (m *CassandraMatcher) Key() string {
return "CASSANDRA"
}
func (m *CassandraMatcher) Name() string {
return "Cassandra"
}
func (m *CassandraMatcher) Meta() csm.Metadata {
return nil
}
func (m *CassandraMatcher) IsPrePacket() bool {
return false
}
func (m *CassandraMatcher) HasResponse(index int) bool {
return true
}
func (m *CassandraMatcher) IsError(info csm.MatchInfo, index int, packet *csm.Packet) bool {
return false
}
func (m *CassandraMatcher) Match(info csm.MatchInfo, index int, packet *csm.Packet) error {
if packet == nil || packet.Buffer == nil || packet.Len == 0 {
return csm.NoPacketReceivedError()
}
reader := new(bytes.Buffer)
reader.Write(packet.Buffer)
c := cassandra{}
if err := binary.Read(reader, binary.BigEndian, &c); err != nil {
return err
}
if c.Version != 0x84 {
return csm.NotMatchedError()
}
if c.Flags != 0x00 {
return csm.NotMatchedError()
}
if c.Stream != 0x00 {
return csm.NotMatchedError()
}
if c.Opcode != 0x06 {
return csm.NotMatchedError()
}
return nil
}
func NewMatcher() csm.Matcher {
m := &CassandraMatcher{}
c := cassandra{
Version: 4,
Flags: 0,
Stream: 0,
Opcode: 5,
Length: 0,
}
writer := new(bytes.Buffer)
binary.Write(writer, binary.LittleEndian, c)
m.AddPacket(csm.NewPacket(writer.Bytes(), writer.Len()))
return m
}