我有两个用于操纵js的自动化的Node.js脚本。
1)launcher.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;
}
};
2)connector.js
启动无头chrome,并尝试使用各种主机名通过WSEndPoint连接chrome。如果我使用运行命令node connector.js localhost
来运行此脚本,它将尝试将WSEndpint与localhost连接为主机名。
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]);
但是我无法使用本地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 }
答案 0 :(得分:1)
您可以代理websocket并连接到代理。我在服务器上对其进行了测试,并成功运行了。
这是我们使用limit_choices_to
创建websocket服务器的方式,
http-proxy
然后,当我们运行它时,我们创建一个代理服务器并监听ws端点。为了简化说明,我没有导出它。
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
}
当我在服务器上运行时,我们得到以下信息,
在液滴上,
// 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
然后我们像这样连接它,
➜ server 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 }
在我的访客计算机上,
const puppeteer = require("puppeteer");
(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]);
这样,我们可以在多台服务器上托管并在它们之间路由,扩展并在需要时进行更多扩展。
和平!