On Nov 30, 2015, at 4:17 PM, Naveen Michaud-Agrawal <naveen.michaudagrawal@gmail.com> wrote:


I've managed to implement ssh tunneling using this answer on SO - http://stackoverflow.com/a/23549091/5605324, and the tunnel can be used from another app. However I would like to use it from within the twisted application, but I'm not sure what to wire up so that I can get a callback when the SSHConnectForwardingChannel is ready for traffic. It looks like I can attach a callback to channel's _channelOpenDeferred, but that seems a bit hacky. Any suggestions?

Don't use listenTCP. Use endpoints: https://twistedmatrix.com/documents/15.5.0/core/howto/endpoints.html 

Once you have adopted endpoints, it (hopefully) becomes clear that one way to do what you want is to create your own in-process IStreamServerEndpoint implementation, and pass 'factory' in that answer to its 'listen' method, you can deliver data for it to relay to the remote port by calling its '.dataReceived' method, and implement your own '.write' method on your transport in order to deliver data to your application.  There's nothing magical about these methods, after all, they're just Python functions - and forwarding.SSHListenForwardingFactory just expects to talk to a transport, it shouldn't matter what kind it is.  (It might need to implement other interfaces, like ITCPTransport; not everything in Twisted is scrupulously careful about declaring what exact interface it needs, and there are often bugs in edge-cases like this.  But those are also just a few more methods you can implement, if necessary.)

Does this make sense?