Skip to content

fix: write i/o timeout on SSH connection #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ jobs:
outformat: out-format
with:
version: ${{ matrix.golangci }}
args: "--%outformat% colored-line-number"
args: "--%outformat% colored-line-number --timeout 2m"
skip-pkg-cache: true
skip-build-cache: true

Expand Down
9 changes: 3 additions & 6 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ func (c *Client) messageHandler() {
for {
if c.scanner.Scan() {
line := c.scanner.Text()
//nolint: gocritic
if line == "error id=0 msg=ok" {
c.err <- nil
} else if matches := respTrailerRe.FindStringSubmatch(line); len(matches) == 4 {
Expand All @@ -229,11 +228,12 @@ func (c *Client) messageHandler() {
}
} else {
err := c.scanErr()
c.err <- err
if errors.Is(err, io.ErrUnexpectedEOF) {
if errors.Is(err, io.ErrUnexpectedEOF) || errors.Is(err, io.EOF) {
close(c.disconnect)
c.err <- err
return
}
c.err <- err
}
}
}
Expand All @@ -253,9 +253,6 @@ func (c *Client) workHandler() {
}

func (c *Client) process(data string) {
if err := c.conn.SetWriteDeadline(time.Now().Add(c.timeout)); err != nil {
c.err <- err
}
if _, err := c.conn.Write([]byte(data)); err != nil {
c.err <- err
}
Expand Down
2 changes: 1 addition & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func TestClientWriteFail(t *testing.T) {
if !assert.NoError(t, err) {
return
}
assert.NoError(t, c.conn.(*legacyConnection).Conn.(*net.TCPConn).CloseWrite())
assert.NoError(t, c.conn.(*legacyConnection).Conn.(*writeTimeoutConn).Conn.(*net.TCPConn).CloseWrite())

_, err = c.Exec("version")
assert.Error(t, err)
Expand Down
34 changes: 32 additions & 2 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,45 @@ const (
DefaultSSHPort = 10022
)

// writeTimeoutConn is a net.Conn that sets the write timeout on every call to Write().
type writeTimeoutConn struct {
net.Conn
timeout time.Duration
}

func (c *writeTimeoutConn) Write(p []byte) (n int, err error) {
if err = c.Conn.SetWriteDeadline(time.Now().Add(c.timeout)); err != nil {
return 0, fmt.Errorf("writeTimeoutConn: SetWriteDeadline: %w", err)
}
if n, err = c.Conn.Write(p); err != nil {
return n, fmt.Errorf("writeTimeoutConn: write: %w", err)
}
return n, nil
}

// legacyConnection is an insecure TCP connection.
type legacyConnection struct {
net.Conn
}

// Connect connects to the address with the given timeout.
// The timeout is used as dial and write timeout.
func (c *legacyConnection) Connect(addr string, timeout time.Duration) error {
addr, err := verifyAddr(addr, DefaultPort)
if err != nil {
return err
}

c.Conn, err = net.DialTimeout("tcp", addr, timeout)
conn, err := net.DialTimeout("tcp", addr, timeout)
if err != nil {
return fmt.Errorf("legacy connection: dial: %w", err)
}

c.Conn = &writeTimeoutConn{
Conn: conn,
timeout: timeout,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is using what the user believes is the connect timeout and not the read / write timeout, same for the ssh version.

Given the use is passing in a read / write / dial timeout the best option might be to just clarify it's use, thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When using NewClient() with the option Timeout() the behavior didn't change. This is also documented for the Timeout option. "Timeout sets read / write / dial timeout for a TeamSpeak 3 Client."

I added some documentation to the Connect methods for clarification though.

}

return nil
}

Expand All @@ -47,16 +70,23 @@ type sshConnection struct {
}

// Connect connects to the address with the given timeout and opens a new SSH channel with attached shell.
// The timeout is used as dial and write timeout.
func (c *sshConnection) Connect(addr string, timeout time.Duration) error {
addr, err := verifyAddr(addr, DefaultSSHPort)
if err != nil {
return err
}

if c.Conn, err = net.DialTimeout("tcp", addr, timeout); err != nil {
conn, err := net.DialTimeout("tcp", addr, timeout)
if err != nil {
return fmt.Errorf("ssh connection: dial: %w", err)
}

c.Conn = &writeTimeoutConn{
Conn: conn,
timeout: timeout,
}

clientConn, chans, reqs, err := ssh.NewClientConn(c.Conn, addr, c.config)
if err != nil {
return fmt.Errorf("ssh connecion: ssh client conn: %w", err)
Expand Down