Skip to content

Commit 8c4bbc0

Browse files
committed
Allow setting timeout during TLS Handshake
Buggy clients might never respond during the TLS handshake phase. This change adds a config setting to set a read timeout before calling handshake. I think the handshake involves multiple reads but this setting should help with clients who never respond.
1 parent 307370c commit 8c4bbc0

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

server.go

+8
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type Server struct {
3838
handler Handler
3939
lastError error
4040
readTimeoutMilliseconds int64
41+
tlsHandshakeTimeout time.Duration
4142
tlsPeerNameFunc TlsPeerNameFunc
4243
datagramPool sync.Pool
4344
}
@@ -66,6 +67,10 @@ func (s *Server) SetTimeout(millseconds int64) {
6667
s.readTimeoutMilliseconds = millseconds
6768
}
6869

70+
func (s *Server) SetTlsHandshakeTimeout(d time.Duration) {
71+
s.tlsHandshakeTimeout = d
72+
}
73+
6974
// Set the function that extracts a TLS peer name from the TLS connection
7075
func (s *Server) SetTlsPeerNameFunc(tlsPeerNameFunc TlsPeerNameFunc) {
7176
s.tlsPeerNameFunc = tlsPeerNameFunc
@@ -206,6 +211,9 @@ func (s *Server) goScanConnection(connection net.Conn) {
206211
tlsPeer := ""
207212
if tlsConn, ok := connection.(*tls.Conn); ok {
208213
// Handshake now so we get the TLS peer information
214+
if s.tlsHandshakeTimeout > 0 {
215+
tlsConn.SetDeadline(time.Now().Add(s.tlsHandshakeTimeout))
216+
}
209217
if err := tlsConn.Handshake(); err != nil {
210218
connection.Close()
211219
return

0 commit comments

Comments
 (0)