mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-04-23 20:20:31 +03:00
sshd: Add keepalive every 30s
Based on work by @prologic in #147 Fixes #89
This commit is contained in:
parent
3a2ab9bc26
commit
8b9ebbdb6b
@ -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
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user