node-mitmproxy icon indicating copy to clipboard operation
node-mitmproxy copied to clipboard

how can i use other res as response?

Open leotian opened this issue 7 years ago • 1 comments

我想请求一个地址例如 https://xxx.abc.com/ckd/ 然后在responseInterceptor把我透明传输本地的一个响应过来

const mitmproxyConfig = {
  sslConnectInterceptor: (req, cltSocket, head) => {
    console.log('head', head)
    return true
  },
  requestInterceptor: (rOptions, req, res, ssl, next) => {
    console.log(`正在访问:${rOptions.protocol}//${rOptions.hostname}:${rOptions.port}`)
    console.log('cookie:', rOptions.headers.cookie)

    // res.end('Hello node-mitmproxy!')
    next()
  },
  responseInterceptor: (req, res, proxyReq, proxyRes, ssl, next) => {
    req.pipe(request('http://localhost:9850/ckd')).pipe(res)
    next()
  },
}

mitmproxy.createProxy(mitmproxyConfig)

但是这样的话一直报错 uncaughtException: Error: write after end

我怎么能做到这一点呢

leotian avatar Jun 13 '18 14:06 leotian

请求阶段:client --> requestInterceptor(proxy) --> server 响应阶段:server --> responseInterceptor(proxy) --> client

你想 client --> requestInterceptor(proxy) --> new server 所以应该配置 requestInterceptor 方法即可

wuchangming avatar Jun 14 '18 02:06 wuchangming