Puppeteer - 如何使用本地 IP 地址连接 WSEndpoint?

NIK*_*C M 8 google-chrome node.js google-chrome-headless puppeteer

我有两个用于 puppeteer 自动化的 Node.js 脚本。

1) 启动器.js

此 Puppeteer 脚本启动 Chrome 浏览器并断开与 chrome 的连接,以便可以使用 WSEndpoint 进行连接。

const puppeteer = require('puppeteer');

module.exports = async () => {
    try {
        const options = {
            headless: false,
            devtools: false,
            ignoreHTTPSErrors: true,
            args: [
                `--no-sandbox`,
                `--disable-setuid-sandbox`,
                `--ignore-certificate-errors`
            ]
        };
        const browser = await puppeteer.launch(options);
        let pagesCount = await browser.pages();
        const browserWSEndpoint = await browser.wsEndpoint();
        // console  WSEndPoint say : "ws://127.0.0.1:42207/devtools/browser/dbb2525b-ce44-43c2-a335-ff15d0306f36"
        console.log("browserWSEndpoint----- :> ", browserWSEndpoint);
        await browser.disconnect();
        return browserWSEndpoint;
    } catch (err) {
        console.error(err);
        process.exit(1);
        return false;
    }
};
Run Code Online (Sandbox Code Playgroud)

2)connector.js

启动无头 chrome 并尝试通过 WSEndPoint 通过各种主机名连接 chrome。如果我使用 run command as 运行此脚本 node connector.js localhost,它会尝试使用 localhost 作为主机名连接 WSEndpint。

const puppeteer = require('puppeteer');
const launcher = require('./launcher');

(async (host) => {
    try {
        let WSEndPoint = await launcher();
        WSEndPoint = WSEndPoint.replace('127.0.0.1', host);
        console.log("WSENDPOINT :", WSEndPoint);
        const browser = await puppeteer.connect({
            browserWSEndpoint: WSEndPoint,
            ignoreHTTPSErrors: true
        });
        let pagesCount = await browser.pages();
        console.log("Pages available :> ", pagesCount.length);
        // const browserWSEndpoint = await browser.wsEndpoint();
        await browser.disconnect();
        process.exit(1);
        return true;
    } catch (err) {
        console.error(err);
        process.exit(1);
        return false;
    }
})(process.argv[2]);
Run Code Online (Sandbox Code Playgroud)

但是我无法使用我的本地 IP 地址(比如 192.168.1.36)连接 chrome 的 WSEndpint。为什么?

错误信息是

{ Error: connect ECONNREFUSED 192.168.1.33:36693
    at Object._errnoException (util.js:992:11)
    at _exceptionWithHostPort (util.js:1014:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1186:14)
  code: 'ECONNREFUSED',
  errno: 'ECONNREFUSED',
  syscall: 'connect',
  address: '192.168.1.33',
  port: 36693 }
Run Code Online (Sandbox Code Playgroud)

Md.*_*her 6

您可以代理 websocket 并连接到代理。我在我的服务器上测试了它,它运行成功。

下面是我们如何使用创建一个 websocket 服务器http-proxy

服务器

const httpProxy = require("http-proxy");
const host = "0.0.0.0";
const port = 8080;
async function createServer(WSEndPoint, host, port) {
  await httpProxy
    .createServer({
      target: WSEndPoint, // where we are connecting
      ws: true,
      localAddress: host // where to bind the proxy
    })
    .listen(port); // which port the proxy should listen to
  return `ws://${host}:${port}`; // ie: ws://123.123.123.123:8080
}
Run Code Online (Sandbox Code Playgroud)

然后当我们运行它时,我们创建一个代理服务器并监听 ws 端点。我不是为了让解释更简单而导出它。

// your other codes
const pagesCount = (await browser.pages()).length; // just to make sure we have the same stuff on both place
const browserWSEndpoint = await browser.wsEndpoint();
const customWSEndpoint = await createServer(browserWSEndpoint, host, port); // create the server here
console.log({ browserWSEndpoint, customWSEndpoint, pagesCount });
// your other code here
Run Code Online (Sandbox Code Playgroud)

当我在服务器上运行时,我们得到以下信息,

在液滴上,

?  node index.js 
{ browserWSEndpoint: 'ws://127.0.0.1:45722/devtools/browser/df0ca6a9-48ba-4962-9a20-a3a536d403fa',
  customWSEndpoint: 'ws://0.0.0.0:8080',
  pagesCount: 1 }
Run Code Online (Sandbox Code Playgroud)

客户

然后我们像这样连接它,

const puppeteer = require("puppeteer-core");
(async serverAddr => {
  const browser = await puppeteer.connect({
    browserWSEndpoint: `ws://${serverAddr}`,
    ignoreHTTPSErrors: true
  });
  const pagesCount = (await browser.pages()).length;
  const browserWSEndpoint = await browser.wsEndpoint();
  console.log({ browserWSEndpoint, pagesCount });
})(process.argv[2]);
Run Code Online (Sandbox Code Playgroud)

在我的访客计算机上,

?  node index.js "123.123.123.123:8080"
Pages available :>  1
{ browserWSEndpoint: 'ws://123.123.123.123:8080' }
Run Code Online (Sandbox Code Playgroud)

通过这种方式,我们可以在多个服务器上托管并在它们之间进行路由,如果需要,可以缩小规模等等。

和平!