Skip to content

Add SOCKS5 proxy functionality - #1873

Open
Dunedan wants to merge 1 commit into
paramiko:mainfrom
yomagroup:add-socks-proxy
Open

Dunedan wants to merge 1 commit into
paramiko:mainfrom
yomagroup:add-socks-proxy

Conversation

@Dunedan

@Dunedan Dunedan commented Jul 2, 2021

Copy link
Copy Markdown

This adds SOCKS5 proxy functionality to paramiko as requested and
discussed in #955.

The functionality is nearly identical feature-wise to the one in
OpenSSH, with the notable exception that it only support SOCKS5 and not
SOCKS4. The reasoning behind it being that SOCKS4 was already
superseded by SOCKS5 25 (!) years ago and that SOCKS4 doesn't even
support tunneling requests to hosts using IPv6.

Common implementation details with OpenSSH include:

  • supports starting multiple SOCKS5 servers bound to different
    local addresses for a single SSH connection
  • exposes the SOCKS5 servers as local IPv4 or IPv6 stream sockets
  • only supports a subset of SOCKS5, in particular tunneling of
    UDP-traffic, authentication and incoming connections aren't supported
  • supports tunneling connections to IPv4 and IPv6 addresses, as well as
    connections to host names, by resolving them on the SOCKS server side
  • 1:1 mapping between SOCKS5 requests and direct-tcpip channels

The implementation works with all Python versions supported by paramiko.
It has so far only been tested with Linux, but should work on other
operating systems as well. The implementation has been used productively
for several weeks so far, using requests with its SOCKS5 proxy
functionality as SOCKS5 client, and works without any problems.

If there are changes needed, before merging this PR, please just let me
know and I'd be happy to change it.

This adds SOCKS5 proxy functionality to paramiko as requested and
discussed in paramiko#955.

The functionality is nearly identical feature-wise to the one in
OpenSSH, with the notable exception that it only support SOCKS5 and not
SOCKS4. The reasoning behind it being that SOCKS4 was already
superseded by SOCKS5 25 (!) years ago and that SOCKS4 doesn't even
support tunneling requests to hosts using IPv6.

Common implementation details with OpenSSH include:
- supports starting multiple SOCKS5 servers bound to different
  local addresses for a single SSH connection
- exposes the SOCKS5 servers as local IPv4 or IPv6 stream sockets
- only supports a subset of SOCKS5, in particular tunneling of
  UDP-traffic, authentication and incoming connections aren't supported
- supports tunneling connections to IPv4 and IPv6 addresses, as well as
  connections to host names, by resolving them on the SOCKS server side
- 1:1 mapping between SOCKS5 requests and direct-tcpip channels

The implementation works with all Python versions supported by paramiko.
It has so far only been tested with Linux, but should work on other
operating systems as well. The implementation has been used productively
for several weeks so far, using `requests` with its SOCKS5 proxy
functionality as SOCKS5 client, and works without any problems.
@SSgtSpoon

Copy link
Copy Markdown

FWIW my team has been using this branch (or the wip branch before this one) in our development code for a couple months with zero issues. We use it to proxy requests from K8s pods through a server with a static IP to hit an API with IP whitelist requirements. The tunnel is often held open for between 2 and 24+ hours without issue.

@SSgtSpoon

Copy link
Copy Markdown

Is there any expected timeline for this feature to be released?

@fogo5

fogo5 commented Oct 30, 2021

Copy link
Copy Markdown

The branch also works flawlessly for me.

@bskinn

bskinn commented Nov 1, 2021

Copy link
Copy Markdown
Contributor

@SSgtSpoon While the feature is slotted for the next feature release, per its milestone, unfortunately, there's no specific release timetable -- @bitprophet is also maintaining fabric and invoke, and the cadence depends on the time he has available and mutual prioritization among the three projects.

@SSgtSpoon

Copy link
Copy Markdown

@SSgtSpoon While the feature is slotted for the next feature release, per its milestone, unfortunately, there's no specific release timetable -- @bitprophet is also maintaining fabric and invoke, and the cadence depends on the time he has available and mutual prioritization among the three projects.

Totally understand. Thank you for the update!

@crane-denny

Copy link
Copy Markdown

Is it possible to use this within a with statement so I don't have to care for propper cleanup if something goes wrong? 🤔
Something like:

with open_proxy(server, username, password, localport):
    do_something()

@Dunedan

Dunedan commented Jan 9, 2022 •

Copy link
Copy Markdown
Author

Is it possible to use this within a with statement so I don't have to care for propper cleanup if something goes wrong? thinking Something like:

with open_proxy(server, username, password, localport):
    do_something()

Sure, that's already possible with the code implemented in this PR.

If you already use a context manager for your SSHClientand you are fine with the SOCKS proxies being cleaned up once leaving this context manager, you're already set, as existing SOCKS proxies will be closed when the SSHClient context manager exits (see https://github.com/paramiko/paramiko/pull/1873/files#diff-beeef3198e4e0c451d6f4b07f625af72ba815566e85397a123200ef8e4eca45dR441-R442).
Code doing that would look like this:

    from paramiko.client import SSHClient

    with SSHClient() as ssh_client:
        ssh_client.connect(…)
        proxy = ssh_client.open_socks_proxy(…)
        do_something()

If you prefer not to use a context manager for your SSHClient, you can also use a context manager just for a SOCKS proxy. That'd look like that:

    from paramiko.client import SSHClient
    from paramiko.socks_proxy import SOCKSProxy

    ssh_client = SSHClient()
    with SOCKSProxy(ssh_client.get_transport(), …) as proxy:
        do_something()

@crane-denny

Copy link
Copy Markdown

Oh nice!
Thanks for the explanation! :)

@dhm116

