mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-06-13 13:52:08 +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"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
"time"
|
||||||
|
|
||||||
"golang.org/x/crypto/ssh"
|
"golang.org/x/crypto/ssh"
|
||||||
"golang.org/x/crypto/ssh/terminal"
|
"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.
|
// Connection is an interface with fields necessary to operate an sshd host.
|
||||||
type Connection interface {
|
type Connection interface {
|
||||||
PublicKey() ssh.PublicKey
|
PublicKey() ssh.PublicKey
|
||||||
@ -72,6 +76,17 @@ func NewTerminal(conn *ssh.ServerConn, ch ssh.NewChannel) (*Terminal, error) {
|
|||||||
channel.Close()
|
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
|
return &term, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user