我想包裝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{})
有什么辦法嗎?
提前致謝
uj5u.com熱心網友回復:
使用以下代碼在 net.Conn 上攔截 Read:
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{})
uj5u.com熱心網友回復:
前面的答案確實完成了作業。
不過我會推薦 Liz Rice 的演講:GopherCon 2018:Liz Rice - Go 程式員安全連接指南
瀏覽她在Github中的代碼,你可能會找到一種更優雅的方式來實作你想要的。
從第 26 行的客戶端代碼開始。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/503751.html
下一篇:TLS1.3中的延遲證書
