Skip to content

Commit ccaaa2d

Browse files
authored
Split different scanners in separate classes (#165)
* Split different scanners in separate classes * fix unit tests * fix phpstan errors * Improve BaseUrlScanner * Use addCommands to fix compatibility with symfony 8 * Update README
1 parent 30546f4 commit ccaaa2d

23 files changed

Lines changed: 637 additions & 346 deletions

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ What is this tool doing?
77
* Check the API version (pad.example.com/api)
88
* Check the etherpad version
99
* Check if the pads are publicly accessible
10+
* Check if websocket is working
1011
* Check if the server is healthy (pad.example.com/health)
1112
* Check if the admin area is accessible with default credentials (pad.example.com/admin)
1213
* Check if any (frontend) plugins are installed
1314
* Check if the server is running since a long time (pad.example.com/stats)
1415

1516
## Try it out
1617

17-
You can try this tool out on the https://scanner.etherpad.org which is using this library.
18+
You can try this tool out on https://scanner.etherpad.org/instances which is using this library.
1819

1920
## Requirements
2021

bin/console.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
require __DIR__ . '/../vendor/autoload.php';
1313

1414
$application = new Application();
15-
$application->add(new ScanCommand());
16-
$application->add(new GenerateRevisionLookupCommand());
17-
$application->add(new GenerateFileHashesCommand());
18-
$application->add(new CheckFileHashesCommand());
19-
$application->add(new GenerateFileHashesAllVersionsCommand());
15+
$application->addCommands([
16+
new ScanCommand(),
17+
new GenerateRevisionLookupCommand(),
18+
new GenerateFileHashesCommand(),
19+
new CheckFileHashesCommand(),
20+
new GenerateFileHashesAllVersionsCommand(),
21+
]);
2022
$application->run();

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"php": ">=8.3",
1717
"ext-json": "*",
1818
"guzzlehttp/guzzle": "^7.8",
19-
"symfony/console": "^6.4|^7.0",
19+
"symfony/console": "^6.4|^7.0|^8.0",
2020
"elephantio/elephant.io": "5.1.*"
2121
},
2222
"require-dev": {

composer.lock

Lines changed: 8 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ parameters:
22
level: 8
33
paths:
44
- src
5+
- bin

src/Console/CheckFileHashesCommand.php

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
use Gared\EtherScan\Model\VersionRange;
77
use Gared\EtherScan\Service\FileHashLookupService;
8+
use Gared\EtherScan\Service\StaticFileClient;
89
use GuzzleHttp\Client;
9-
use GuzzleHttp\Exception\GuzzleException;
1010
use Symfony\Component\Console\Attribute\AsCommand;
1111
use Symfony\Component\Console\Command\Command;
1212
use Symfony\Component\Console\Input\InputArgument;
@@ -19,7 +19,7 @@
1919
)]
2020
class CheckFileHashesCommand extends Command
2121
{
22-
protected function configure()
22+
protected function configure(): void
2323
{
2424
$this
2525
->addArgument('url', InputArgument::REQUIRED, 'Url to etherpad instance')
@@ -32,12 +32,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
3232
$version = $input->getArgument('version');
3333

3434
$fileHashLookup = new FileHashLookupService();
35+
$staticFileClient = new StaticFileClient(new Client());
3536

3637
$files = FileHashLookupService::getFileNames();
3738

3839
$versionRanges = [];
3940
foreach ($files as $file) {
40-
$fileHash = $this->getFileHash($url, $file);
41+
$fileHash = $staticFileClient->getFileHash($url, $file);
4142
$versionRange = $fileHashLookup->getEtherpadVersionRange($file, $fileHash);
4243
if ($versionRange !== null) {
4344
$versionRanges[] = $versionRange;
@@ -84,24 +85,4 @@ private function calculateVersion(array $versionRanges): VersionRange
8485

8586
return new VersionRange($minVersion, $maxVersion);
8687
}
87-
88-
private function getFileHash(string $url, string $path): ?string
89-
{
90-
try {
91-
$client = new Client([
92-
'base_uri' => $url,
93-
'timeout' => 5.0,
94-
'verify' => false,
95-
]);
96-
$response = $client->get($path, [
97-
'headers' => ['Accept-Encoding' => 'gzip'],
98-
]);
99-
100-
$body = (string)$response->getBody();
101-
return hash('md5', $body);
102-
} catch (GuzzleException) {
103-
}
104-
105-
return null;
106-
}
10788
}

src/Console/GenerateFileHashesCommand.php

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
namespace Gared\EtherScan\Console;
55

66
use Gared\EtherScan\Service\FileHashLookupService;
7+
use Gared\EtherScan\Service\StaticFileClient;
78
use GuzzleHttp\Client;
8-
use GuzzleHttp\Exception\GuzzleException;
99
use Symfony\Component\Console\Attribute\AsCommand;
1010
use Symfony\Component\Console\Command\Command;
1111
use Symfony\Component\Console\Input\InputArgument;
@@ -19,7 +19,7 @@
1919
)]
2020
class GenerateFileHashesCommand extends Command
2121
{
22-
protected function configure()
22+
protected function configure(): void
2323
{
2424
$this->addArgument('url', InputArgument::REQUIRED, 'Url to etherpad instance');
2525
}
@@ -29,13 +29,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
2929
$url = $input->getArgument('url');
3030

3131
$fileHashLookup = new FileHashLookupService();
32+
$staticFileClient = new StaticFileClient(new Client());
3233

3334
$files = FileHashLookupService::getFileNames();
3435

3536
$tableRows = [];
3637

3738
foreach ($files as $file) {
38-
$fileHash = $this->getFileHash($url, $file);
39+
$fileHash = $staticFileClient->getFileHash($url, $file);
3940
$versionRange = $fileHashLookup->getEtherpadVersionRange($file, $fileHash);
4041
$tableRows[] = [$file, $fileHash, $versionRange];
4142
}
@@ -45,25 +46,4 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4546

4647
return self::SUCCESS;
4748
}
48-
49-
private function getFileHash(string $url, string $path): ?string
50-
{
51-
try {
52-
$client = new Client([
53-
'base_uri' => $url,
54-
'timeout' => 5.0,
55-
'verify' => false,
56-
]);
57-
$response = $client->get($path, [
58-
'headers' => ['Accept-Encoding' => 'gzip'],
59-
]);
60-
61-
$body = (string)$response->getBody();
62-
return hash('md5', $body);
63-
} catch (GuzzleException $e) {
64-
var_dump($e->getMessage());
65-
}
66-
67-
return null;
68-
}
6949
}

