2 回答

TA貢獻1830條經驗 獲得超3個贊
使用以下代碼攔截 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{})

TA貢獻1803條經驗 獲得超6個贊
以前的答案確實完成了工作。
不過,我會推薦 Liz Rice 的演講:GopherCon 2018:Liz Rice - The Go Programmer's Guide to Secure Connections
瀏覽她在Github中的代碼,您可能會找到一種更優雅的方式來實現您想要的。
從第 26 行的客戶端代碼開始。
- 2 回答
- 0 關注
- 200 瀏覽
添加回答
舉報