service_matcher-go/cassandra/cassandra.go
2018-09-03 22:36:57 +09:00

89 lines
1.6 KiB
Go

package cassandra
import (
"bytes"
"encoding/binary"
osm "git.loafle.net/overflow/service_matcher-go"
)
type cassandra struct {
Version uint8
Flags uint8
Stream uint16
Opcode uint8
Length uint32
}
type CassandraMatcher struct {
osm.Matchers
}
func (m *CassandraMatcher) Key() string {
return "CASSANDRA"
}
func (m *CassandraMatcher) Name(matchCtx *osm.MatchCtx) string {
return "Cassandra"
}
func (m *CassandraMatcher) IsPrePacket(matchCtx *osm.MatchCtx) bool {
return false
}
func (m *CassandraMatcher) HasResponse(matchCtx *osm.MatchCtx, index int) bool {
return true
}
func (m *CassandraMatcher) IsError(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) bool {
return false
}
func (m *CassandraMatcher) Match(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) error {
if packet == nil || !packet.Valid() {
return osm.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 osm.NotMatchedError()
}
if c.Flags != 0x00 {
return osm.NotMatchedError()
}
if c.Stream != 0x00 {
return osm.NotMatchedError()
}
if c.Opcode != 0x06 {
return osm.NotMatchedError()
}
return nil
}
func NewMatcher() osm.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(osm.NewPacket(writer.Bytes(), writer.Len()))
return m
}