Skip to content

Commit 076a8d9

Browse files
committed
Allow client to use port 0 when requesting reverse port forwarding
Bind the port to forward before calling the ReversePortForwardingCallback callback, with the actual bound port instead of 0.
1 parent c9fc441 commit 076a8d9

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

tcpip.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,6 @@ func (h *ForwardedTCPHandler) HandleSSHRequest(ctx Context, srv *Server, req *go
108108
// TODO: log parse failure
109109
return false, []byte{}
110110
}
111-
if srv.ReversePortForwardingCallback == nil || !srv.ReversePortForwardingCallback(ctx, reqPayload.BindAddr, reqPayload.BindPort) {
112-
return false, []byte("port forwarding is disabled")
113-
}
114111
addr := net.JoinHostPort(reqPayload.BindAddr, strconv.Itoa(int(reqPayload.BindPort)))
115112
ln, err := net.Listen("tcp", addr)
116113
if err != nil {
@@ -119,6 +116,10 @@ func (h *ForwardedTCPHandler) HandleSSHRequest(ctx Context, srv *Server, req *go
119116
}
120117
_, destPortStr, _ := net.SplitHostPort(ln.Addr().String())
121118
destPort, _ := strconv.Atoi(destPortStr)
119+
if srv.ReversePortForwardingCallback == nil || !srv.ReversePortForwardingCallback(ctx, reqPayload.BindAddr, uint32(destPort)) {
120+
ln.Close()
121+
return false, []byte("port forwarding is disabled")
122+
}
122123
h.Lock()
123124
h.forwards[addr] = ln
124125
h.Unlock()

0 commit comments

Comments
 (0)