亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

Go
MMMHUHU 2023-02-06 19:35:44
我想包裝Read函數 net.Conn.Read()。這樣做的目的是讀取 SSL 握手消息。https://pkg.go.dev/net#TCPConn.Readnc, err := net.Dial("tcp", "google.com:443")if err != nil {    fmt.Println(err)}tls.Client(nc, &tls.Config{})有什么辦法嗎?
查看完整描述

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{})


查看完整回答
反對 回復 2023-02-06
?
慕碼人8056858

TA貢獻1803條經驗 獲得超6個贊

以前的答案確實完成了工作。

不過,我會推薦 Liz Rice 的演講:GopherCon 2018:Liz Rice - The Go Programmer's Guide to Secure Connections

瀏覽她在Github中的代碼,您可能會找到一種更優雅的方式來實現您想要的。

從第 26 行的客戶端代碼開始。


查看完整回答
反對 回復 2023-02-06
  • 2 回答
  • 0 關注
  • 200 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號