mirror of
https://github.com/tailscale/tailscale.git
synced 2024-11-25 19:15:34 +00:00
5a44f9f5b5
While we rearrange/upstream things.
gliderlabs/ssh is forked into tempfork from our prior fork
at be8b7add40
x/crypto/ssh OTOH is forked at
https://github.com/tailscale/golang-x-crypto because it was gnarlier
to vendor with various internal packages, etc.
Its git history shows where it starts (2c7772ba30643b7a2026cbea938420dce7c6384d).
Updates #3802
Change-Id: I546e5cdf831cfc030a6c42557c0ad2c58766c65f
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
56 lines
1.0 KiB
Go
56 lines
1.0 KiB
Go
package ssh
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"time"
|
|
)
|
|
|
|
type serverConn struct {
|
|
net.Conn
|
|
|
|
idleTimeout time.Duration
|
|
maxDeadline time.Time
|
|
closeCanceler context.CancelFunc
|
|
}
|
|
|
|
func (c *serverConn) Write(p []byte) (n int, err error) {
|
|
c.updateDeadline()
|
|
n, err = c.Conn.Write(p)
|
|
if _, isNetErr := err.(net.Error); isNetErr && c.closeCanceler != nil {
|
|
c.closeCanceler()
|
|
}
|
|
return
|
|
}
|
|
|
|
func (c *serverConn) Read(b []byte) (n int, err error) {
|
|
c.updateDeadline()
|
|
n, err = c.Conn.Read(b)
|
|
if _, isNetErr := err.(net.Error); isNetErr && c.closeCanceler != nil {
|
|
c.closeCanceler()
|
|
}
|
|
return
|
|
}
|
|
|
|
func (c *serverConn) Close() (err error) {
|
|
err = c.Conn.Close()
|
|
if c.closeCanceler != nil {
|
|
c.closeCanceler()
|
|
}
|
|
return
|
|
}
|
|
|
|
func (c *serverConn) updateDeadline() {
|
|
switch {
|
|
case c.idleTimeout > 0:
|
|
idleDeadline := time.Now().Add(c.idleTimeout)
|
|
if idleDeadline.Unix() < c.maxDeadline.Unix() || c.maxDeadline.IsZero() {
|
|
c.Conn.SetDeadline(idleDeadline)
|
|
return
|
|
}
|
|
fallthrough
|
|
default:
|
|
c.Conn.SetDeadline(c.maxDeadline)
|
|
}
|
|
}
|