Skip to content

Commit

Permalink
Allow client to use port 0 when requesting reverse port forwarding
Browse files Browse the repository at this point in the history
Bind the port to forward before calling the ReversePortForwardingCallback
callback, with the actual bound port instead of 0.
  • Loading branch information
rawoul committed Dec 13, 2024
1 parent d137aad commit dd810c5
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions tcpip.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,6 @@ func (h *ForwardedTCPHandler) HandleSSHRequest(ctx Context, srv *Server, req *go
// TODO: log parse failure
return false, []byte{}
}
if srv.ReversePortForwardingCallback == nil || !srv.ReversePortForwardingCallback(ctx, reqPayload.BindAddr, reqPayload.BindPort) {
return false, []byte("port forwarding is disabled")
}
addr := net.JoinHostPort(reqPayload.BindAddr, strconv.Itoa(int(reqPayload.BindPort)))
ln, err := net.Listen("tcp", addr)
if err != nil {
Expand All @@ -119,6 +116,10 @@ func (h *ForwardedTCPHandler) HandleSSHRequest(ctx Context, srv *Server, req *go
}
_, destPortStr, _ := net.SplitHostPort(ln.Addr().String())
destPort, _ := strconv.Atoi(destPortStr)
if srv.ReversePortForwardingCallback == nil || !srv.ReversePortForwardingCallback(ctx, reqPayload.BindAddr, uint32(destPort)) {
ln.Close()
return false, []byte("port forwarding is disabled")
}
h.Lock()
h.forwards[addr] = ln
h.Unlock()
Expand Down

0 comments on commit dd810c5

Please sign in to comment.