sshd: Apply read deadline to connection handler (#331)

This should prevent connections from stalling out and eating up file descriptors without ever joining the chat.
This commit is contained in:
Andrey Petrov authored and GitHub committed 2020-01-06 20:09:34 -05:00
1 parent 1b2a3e97a0
commit 5af617f3b9
2 files changed
+9

No files matched your search

+2
View File
@@ -8,3 +8,5 @@ require (
golang.org/x/crypto v0.0.0-20190320223903-b7391e95e576
golang.org/x/sys v0.0.0-20190321052220-f7bb7a8bee54
)
go 1.13
+7
View File
@@ -2,6 +2,7 @@ package sshd
import (
"net"
"time"
"github.com/shazow/rateio"
"golang.org/x/crypto/ssh"
@@ -32,6 +33,12 @@ func (l *SSHListener) handleConn(conn net.Conn) (*Terminal, error) {
conn = ReadLimitConn(conn, l.RateLimit())
}
// If the connection doesn't write anything back for too long before we get
// a valid session, it should be dropped.
var handleTimeout = 20 * time.Second
conn.SetReadDeadline(time.Now().Add(handleTimeout))
defer conn.SetReadDeadline(time.Time{})
// Upgrade TCP connection to SSH connection
sshConn, channels, requests, err := ssh.NewServerConn(conn, l.config)
if err != nil {