dhm116 commented Mar 14, 2022

Copy link
Copy Markdown

OK so it's been nearly 6 months and several versions have been released in that time, so could we please get this feature merged in?

@bskinn

bskinn commented Mar 14, 2022

Copy link
Copy Markdown
Contributor

@dhm116 I believe this feature got passed over in the recent releases due to multiple time- and/or security-critical fixes/features that emerged in the interim.

It's flagged for inclusion soon; unfortunately there's no specific timeline to offer. Please bear in mind that @bitprophet only has a few hours per week to dedicate to OSS, and has to spread that time across all three of paramiko, fabric and invoke. As a result, the project can only move so quickly.

@bskinn bskinn added the Feature label May 20, 2022
@aarestu

aarestu commented Jun 22, 2022

Copy link
Copy Markdown

hmm, still not merged to release yet.
is this feature still have significant issues, if I use it from this branch?

@Dunedan

Dunedan commented Jun 22, 2022

Copy link
Copy Markdown
Author

is this feature still have significant issues, if I use it from this branch?

Since implementing this functionality, we've used the code from this branch without any issues in a production setup.

@aarestu

aarestu commented Jun 22, 2022 •

Copy link
Copy Markdown

thanks @Dunedan, I have already tried this branch today and working nicely for my sample case.
but if I connected to HTTP/HTTPS external internet requests a couple of times via this socks5, I got this:

DEBUG:paramiko.transport:[chan 0] EOF received (0)
DEBUG:paramiko.socks:Accepting connection from 127.0.0.1:64174
DEBUG:paramiko.transport:[chan 1] Max packet in: 32768 bytes
DEBUG:paramiko.transport:[chan 1] Max packet out: 32759 bytes
DEBUG:paramiko.transport:Secsh channel 1 opened.
DEBUG:paramiko.socks:Established direct-tcpip channel with 74.125.130.113:443
DEBUG:paramiko.transport:EOF in transport thread
.
.
.
DEBUG:paramiko.socks:Accepting connection from 127.0.0.1:64198
paramiko.ssh_exception.SSHException: SSH session not active

I don't understand what happened


Edit:
I have no problem after setting set_keepalive interval

@Chaz6

Chaz6 commented Jul 21, 2022

Copy link
Copy Markdown

Could you please add support for using a username and password with the socks proxy?

@aarestu

aarestu commented Jul 22, 2022

Copy link
Copy Markdown

I am just curious what if this feature is implemented with asyncIO, is it possible? and is it will have a good impact on performance and stability?

@Dunedan

Dunedan commented Jul 24, 2022

Copy link
Copy Markdown
Author

Could you please add support for using a username and password with the socks proxy?

I believe we should focus on getting this PR merged first. Additional features can always be considered afterwards.

I am just curious what if this feature is implemented with asyncIO, is it possible? and is it will have a good impact on performance and stability?

Using asyncio would only make sense from my point of view if paramiko's core functionality would use it as well. There is an open discussion regarding that in #389.

@bskinn

bskinn commented Jul 28, 2022

Copy link
Copy Markdown
Contributor

Could you please add support for using a username and password with the socks proxy?

I believe we should focus on getting this PR merged first. Additional features can always be considered afterwards.

Agreed -- @Chaz6, this would be best added as a brand new issue, with a reference that it would follow implementation of the feature of this issue.

@Dunedan

Dunedan commented Nov 25, 2022

Copy link
Copy Markdown
Author

@bitprophet Is there anything we can do to speed up review and merge of this PR? It's been a while since we opened it and we'd really like to get this feature shipped to all paramiko users.

@bskinn

bskinn commented Nov 25, 2022

Copy link
Copy Markdown
Contributor

@Dunedan paramiko is currently on a feature freeze until bitprophet completes the drop of Python 2 and older 3.x, plus some other modernization, for the suite of projects he maintains. I don't think this or any other features are likely to be merged until after that.

Please do check in periodically, though. It's good to know which fixes/features people still care about strongly.

@mechanyx

Copy link
Copy Markdown

I apologize if this info is elsewhere but I'm unable to find info on the feature freeze status and/or timeline. Trying to understand what options are available for SOCKS proxy support. Hesitant to make a production environment dependent upon on out of tree branch for a variety of reasons. This PR is almost 2 years old. Is Paramiko still frozen? Is there a timeline to unfreeze? Is this PR likely to get merged? If there is blocking work on Paramiko itself, is there a way to contribute to that work? Appreciate everyone's time and effort and any info you can provide. Thanks

@bskinn

bskinn commented Jun 25, 2023

Copy link
Copy Markdown
Contributor

@mechanyx No, I don't think the information is available elsewhere. paramiko is still on a general feature/fix freeze as bitprophet was working on the drop of Python 2, and as he is working on the key/auth overhaul of #387/#23. Progressive release & user testing of the key/auth work will be the focus of the next couple (few?) minor releases, after which point I believe he plans to return to broader features/fixes. My understanding is that this would be back in scope at that time.

The best thing to do at this time is to test out the new, experimental auth features in v3.2 (see red annotations in the 3.2 CHANGELOG section) to help get those shaken out and solidified.

The paradigm of the new auth system is pretty strongly cross-coupled between paramiko and fabric, so I don't know if it's feasible for anyone to directly assist bitprophet with the architecture and code work itself.

@HengyueLi

Copy link
Copy Markdown

@Dunedan
Once again, good work!
I have used your branch for YEARS...Each time when I startup a new working environment, I need to have a callback of this page and spend time to think about how to recorrect pip lib after pip install paramiko directly. Is it possible to write a script (or other lite tools) to speedup this process? For example, first let user pip install paramiko==? directly, then run the script to make a correction so that corresponding Python code can work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.