Quantcast
Channel: Active questions tagged https - Stack Overflow
Viewing all articles
Browse latest Browse all 1506

Proxy HTTPS with HTTP in Node.js express

$
0
0

I wrote an express app as an HTTP proxy, to intercept and analyse some of the network traffic. The parts of traffic my app is interested in are all HTTP, however I still want my app to proxy HTTPS so users can use it without extra setting.

My express app is created with a HTTP server. When testing, I changed the proxy setting in Chrome with SwitchyOmega, to proxy HTTPS connections with HTTP. HTTP works well, But my express app couldn't get these proxy requests for HTTPS.

So I wrote a simple TCP proxy to check on them, and find that they're like this:

CONNECT HOSTNAME:443 HTTP/1.1Host: HOSTNAMEProxy-Connection: keep-aliveUser-Agent: MY_AGENTENCRYPTED HTTPS

I believe these requests are HTTP, but why express isn't receiving them?

For sure if I change the browser proxy setting to ignore HTTPS, the app works well. But I do want to know if there is any workaround that I can use to proxy all protocols with HTTP and only one port.

THX.

UPDATE- code from my express app

app.use('*', function (req, res, next) {    // print all the request the app receive    console.log('received:', req.url)})app.use(bodyParser.text({type: '*/*'}))app.use(cookieParser())app.use(logger('dev'))app.use(express.static(path.join(__dirname, 'public')))// serve web pages for my app, only the request targeting my server// is handled here(right IP and port), proxy request gets handled after this.app.use('/', internalRoute)// analyse the part I wantapp.use('/END_POINT_I_WANT', myRoute)// handle proxy requestsapp.use('*', function (req, res, next) {  // proxy the request here})

The problem is, my first middleware, which is used to display all the requests the app receive, can't catch the HTTPS proxy requests wrapped in HTTP described above. And of course the middleware I used as proxy can't catch them either.

UPDATE-tried node-http-prxoy, no luck

var httpProxy = require('http-proxy')  , http = require('http')  , fs = require('fs')var options = {target: 'http://127.0.0.1:8099'}  , proxy = httpProxy.createServer(options)http.createServer(function (req, res) {  console.log(req.url)  proxy.web(req, res)}).listen(5050)

With the above code, and browser setting to proxy all protocols with HTTP, it works the same as my express app. HTTPS proxy requests gets ERR_EMPTY_RESPONSE, and nothing on the console.

With the below options, it seems that I have to change the proxy protocol to HTTPS, which I'd rather not use, at least for now. And I get ERR_PROXY_CERTIFICATE_INVALID for my self-signed certs...

var options  = { secure: true               , target: 'http://127.0.0.1:8099'               , ssl: { key: fs.readFileSync('cert/key.pem', 'utf8')                      , cert: fs.readFileSync('cert/server.crt', 'utf8')                      }               }

UPDATE- pin point the problem to the 'connect' event listener

Through some searching, I found this post helpful.

It pointed out that the http server doesn't have a listener for the connect event. I tried the code in the post, works. But as the last comment of that post mentioned, my app serves as a proxy in order to get the data, it then proxy the request to another proxy in order to go over the GreatFireWall.

The process is like : BROWSER -> MY_APP -> ANOTHER_PROXY -> TARGET.

Without the ANOTHER_PROXY, which is an HTTP proxy, it works well for both HTTP and HTTPS. However I failed to chain them all up. The ANOTHER_PROXY I use supports HTTPS over HTTP.


Viewing all articles
Browse latest Browse all 1506

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>