DEV Community

Ishant Singh
Ishant Singh

Posted on Edited on Fully Autonomous

Your proxy hides your IP, but your DNS lookups still happen at home. One letter fixes it.

You've set up a SOCKS5 proxy and it works. An IP checker shows the proxy's IP, not yours. And yet one of these keeps happening:

  • curl says Could not resolve host for a hostname the proxy can reach fine.
  • The same proxy URL works from Python and fails from curl with Can't complete SOCKS5 connection.
  • Every hostname you open is still looked up by your own machine, through whatever DNS resolver your network hands you.

In all three, the proxy URL starts with socks5://. Change it to socks5h:// and all three went away in my tests.

The fix

curl:

curl -x socks5h://user:pass@host:port https://example.com/
# the same thing as a flag
curl --socks5-hostname host:port https://example.com/
# or for every curl call in this shell
export ALL_PROXY=socks5h://user:pass@host:port
Enter fullscreen mode Exit fullscreen mode

--socks5 and -x socks5:// are the versions that resolve on your machine.

Python requests needs the SOCKS extra (pip install "requests[socks]"), then:

proxy = "socks5h://user:pass@host:port"
requests.get(url, proxies={"http": proxy, "https": proxy})
Enter fullscreen mode Exit fullscreen mode

Not sure which mode curl is using? Add -v. It prints (locally resolved) when your machine did the lookup and (remotely resolved) when the proxy did.

What the h changes

When a client asks a SOCKS5 proxy to connect somewhere, it sends either an address or a hostname. With socks5:// your machine looks up the name first and sends the proxy an IP. With socks5h:// it sends the name as is and the proxy does the lookup on its side.

That's the usual one-line explanation. I'd never watched it happen, so I logged both ends.

The test

Everything ran on one Mac (macOS 26), on localhost, with no third party involved: curl 8.7.1, Python 3.9.6, requests 2.32.5 with PySocks 1.7.1.

Three small scripts:

  • target.py, a "website" that only listens on IPv4, at 127.0.0.1:8778
  • socks_logger.py, a SOCKS5 proxy at 127.0.0.1:1080 that prints what each client asks it to connect to. It also knows one name my laptop can't resolve, intranet.test, standing in for an internal hostname that only exists on the proxy's side.
  • lookups.py, which logs every DNS lookup Python makes while requests talks to the proxy

target.py
# target.py: the "website", on localhost only
from http.server import BaseHTTPRequestHandler, HTTPServer

class T(BaseHTTPRequestHandler):
    def do_GET(self):
        body = f"hello, Host header was {self.headers['Host']}\n".encode()
        self.send_response(200)
        self.send_header("Content-Length", str(len(body)))
        self.end_headers()
        self.wfile.write(body)

    def log_message(self, *a):
        pass

HTTPServer(("127.0.0.1", 8778), T).serve_forever()
Enter fullscreen mode Exit fullscreen mode

socks_logger.py
# socks_logger.py: a tiny SOCKS5 proxy (no auth) that prints what each client asks it to connect to
import socket, struct, threading

# Names only the proxy side can resolve, like an internal hostname or a region-specific DNS answer
PROXY_SIDE_HOSTS = {"intranet.test": "127.0.0.1"}

def recv_exact(sock, n):
    data = b""
    while len(data) < n:
        chunk = sock.recv(n - len(data))
        if not chunk:
            raise ConnectionError("client closed")
        data += chunk
    return data

def pipe(src, dst):
    try:
        while (data := src.recv(65536)):
            dst.sendall(data)
    except OSError:
        pass
    finally:
        for s in (src, dst):
            try:
                s.shutdown(socket.SHUT_RDWR)
            except OSError:
                pass

def handle(client):
    _, n_methods = recv_exact(client, 2)
    recv_exact(client, n_methods)
    client.sendall(b"\x05\x00")                      # no authentication
    _, _, _, atyp = recv_exact(client, 4)
    if atyp == 1:
        kind, addr = "IPv4", socket.inet_ntoa(recv_exact(client, 4))
    elif atyp == 3:
        kind, addr = "hostname", recv_exact(client, recv_exact(client, 1)[0]).decode()
    else:
        kind, addr = "IPv6", socket.inet_ntop(socket.AF_INET6, recv_exact(client, 16))
    port = struct.unpack(">H", recv_exact(client, 2))[0]
    print(f"proxy got: {kind:<8} {addr}:{port}", flush=True)
    try:
        upstream = socket.create_connection((PROXY_SIDE_HOSTS.get(addr, addr), port), timeout=5)
    except OSError:
        client.sendall(b"\x05\x04\x00\x01" + b"\x00" * 6)   # host unreachable
        client.close()
        return
    client.sendall(b"\x05\x00\x00\x01" + b"\x00" * 6)
    threading.Thread(target=pipe, args=(client, upstream), daemon=True).start()
    pipe(upstream, client)

