A: go.mod

This commit is contained in:
Shuo
2020-03-01 15:58:57 +08:00
parent e209ea8ccc
commit 4f174b3415
21 changed files with 394 additions and 386 deletions

View File

@@ -279,7 +279,7 @@ var nullBytes = []byte("null")
func (id *ObjectId) UnmarshalJSON(data []byte) error {
if len(data) > 0 && (data[0] == '{' || data[0] == 'O') {
var v struct {
Id json.RawMessage `json:"$oid"`
Id json.RawMessage `json:"$oid"`
Func struct {
Id json.RawMessage
} `json:"$oidFunc"`

View File

@@ -58,7 +58,7 @@ func (d Decimal128) String() string {
// Bits: 1*sign 2*ignored 14*exponent 111*significand.
// Implicit 0b100 prefix in significand.
e = int(d.h>>47&(1<<14-1)) - 6176
//h = 4<<47 | d.h&(1<<47-1)
// h = 4<<47 | d.h&(1<<47-1)
// Spec says all of these values are out of range.
h, l = 0, 0
} else {

View File

@@ -4,9 +4,10 @@ import (
"bytes"
"encoding/base64"
"fmt"
"github.com/40t/go-sniffer/plugSrc/mongodb/build/internal/json"
"strconv"
"time"
"github.com/40t/go-sniffer/plugSrc/mongodb/build/internal/json"
)
// UnmarshalJSON unmarshals a JSON value that may hold non-standard

View File

@@ -1,17 +1,17 @@
package build
const (
OP_REPLY = 1 //Reply to a client request. responseTo is set.
OP_UPDATE = 2001 //Update document.
OP_INSERT = 2002 //Insert new document.
RESERVED = 2003 //Formerly used for OP_GET_BY_OID.
OP_REPLY = 1 // Reply to a client request. responseTo is set.
OP_UPDATE = 2001 // Update document.
OP_INSERT = 2002 // Insert new document.
RESERVED = 2003 // Formerly used for OP_GET_BY_OID.
OP_QUERY = 2004 //Query a collection.
OP_GET_MORE = 2005 //Get more data from a query. See Cursors.
OP_DELETE = 2006 //Delete documents.
OP_KILL_CURSORS = 2007 //Notify database that the client has finished with the cursor.
OP_QUERY = 2004 // Query a collection.
OP_GET_MORE = 2005 // Get more data from a query. See Cursors.
OP_DELETE = 2006 // Delete documents.
OP_KILL_CURSORS = 2007 // Notify database that the client has finished with the cursor.
OP_COMMAND = 2010 //Cluster internal protocol representing a command request.
OP_COMMANDREPLY = 2011 //Cluster internal protocol representing a reply to an OP_COMMAND.
OP_MSG = 2013 //Send a message using the format introduced in MongoDB 3.6.
)
OP_COMMAND = 2010 // Cluster internal protocol representing a command request.
OP_COMMANDREPLY = 2011 // Cluster internal protocol representing a reply to an OP_COMMAND.
OP_MSG = 2013 // Send a message using the format introduced in MongoDB 3.6.
)

View File

@@ -4,13 +4,14 @@ import (
"bytes"
"encoding/binary"
"fmt"
"github.com/google/gopacket"
"io"
"strconv"
"github.com/google/gopacket"
)
const (
Port = 27017
Port = 27017
Version = "0.1"
CmdPort = "-p"
)
@@ -26,15 +27,14 @@ type stream struct {
}
type packet struct {
isClientFlow bool //client->server
isClientFlow bool // client->server
messageLength int
requestID int
responseTo int
opCode int //request type
opCode int // request type
payload io.Reader
payload io.Reader
}
var mongodbInstance *Mongodb
@@ -42,29 +42,29 @@ var mongodbInstance *Mongodb
func NewInstance() *Mongodb {
if mongodbInstance == nil {
mongodbInstance = &Mongodb{
port :Port,
version:Version,
source: make(map[string]*stream),
port: Port,
version: Version,
source: make(map[string]*stream),
}
}
return mongodbInstance
}
func (m *Mongodb) SetFlag(flg []string) {
func (m *Mongodb) SetFlag(flg []string) {
c := len(flg)
if c == 0 {
return
}
if c >> 1 != 1 {
if c>>1 != 1 {
panic("ERR : Mongodb Number of parameters")
}
for i:=0;i<c;i=i+2 {
for i := 0; i < c; i = i + 2 {
key := flg[i]
val := flg[i+1]
switch key {
case CmdPort:
p, err := strconv.Atoi(val);
p, err := strconv.Atoi(val)
if err != nil {
panic("ERR : port")
}
@@ -80,7 +80,7 @@ func (m *Mongodb) SetFlag(flg []string) {
}
func (m *Mongodb) BPFFilter() string {
return "tcp and port "+strconv.Itoa(m.port);
return "tcp and port " + strconv.Itoa(m.port)
}
func (m *Mongodb) Version() string {
@@ -89,22 +89,22 @@ func (m *Mongodb) Version() string {
func (m *Mongodb) ResolveStream(net, transport gopacket.Flow, buf io.Reader) {
//uuid
// uuid
uuid := fmt.Sprintf("%v:%v", net.FastHash(), transport.FastHash())
//resolve packet
// resolve packet
if _, ok := m.source[uuid]; !ok {
var newStream = stream {
packets:make(chan *packet, 100),
var newStream = stream{
packets: make(chan *packet, 100),
}
m.source[uuid] = &newStream
go newStream.resolve()
}
//read bi-directional packet
//server -> client || client -> server
// read bi-directional packet
// server -> client || client -> server
for {
newPacket := m.newPacket(net, transport, buf)
@@ -118,12 +118,12 @@ func (m *Mongodb) ResolveStream(net, transport gopacket.Flow, buf io.Reader) {
func (m *Mongodb) newPacket(net, transport gopacket.Flow, r io.Reader) *packet {
//read packet
// read packet
var packet *packet
var err error
packet, err = readStream(r)
//stream close
// stream close
if err == io.EOF {
fmt.Println(net, transport, " close")
return nil
@@ -132,10 +132,10 @@ func (m *Mongodb) newPacket(net, transport gopacket.Flow, r io.Reader) *packet {
return nil
}
//set flow direction
// set flow direction
if transport.Src().String() == strconv.Itoa(m.port) {
packet.isClientFlow = false
}else{
} else {
packet.isClientFlow = true
}
@@ -145,7 +145,7 @@ func (m *Mongodb) newPacket(net, transport gopacket.Flow, r io.Reader) *packet {
func (stm *stream) resolve() {
for {
select {
case packet := <- stm.packets:
case packet := <-stm.packets:
if packet.isClientFlow {
stm.resolveClientPacket(packet)
} else {
@@ -165,11 +165,11 @@ func (stm *stream) resolveClientPacket(pk *packet) {
switch pk.opCode {
case OP_UPDATE:
zero := ReadInt32(pk.payload)
zero := ReadInt32(pk.payload)
fullCollectionName := ReadString(pk.payload)
flags := ReadInt32(pk.payload)
selector := ReadBson2Json(pk.payload)
update := ReadBson2Json(pk.payload)
flags := ReadInt32(pk.payload)
selector := ReadBson2Json(pk.payload)
update := ReadBson2Json(pk.payload)
_ = zero
_ = flags
@@ -180,9 +180,9 @@ func (stm *stream) resolveClientPacket(pk *packet) {
)
case OP_INSERT:
flags := ReadInt32(pk.payload)
flags := ReadInt32(pk.payload)
fullCollectionName := ReadString(pk.payload)
command := ReadBson2Json(pk.payload)
command := ReadBson2Json(pk.payload)
_ = flags
msg = fmt.Sprintf(" [Insert] [coll:%s] %v",
@@ -191,16 +191,16 @@ func (stm *stream) resolveClientPacket(pk *packet) {
)
case OP_QUERY:
flags := ReadInt32(pk.payload)
flags := ReadInt32(pk.payload)
fullCollectionName := ReadString(pk.payload)
numberToSkip := ReadInt32(pk.payload)
numberToReturn := ReadInt32(pk.payload)
numberToSkip := ReadInt32(pk.payload)
numberToReturn := ReadInt32(pk.payload)
_ = flags
_ = numberToSkip
_ = numberToReturn
command := ReadBson2Json(pk.payload)
selector := ReadBson2Json(pk.payload)
command := ReadBson2Json(pk.payload)
selector := ReadBson2Json(pk.payload)
msg = fmt.Sprintf(" [Query] [coll:%s] %v %v",
fullCollectionName,
@@ -209,11 +209,11 @@ func (stm *stream) resolveClientPacket(pk *packet) {
)
case OP_COMMAND:
database := ReadString(pk.payload)
commandName := ReadString(pk.payload)
metaData := ReadBson2Json(pk.payload)
commandArgs := ReadBson2Json(pk.payload)
inputDocs := ReadBson2Json(pk.payload)
database := ReadString(pk.payload)
commandName := ReadString(pk.payload)
metaData := ReadBson2Json(pk.payload)
commandArgs := ReadBson2Json(pk.payload)
inputDocs := ReadBson2Json(pk.payload)
msg = fmt.Sprintf(" [Commend] [DB:%s] [Cmd:%s] %v %v %v",
database,
@@ -224,10 +224,10 @@ func (stm *stream) resolveClientPacket(pk *packet) {
)
case OP_GET_MORE:
zero := ReadInt32(pk.payload)
zero := ReadInt32(pk.payload)
fullCollectionName := ReadString(pk.payload)
numberToReturn := ReadInt32(pk.payload)
cursorId := ReadInt64(pk.payload)
numberToReturn := ReadInt32(pk.payload)
cursorId := ReadInt64(pk.payload)
_ = zero
msg = fmt.Sprintf(" [Query more] [coll:%s] [num of reply:%v] [cursor:%v]",
@@ -237,10 +237,10 @@ func (stm *stream) resolveClientPacket(pk *packet) {
)
case OP_DELETE:
zero := ReadInt32(pk.payload)
zero := ReadInt32(pk.payload)
fullCollectionName := ReadString(pk.payload)
flags := ReadInt32(pk.payload)
selector := ReadBson2Json(pk.payload)
flags := ReadInt32(pk.payload)
selector := ReadBson2Json(pk.payload)
_ = zero
_ = flags
@@ -263,10 +263,10 @@ func readStream(r io.Reader) (*packet, error) {
var buf bytes.Buffer
p := &packet{}
//header
// header
header := make([]byte, 16)
if _, err := io.ReadFull(r, header); err != nil {
return nil,err
return nil, err
}
// message length

View File

@@ -773,7 +773,7 @@ func (d *decodeState) isNull(off int) bool {
// name consumes a const or function from d.data[d.off-1:], decoding into the value v.
// the first byte of the function name has been read already.
func (d *decodeState) name(v reflect.Value) {
if d.isNull(d.off-1) {
if d.isNull(d.off - 1) {
d.literal(v)
return
}
@@ -859,7 +859,7 @@ func (d *decodeState) name(v reflect.Value) {
}
// TODO Fix case of func field as map.
//topv := v
// topv := v
// Figure out field corresponding to function.
key := []byte(funcData.key)
@@ -1076,9 +1076,9 @@ func (d *decodeState) storeKeyed(v reflect.Value) bool {
}
var (
trueBytes = []byte("true")
trueBytes = []byte("true")
falseBytes = []byte("false")
nullBytes = []byte("null")
nullBytes = []byte("null")
)
func (d *decodeState) storeValue(v reflect.Value, from interface{}) {
@@ -1173,7 +1173,7 @@ var numberType = reflect.TypeOf(Number(""))
func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool) {
// Check for unmarshaler.
if len(item) == 0 {
//Empty string given
// Empty string given
d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type()))
return
}

View File

@@ -4,8 +4,9 @@ import (
"encoding/binary"
"encoding/json"
"fmt"
"time"
"io"
"time"
"github.com/40t/go-sniffer/plugSrc/mongodb/build/bson"
)
@@ -15,7 +16,7 @@ func GetNowStr(isClient bool) string {
msg += time.Now().Format(layout)
if isClient {
msg += "| cli -> ser |"
}else{
} else {
msg += "| ser -> cli |"
}
return msg
@@ -54,33 +55,32 @@ func ReadString(r io.Reader) string {
return string(result)
}
func ReadBson2Json(r io.Reader) (string) {
func ReadBson2Json(r io.Reader) string {
//read len
// read len
docLen := ReadInt32(r)
if docLen == 0 {
return ""
}
//document []byte
// document []byte
docBytes := make([]byte, int(docLen))
binary.LittleEndian.PutUint32(docBytes, uint32(docLen))
if _, err := io.ReadFull(r, docBytes[4:]); err != nil {
panic(err)
}
//resolve document
// resolve document
var bsn bson.M
err := bson.Unmarshal(docBytes, &bsn)
if err != nil {
panic(err)
}
//format to Json
// format to Json
jsonStr, err := json.Marshal(bsn)
if err != nil {
return fmt.Sprintf("{\"error\":%s}", err.Error())
}
return string(jsonStr)
}