mongo.go 864 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package l7
  2. import (
  3. "encoding/binary"
  4. "go.mongodb.org/mongo-driver/bson"
  5. )
  6. const (
  7. MongoOpMSG = 2013
  8. mongoHeaderLength = 20
  9. mongoOpCodeOffset = 12
  10. mongoSectionKindLength = 1
  11. mongoSectionSizeLength = 4
  12. mongoSectionKindBody = 0
  13. )
  14. func ParseMongo(payload []byte) (res string) {
  15. res = "<truncated>"
  16. if len(payload) < mongoHeaderLength+mongoSectionKindLength+mongoSectionSizeLength {
  17. return
  18. }
  19. opCode := binary.LittleEndian.Uint32(payload[mongoOpCodeOffset:])
  20. if opCode != MongoOpMSG {
  21. return
  22. }
  23. sectionKind := payload[mongoHeaderLength]
  24. if sectionKind != mongoSectionKindBody {
  25. return
  26. }
  27. sectionData := payload[mongoHeaderLength+mongoSectionKindLength:]
  28. sectionLength := binary.LittleEndian.Uint32(sectionData)
  29. if sectionLength < 1 || int(sectionLength) > len(sectionData) {
  30. return
  31. }
  32. return bson.Raw(sectionData).String()
  33. }