puppeteer 在本地工作但不在 EC2 AWS 上工作

puppeteer Works Local But Not On EC2 AWS

local 很好,但是当我部署在 aws ec2 服务器上时 它卡在 page.goto,并出现 **错误 ERR_CONNECTION_REFUSED**

所以我尝试为 GUI 安装 TightVNC 与卡在 page.goto 相同 我检查了 google-chrome 是否已安装, 当我在 tightVNC 查看器中的 aws ec2 上输入“google-chrome”时,它可以很好地显示 chrome

os: aws ec2 ubuntu18.04 (30Gib)

aws 安全组入站: 5000 TCP 0.0.0.0/0

5901 TCP 0.0.0.0/0

80 TCP 0.0.0.0/0

3000 TCP 0.0.0.0/0

22 TCP 0.0.0.0/0

443 TCP 0.0.0.0/0

5901 TCP ::/0

节点--版本:v16.14.2

sudo node --version: v16.14.2

npm --version: 8.5.0

sudo npm --version: 8.5.0

google-chrome --version: Google Chrome 101.0.4951.64

其中 google-chrome: /usr/bin/google-chrome 我试过 'executablePath: "/usr/bin/google-chrome"' 但与错误相同

~/file_name/node_modules/puppeteer/.local-chromium/linux-991974/chrome-linux 当我输入“ldd chrome | grep not”时没有任何显示,我安装了所有东西

我认为 most 与 puppeteer 和 aws ec2 有关,我搜索并尝试过

关我什么事? 请告诉我

const express = require("express");
const router = express.Router();
const puppeteer = require("puppeteer-extra");
const deepai = require("deepai");
const fs = require("fs");
const CryptoJS = require("crypto-js");
const axios = require("axios");
const StealthPlugin = require("puppeteer-extra-plugin-stealth");
puppeteer.use(StealthPlugin());
const puppeteerOptions = {
  headless: true,
  ignoreHTTPSErrors: false,
  args: [
    "--no-sandbox",
    "--single-process",
    "--no-zygote",
    "--disable-setuid-sandbox",
    "--disable-gpu",
    "--use-gl=egl",
  ],
  dumpio: true,
};
// executablePath: "/usr/bin/google-chrome",
//slowMo: 30, //사람이 타이핑하는 듯이 볼수 있음
// /usr/bin/google-chrome
//      /usr/lib/node_modules/puppeteer/.local-chromium/linux-991974

let browser;
let pageNameH;
router.post("/rpa", (req, res) => {
  (async function () {
    browser = await puppeteer.launch(puppeteerOptions);
    pageNameH = await browser.newPage();
    await pageNameH.setDefaultNavigationTimeout(0);
    await pageNameH.setViewport({ width: 1366, height: 768 });
    console.log("Running tests..");
    console.log("req.body.length", req.body.length); //5
    console.log("req.body[0]", req.body[0]); //{ Link: 'www.coupang.com' }
    console.log("req.body[0].Link", req.body[0].Link); //www.coupang.com
    console.log("req.body", req.body);
    var click = "";
    var ScreenshotName = "";
    for (var i = 0; i < req.body.length; i++) {
      if (req.body[i].Link) {
        await pageNameH.goto(req.body[i].Link);
        // await pageNameH.waitFor(500);
        console.log("Link");
      } else if (req.body[i].Click) {
        click = "#" + req.body[i].Click;
        console.log("Click");
      } else if (req.body[i].Input) {
        await pageNameH.type(click, req.body[i].Input);
        console.log("Input");
      } else if (req.body[i] == "Click") {
        await pageNameH.waitFor(500);
        await pageNameH.keyboard.press("Enter");
        await pageNameH.waitFor(500);
        await pageNameH.waitForNavigation();
        console.log("Button");
      } else if (req.body[i].ScreenShot) {
        ScreenshotName = req.body[i].ScreenShot + ".png";
        await pageNameH.screenshot({
          path: ScreenshotName,
          fullPage: true,
        });
        console.log("ScreenShot");
      } else if (req.body[i].sendSMS) {
        var userphoneNo = req.body[i].sendSMS;
        // browser.close();
        browser.close();
        console.log("browser.close");
        deepai.setApiKey("241c47f5-d430-4e76-b078-10df6ce6be7f");
        var resp = await deepai.callStandardApi("image-similarity", {
          image1: fs.createReadStream("coupang.png"),
          image2: fs.createReadStream(ScreenshotName),
        });
        if (resp["output"]["distance"] > 0) {
          const user_phone_number = userphoneNo; // SMS를 수신할 전화번호
          console.log("user_phone_number", user_phone_number);
          const date = Date.now().toString(); // 날짜 string
          // 환경 변수
          const sens_service_id = "ncp:sms:kr:284757485087:projectd";
          const sens_access_key = "***";
          const sens_secret_key = "***";
          const sens_call_number = "01012345678"; //회사 번호
          // url 관련 변수 선언
          const method = "POST";
          const space = " ";
          const newLine = "\n";
          const url = `https://sens.apigw.ntruss.com/sms/v2/services/${sens_service_id}/messages`;
          const url2 = `/sms/v2/services/${sens_service_id}/messages`;
          // signature 작성 : crypto-js 모듈을 이용하여 암호화
          console.log(1);
          const hmac = CryptoJS.algo.HMAC.create(
            CryptoJS.algo.SHA256,
            sens_secret_key
          );
          console.log(2);
          hmac.update(method);
          hmac.update(space);
          hmac.update(url2);
          hmac.update(newLine);
          hmac.update(date);
          hmac.update(newLine);
          console.log(sens_access_key);
          hmac.update(sens_access_key);
          const hash = hmac.finalize();
          console.log(4);
          const signature = hash.toString(CryptoJS.enc.Base64);
          console.log(5);
          // sens 서버로 요청 전송
          const smsRes = await axios({
            method: method,
            url: url,
            headers: {
              "Contenc-type": "application/json; charset=utf-8",
              "x-ncp-iam-access-key": sens_access_key,
              "x-ncp-apigw-timestamp": date,
              "x-ncp-apigw-signature-v2": signature,
            },
            data: {
              type: "SMS",
              countryCode: "82",
              from: sens_call_number,
              content: `쿠팡 홈쇼핑에 고객이 물건을 샀습니다.`,
              messages: [{ to: `${user_phone_number}` }],
            },
          });
          console.log("response", smsRes.data);
          return res.status(200).json({
            message: "SMS sent",
          });
        } else {
          return res.status(404).json({ message: "고객이 물건을 사지 않았다" });
        }
      }
    }
  })();
});
module.exports = router;

ERR_CONNECTION_REFUSED 表示目标站点正在主动拒绝来自您的服务器的连接。

AWS 和其他大型托管服务提供商的 IP 地址经常被商业网站禁止以避免抓取和机器人程序,因为,让我们面对现实吧,大多数时候有人从 AWS 访问它可能是一个机器人程序。

尝试使用更隐蔽的提供商and/or购买一些代理。