doubaoxue5788 2016-06-25 22:29
浏览 139

Golang:tls。连接升级后握手挂起

I am writing a TCP text protocol for a project that I am working on. One of the commands in the protocol is STARTTLS which should upgrade the connection to TLS and continue on. My code to upgrade the connection is similar to the answer in this question. The problem I am having is when I am upgrading the TLS connection, the tlsConn.Handshake will hang and never let go. There are some code samples below. Any help is greatly appreciated.

After the STARTTLS command is received...

// 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")

The client is currently upgrading the connection right after it sends the STARTTLS command like this...

c.conn = tls.Client(c.conn, clientTLSConfig)

The server *tls.Config looks like this...

// 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(),
}

The client *tls.Config looks like this...

clientTLSConfig := &tls.Config{
    InsecureSkipVerify: true,
}
  • 写回答

1条回答 默认 最新

  • dtyqflrr775518 2016-06-27 22:13
    关注

    Do you call c.conn.Handshake() or do something else to initiate the TLS handshake on the client side?

    If the client does not initiate the handshake by sending TLS Client Hello, the server will sit forever waiting for it.

    This is my best guess as you did not provide much of the client side code. Also checking with tcpdump would help to narrow the problem down (to server or client side).

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器