Node.js + Polipo https request error

631 Views Asked by At

I'm doing a simple example using polipo with tor and it's working fine on my firefox browser. I've set polipo as the proxy and the browser works just fine.

Then I tried doing a simple request in node.js based on a simple example, and it didn't work. If I try doing the request to http://check.torproject.org, it works fine. But If I do the request to https://, the following error occurs:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html><head>
<title>Proxy error: 400 Couldn't parse URL.</title>
</head><body>
<h1>400 Couldn't parse URL</h1>
<p>The following error occurred while trying to access <strong>https://check.torproject.org</strong>:<br><br>
<strong>400 Couldn't parse URL</strong></p>
<hr>Generated Mon, 23 Dec 2013 17:22:04 BRST by Polipo on <em>localhost:8118</em>.
</body></html>

The headers I'm sending:

options: { protocol: 'http:',
slashes: true,
auth: null,
host: 'localhost:8118',
port: '8118',
hostname: 'localhost',
hash: null,
search: null,
query: null,
pathname: '/',
path: 'https://check.torproject.org',
href: 'https://check.torproject.org',
headers: 
{ accept: '*/*',
 'content-length': 0,
 host: 'https://check.torproject.org' } 
}

The weird thing is that this page works fine on firefox. I'm wondering if I'm doing something wrong with this code, or if I simply can't do a HTTPS request using polipo.

Does anyone have any solution or something that I may test? (I'm using mac btw)

Thank you!

THE CODE:

var request = function(name, url, proxy) {
// options
var options = URL.parse(url, false);
// headers
options.headers = {
    accept: '*/*',
    'content-length': 0
};
var body = '';
// proxy set
if(proxy) {
    var proxy = URL.parse(proxy, false);
    options.path = options.protocol+ '//'+ options.host+ options.path;
    options.headers.host = options.path;
    options.protocol = proxy.protocol;
    options.host = proxy.host;
    options.port = proxy.port;
    options.hostname = proxy.hostname;
}
console.log(name, 'request options:', options);

var r = (options.protocol == 'http:'?http:https).request(options, function(res) {
    res.on('end', function() {
        // just print ip, instead of whole body
        //console.log(name, body.match(/check_ip" value="([^"]*)"/)[1]);
        console.log(body);
        // console.log(name, body);
    });
    res.on('readable', function() {
        body += this.read().toString();
    });
});
r.end();
};

request('with proxy', 'https://check.torproject.org/', "http://localhost:8118");
1

There are 1 best solutions below

0
On

Polipo does not terminate HTTPS: an request over HTTPS must be tunneled through the proxy using the CONNECT request rather than sent to the proxy using the GET request.