Skip to content Skip to sidebar Skip to footer

DNS Over Proxy?

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,

Solution 1:

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


Post a Comment for "DNS Over Proxy?"