Skip to content

Commit

Permalink
Merge pull request #239 from ZFC-Digital/fix-change-flags
Browse files Browse the repository at this point in the history
fix: fix docker freezing issue
  • Loading branch information
mdervisaygan authored Dec 8, 2024
2 parents 5638bb7 + 9309383 commit f6d79dc
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 122 deletions.
155 changes: 95 additions & 60 deletions lib/cjs/index.js
Original file line number Diff line number Diff line change
@@ -1,78 +1,113 @@
var puppeteer = require('rebrowser-puppeteer-core')
const { pageController } = require('./module/pageController.js')
const Xvfb = require('xvfb');
var puppeteer = require("rebrowser-puppeteer-core");
const { pageController } = require("./module/pageController.js");
const Xvfb = require("xvfb");

async function connect({ args = [], headless = false, customConfig = {}, proxy = {}, turnstile = false, connectOption = {}, disableXvfb = false, plugins = [], ignoreAllFlags = false }) {
const { launch, Launcher } = await import('chrome-launcher');
async function connect({
args = [],
headless = false,
customConfig = {},
proxy = {},
turnstile = false,
connectOption = {},
disableXvfb = false,
plugins = [],
ignoreAllFlags = false,
}) {
const { launch, Launcher } = await import("chrome-launcher");

let xvfbsession = null
if (headless == 'auto') headless = false
let xvfbsession = null;
if (headless == "auto") headless = false;

if (process.platform === 'linux' && disableXvfb === false) {
try {
xvfbsession = new Xvfb({
silent: true,
xvfb_args: ['-screen', '0', '1920x1080x24', '-ac']
});
xvfbsession.startSync();
} catch (err) {
console.log('You are running on a Linux platform but do not have xvfb installed. The browser can be captured. Please install it with the following command\n\nsudo apt-get install xvfb\n\n' + err.message);
}
if (process.platform === "linux" && disableXvfb === false) {
try {
xvfbsession = new Xvfb({
silent: true,
xvfb_args: ["-screen", "0", "1920x1080x24", "-ac"],
});
xvfbsession.startSync();
} catch (err) {
console.log(
"You are running on a Linux platform but do not have xvfb installed. The browser can be captured. Please install it with the following command\n\nsudo apt-get install xvfb\n\n" +
err.message
);
}
}

const chrome = await launch({
ignoreDefaultFlags: true,
chromeFlags: [
...(
(ignoreAllFlags === true)
? [
...((proxy && proxy.host && proxy.port) ? [`--proxy-server=${proxy.host}:${proxy.port}`] : []),
...args, ...((headless !== false) ? [`--headless=${headless}`] : []),
] : [
...Launcher.defaultFlags().filter(item => !item.includes("--disable-features") && !item.includes("component-update")),
...args, ...((headless !== false) ? [`--headless=${headless}`] : []),
...((proxy && proxy.host && proxy.port) ? [`--proxy-server=${proxy.host}:${proxy.port}`] : []),
'--disable-features=Translate,OptimizationHints,MediaRouter,DialMediaRouteProvider,CalculateNativeWinOcclusion,InterestFeedContentSuggestions,CertificateTransparencyComponentUpdater,AutofillServerCommunication,PrivacySandboxSettings4,AutomationControlled',
"--no-sandbox"
]
const chrome = await launch({
ignoreDefaultFlags: true,
chromeFlags: [
...(ignoreAllFlags === true
? [
...(proxy && proxy.host && proxy.port
? [`--proxy-server=${proxy.host}:${proxy.port}`]
: []),
...args,
...(headless !== false ? [`--headless=${headless}`] : []),
]
: [
...Launcher.defaultFlags().filter(
(item) =>
!item.includes("--disable-features") &&
!item.includes("component-update")
),
],
...customConfig
});
...args,
...(headless !== false ? [`--headless=${headless}`] : []),
...(proxy && proxy.host && proxy.port
? [`--proxy-server=${proxy.host}:${proxy.port}`]
: []),
"--disable-features=Translate,OptimizationHints,MediaRouter,DialMediaRouteProvider,CalculateNativeWinOcclusion,InterestFeedContentSuggestions,CertificateTransparencyComponentUpdater,AutofillServerCommunication,PrivacySandboxSettings4,AutomationControlled",
"--no-sandbox",
"--disable-dev-shm-usage",
]),
],
...customConfig,
});

if (plugins.length > 0) {
const { addExtra } = await import('puppeteer-extra');
if (plugins.length > 0) {
const { addExtra } = await import("puppeteer-extra");

puppeteer = addExtra(puppeteer);
puppeteer = addExtra(puppeteer);

for (const item of plugins) {
puppeteer.use(item);
}
for (const item of plugins) {
puppeteer.use(item);
}
}

const browser = await puppeteer.connect({
browserURL: `http://127.0.0.1:${chrome.port}`,
...connectOption
});
const browser = await puppeteer.connect({
browserURL: `http://127.0.0.1:${chrome.port}`,
...connectOption,
});

let [page] = await browser.pages();
let [page] = await browser.pages();

let pageControllerConfig = { browser, page, proxy, turnstile, xvfbsession, pid: chrome.pid, plugins }
let pageControllerConfig = {
browser,
page,
proxy,
turnstile,
xvfbsession,
pid: chrome.pid,
plugins,
};

page = await pageController({ ...pageControllerConfig, killProcess: true, chrome });
page = await pageController({
...pageControllerConfig,
killProcess: true,
chrome,
});

browser.on('targetcreated', async target => {
if (target.type() === 'page') {
let newPage = await target.page();
pageControllerConfig.page = newPage
newPage = await pageController(pageControllerConfig);
}
});

return {
browser,
page
browser.on("targetcreated", async (target) => {
if (target.type() === "page") {
let newPage = await target.page();
pageControllerConfig.page = newPage;
newPage = await pageController(pageControllerConfig);
}
});

return {
browser,
page,
};
}

module.exports = { connect }
module.exports = { connect };
157 changes: 96 additions & 61 deletions lib/esm/index.mjs
Original file line number Diff line number Diff line change
@@ -1,76 +1,111 @@
import { launch, Launcher } from 'chrome-launcher';
import puppeteer from 'rebrowser-puppeteer-core';
import { pageController } from './module/pageController.mjs';
import Xvfb from 'xvfb';
import { launch, Launcher } from "chrome-launcher";
import puppeteer from "rebrowser-puppeteer-core";
import { pageController } from "./module/pageController.mjs";
import Xvfb from "xvfb";

// process.env.REBROWSER_PATCHES_DEBUG=1
export async function connect({ args = [], headless = false, customConfig = {}, proxy = {}, turnstile = false, connectOption = {}, disableXvfb = false, plugins = [], ignoreAllFlags = false }) {
let xvfbsession = null
if (headless == 'auto') headless = false
export async function connect({
args = [],
headless = false,
customConfig = {},
proxy = {},
turnstile = false,
connectOption = {},
disableXvfb = false,
plugins = [],
ignoreAllFlags = false,
}) {
let xvfbsession = null;
if (headless == "auto") headless = false;

if (process.platform === 'linux' && disableXvfb === false) {
try {
xvfbsession = new Xvfb({
silent: true,
xvfb_args: ['-screen', '0', '1920x1080x24', '-ac']
});
xvfbsession.startSync();
} catch (err) {
console.log('You are running on a Linux platform but do not have xvfb installed. The browser can be captured. Please install it with the following command\n\nsudo apt-get install xvfb\n\n' + err.message);
}
if (process.platform === "linux" && disableXvfb === false) {
try {
xvfbsession = new Xvfb({
silent: true,
xvfb_args: ["-screen", "0", "1920x1080x24", "-ac"],
});
xvfbsession.startSync();
} catch (err) {
console.log(
"You are running on a Linux platform but do not have xvfb installed. The browser can be captured. Please install it with the following command\n\nsudo apt-get install xvfb\n\n" +
err.message
);
}
}

const chrome = await launch({
ignoreDefaultFlags: true,
chromeFlags: [
...(
(ignoreAllFlags === true)
? [
...((proxy && proxy.host && proxy.port) ? [`--proxy-server=${proxy.host}:${proxy.port}`] : []),
...args, ...((headless !== false) ? [`--headless=${headless}`] : []),
] : [
...Launcher.defaultFlags().filter(item => !item.includes("--disable-features") && !item.includes("component-update")),
...args, ...((headless !== false) ? [`--headless=${headless}`] : []),
...((proxy && proxy.host && proxy.port) ? [`--proxy-server=${proxy.host}:${proxy.port}`] : []),
'--disable-features=Translate,OptimizationHints,MediaRouter,DialMediaRouteProvider,CalculateNativeWinOcclusion,InterestFeedContentSuggestions,CertificateTransparencyComponentUpdater,AutofillServerCommunication,PrivacySandboxSettings4,AutomationControlled',
"--no-sandbox"
]
const chrome = await launch({
ignoreDefaultFlags: true,
chromeFlags: [
...(ignoreAllFlags === true
? [
...(proxy && proxy.host && proxy.port
? [`--proxy-server=${proxy.host}:${proxy.port}`]
: []),
...args,
...(headless !== false ? [`--headless=${headless}`] : []),
]
: [
...Launcher.defaultFlags().filter(
(item) =>
!item.includes("--disable-features") &&
!item.includes("component-update")
),
],
...customConfig
});
let pextra = null
if (plugins.length > 0) {
const { addExtra } = await import('puppeteer-extra');
...args,
...(headless !== false ? [`--headless=${headless}`] : []),
...(proxy && proxy.host && proxy.port
? [`--proxy-server=${proxy.host}:${proxy.port}`]
: []),
"--disable-features=Translate,OptimizationHints,MediaRouter,DialMediaRouteProvider,CalculateNativeWinOcclusion,InterestFeedContentSuggestions,CertificateTransparencyComponentUpdater,AutofillServerCommunication,PrivacySandboxSettings4,AutomationControlled",
"--no-sandbox",
"--disable-dev-shm-usage",
]),
],
...customConfig,
});
let pextra = null;
if (plugins.length > 0) {
const { addExtra } = await import("puppeteer-extra");

pextra = addExtra(puppeteer);
pextra = addExtra(puppeteer);

for (const item of plugins) {
pextra.use(item);
}
for (const item of plugins) {
pextra.use(item);
}
}

const browser = await (pextra ? pextra : puppeteer).connect({
browserURL: `http://127.0.0.1:${chrome.port}`,
...connectOption
});
const browser = await (pextra ? pextra : puppeteer).connect({
browserURL: `http://127.0.0.1:${chrome.port}`,
...connectOption,
});

let [page] = await browser.pages();
let [page] = await browser.pages();

let pageControllerConfig = { browser, page, proxy, turnstile, xvfbsession, pid: chrome.pid, plugins }
let pageControllerConfig = {
browser,
page,
proxy,
turnstile,
xvfbsession,
pid: chrome.pid,
plugins,
};

page = await pageController({ ...pageControllerConfig, chrome, killProcess: true });
page = await pageController({
...pageControllerConfig,
chrome,
killProcess: true,
});

browser.on('targetcreated', async target => {
if (target.type() === 'page') {
let newPage = await target.page();
pageControllerConfig.page = newPage
newPage = await pageController(pageControllerConfig);
}
});

return {
browser,
page
browser.on("targetcreated", async (target) => {
if (target.type() === "page") {
let newPage = await target.page();
pageControllerConfig.page = newPage;
newPage = await pageController(pageControllerConfig);
}
}
});

return {
browser,
page,
};
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "puppeteer-real-browser",
"version": "1.3.18",
"version": "1.3.20",
"description": "This package is designed to bypass puppeteer's bot-detecting captchas such as Cloudflare. It acts like a real browser and can be managed with puppeteer.",
"main": "lib/cjs/index.js",
"module": "lib/esm/index.mjs",
Expand Down

0 comments on commit f6d79dc

Please sign in to comment.