diff --git a/sshd/terminal.go b/sshd/terminal.go index 3da2d65..6a92bf9 100644 --- a/sshd/terminal.go +++ b/sshd/terminal.go @@ -4,11 +4,15 @@ import ( "errors" "fmt" "net" + "time" "golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh/terminal" ) +var keepaliveInterval = time.Second * 30 +var keepaliveRequest = "keepalive@ssh-chat" + // Connection is an interface with fields necessary to operate an sshd host. type Connection interface { PublicKey() ssh.PublicKey @@ -72,6 +76,17 @@ func NewTerminal(conn *ssh.ServerConn, ch ssh.NewChannel) (*Terminal, error) { channel.Close() }() + go func() { + for range time.Tick(keepaliveInterval) { + _, err := channel.SendRequest(keepaliveRequest, true, nil) + if err != nil { + // Connection is gone + conn.Close() + return + } + } + }() + return &term, nil }