chimurai / http-proxy-middleware

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

`urlencoded` does not work when using `socks-proxy-agent` #878

Open xTCry opened 1 year ago

xTCry commented 1 year ago

Checks

Describe the bug (be clear and concise)

If a custom agent is specified in the configuration, for example, in my case, socks-proxy-agent, then the urlencode function cannot complete its work.

That is, no events for socket are called inside it.

The debug log body-parser:urlencoded has this output:

  body-parser:urlencoded content-type "application/x-www-form-urlencoded" +0ms
  body-parser:urlencoded content-encoding "identity" +2ms
  body-parser:urlencoded read body +1ms

Step-by-step reproduction instructions

1. Create a express server using createProxyMiddleware with configured agent socks-proxy-agent (socks5)
2. Run proxy app
3. Create a POST request

Expected behavior (be clear and concise)

It is expected that the urlencode function will give the result as without using a custom agent. The debug log body-parser:urlencoded should contain this output:

  body-parser:urlencoded content-type "application/x-www-form-urlencoded" +0ms
  body-parser:urlencoded content-encoding "identity" +2ms
  body-parser:urlencoded read body +0ms
  body-parser:urlencoded parse body +11ms
  body-parser:urlencoded parse extended urlencoding +1ms

How is http-proxy-middleware used in your project?

http-proxy-middleware@2.0.6
socks-proxy-agent@^7.0.0

What http-proxy-middleware configuration are you using?

import { SocksProxyAgent } from 'socks-proxy-agent';
const httpAgent = new SocksProxyAgent('socks5://...');

// createProxyMiddleware config:
  {
    target: xEnv.PROXY_TARGET,
    changeOrigin: true,
    toProxy: true,
    secure: false,
    // onProxyRes: this.onProxyRes.bind(this),
    onProxyReq: (proxyReq, req, res) => {
      if (req.method !== 'POST') return;
      urlencoded({ extended: false })(req, res, (err) => {
        console.log('REQ && ERR', req, err);
        !err && this.featuresService.logPostRequest(req as Request);
      });
    },
    onError(err, req, res) {
      const { code = '', message } = err as any;
      if (['ECONNRESET', 'ETIMEDOUT'].some((e) => code.includes(e) || message.includes(e))) {
        res.writeHead(503, { 'Content-Type': 'text/plain; charset=utf-8' });
        res.end('The server is currently unavailable. Try again later.');
        return;
      }

      res.writeHead(500, { 'Content-Type': 'text/plain; charset=utf-8' });
      res.end('Something went wrong.');
    },
    logLevel: 'debug',
    prependPath: false,
    agent: httpAgent,
  }

What OS/version and node/version are you seeing the problem?

Windows 10 & Node: 16.19.0

Additional context (optional)

No response

314171441 commented 9 months ago

me too