Created
February 10, 2017 19:53
-
-
Save mpobrien/bc76288958b8d5265280a377a1ab36ac to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"crypto/tls" | |
"log" | |
) | |
type OpCode int32 | |
const MsgHeaderLen = 16 | |
// SetInt32 sets the 32-bit int into the given byte array at position post | |
// Taken from gopkg.in/mgo.v2/socket.go | |
func SetInt32(b []byte, pos int, i int32) { | |
b[pos] = byte(i) | |
b[pos+1] = byte(i >> 8) | |
b[pos+2] = byte(i >> 16) | |
b[pos+3] = byte(i >> 24) | |
} | |
func main() { | |
log.SetFlags(log.Lshortfile) | |
conf := &tls.Config{ | |
InsecureSkipVerify: true, | |
} | |
//mongodb://user:<PASSWORD>@cluster0-shard-00-00-f4rbl.mongodb-qa.net:27017,cluster0-shard-00-01-f4rbl.mongodb-qa.net:27017,cluster0-shard-00-02-f4rbl.mongodb-qa.net:27017/<DATABASE>?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin | |
conn, err := tls.Dial("tcp", "localhost:9900", conf) | |
if err != nil { | |
log.Println(err) | |
return | |
} | |
defer conn.Close() | |
ms := MsgHeader{ | |
OpCode: 2011, | |
RequestID: 1000, | |
ResponseTo: 999, | |
MessageLength: 10, | |
} | |
n, err := conn.Write(ms.ToWire()) | |
if err != nil { | |
log.Println(n, err) | |
return | |
} | |
buf := make([]byte, 100) | |
n, err = conn.Read(buf) | |
if err != nil { | |
log.Println(n, err) | |
return | |
} | |
} | |
// MsgHeader is the mongo MessageHeader | |
type MsgHeader struct { | |
// MessageLength is the total message size, including this header | |
MessageLength int32 | |
// RequestID is the identifier for this miessage | |
RequestID int32 | |
// ResponseTo is the RequestID of the message being responded to; | |
// used in DB responses | |
ResponseTo int32 | |
// OpCode is the request type, see consts above. | |
OpCode OpCode | |
} | |
// ToWire converts the MsgHeader to the wire protocol | |
func (m MsgHeader) ToWire() []byte { | |
var d [MsgHeaderLen]byte | |
b := d[:] | |
SetInt32(b, 0, m.MessageLength) | |
SetInt32(b, 4, m.RequestID) | |
SetInt32(b, 8, m.ResponseTo) | |
SetInt32(b, 12, int32(m.OpCode)) | |
return b | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment