Node.js 具有自定义 Http(s) 代理和 Connect.js 中间件的代理

Node.js Proxy with custom Http(s) Agent and Connect.js Middleware

我在 Node 中组装了一个代理服务器,它需要能够通过 tls 传输 https 请求并且一切正常。使用以下两个包非常容易设置:proxy, https-proxy-agent. My issue is that I'm trying to capture HAR files using connect 作为中间件层,我收到以下错误:

_http_outgoing.js:357
throw new Error('Can\'t set headers after they are sent.');
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:357:11)
at ServerResponse.writeHead (_http_server.js:180:21)
at ClientRequest.<anonymous> (/.../node_modules/proxy/proxy.js:233:11)
at emitOne (events.js:96:13)
at ClientRequest.emit (events.js:188:7)
at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:473:21)
at HTTPParser.parserOnHeadersComplete (_http_common.js:99:23)
at Socket.socketOnData (_http_client.js:362:20)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)

就像下面这样简单,它似乎只在我使用 connect and proxying through my local browser(this proxy is actually being used with BrowserStackLocal 时才会发生。当我从我的本地机器浏览器以外的任何东西通过代理时,就好像它甚至不知道中间件的存在。

所以基本上,我只需要在这种情况下连接工作,我不确定是否需要暂停某些内容并继续,或者什么...任何想法将不胜感激。基本代码如下:

const path = require('path');
const http = require('http');
const proxy = require('proxy');
const Agent = require('https-proxy-agent');
const connect = require('connect');
const har = require('./har');

const middleware = connect();

middleware.use(har({
  harOutputDir: path.resolve(process.cwd(), 'har/')
}));

const server = proxy(http.createServer(middleware));
server.agent = new Agent('http://localhost:8081');

server.listen(8081)

谢谢!

编辑:请注意:har 中间件根本没有修改 headers。

proxy 好久没维护了。构建未通过,最后一次提交未通过测试。您提供的堆栈跟踪的来源来自 here in Line 233 - 错误的库代码。

编写类似的代理很简单。以下代码说明了如何创建一个。

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

const proxiedServer = 'http://localhost:8888';

// Proxy server
http.createServer((req, res) => {
  console.log(`Proxy: Got ${req.url}`);

  const _r = http.request(
    Object.assign(
      {},
      urlP.parse(proxiedServer),
      {
        method: req.method,
        path: req.url
      }
    ),
    _res => {
      res.writeHead(_res.statusCode, _res.headers);
      _res.pipe(res);
    }
  );

  req.pipe(_r);

}).listen(3124, () => {
  console.log("Listening on 3124");
});


// Regular server. Could be Express
http.createServer((req, res) => {
  console.log('Proxied server: ', req.url);
  let b = '';
  req.on('data', c => {
    b += c;
  });
  req.on('end', () => {
    console.log('Proxied server: ', b);
  });
  res.writeHead(200);
  res.end('ok');
}).listen(8888, () => {
  console.log('proxied server listening on 8888');
});

您使用自己的自定义代理的代码如下所示:

const urlP = require('url');
const path = require('path');
const http = require('http');
const connect = require('connect');
const har = require('./har');

const proxiedServer = 'http://localhost:8888';

// Proxy server
http.createServer((req, res) => {
  console.log(`Proxy: Got ${req.url}`);

  const _r = http.request(
    Object.assign(
      {},
      urlP.parse(proxiedServer),
      {
        method: req.method,
        path: req.url
      }
    ),
    _res => {
      res.writeHead(_res.statusCode, _res.headers);
      _res.pipe(res);
    }
  );

  req.pipe(_r);

}).listen(3124, () => {
  console.log("Listening on 3124");
});

const middleware = connect();
middleware.use(har({
  harOutputDir: path.resolve(process.cwd(), 'har/')
}));

middleware.use((req, res) => {
  console.log('Proxied server: ', req.url);
  let b = '';
  req.on('data', c => {
    b += c;
  });
  req.on('end', () => {
    console.log('Proxied server: ', b);
  });
  res.writeHead(200);
  res.end('ok');
});


http.createServer(middleware).listen(8888, () => {
  console.log('proxied server listening on 8888');
});