rvagg/node-libssh — pull request #32 by tlrobinson (2014-06-11 08:20:50+00:00)
Title: Add support for remote port forwarding

This adds support for remote port forwarding, e.x. `ssh -R :REMOTE_PORT:localhost:LOCAL_PORT me@myserver`

It mostly works, except for communicating the port back to the client with `replyGlobalMessageSuccess` doesn't work asynchronously, and in fact will often segfault. This is due to the message being deallocated by libssh before we get a chance to send the response. Also, I believe libssh sends a success reply if you don't send one right away.

I'd love suggestions on how to fix this. I've tried a few things with little success.

Here's an example server (CoffeeScript):

net = require "net"
libssh = require "ssh"

server = libssh.createServer
hostRsaKeyFile: "keys/id_rsa"
hostDsaKeyFile: "keys/id_dsa"
debug: true

server.on "connection", (session) ->
session.on "globalrequest", console.log
session.on "tcpipforward", (message) ->
console.log "tcpipforward", message

proxy = net.createServer (con) ->
chan = session.openReverseForward(message.requestAddress, message.requestPort, con.remoteAddress, con.remotePort)
console.log "chan", chan
setImmediate ->
con.pipe(chan).pipe(con)

session.on "close", ->
console.log "session ended!"
proxy.close()

proxy.listen message.requestPort, message.requestAddress, ->
console.log "listening", proxy.address()
# TODO: crashes!
# message.replyGlobalMessageSuccess(proxy.address().port)

session.on "auth", (message) ->
console.log "auth", message
return message.replyAuthSuccess()

server.listen 3333