HTTPS Proxy Server in node.js

后端 未结 4 1910
名媛妹妹
名媛妹妹 2020-12-07 12:38

I am developing a node.js proxy server application and I want it to support HTTP and HTTPS(SSL) protocols (as server).

I\'m cu

4条回答
  •  盖世英雄少女心
    2020-12-07 13:07

    I have created a http/https proxy with the aid of the http-proxy module: https://gist.github.com/ncthis/6863947

    Code as of now:

    var fs = require('fs'),
      http = require('http'),
      https = require('https'),
      httpProxy = require('http-proxy');
    
    var isHttps = true; // do you want a https proxy?
    
    var options = {
      https: {
        key: fs.readFileSync('key.pem'),
        cert: fs.readFileSync('key-cert.pem')
      }
    };
    
    // this is the target server
    var proxy = new httpProxy.HttpProxy({
      target: {
        host: '127.0.0.1',
        port: 8080
      }
    });
    
    if (isHttps)
      https.createServer(options.https, function(req, res) {
        console.log('Proxying https request at %s', new Date());
        proxy.proxyRequest(req, res);
      }).listen(443, function(err) {
        if (err)
          console.log('Error serving https proxy request: %s', req);
    
        console.log('Created https proxy. Forwarding requests from %s to %s:%s', '443', proxy.target.host, proxy.target.port);
      });
    else
      http.createServer(options.https, function(req, res) {
        console.log('Proxying http request at %s', new Date());
        console.log(req);
        proxy.proxyRequest(req, res);
      }).listen(80, function(err) {
        if (err)
          console.log('Error serving http proxy request: %s', req);
    
        console.log('Created http proxy. Forwarding requests from %s to %s:%s', '80', proxy.target.host, proxy.target.port);
      });
    

提交回复
热议问题