Skip to content

Commit

Permalink
support headless proxy with username and password
Browse files Browse the repository at this point in the history
  • Loading branch information
blogdaren committed Nov 23, 2024
1 parent c3595d9 commit 82acc93
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Downloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ public function useHeadlessBrowser($args = [])
if(property_exists($this, 'onHeadlessBrowserOpenPage')){
$this->headlessBrowser->getBrowserFactoryInstance()->setOptions($merged_options);
$browser = $this->headlessBrowser->getBrowserInstance();
$page = $this->headlessBrowser->getPage();
$page = $this->headlessBrowser->getPage($merged_options);
$returning = $this->triggerUserCallback('onHeadlessBrowserOpenPage', $this, $browser, $page, $args['url']);
if(false === $returning){
$page->close();
Expand Down
2 changes: 2 additions & 0 deletions src/Kernel/Language/en.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
'headless_browser_disabled' => "Headless Browser: headless browser is configured as disabled.",
'headless_browser_binary_not_found' => "Headless Browser:headless browser binary file (%bin%) not found, please install your own headless browser.",
'headless_browser_exception' => "Headless Browser:",
'headless_browser_proxy_server' => "Headless Browser:detect the proxy server is enabled: %proxy_server%",
'headless_browser_proxy_credential' => "Headless Browser:detect the credential submitted to the proxy server【%proxy_username% and %proxy_password%】",

//more fields to be configured......
];
Expand Down
2 changes: 2 additions & 0 deletions src/Kernel/Language/zh.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
'headless_browser_disabled' => "无头浏览:检测到已禁用无头浏览器......................................",
'headless_browser_binary_not_found' => "无头浏览:无头浏览器二进制文件【%bin%】找不到, 请自行安装无头浏览器...",
'headless_browser_exception' => "无头浏览:",
'headless_browser_proxy_server' => "无头浏览:检测到已启用代理服务器: %proxy_server%",
'headless_browser_proxy_credential' => "无头浏览:检测到提交给代理服务器的使用凭据【%proxy_username% 和 %proxy_password%】",

//若干待配字段......
];
Expand Down
51 changes: 48 additions & 3 deletions src/Kernel/Middleware/HeadlessBrowser/Chrome.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use HeadlessChromium\Page;
use HeadlessChromium\Exception\OperationTimedOut;
use HeadlessChromium\Exception\NavigationExpired;
use HeadlessChromium\Communication\Message;

class Chrome
{
Expand Down Expand Up @@ -263,7 +264,7 @@ public function request($method, $url, $args = [])
}

try{
$page = self::getPage();
$page = self::getPage($options);
$page->navigate($url)->waitForNavigation($page_event, $navigate_timeout);
$html = $page->getHtml();
$page->close();
Expand All @@ -279,11 +280,55 @@ public function request($method, $url, $args = [])
/**
* @brief get page
*
* @param array $options
*
* @return object
*/
public function getPage()
public function getPage($options)
{
return self::getBrowserInstance()->createPage();
$page = self::getBrowserInstance()->createPage();

if(!empty($options['proxyServer']))
{
Logger::warn(Tool::replacePlaceHolder($this->getWorker()->langConfig['headless_browser_proxy_server'], [
'proxy_server' => $options['proxyServer']
]));

if(!empty($options['proxyServerAuth']['username']) && !empty($options['proxyServerAuth']['password']))
{
Logger::warn(Tool::replacePlaceHolder($this->getWorker()->langConfig['headless_browser_proxy_credential'], [
'proxy_username' => $options['proxyServerAuth']['username'],
'proxy_password' => $options['proxyServerAuth']['password'],
]));
}
}

if(!empty($options['proxyServer']) && !empty($options['proxyServerAuth']['username']) && !empty($options['proxyServerAuth']['password']))
{
$username = $options['proxyServerAuth']['username'];
$password = $options['proxyServerAuth']['password'];
$page->getSession()->sendMessageSync(new Message('Network.setRequestInterception', ['patterns' => [['urlPattern' => '*']]]));
$page->getSession()->on('method:Network.requestIntercepted', function (array $params) use ($page, $username, $password) {
if (isset($params["authChallenge"])) {
$page->getSession()->sendMessageSync(
new Message('Network.continueInterceptedRequest', [
'interceptionId' => $params["interceptionId"],
'authChallengeResponse' => [
'response' => 'ProvideCredentials',
'username' => $username,
'password' => $password,
]
])
);
} else {
$page->getSession()->sendMessageSync(
new Message('Network.continueInterceptedRequest', ['interceptionId' => $params["interceptionId"]])
);
}
});
}

return $page;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Kernel/PHPCreeper.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class PHPCreeper extends Worker
*
* @var string
*/
public const CURRENT_VERSION = '2.0.0';
public const CURRENT_VERSION = '2.0.1';

/**
* engine name
Expand Down

0 comments on commit 82acc93

Please sign in to comment.