touv/node-fetch-with-proxy

Report correct hostname in proxy requests

Opened this issue · 0 comments

When testing fetch-with-proxy in my development environment using a local proxy, it seems that all proxied requests have their Host header set to the proxy itself, instead of the final destination server. This causes errors in my environment, and seems to be contrary to the behavior exhibited by other proxy-capable tools like curl:

> curl -x http://localhost:8080 http://www.google.com/
GET http://www.google.com/ HTTP/1.1
Host: www.google.com
User-Agent: curl/7.54.0
Accept: */*
Proxy-Connection: Keep-Alive

I assume that target.hostname needs to be set to the proxy in order for the fetch request to work properly, but doing so seems to provide the wrong headers value for Host.

I've resolved my issue by recording and restoring the target.hostname as follows.

    // https://github.com/request/request/blob/b12a6245d9acdb1e13c6486d427801e123fdafae/lib/tunnel.js#L124-L130
    if (method.startsWith('httpOver')) {
        const targetHostname = target.hostname;
        target.path = target.protocol
            .concat('//')
            .concat(target.host)
            .concat(target.path);
        target.port = proxy.port;
        target.host = proxy.host;
        target.hostname = proxy.hostname;
        target.auth = proxy.auth;
        return crossFetch(target, {
            ...options,
            headers: { ...options.headers, 'Host': targetHostname },
        });