TooTallNate / proxy-agents

Node.js HTTP Proxy Agents Monorepo
https://proxy-agents.n8.io
872 stars 229 forks source link

[proxy] how to specify SSL certs/keys? #265

Closed trusktr closed 6 months ago

trusktr commented 6 months ago

With http-proxy (last I used it years ago), there's an option like this:

httpProxy.createServer({
        https: {
                ca: [fs.readFileSync('/etc/letsencrypt/live/example.com/chain.pem')],
                key: fs.readFileSync('/etc/letsencrypt/live/example.com/privkey.pem', 'utf8'),
                cert: fs.readFileSync('/etc/letsencrypt/live/example.com/cert.pem', 'utf8'),
        },
})

How do we do this with proxy?

trusktr commented 6 months ago

Oh, we pass the options to Node http.createServer directly I think.

trusktr commented 6 months ago

Yep, that's it, using built-in https instead of http:

import https from 'https' // native Node https module
import httpProxy from 'http-proxy'

const proxy = httpProxy.createProxyServer({})

const server = https.createServer({
    ca: [fs.readFileSync('/etc/letsencrypt/live/example.com/chain.pem')],
    key: fs.readFileSync('/etc/letsencrypt/live/example.com/privkey.pem', 'utf8'),
    cert: fs.readFileSync('/etc/letsencrypt/live/example.com/cert.pem', 'utf8'),
}, function(req, res) {
  proxy.web(req, res, { target: newHost })
})