如何在 Golang 中包装 net.Conn.Read()

我想包装Read函数 net.Conn.Read()。这样做的目的是读取 SSL 握手消息。https://pkg.go.dev/net#TCPConn.Read


nc, err := net.Dial("tcp", "google.com:443")

if err != nil {

    fmt.Println(err)

}

tls.Client(nc, &tls.Config{})

有什么办法吗?


MMMHUHU
浏览 167回答 2
2回答

牛魔王的故事

使用以下代码拦截 read on a net.Conn: type wrap {     // Conn is the wrapped net.Conn.     // Because it's an embedded field, the      // net.Conn methods are automatically promoted     // to wrap.     net.Conn  } // Read calls through to the wrapped read and // prints the bytes that flow through. Replace // the print statement with whatever is appropriate // for your application. func (w wrap) Read(p []byte) (int, error) {     n, err := w.Conn.Read()     fmt.Printf("%x\n", p[:n]) // example     return n, err }像这样包裹: tnc, err :=tls.Client(wrap{nc}, &tls.Config{})

慕码人8056858

以前的答案确实完成了工作。不过,我会推荐 Liz Rice 的演讲:GopherCon 2018:Liz Rice - The Go Programmer's Guide to Secure Connections浏览她在Github中的代码,您可能会找到一种更优雅的方式来实现您想要的。从第 26 行的客户端代码开始。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go