node.js - 在 headless 模式下运行时是否可以远程跟踪 chromium 的 GUI?

标签 node.js linux puppeteer google-chrome-headless chrome-remote-debugging

我在 AWS EC2 AMI (Linux) 上以 headless 模式运行 puppeteer 来执行一些网络抓取。是否可以从我的本地 Windows 设置远程跟踪它的 GUI?

我提到了this文章,想知道是否将 --remote-debugging-port=9222 添加到我的代码中,然后在本地浏览器上访问 http://localhost:9222 会让我看到图形用户界面。是否有可能做到这一点?我不确定我是否走在正确的轨道上。任何帮助将不胜感激。

最佳答案

是的,这是可能的。您必须设置 --remote-debugging-address 参数。

引自 List of Chromium Command Line Switches :

Use the given address instead of the default loopback for accepting remote debugging connections. Should be used together with --remote-debugging-port. Note that the remote debugging protocol does not perform any authentication, so exposing it too widely can be a security risk.

说明

默认情况下,Chrome 绑定(bind)到 127.0.0.1(仅限本地接口(interface)),该接口(interface)仅允许来自计算机本身的连接。如果您将参数设置为 0.0.0.0,Chrome 会监听所有允许从计算机外部进行连接的网络接口(interface)(请查看 this answer on stackoverflow 了解更多信息)。但是,您仍然需要确保没有可能阻止连接的防火墙,但默认情况下不应出现这种情况。

这意味着,像这样启动 Chrome 将允许从另一台计算机进行调试:

chrome --remote-debugging-port=9222 --remote-debugging-address=0.0.0.0 [possible other flags]

您现在可以访问 http://IP_OF_YOUR_MACHINE:9222 并会看到 Chrome 的 DevTools GUI。或者,您可以使用 puppeteer.connect 连接到计算机。 :

const browser = await puppeteer.connect({ browserURL: 'http://...:9222' });
// ...

安全考虑

请记住,此端口向任何能够访问互联网的人公开。您可能需要考虑使用防火墙来阻止连接或使用类似 node-http-proxy 的库在将任何连接传递到浏览器之前对其进行过滤。

关于node.js - 在 headless 模式下运行时是否可以远程跟踪 chromium 的 GUI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57550552/

相关文章:

c - 如何从 c 程序准确复制 linux 上的文件

javascript - 使用 Puppeteer 保存网页抓取密码的最安全方法是什么?

javascript - 使用nodejs和socketio从arduino读取数据

javascript - bundle.js 显示 HTML 代码而不是 javascript

javascript - 将异步函数传递给 Node.js Express.js 路由器

c++ - 简单的 C++ 声音 API

linux - 在文件中查找以字符开头的单词并添加/添加文本

web-scraping - 点击 "_blank"后使用 Puppeteer 获取新页面的句柄?

puppeteer - 如何在 puppeteer( headless chrome)中获取超时内容?

javascript - 在 Mocha 和 SuperTest 中设置基本身份验证