Skip to content

Commit c072a10

Browse files
mfieldingprogrium
authored andcommitted
Support port forwarding of literal IPv6 addresses (#85)
* Support port forwarding of literal IPv6 addresses To disambiguate between colons as host:port separators and as IPv6 address separators, literal IPv6 addresses use square brackets around the address (https://en.wikipedia.org/wiki/IPv6_address#Literal_IPv6_addresses_in_network_resource_identifiers). So host ::1, port 22 is written as [::1]:22, and therefore a simple concatenation of host, colon, and port doesn't work. Fortunately net.JoinHostPort already implements this functionality, so with a bit of type gymnastics we can generate dest in an IPv6-safe way. * Support port forwarding of literal IPv6 addresses To disambiguate between colons as host:port separators and as IPv6 address separators, literal IPv6 addresses use square brackets around the address (https://en.wikipedia.org/wiki/IPv6_address#Literal_IPv6_addresses_in_network_resource_identifiers). So host ::1, port 22 is written as [::1]:22, and therefore a simple concatenation of host, colon, and port doesn't work. Fortunately net.JoinHostPort already implements this functionality, so with a bit of type gymnastics we can generate dest in an IPv6-safe way.
1 parent d3a6756 commit c072a10

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

tcpip.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package ssh
22

33
import (
4-
"fmt"
54
"io"
65
"net"
6+
"strconv"
77

88
gossh "golang.org/x/crypto/ssh"
99
)
@@ -29,8 +29,8 @@ func directTcpipHandler(srv *Server, conn *gossh.ServerConn, newChan gossh.NewCh
2929
return
3030
}
3131

32-
dest := fmt.Sprintf("%s:%d", d.DestinationHost, d.DestinationPort)
33-
32+
dest := net.JoinHostPort(d.DestinationHost, strconv.FormatInt(int64(d.DestinationPort), 10))
33+
3434
var dialer net.Dialer
3535
dconn, err := dialer.DialContext(ctx, "tcp", dest)
3636
if err != nil {

tcpip_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package ssh
22

33
import (
44
"bytes"
5-
"fmt"
65
"io/ioutil"
76
"net"
7+
"strconv"
88
"strings"
99
"testing"
1010

@@ -34,7 +34,7 @@ func newTestSessionWithForwarding(t *testing.T, forwardingEnabled bool) (net.Lis
3434
_, client, cleanup := newTestSession(t, &Server{
3535
Handler: func(s Session) {},
3636
LocalPortForwardingCallback: func(ctx Context, destinationHost string, destinationPort uint32) bool {
37-
addr := fmt.Sprintf("%s:%d", destinationHost, destinationPort)
37+
addr := net.JoinHostPort(destinationHost, strconv.FormatInt(int64(destinationPort), 10))
3838
if addr != l.Addr().String() {
3939
panic("unexpected destinationHost: " + addr)
4040
}

0 commit comments

Comments
 (0)