ssh icon indicating copy to clipboard operation
ssh copied to clipboard

Add option for keep-alive messages

Open belak opened this issue 8 years ago • 6 comments

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.

belak avatar Feb 03 '17 22:02 belak

👍

progrium avatar Feb 03 '17 22:02 progrium

Here's how we do it in ssh-chat: https://github.com/shazow/ssh-chat/blob/ee8d60ec00526be52d2acadfb088bad96790640d/sshd/terminal.go#L87-L104

shazow avatar Mar 23 '19 15:03 shazow

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.

belak avatar Mar 23 '19 16:03 belak

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
	}),
)

9072997 avatar Dec 11 '20 21:12 9072997

that's pretty cool. i think people avoid tcp keepalives though because of proxies but ymmv

progrium avatar Dec 11 '20 21:12 progrium

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.

quackduck avatar Oct 27 '22 19:10 quackduck