selenium-tor/example.php

75 lines
2.2 KiB
PHP

<?php
use Sikofitt\WebDriver\FirefoxBinary;
use Sikofitt\WebDriver\ImageDownloader;
use Sikofitt\WebDriver\Remote\DesiredCapabilities;
use Sikofitt\WebDriver\Tor\TorProfile;
use Sikofitt\WebDriver\TorLauncher;
use Symfony\Component\Process\ProcessBuilder;
require __DIR__ . '/vendor/autoload.php';
$selenium = new Symfony\Component\Process\ProcessBuilder();
$selenium->setPrefix(__DIR__ . '/vendor/bin/selenium-server-standalone');
$process = $selenium->getProcess();
$process->start();
$process->wait(function() use($process) {
while(false == $process->isRunning()) {
print 'Waiting ...';
}
return true;
});
$firefoxBinary = new FirefoxBinary(__DIR__ . '/tor-browser_en-US/Browser/start-tor-browser');
$caps = DesiredCapabilities::tor($firefoxBinary);
$profile = new TorProfile();
$caps->setCapability('timeout', 3600);
$caps->setCapability(FirefoxDriver::PROFILE, $profile);
$torProcess = TorLauncher::launch($firefoxBinary);
// Wait for Tor to connect
$sleepTime = 5;
$torProcess->wait(function() use ($sleepTime) {
sleep($sleepTime);
});
$webDriver = RemoteWebDriver::create('http://localhost:4444/wd/hub', $caps);
$webDriver->manage()->timeouts()->implicitlyWait(5);
$webDriver->navigate()->to('http://32b5oz2bbtn6gqj3.onion');
// Or $this-webDriver->get('http://32b5oz2bbtn6gqj3.onion');
$pageImages = $webDriver->findElements(WebDriverBy::tagName('img'));
$images = [];
$downloader = new ImageDownloader();
foreach($pageImages as $image)
{
$downloader->open($image->getAttribute('src'));
print 'Saving image ' . $downloader->getFileName();
// write some data about the request
file_put_contents(
sys_get_temp_dir() . '/' . $downloader->getFileName() . '.meta.json',
json_encode([
'pathinfo' => $downloader->getPathInfo(),
'curlinfo' => $downloader->getCurlInfo(),
'webdriver' => [
'title' => $webDriver->getTitle(),
'source' => base64_encode($webDriver->getPageSource())
]
], JSON_PRETTY_PRINT)
);
// save the image
$downloader->save(__DIR__ . '/images/');
// save the image in base64 format.
// base64_decode to view the raw image data.
$downloader->save(__DIR__ . '/images/', true);
}
TorLauncher::stop($torProcess);
$process->stop();