ssh
ssh copied to clipboard
Add option for keep-alive messages
Pretty self explanatory. Just sending keepalive@whatever as a request which needs a reply is enough to get both sides to send a little data to keep the connection living. openssh uses keepalive@openssh (or something very similar) this way.
👍
Here's how we do it in ssh-chat: https://github.com/shazow/ssh-chat/blob/ee8d60ec00526be52d2acadfb088bad96790640d/sshd/terminal.go#L87-L104
That looks almost identical to how I've done it in the past as well... Though my personal preference is to do it at the connection level so you don't need separate keep-alives for every channel.
you can use TCP keepalives for this relatively easily
err = ssh.ListenAndServe(
":22",
nil,
ssh.WrapConn(func(s ssh.Context, conn net.Conn) net.Conn {
conn.(*net.TCPConn).SetKeepAlive(true)
conn.(*net.TCPConn).SetKeepAlivePeriod(time.Second * 100)
return conn
}),
)
that's pretty cool. i think people avoid tcp keepalives though because of proxies but ymmv
you can use TCP keepalives for this relatively easily
err = ssh.ListenAndServe( ":22", nil, ssh.WrapConn(func(s ssh.Context, conn net.Conn) net.Conn { conn.(*net.TCPConn).SetKeepAlive(true) conn.(*net.TCPConn).SetKeepAlivePeriod(time.Second * 100) return conn }), )
hmm this didn't work for me
Here's how we do it in ssh-chat: shazow/ssh-chat@
ee8d60e/sshd/terminal.go#L87-L104
Somehow even this didn't prevent the connection from being terminated. I checked that the client was receiving the requests, and it was.