node-http-proxy icon indicating copy to clipboard operation
node-http-proxy copied to clipboard

Can this work with HTTPS?

Open daolf opened this issue 4 years ago • 4 comments
trafficstars

Hi there,

When using

curl --proxy-insecure -k -x "127.0.0.1:5050" 'http://httpbin.org/anything?json' -v

Everything works as expected.

When using

curl --proxy-insecure -k -x "127.0.0.1:5050" 'http://httpbin.org/anything?json' -v

All I get back is a :

*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 5050 (#0)
* allocate connect buffer!
* Establish HTTP proxy tunnel to httpbin.org:443
* Proxy auth using Basic with user 'XXXXXXX'
> CONNECT httpbin.org:443 HTTP/1.1
> Host: httpbin.org:443
> Proxy-Authorization: Basic XXXXXXXX==
> User-Agent: curl/7.64.1
> Proxy-Connection: Keep-Alive
> 
* Proxy CONNECT aborted
* CONNECT phase completed!
* Closing connection 0
curl: (56) Proxy CONNECT aborted

I could not find anything in the documentation.

Here is my config:

var http = require('http'),
    httpProxy = require('http-proxy');

//
// Create a proxy server with custom application logic
//
var proxy = httpProxy.createProxyServer({
    secure: false,
    ignorePath: true,
    hostRewrite: true,
    followRedirects: true,
    protocolRewrite: true,
    changeOrigin: true
});

//
// Create your custom server and just call `proxy.web()` to proxy
// a web request to the target passed in the options
// also you can use `proxy.ws()` to proxy a websockets request
//
var server = http.createServer(function(req, res) {
    // You can define here your custom logic to handle the request
    // and then proxy the request.
    var header_auth = req.headers["proxy-authorization"]
    var decoded_auth = Buffer.from(header_auth.split(" ")[1], 'base64').toString()
    var api_key = decoded_auth.split(":")[0]
    var params = decoded_auth.split(":")[1]
    var url = req.url

    var api_url = `https://app.scrapingbee.com/api/v1?api_key=${api_key}&proxy_mode=True&${params}&url=${url}`

    req.url = api_url

    proxy.web(req, res, { target: api_url });
});

console.log("listening on port 5050")
server.listen(5050);

daolf avatar Feb 11 '21 21:02 daolf

https://github.com/http-party/node-http-proxy#using-https

You need ssl key and cert. They can be self-signed, but you still need them.

Maxim-Mazurok avatar Feb 16 '21 06:02 Maxim-Mazurok

HI there,

And thank you for answering. I thought about this section too, but somehow it can't work:

var fs = require('fs')
var http = require('http'),
    httpProxy = require('http-proxy');

//
// Create a proxy server with custom application logic
//
var proxy = httpProxy.createProxyServer({
    secure: false,
    ignorePath: true,
    hostRewrite: true,
    followRedirects: true,
    protocolRewrite: true,
    changeOrigin: true,
    ssl: {
        key: fs.readFileSync('example.key', 'utf8'),
        cert: fs.readFileSync('example.crt', 'utf8')
    },
}).listen(443);

//
// Create your custom server and just call `proxy.web()` to proxy
// a web request to the target passed in the options
// also you can use `proxy.ws()` to proxy a websockets request
//
var server = http.createServer(function(req, res) {
    // You can define here your custom logic to handle the request
    // and then proxy the request.
    var header_auth = req.headers["proxy-authorization"]
    var decoded_auth = Buffer.from(header_auth.split(" ")[1], 'base64').toString()
    var api_key = decoded_auth.split(":")[0]
    var params = decoded_auth.split(":")[1]
    var url = req.url

    var api_url = `https://app.myapi.com/api/v1?api_key=${api_key}&proxy_mode=True&${params}&url=${url}`

    req.url = api_url

    proxy.web(req, res, { target: api_url });
});

console.log("listening on port 5050")
server.listen(5050);

and if I do curl -k -x "http://user:[email protected]:5050" 'https://httpbin.org/anything?json' -v

All I can see is

*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 5050 (#0)
* allocate connect buffer!
* Establish HTTP proxy tunnel to httpbin.org:443
* Proxy auth using Basic with user 'user'
> CONNECT httpbin.org:443 HTTP/1.1
> Host: httpbin.org:443
> Proxy-Authorization: Basic <REDACTED>==
> User-Agent: curl/7.64.1
> Proxy-Connection: Keep-Alive
>
* Proxy CONNECT aborted
* CONNECT phase completed!
* Closing connection 0
curl: (56) Proxy CONNECT aborted

daolf avatar Feb 16 '21 20:02 daolf

Any idea? I get the same proxy connect aborted

javiermolinar avatar Jun 29 '21 10:06 javiermolinar

HI there,

And thank you for answering. I thought about this section too, but somehow it can't work:

var fs = require('fs')
var http = require('http'),
    httpProxy = require('http-proxy');

//
// Create a proxy server with custom application logic
//
var proxy = httpProxy.createProxyServer({
    secure: false,
    ignorePath: true,
    hostRewrite: true,
    followRedirects: true,
    protocolRewrite: true,
    changeOrigin: true,
    ssl: {
        key: fs.readFileSync('example.key', 'utf8'),
        cert: fs.readFileSync('example.crt', 'utf8')
    },
}).listen(443);

//
// Create your custom server and just call `proxy.web()` to proxy
// a web request to the target passed in the options
// also you can use `proxy.ws()` to proxy a websockets request
//
var server = http.createServer(function(req, res) {
    // You can define here your custom logic to handle the request
    // and then proxy the request.
    var header_auth = req.headers["proxy-authorization"]
    var decoded_auth = Buffer.from(header_auth.split(" ")[1], 'base64').toString()
    var api_key = decoded_auth.split(":")[0]
    var params = decoded_auth.split(":")[1]
    var url = req.url

    var api_url = `https://app.myapi.com/api/v1?api_key=${api_key}&proxy_mode=True&${params}&url=${url}`

    req.url = api_url

    proxy.web(req, res, { target: api_url });
});

console.log("listening on port 5050")
server.listen(5050);

and if I do curl -k -x "http://user:[email protected]:5050" 'https://httpbin.org/anything?json' -v

All I can see is

*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 5050 (#0)
* allocate connect buffer!
* Establish HTTP proxy tunnel to httpbin.org:443
* Proxy auth using Basic with user 'user'
> CONNECT httpbin.org:443 HTTP/1.1
> Host: httpbin.org:443
> Proxy-Authorization: Basic <REDACTED>==
> User-Agent: curl/7.64.1
> Proxy-Connection: Keep-Alive
>
* Proxy CONNECT aborted
* CONNECT phase completed!
* Closing connection 0
curl: (56) Proxy CONNECT aborted

You're starting proxy on 443 port, and your server on 5050. Then when using curl, you specify that proxy is on 5050, which is not correct. So it tries to use your server as proxy. You have to switch them. Probably just a typo, check that.

Maxim-Mazurok avatar Jun 30 '21 01:06 Maxim-Mazurok