src/Console/ScanCommand.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
2626
{
2727
$outputHelper = new ScanCommandOutputHelper(new SymfonyStyle($input, $output), $output);
2828

29-
$scanner = new ScannerService($input->getArgument('url'));
30-
$scanner->scan($outputHelper);
29+
$scanner = new ScannerService();
30+
$scanner->scan(
31+
url: $input->getArgument('url'),
32+
callback: $outputHelper,
33+
);
3134

3235
return self::SUCCESS;
3336
}
34-
}
37+
}

src/Console/ScanCommandOutputHelper.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
use DateTimeImmutable;
77
use DateTimeInterface;
8-
use Exception;
98
use Gared\EtherScan\Service\Scanner\Health\HealthResponseException;
109
use Gared\EtherScan\Service\ScannerServiceCallbackInterface;
1110
use GuzzleHttp\Exception\GuzzleException;

src/Model/Config.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Gared\EtherScan\Model;
4+
5+
class Config
6+
{
7+
public readonly string $padId;
8+
public ?string $pathPrefix = null;
9+
10+
public function __construct(
11+
public string $baseUrl,
12+
public readonly float $timeout = 5.0,
13+
) {
14+
$domain = parse_url($this->baseUrl, PHP_URL_HOST);
15+
if (is_string($domain) === false) {
16+
$domain = $this->baseUrl;
17+
}
18+
$hash = $this->generateShortHash($domain);
19+
$this->padId = 'test' . $hash;
20+
}
21+
22+
private function generateShortHash(string $domain): string
23+
{
24+
$base64 = base64_encode(hash('sha256', $domain, true));
25+
$hashStripped = str_replace(['+', '/'], '', $base64);
26+
return substr($hashStripped, 0, 5);
27+
}
28+
}

0 commit comments

Comments
 (0)