service_matcher-go/cassandra/cassandra.go
crusader 6ce87348ba ing
2018-09-03 15:42:56 +09:00

93 lines
1.5 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() string {
return "Cassandra"
}
func (m *CassandraMatcher) Meta() osm.Metadata {
return nil
}
func (m *CassandraMatcher) IsPrePacket() bool {
return false
}
func (m *CassandraMatcher) HasResponse(index int) bool {
return true
}
func (m *CassandraMatcher) IsError(info osm.MatchInfo, index int, packet *osm.Packet) bool {
return false
}
func (m *CassandraMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) error {
if packet == nil || !packet.Valid() {
return osm.NoPacketReceivedError()
}
reader := new(bytes.Buffer)
reader.Write(packet.Bytes())
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
}