为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 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 关注
  • 166 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号