-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #239 from ZFC-Digital/fix-change-flags
fix: fix docker freezing issue
- Loading branch information
Showing
3 changed files
with
192 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters