service_matcher-go/mongodb/mongodb.go
2018-08-13 16:48:32 +09:00

146 lines
2.8 KiB
Go

package mongodb
import (
"bytes"
"encoding/binary"
"math/rand"
csm "git.loafle.net/commons/service_matcher-go"
)
const (
MONGO_OP_REQUEST uint32 = 2004
MONGO_OP_REPLY uint32 = 1
MONGO_FCNAME string = "admin.$cmd"
MONGO_ELEMENT string = "ismaster"
)
var MONGO_REQUEST_ID uint32
type MsgHeader struct {
MessageLength uint32
RequestId uint32
ResponseTo uint32
OpCode uint32
}
type OP_request struct {
Header MsgHeader
Flags uint32
FullCollectionName [11]byte
NumberToSkip uint32
NumberToReturn int32
DocumentLength uint32
Type_ uint8
Element [9]byte
Value uint8
_ uint8
}
type OP_reply struct {
Header MsgHeader
ResponseFlags int32
CursorID int64
StartingFrom int32
NumberReturned int32
Documents [512]byte
}
type MongoDBMatcher struct {
csm.Matchers
meta csm.Metadata
}
func (m *MongoDBMatcher) Key() string {
return "MONGODB"
}
func (m *MongoDBMatcher) Name() string {
return "MongoDB"
}
func (m *MongoDBMatcher) Meta() csm.Metadata {
return m.meta
}
func (m *MongoDBMatcher) IsPrePacket() bool {
return false
}
func (m *MongoDBMatcher) HasResponse(index int) bool {
return true
}
func (m *MongoDBMatcher) IsError(info csm.MatchInfo, index int, packet *csm.Packet) bool {
return false
}
func (m *MongoDBMatcher) 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)
reply := OP_reply{}
if err := binary.Read(reader, binary.LittleEndian, &reply); err != nil {
return err
}
if uint32(packet.Len) != reply.Header.MessageLength {
return csm.NotMatchedError()
}
if reply.Header.ResponseTo != MONGO_REQUEST_ID {
return csm.NotMatchedError()
}
if reply.Header.OpCode != MONGO_OP_REPLY {
return csm.NotMatchedError()
}
return nil
}
func NewMatcher() csm.Matcher {
mm := &MongoDBMatcher{}
mm.meta = csm.NewMetadata()
tempBuf := new(bytes.Buffer)
binary.Write(tempBuf, binary.BigEndian, OP_request{})
var fcn [11]byte
copy(fcn[:], MONGO_FCNAME)
var elem [9]byte
copy(elem[:], MONGO_ELEMENT)
MONGO_REQUEST_ID = rand.Uint32()
m := OP_request{
Header: MsgHeader{
MessageLength: uint32(len(tempBuf.Bytes())),
RequestId: MONGO_REQUEST_ID,
ResponseTo: 0,
OpCode: MONGO_OP_REQUEST,
},
Flags: 0,
FullCollectionName: fcn,
NumberToSkip: 0,
NumberToReturn: -1,
DocumentLength: 16,
Type_: 0x08,
Element: elem,
Value: 1,
}
writer := new(bytes.Buffer)
binary.Write(writer, binary.LittleEndian, m)
mm.AddPacket(csm.NewPacket(writer.Bytes(), writer.Len()))
return mm
}