From 4083ef64ae1cc41a6f767386793f4f4c5fea93e5 Mon Sep 17 00:00:00 2001 From: JeremyRand Date: Sun, 17 Sep 2017 10:38:30 +0000 Subject: [PATCH 1/2] Make wrap_module patch socket.getaddrinfo. --- socks.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/socks.py b/socks.py index 23504c7..ee3f472 100644 --- a/socks.py +++ b/socks.py @@ -86,7 +86,7 @@ PRINTABLE_PROXY_TYPES = dict(zip(PROXY_TYPES.values(), PROXY_TYPES.keys())) _orgsocket = _orig_socket = socket.socket - +_orig_getaddrinfo = socket.getaddrinfo def set_self_blocking(function): @@ -188,6 +188,15 @@ def get_default_proxy(): getdefaultproxy = get_default_proxy +# https://web.archive.org/web/20161211104525/http://fitblip.pub/2012/11/13/proxying-dns-with-python/ +def getaddrinfo(*args): + (proxy_type, proxy_addr, proxy_port, rdns, username, + password) = get_default_proxy() + + if proxy_type is not None and rdns: + return [(socket.AF_INET, socket.SOCK_STREAM, 6, '', (args[0], args[1]))] + + return _orig_getaddrinfo(*args) def wrap_module(module): """Attempts to replace a module's socket library with a SOCKS socket. @@ -196,6 +205,7 @@ def wrap_module(module): only work on modules that import socket directly into the namespace; most of the Python Standard Library falls into this category.""" if socksocket.default_proxy: + module.socket.getaddrinfo = getaddrinfo module.socket.socket = socksocket else: raise GeneralProxyError("No default proxy specified") From bc3148e0889f037c822a785b30969103ace40494 Mon Sep 17 00:00:00 2001 From: JeremyRand Date: Sun, 17 Sep 2017 10:46:59 +0000 Subject: [PATCH 2/2] README: Make monkeypatching section use wrap_module so that socket.getaddrinfo also gets patched. --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c9462cc..6479efc 100644 --- a/README.md +++ b/README.md @@ -66,9 +66,10 @@ To monkeypatch the entire standard library with a single default proxy: import urllib2 import socket import socks + import sys socks.set_default_proxy(socks.SOCKS5, "localhost") - socket.socket = socks.socksocket + socks.wrap_module(sys.modules[__name__]) urllib2.urlopen("http://www.somesite.com/") # All requests will pass through the SOCKS proxy