Discussion:
[Twisted-Python] Notification when SSHConnectForwardingChannel is ready for traffic?
Naveen Michaud-Agrawal
2015-12-01 00:17:43 UTC
Permalink
Hi,

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?

Thanks
Naveen Michaud-Agrawal
Glyph Lefkowitz
2015-12-01 08:30:00 UTC
Permalink
Hi,
I've managed to implement ssh tunneling using this answer on SO - http://stackoverflow.com/a/23549091/5605324 <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?

-glyph
Naveen Michaud-Agrawal
2015-12-01 16:30:20 UTC
Permalink
Yes it does. It looks like endpoints will also help in implementing the
next piece which is to support connections over SSL (instead of SSH
tunnelling). I haven't really used twisted since 8.0 - looks like I have a
lot of catching up to do! Thanks for the quick response.

Regards,
Naveen
On Nov 30, 2015, at 4:17 PM, Naveen Michaud-Agrawal <
Hi,
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?
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?
-glyph
_______________________________________________
Twisted-Python mailing list
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
--
-----------------------------------
Naveen Michaud-Agrawal
Loading...