我正在为我正在进行的项目编写 TCP 文本协议(protocol)。协议(protocol)中的命令之一是 STARTTLS
,它应该将连接升级到 TLS 并继续。我升级连接的代码类似于 this question 中的答案。 .我遇到的问题是当我升级 TLS 连接时,tlsConn.Handshake
将挂起并且永远不会松开。下面有一些代码示例。非常感谢任何帮助。
收到STARTTLS
命令后...
// Init a new TLS connection. I need a *tls.Conn type
// so that I can do the Handshake()
s.Logf("++> Upgrading connection to TLS")
tlsConn := tls.Server(s.Conn, s.Server.TLSConfig)
s.Logf("++> Attempting TLS Handshake")
tlsConn.Handshake()
s.Logf("++> TLS Handshake Successful")
// Here is the trick. Since I do not need to access
// any of the TLS functions anymore,
// I can convert tlsConn back in to a net.Conn type
s.Conn = net.Conn(tlsConn)
s.Logf("++> Updating read/write buffers")
s.reader = textproto.NewReader(bufio.NewReader(s.Conn))
s.writer = textproto.NewWriter(bufio.NewWriter(s.Conn))
s.Printf("100 SUCCESS")
客户端在发送 STARTTLS
命令后立即升级连接...
c.conn = tls.Client(c.conn, clientTLSConfig)
服务器 *tls.Config
看起来像这样......
// Load the key and certificate - paths are provided in flags.
cert, err := tls.LoadX509KeyPair(flagTLSCert, flagTLSKey)
if err != nil {
log.Fatal(err)
}
// Create the TLS config
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{cert},
ClientAuth: tls.VerifyClientCertIfGiven,
ServerName: fqdn(),
}
客户端 *tls.Config
看起来像这样......
clientTLSConfig := &tls.Config{
InsecureSkipVerify: true,
}
最佳答案
您是调用 c.conn.Handshake() 还是执行其他操作以在客户端发起 TLS 握手?
如果客户端没有通过发送 TLS Client Hello 来启动握手,服务器将永远等待它。
这是我最好的猜测,因为您没有提供很多客户端代码。此外,使用 tcpdump 检查将有助于缩小问题范围(到服务器或客户端)。
关于ssl - Golang : tls. 连接升级后握手挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38033511/