init
This commit is contained in:
145
mongodb/mongodb.go
Normal file
145
mongodb/mongodb.go
Normal file
@@ -0,0 +1,145 @@
|
||||
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
|
||||
}
|
||||
55
mongodb/mongodb_test.go
Normal file
55
mongodb/mongodb_test.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package mongodb
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
csm "git.loafle.net/commons/service_matcher-go"
|
||||
)
|
||||
|
||||
func TestMongoNor(t *testing.T) {
|
||||
|
||||
conn, _ := net.Dial("tcp", "192.168.1.229:27036")
|
||||
|
||||
defer conn.Close()
|
||||
|
||||
MongoRun(conn, t)
|
||||
|
||||
}
|
||||
|
||||
func TestMongoTLS(t *testing.T) {
|
||||
conn, _ := tls.Dial(
|
||||
"tcp",
|
||||
"192.168.1.229:27036",
|
||||
&tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
ServerName: "192.168.1.229",
|
||||
},
|
||||
)
|
||||
|
||||
defer conn.Close()
|
||||
|
||||
MongoRun(conn, t)
|
||||
}
|
||||
|
||||
func MongoRun(conn net.Conn, t *testing.T) {
|
||||
|
||||
m := NewMatcher()
|
||||
|
||||
for i := 0; i < m.PacketCount(); i++ {
|
||||
|
||||
pack := m.Packet(i)
|
||||
conn.Write(pack.Buffer)
|
||||
bytes := make([]byte, 1024)
|
||||
n, _ := conn.Read(bytes)
|
||||
p := csm.NewPacket(bytes, n)
|
||||
|
||||
if err := m.Match(nil, i, p); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
}
|
||||
t.Log(m.Name())
|
||||
t.Log(m.Meta())
|
||||
}
|
||||
Reference in New Issue
Block a user