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 2020-01-06 20:09:34 -05:00 committed by GitHub
parent 1b2a3e97a0
commit 5af617f3b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 0 deletions

2
go.mod
View File

@ -8,3 +8,5 @@ require (
golang.org/x/crypto v0.0.0-20190320223903-b7391e95e576 golang.org/x/crypto v0.0.0-20190320223903-b7391e95e576
golang.org/x/sys v0.0.0-20190321052220-f7bb7a8bee54 golang.org/x/sys v0.0.0-20190321052220-f7bb7a8bee54
) )
go 1.13

View File

@ -2,6 +2,7 @@ package sshd
import ( import (
"net" "net"
"time"
"github.com/shazow/rateio" "github.com/shazow/rateio"
"golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh"
@ -32,6 +33,12 @@ func (l *SSHListener) handleConn(conn net.Conn) (*Terminal, error) {
conn = ReadLimitConn(conn, l.RateLimit()) 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 // Upgrade TCP connection to SSH connection
sshConn, channels, requests, err := ssh.NewServerConn(conn, l.config) sshConn, channels, requests, err := ssh.NewServerConn(conn, l.config)
if err != nil { if err != nil {