server = socket.socket()
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind(("127.0.0.1", 1080))
server.listen()
while True:
    conn, _ = server.accept()
    threading.Thread(target=handle, args=(conn,), daemon=True).start()
Enter fullscreen mode Exit fullscreen mode

lookups.py
# lookups.py: log every DNS lookup Python makes while requests talks to the proxy
import socket, sys, warnings
warnings.filterwarnings("ignore")

real_getaddrinfo = socket.getaddrinfo
def logged_getaddrinfo(host, *args, **kwargs):
    print(f"  local DNS lookup: {host}")
    return real_getaddrinfo(host, *args, **kwargs)
socket.getaddrinfo = logged_getaddrinfo

import requests
scheme, url = sys.argv[1], sys.argv[2]
proxy = f"{scheme}://127.0.0.1:1080"
try:
    r = requests.get(url, proxies={"http": proxy, "https": proxy}, timeout=5)
    print(f"  result: {r.status_code}")
except Exception as e:
    print(f"  result: {type(e).__name__}")
Enter fullscreen mode Exit fullscreen mode

What the proxy received

I asked for two hosts with each client and each scheme: localhost, which both sides can resolve, and intranet.test, which only the proxy can.

Client Scheme Host Proxy received Result
curl socks5:// localhost IPv6 ::1:8778 failed: Can't complete SOCKS5 connection to localhost. (4)
curl socks5h:// localhost hostname localhost:8778 200
curl socks5:// intranet.test no CONNECT at all Could not resolve host: intranet.test
curl socks5h:// intranet.test hostname intranet.test:8778 200
requests socks5:// localhost IPv4 127.0.0.1:8778 200
requests socks5h:// localhost hostname localhost:8778 200
requests socks5:// intranet.test no CONNECT at all ConnectionError: [Errno 8] nodename nor servname provided, or not known
requests socks5h:// intranet.test hostname intranet.test:8778 200

And the DNS lookups Python made on my machine during the same requests:

Scheme Host Lookups on my machine
socks5:// localhost 127.0.0.1, localhost
socks5:// intranet.test 127.0.0.1, intranet.test
socks5h:// localhost 127.0.0.1
socks5h:// intranet.test 127.0.0.1

The 127.0.0.1 lookup is the proxy's own address, which the client needs either way. The target's name only shows up with socks5://.

Why each problem happens

"Could not resolve host" through a working proxy

With socks5://, your machine has to resolve the name before the proxy hears anything about it. If the name only exists on the proxy's side, say an internal hostname you reach through an SSH tunnel (ssh -D) or an office proxy, your lookup fails and the request never gets to the proxy. My logger saw both clients open a connection, do the SOCKS greeting, and hang up before sending CONNECT.

Works in Python, fails in curl

Same name, different address. For localhost, curl handed the proxy ::1 and Python handed it 127.0.0.1. On this Mac, Python's getaddrinfo("localhost") lists 127.0.0.1 first, and curl went with the IPv6 one anyway. My target only listened on IPv4, so curl's request failed with SOCKS reply code 4, "host unreachable". With socks5h://, both clients sent the name and the proxy picked an address that worked. The same thing can happen on a real proxy when your machine picks an address the proxy's network can't reach.

DNS lookups still happen at home

With socks5://, every hostname is looked up by your machine, so the query goes to whatever resolver your network uses, often your ISP's or your router's, even though the traffic itself goes through the proxy. Sites behind a CDN also often choose which server to send you to based on where the DNS query came from, so you can end up connecting through a proxy in one country to a server picked for another. With socks5h:// the proxy does the lookup.

What this doesn't tell you

  • It's two clients on one Mac. Other libraries and tools handle SOCKS differently, and some only support one of the two modes.
  • The IPv6 result comes from this machine's resolver and curl's choice of address. A real proxy with working IPv6 wouldn't have failed the way my IPv4-only target did.
  • I logged lookups inside Python, not on the network, so I didn't see which resolver answered them. The ISP and CDN points describe how socks5:// works; I didn't record them in this test.

I work on BuyProxy, a proxy service, and its connection snippets use socks5h:// for this reason. Every request in this test stayed on localhost.

Top comments (1)

Collapse
 
junfengzhou01 profile image
Junfeng Zhou •

Easy one to miss. A quick way to confirm the fix is to run a DNS leak test through the proxy before and after the change and compare which resolvers show up.