Skip to content

Commit

Permalink
ssh: add (*Client).DialContext method
Browse files Browse the repository at this point in the history
  • Loading branch information
ydnar committed Jun 22, 2023
1 parent 64c3993 commit d20da86
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 12 deletions.
32 changes: 32 additions & 0 deletions ssh/tcpip.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package ssh

import (
"context"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -332,6 +333,37 @@ func (l *tcpListener) Addr() net.Addr {
return l.laddr
}

// DialContext initiates a connection to the addr from the remote host.
// If the supplied context is cancelled before the connection can be opened,
// ctx.Err() will be returned.
// The resulting connection has a zero LocalAddr() and RemoteAddr().
func (c *Client) DialContext(ctx context.Context, n, addr string) (net.Conn, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
type connErr struct {
conn net.Conn
err error
}
ch := make(chan connErr)
go func() {
conn, err := c.Dial(n, addr)
select {
case ch <- connErr{conn, err}:
case <-ctx.Done():
if conn != nil {
conn.Close()
}
}
}()
select {
case res := <-ch:
return res.conn, res.err
case <-ctx.Done():
return nil, ctx.Err()
}
}

// Dial initiates a connection to the addr from the remote host.
// The resulting connection has a zero LocalAddr() and RemoteAddr().
func (c *Client) Dial(n, addr string) (net.Conn, error) {
Expand Down
40 changes: 40 additions & 0 deletions ssh/tcpip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
package ssh

import (
"context"
"net"
"testing"
"time"
)

func TestAutoPortListenBroken(t *testing.T) {
Expand All @@ -18,3 +21,40 @@ func TestAutoPortListenBroken(t *testing.T) {
t.Errorf("version %q marked as broken", works)
}
}

func TestClientImplementsDialContext(t *testing.T) {
type ContextDialer interface {
DialContext(context.Context, string, string) (net.Conn, error)
}
var _ ContextDialer = &Client{}
}

func TestClientDialContextWithCancel(t *testing.T) {
c := &Client{}
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, err := c.DialContext(ctx, "tcp", "localhost:1000")
if err != context.Canceled {
t.Errorf("DialContext: got nil error, expected %v", context.Canceled)
}
}

func TestClientDialContextWithDeadline(t *testing.T) {
c := &Client{}
ctx, cancel := context.WithDeadline(context.Background(), time.Now())
defer cancel()
_, err := c.DialContext(ctx, "tcp", "localhost:1000")
if err != context.DeadlineExceeded {
t.Errorf("DialContext: got nil error, expected %v", context.DeadlineExceeded)
}
}

func TestClientDialContextWithTimeout(t *testing.T) {
c := &Client{}
ctx, cancel := context.WithTimeout(context.Background(), 0)
defer cancel()
_, err := c.DialContext(ctx, "tcp", "localhost:1000")
if err != context.DeadlineExceeded {
t.Errorf("DialContext: got nil error, expected %v", context.DeadlineExceeded)
}
}
44 changes: 32 additions & 12 deletions ssh/test/dial_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package test
// direct-tcpip and direct-streamlocal functional tests

import (
"context"
"fmt"
"io"
"net"
Expand Down Expand Up @@ -47,19 +48,38 @@ func testDial(t *testing.T, n, listenAddr string, x dialTester) {
}
}()

conn, err := sshConn.Dial(n, l.Addr().String())
if err != nil {
t.Fatalf("Dial: %v", err)
}
x.TestClientConn(t, conn)
defer conn.Close()
b, err := io.ReadAll(conn)
if err != nil {
t.Fatalf("ReadAll: %v", err)
{
conn, err := sshConn.Dial(n, l.Addr().String())
if err != nil {
t.Fatalf("Dial: %v", err)
}
x.TestClientConn(t, conn)
defer conn.Close()
b, err := io.ReadAll(conn)
if err != nil {
t.Fatalf("ReadAll: %v", err)
}
t.Logf("got %q", string(b))
if string(b) != testData {
t.Fatalf("expected %q, got %q", testData, string(b))
}
}
t.Logf("got %q", string(b))
if string(b) != testData {
t.Fatalf("expected %q, got %q", testData, string(b))

{
conn, err := sshConn.DialContext(context.Background(), n, l.Addr().String())
if err != nil {
t.Fatalf("Dial: %v", err)
}
x.TestClientConn(t, conn)
defer conn.Close()
b, err := io.ReadAll(conn)
if err != nil {
t.Fatalf("ReadAll: %v", err)
}
t.Logf("got %q", string(b))
if string(b) != testData {
t.Fatalf("expected %q, got %q", testData, string(b))
}
}
}

Expand Down

0 comments on commit d20da86

Please sign in to comment.