DNS over proxy?

后端 未结 1 1762
时光取名叫无心
时光取名叫无心 2020-12-24 15:39

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(

相关标签:
1条回答
  • 2020-12-24 16:17

    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

    0 讨论(0)
提交回复
热议问题