New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SOCKS Proxy Support #2953
SOCKS Proxy Support #2953
Conversation
🙏 |
@@ -150,14 +156,26 @@ def proxy_manager_for(self, proxy, **proxy_kwargs): | |||
:returns: ProxyManager | |||
""" | |||
if not proxy in self.proxy_manager: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While we're here, let's reformat this a bit:
if proxy in self.proxy_manager:
manager = self.proxy_manager[proxy]
elif proxy.lower().startswith('socks'):
username, password = get_auth_from_url(proxy)
manager = self.proxy_manager[proxy] = SOCKSProxyManager(
# ...
)
else:
proxy_headers = self.proxy_headers(proxy)
manager = self.proxy_manager[proxy] = proxy_from_url(
# ...
)
return manager
I feel like this will be easier to read
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
if proxy: | ||
proxy_scheme = urlparse(proxy).scheme | ||
|
||
if proxy and proxy_scheme.lower().startswith('socks'): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see how you were being tricksy now. I wonder if we can be better about this.
We essentially have the same code path (url = request.path_url
) if we're using a 'socks' proxy or if we're making an HTTPS request. What if we do
proxied_http_request = proxy and scheme != 'https'
using_socks_proxy = False
if proxy:
using_socks_proxy == urlparse(proxy).scheme.lower().startswith('socks')
url = request.path_url
if proxied_http_request and not using_socks_proxy:
url = urldefragauth(request.url)
I puzzled through my logic with
proxy | http_scheme && !socks_proxy_scheme | https_scheme | socks_proxy_scheme |
---|---|---|---|
truthy | urldefragauth(request.url) | request.path_url | request.path_url |
falsey | request.path_url | request.path_url | request.path_url |
where http_scheme
is where (scheme != 'https') is True
and https_scheme
is where (scheme != 'https') is False
. Hopefully the table makes sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I'm going with that. I degolfed this a little bit to aid clarity, but not much.
👍 on the code and 👍 on the milestone. |
@Lukasa what do you think about having a |
Done. =) |
Does PySocks require any compilation during installation? We should consider vendoring it if we want to consider this an actual feature of Requests. |
PySocks requires no compilation. However, we'll have to rewrite import paths in urllib3 if you want to vendor PySocks. |
Well, if it's going to be used by a very small subset of users (likely the case), it's likely fine to leave it as an optional dependency. We could pack it in without modifying urllib3's code, technically: ( try:
from . import socks
sys.modules['socks'] = socks
except ImportError:
try:
import socks
except ImportError:
pass This is obviously not the best idea, but it does function properly. |
Mucking with sys.modules in the past has caused us enough headaches that I'd rather not go too far down that path again |
I don't disagree :) |
@Lukasa is this was merged or not? Btw, how to configure the socks proxy? |
This has not been merged. When it is released, you can configure the socks proxy as you would configure other proxies: |
@Lukasa so it should work like #proxy
# SOCKS5 proxy for HTTP/HTTPS
proxies = {
'http' : "socks5://myproxy:9191",
'https' : "socks5://myproxy:9191"
}
#headers
headers = {
}
url='http://icanhazip.com/'
res = requests.get(url, headers=headers, proxies=proxies) |
Correct. Of course, that won't work unless you use a patched version of requests that contains both this patch and an updated urllib3, and you install the appropriate optional dependencies. |
@Lukasa uhm, so supposed to use on google appengine, I have to wait the |
On GAE I suspect using a SOCKS proxy is going to take a while. I should also note that to use a SOCKS proxy on GAE will require that you enable socket support, which as I understand it is substantially more expensive than simply using the standard httplib replacement. |
btw, In the meanwhile I'm doing >>> import requesocks
>>> import requests
>>> session = requesocks.session()
>>> session.proxies = {
... 'http' : "socks5://myproxy.net:9050",
... 'https' : "socks5://smyproxy.net:9050"
... }
>>> url='http://icanhazip.com/'
>>> resp = requests.get(url)
>>> resp
<Response [200]>
>>> resp.text
u'89.97.90.230\n'
>>> resp.text.strip()
u'89.97.90.230'
>>> resp = session.get(url)
>>> resp.text.strip()
u'171.25.193.78'
>>> that in most envs will work ( a part again appengine that lacks of |
Is this done? Let's merge it if so. |
We need an updated urllib3. I think @shazow is expecting to release this week. |
Released! |
Sweet, let's do it. |
We'll include it in the next release, which can therefore happen this week (or even today). |
@kennethreitz I'm away from my laptop, so if you're in a merging mood you can land the urllib3 update and then merge this. Please note that it makes everyone's life easier if you merge in urllib3 directly from the brand new tag (1.15). Just a suggestion! |
The anticipation is killing me. |
What's [WIP] about this? Previous comments seem to suggest it's done, just waiting for the release. (Also, referencing urllib3/urllib3#762 and #478) |
Currently, this has to be done by monkey-patching `socket.socket`, using the [PySocks][pysocks] library to create a replacement that checks for `ALL_PROXY` and routes connections through the proxy if present. The hopefully-soon release of [Requests 2.10.0][requests] with [this update][urllib3-socks] to [urllib3][urllib3-release] (assuming its adoption docker-py), should make this unnecessary, but I'm not sure a) what's been keeping that from happening; or b) whether docker-compose would be able to quickly adopt the brand-new version of Requests (it's currently a few minor versions behind), so this seems a reasonable stopgap. Signed-off-by: Brett Higgins <brhiggins@arbor.net> [pysocks]: https://github.com/Anorov/PySocks [requests]: https://github.com/kennethreitz/requests/pull/2953 [urllib3-socks]: urllib3/urllib3#762 [urllib3-release]: https://github.com/shazow/urllib3/blob/master/CHANGES.rst#114-2015-12-29
✨ 🍰 ✨ |
Released, announced. |
The recent release of [Requests 2.10.0][requests] with [this update][urllib3-socks] to [urllib3][urllib3-release] means that docker-compose can support a SOCKS proxy configured via the HTTP[S]_PROXY environment variables. We simply need to update the version of requests to 2.10.0, with the 'socks' extra, and the rest just works. Signed-off-by: Brett Higgins <brhiggins@arbor.net> [pysocks]: https://github.com/Anorov/PySocks [requests]: https://github.com/kennethreitz/requests/pull/2953 [urllib3-socks]: urllib3/urllib3#762 [urllib3-release]: https://github.com/shazow/urllib3/blob/master/CHANGES.rst#114-2015-12-29 Signed-off-by: Brett Higgins <brhiggins@arbor.net>
The recent release of [Requests 2.10.0][requests] with [this update][urllib3-socks] to [urllib3][urllib3-release] means that docker-compose can support a SOCKS proxy configured via the HTTP[S]_PROXY environment variables. We simply need to update the version of requests to 2.10.0, with the 'socks' extra, and the rest just works. Signed-off-by: Brett Higgins <brhiggins@arbor.net> [pysocks]: https://github.com/Anorov/PySocks [requests]: https://github.com/kennethreitz/requests/pull/2953 [urllib3-socks]: urllib3/urllib3#762 [urllib3-release]: https://github.com/shazow/urllib3/blob/master/CHANGES.rst#114-2015-12-29 Signed-off-by: Brett Higgins <brhiggins@arbor.net>
Related: docker/compose#3376 The recent release of [Requests 2.10.0][requests] with [this update][urllib3-socks] to [urllib3][urllib3-release] means that docker-py can support a SOCKS proxy configured via the HTTP[S]_PROXY environment variables. We simply need to update the version of requests to 2.10.0, with the 'socks' extra, and the rest just works. [pysocks]: https://github.com/Anorov/PySocks [requests]: kennethreitz/requests#2953 [urllib3-socks]: urllib3/urllib3#762 [urllib3-release]: https://github.com/shazow/urllib3/blob/master/CHANGES.rst#114-2015-12-29 Signed-off-by: Brett Higgins <brhiggins@arbor.net>
The recent release of [Requests 2.10.0][requests] with [this update][urllib3-socks] to [urllib3][urllib3-release] means that docker-compose can support a SOCKS proxy configured via the HTTP[S]_PROXY environment variables. We simply need to update the version of requests to 2.10.0, with the 'socks' extra, and the rest just works. [pysocks]: https://github.com/Anorov/PySocks [requests]: https://github.com/kennethreitz/requests/pull/2953 [urllib3-socks]: urllib3/urllib3#762 [urllib3-release]: https://github.com/shazow/urllib3/blob/master/CHANGES.rst#114-2015-12-29 Signed-off-by: Brett Higgins <brhiggins@arbor.net>
The recent release of [Requests 2.10.0][requests] with [this update][urllib3-socks] to [urllib3][urllib3-release] means that docker-compose can support a SOCKS proxy configured via the HTTP[S]_PROXY environment variables. We simply need to update the version of requests to 2.10.0, with the 'socks' extra, and the rest just works. [pysocks]: https://github.com/Anorov/PySocks [requests]: https://github.com/kennethreitz/requests/pull/2953 [urllib3-socks]: urllib3/urllib3#762 [urllib3-release]: https://github.com/shazow/urllib3/blob/master/CHANGES.rst#114-2015-12-29 Signed-off-by: Brett Higgins <brhiggins@arbor.net>
This pull request finally adds SOCKS proxy support to the Requests module, pretty much four years to the day since we were first asked (in #324 for those who want to follow it).
This pull request relies on a pull request I have opened against urllib3, so we can't land it until urllib3 1.14.1 is released containing that code. However, once that's done this PR should be ready to rock.
I'm setting this up for the 2.10.0 milestone as I see no reason to wait for 3.0.0 for SOCKS support.
Resolves #324, #723, #1147, #1982, #2156, #2425, and #2562.