chimurai / http-proxy-middleware

:zap: The one-liner node.js http-proxy middleware for connect, express, next.js and more
MIT License
10.79k stars 853 forks source link

proxyReq 事件改变 header User-Agent 无效 #1040

Open h0medev2024 opened 2 months ago

h0medev2024 commented 2 months ago

Checks

Describe the bug (be clear and concise)

const proxyMiddleware = createProxyMiddleware({ target: 'https://xxx.aaa.com', ws:true, secure: false, changeOrigin: true, selfHandleResponse : true, on: { proxyReq: (proxyReq, req, res) => {

 // 抓包显示User-Agent未被改变
  proxyReq.setHeader('User-Agent', 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148'); 

  // 抓包显示User-Agent未被改变
  req.headers['User-Agent'] = 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148'; 

  console.log('[DEMO] Request header:' + req.rawHeaders);

  if (req.method == 'POST') {
    console.log('[DEMO] Request body:' + req.body);
  }
},
proxyRes: responseInterceptor(async (responseBuffer, proxyRes, req, res) => {
  const response = responseBuffer.toString('utf8');
  console.log('[DEMO] Response body:' + response);
  return responseBuffer;
}),
error: (err, req, res) => {
  console.log('[DEMO] Error:' + err.message);
},

}, logger: console, });

如代码所示,代理出口抓包显示User-Agent未被改变 Mac 通过npn安装的最新版