sshd: Add keepalive every 30s
authorAndrey Petrov <andrey.petrov@shazow.net>
Mon, 18 Jul 2016 00:13:48 +0000 (20:13 -0400)
committerAndrey Petrov <andrey.petrov@shazow.net>
Mon, 18 Jul 2016 00:13:48 +0000 (20:13 -0400)
Based on work by @prologic in #147
Fixes #89

sshd/terminal.go

index 3da2d651bd2381060bb7ac69402a004bba33b360..6a92bf95e1a79dcd02a7a491ff503b2d6c81883d 100644 (file)
@@ -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
 }