javascript - nodejs - 无法使用代理服务器拦截应用程序流量
问题描述
我想拦截和镜像来自应用程序的流量。我创建了一个简单的 nodejs cli 脚本,它将运行代理服务器。我需要打开目标应用程序蚂蚁然后拦截所有请求以将它们记录到控制台中。这是代码
const exec = require('child_process').execSync;
const http = require('http');
http.createServer( (clientReq, clientRes) => {
console.log('requesting: ' + clientReq.url);
const options = {
hostname: clientReq.url,
port: 80,
path: clientReq.url,
method: clientReq.method,
headers: clientReq.headers
}
const proxy = http.request(options, (res) => {
clientRes.writeHead(res.statusCode, res.headers);
res.pipe(clientRes, {
end: true
});
});
clientReq.pipe(proxy, {
end: true
});
}).listen(3000);
发生的事情是,如果我尝试打开应用程序然后运行代理服务器,脚本会给我这个错误,如果我从控制台运行它,相同的命令将起作用
//command
exec('open /Users/dev/Applications/My\ applicationo.app --args --proxy-server=localhost:3000 --ignore-certificate-errors');
//error in console
The files /Users/dev/Applications/My and /Users/dev/Desktop/proxy-req/applicationo.app do not exist.
node:child_process:747
throw err;
^
Error: Command failed: open /Users/dev/Applications/My applicationo.app --args --proxy-server=localhost:3000 --ignore-certificate-errors
The files /Users/dev/Applications/My and /Users/dev/Desktop/proxy-req/applicationo.app do not exist.
为了避免错误,execSync
我尝试直接从终端运行命令,目标应用程序将正确打开,但是当应用程序尝试发出请求时,脚本将停止并记录此错误:
node:events:342
throw er; // Unhandled 'error' event
^
Error: getaddrinfo ENOTFOUND http://dpm.demdex.net/id?d_visid_ver=2.0.0&d_fieldgroup=AAM&d_rtbd=json&d_ver=2&d_orgid=0ABA4673527831C00A490D45%40AdobeOrg&d_nsid=0&d_mid=08216528577836205561353312884345464264&d_blob=RKhpRz8krg2tLO6pguXWp5olkAcUniQYPHaMWWgdJ3xzPWQmdj0y&ts=1621764238127
at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:69:26)
Emitted 'error' event on ClientRequest instance at:
at ClientRequest.onerror (node:internal/streams/readable:770:14)
at ClientRequest.emit (node:events:365:28)
at Socket.socketErrorListener (node:_http_client:447:9)
at Socket.emit (node:events:365:28)
at emitErrorNT (node:internal/streams/destroy:193:8)
at emitErrorCloseNT (node:internal/streams/destroy:158:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
errno: -3008,
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'http://dpm.demdex.net/id?d_visid_ver=2.0.0&d_fieldgroup=AAM&d_rtbd=json&d_ver=2&d_orgid=0ABA4673527831C00A490D45%40AdobeOrg&d_nsid=0&d_mid=08216528577836205561353312884345464264&d_blob=RKhpRz8krg2tLO6pguXWp5olkAcUniQYPHaMWWgdJ3xzPWQmdj0y&ts=1621764238127'
}
如果我单击它记录的 url 工作正常。我不确定问题是否出在代码和请求选项配置上。如何将命令修复到脚本中,以便在执行脚本时直接打开应用程序以及如何修复有关请求的错误?
解决方案
推荐阅读
- c - 尽管 SIF_DISABLENOSCROLL,滚动条仍不可见
- c# - Unity C#:第三人称相机控制器:向相机方向旋转时捕捉
- azure-devops - Azure 自托管 Windows 代理挂钩
- mysql - 错误 1364 (HY000):字段“名称”没有默认值
- c# - EF Core 自动迁移流程
- python - 如何为同一个包添加多个依赖 URL 到 setuptools?
- bash - 将 github 字节粗略近似为 loc
- javascript - Google Chrome 扩展程序:有时 event.js 中的 chrome.runtime.onMessage.addListener 不会触发
- python - Google Translate API python 每次都不工作,有时我得到输出,有时我没有?
- angular - 是否打算在 Angular 中评估投影内容,即使它没有呈现给 DOM?