Skip to content

Commit

Permalink
add host & timeout options
Browse files Browse the repository at this point in the history
  • Loading branch information
alibo committed Mar 25, 2016
1 parent e2cd487 commit ed0a3bb
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 13 deletions.
2 changes: 1 addition & 1 deletion filternet
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use Symfony\Component\Console\Application;

$application = new Application();
$application->setName('Filternet Utility');
$application->setVersion('1.1.0');
$application->setVersion('1.1.1');
$application->add(new CheckSniCommand());
$application->add(new CheckDnsCommand());
$application->add(new CheckHttpCommand());
Expand Down
3 changes: 2 additions & 1 deletion src/Checkers/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public function check($url)
* @param string $url
* @return bool
*/
public function blocked($url) {
public function blocked($url)
{
$response = $this->request($url);

return $this->isBlocked($response);
Expand Down
17 changes: 16 additions & 1 deletion src/Checkers/Sni.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ class Sni
*/
private $domain;

/**
* Host to connect via TLS
*
* @var string
*/
private $host = 'https://google.com';

/**
* whether domain is blocked
*
Expand Down Expand Up @@ -63,7 +70,7 @@ protected function createContext()
*/
protected function request($context)
{
return @file_get_contents('https://google.com', false, $context);
return @file_get_contents($this->host, false, $context);
}

/**
Expand All @@ -78,4 +85,12 @@ protected function failed($context)

return !isset($meta['ssl']['session_meta']['protocol']);
}

/**
* @param string $host
*/
public function setHost($host)
{
$this->host = 'https://' . ltrim($host, 'https://');
}
}
48 changes: 38 additions & 10 deletions src/Commands/CheckAlexaCsvFileCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ class CheckAlexaCsvFileCommand extends Command
* @var array
*/
protected $sites = [];
/**
* HTTP Timeout
*
* @var int
*/
protected $httpTimeout;
/**
* @var string
*/
private $sniHost;

/**
* Configure command
Expand All @@ -48,14 +58,28 @@ protected function configure()
'max',
'm',
InputOption::VALUE_OPTIONAL,
'Top x files?',
'Top x sites?',
100
)
->addOption(
'output',
'o',
InputOption::VALUE_OPTIONAL,
'Output csv file'
'Path to csv output file'
)
->addOption(
'timeout',
't',
InputOption::VALUE_OPTIONAL,
'Http timeout',
10
)
->addOption(
'host',
null,
InputOption::VALUE_OPTIONAL,
'Connect to which host for SNI',
'google.com'
);
}

Expand All @@ -71,15 +95,17 @@ protected function execute(InputInterface $input, OutputInterface $output)
$max = $input->getOption('max');
$path = $input->getArgument('path');
$outputFile = $input->getOption('output');
$this->httpTimeout = $input->getOption('timeout');
$this->sniHost = $input->getOption('host');

if ($outputFile) {
file_put_contents($outputFile, implode(',', [
'Rank',
'Site',
'SNI',
'HTTP',
'DNS'
]) . "\n", FILE_APPEND);
'Rank',
'Site',
'SNI',
'HTTP',
'DNS'
]) . "\n", FILE_APPEND);
}

if (($handle = fopen($path, "r")) !== false) {
Expand Down Expand Up @@ -144,6 +170,8 @@ protected function check($rank, $domain, OutputInterface $output)
protected function checkSni($rank, $domain)
{
$sni = new Sni();
$sni->setHost($this->sniHost);

if ($sni->blocked($domain)) {
$this->stats['sni']++;
$this->sites[$rank][2] = 'blocked';
Expand All @@ -164,7 +192,7 @@ protected function checkSni($rank, $domain)
protected function checkHttp($rank, $url)
{
$http = new Http();
$http->setTimeout(5);
$http->setTimeout($this->httpTimeout);

if ($http->blocked($url)) {
$this->stats['http']++;
Expand Down Expand Up @@ -205,4 +233,4 @@ protected function checkDns($rank, $domain)
return $status;

}
}
}
9 changes: 9 additions & 0 deletions src/Commands/CheckHttpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ protected function configure()
'url',
InputArgument::REQUIRED,
'Url (example: http://dropbox.com)'
)
->addOption(
'timeout',
't',
InputOption::VALUE_OPTIONAL,
'Http timeout',
15
);
}

Expand All @@ -38,13 +45,15 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$url = $input->getArgument('url');
$timeout = $input->getOption('timeout');

if (!filter_var($url, FILTER_VALIDATE_URL)) {
$output->writeln('<error>Url is invalid!</error>');
return;
}

$http = new Http();
$http->setTimeout($timeout);
$progress = new ProgressBar($output, 100);
$progress->advance(10);
$result = $http->check($url);
Expand Down
9 changes: 9 additions & 0 deletions src/Commands/CheckSniCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ protected function configure()
'domain',
InputArgument::REQUIRED,
'Domain name (example: youtube.com)'
)
->addOption(
'host',
null,
InputOption::VALUE_OPTIONAL,
'Connect to which host (example: google.com)',
'google.com'
);
}

Expand All @@ -41,6 +48,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
$upperDomain = strtoupper($lowerDomain);

$sni = new Sni();
$sni->setHost($input->getOption('host'));

$progress = new ProgressBar($output, 2);

$lowerResult = $sni->blocked($lowerDomain) ? $this->blockedText() : $this->openText();
Expand Down

0 comments on commit ed0a3bb

Please sign in to comment.