Sunday, 9 April 2017

Simple proxy server in node.js: https requests not working

I have created a very simple proxy server for debugging network traffic (code below). Basic flow: user -> proxy -> server -> proxy -> user

This works as expected for regular http requests from client, but all https requests fail (hangs forever). I also tried using the https node module, but same result. Any input is appreciated.

const http = require('http')
const url = require('url')

const requestHandler = (req, res) => {

    var options = url.parse(req.url)
    options.headers = req.headers
    options.method = req.method
    options.agent = false

    const connector = http.request(options, (serverResponse) => {
        serverResponse.pause()
        res.writeHeader(serverResponse.statusCode, serverResponse.headers)
        serverResponse.pipe(res)
        serverResponse.resume()
    })
    req.pipe(connector)
    req.resume()
}

http.createServer(requestHandler).listen(8888)



via Øyvind Hauge

No comments:

Post a Comment