问题描述
过去几天,我一直在拉我的头发,寻找一个很好的解决方案,以防止通过socks4 / 5代理的DNS泄漏。
我研究了SocksiPy(-branch)模块,并试图包装一些东西(urllib,urllib2,dnstools),但是它们似乎还是泄漏DNS请求我也知道,proxychains / proxyresolv可以通过一个socks4 / 5代理来扔DNS请求,而且一些LD_PRELOAD库对于猴子补丁来说都是神奇的。套接字的功能,非常像SocksiPy,但我似乎无法弄清楚为什么它不会通过socks4或socks5代理发送DNS。
我想linux我可能可以用libproxychains.so来使用CTyp来做我的解决方案,但是我正在寻找一个多平台的东西,所以我认为插座模块的猴子插件是要走的路。有没有人想出了一个很好的办法来解决这个问题?
我想做所有代码中的便携性,我不想诉诸运行另一个代理服务器!
谢谢!
我想出来了。在您开始使用套接字之前,您需要设置默认代理(例如在导入使用它的任何东西之前)。您还需要对套接字的getaddrinfo部分进行monkeypatch,然后一切正常。
import socks
import socket
#可以是socks4 / 5
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS4,'127.0.0.1',9050)
socket.socket = socks.socksocket
#魔术!
def getaddrinfo(* args):
return [(socket.AF_INET,socket.SOCK_STREAM,6,'',(args [0],args [1])]]
套接字。 getaddrinfo = getaddrinfo
import urllib
这个工作并代理了所有的DNS请求您导入的任何模块代替urllib。希望它帮助某人在那里!
编辑:您可以在我的
I've been pulling my hair out over the past few days looking around for a good solution to prevent DNS leaks over a socks4/5 proxy.
I've looked into the SocksiPy(-branch) module, and tried to wrap a number of things (urllib,urllib2,dnstools), but they all seem to still leak DNS requests. So does pyCurl.
I know that proxychains/proxyresolv can throw DNS requests over a socks4/5 proxy, and it does all it's magic with some LD_PRELOAD libraries to monkey-patch socket's functions, much like SocksiPy does, but I can't seem to figure out why it doesn't send DNS over either a socks4 or socks5 proxy.
I suppose for linux I may be able to use CTypes with libproxychains.so to do my resolution, but I'm looking for something multi-platform, so I think monkey-patching the socket module is the way to go.
Has anyone figured out a good way to get around this? I want to do it all in-code for portability's sake, and I don't want to resort to running another proxy server!
Thanks!
Well I figured it out. You need to set your default proxy BEFORE you start using the socket (e.g. before you import anything that uses it.). You also need to monkeypatch the getaddrinfo part of socket, then everything works fine.
import socks
import socket
# Can be socks4/5
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS4,'127.0.0.1', 9050)
socket.socket = socks.socksocket
# Magic!
def getaddrinfo(*args):
return [(socket.AF_INET, socket.SOCK_STREAM, 6, '', (args[0], args[1]))]
socket.getaddrinfo = getaddrinfo
import urllib
This works and proxies all DNS requests through whatever module you import in lieu of urllib. Hope it helps someone out there!
EDIT: You can find updated code and stuff on my blog
这篇关于DNS代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!