From 8b9ebbdb6b27f0a00880bd72446e9cac08a4091e Mon Sep 17 00:00:00 2001 From: Andrey Petrov Date: Sun, 17 Jul 2016 20:13:48 -0400 Subject: [PATCH] sshd: Add keepalive every 30s Based on work by @prologic in #147 Fixes #89 --- sshd/terminal.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) 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 }