service_matcher-go/cassandra/cassandra.go
crusader bce329e44e ing
2018-10-23 13:31:25 +09:00

109 lines
2.0 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(matchCtx *osm.MatchCtx) string {
return "CASSANDRA"
}
func (m *CassandraMatcher) Type(matchCtx *osm.MatchCtx) string {
return "NOSQL"
}
func (m *CassandraMatcher) Vendor(matchCtx *osm.MatchCtx) string {
return "The Apache Software Foundation"
}
func (m *CassandraMatcher) Version(matchCtx *osm.MatchCtx) string {
return "UNKNOWN"
}
func (m *CassandraMatcher) OsType(matchCtx *osm.MatchCtx) string {
return "UNKNOWN"
}
func (m *CassandraMatcher) OsVersion(matchCtx *osm.MatchCtx) string {
return "UNKNOWN"
}
func (m *CassandraMatcher) Name(matchCtx *osm.MatchCtx) string {
return "Cassandra"
}
func (m *CassandraMatcher) IsPrePacket() 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
}