Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ What is this tool doing?
* Check the API version (pad.example.com/api)
* Check the etherpad version
* Check if the pads are publicly accessible
* Check if websocket is working
* Check if the server is healthy (pad.example.com/health)
* Check if the admin area is accessible with default credentials (pad.example.com/admin)
* Check if any (frontend) plugins are installed
* Check if the server is running since a long time (pad.example.com/stats)

## Try it out

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

## Requirements

Expand Down
12 changes: 7 additions & 5 deletions bin/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
require __DIR__ . '/../vendor/autoload.php';

$application = new Application();
$application->add(new ScanCommand());
$application->add(new GenerateRevisionLookupCommand());
$application->add(new GenerateFileHashesCommand());
$application->add(new CheckFileHashesCommand());
$application->add(new GenerateFileHashesAllVersionsCommand());
$application->addCommands([
new ScanCommand(),
new GenerateRevisionLookupCommand(),
new GenerateFileHashesCommand(),
new CheckFileHashesCommand(),
new GenerateFileHashesAllVersionsCommand(),
]);
$application->run();
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"php": ">=8.3",
"ext-json": "*",
"guzzlehttp/guzzle": "^7.8",
"symfony/console": "^6.4|^7.0",
"symfony/console": "^6.4|^7.0|^8.0",
"elephantio/elephant.io": "5.1.*"
},
"require-dev": {
Expand Down
15 changes: 8 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ parameters:
level: 8
paths:
- src
- bin
27 changes: 4 additions & 23 deletions src/Console/CheckFileHashesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

use Gared\EtherScan\Model\VersionRange;
use Gared\EtherScan\Service\FileHashLookupService;
use Gared\EtherScan\Service\StaticFileClient;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -19,7 +19,7 @@
)]
class CheckFileHashesCommand extends Command
{
protected function configure()
protected function configure(): void
{
$this
->addArgument('url', InputArgument::REQUIRED, 'Url to etherpad instance')
Expand All @@ -32,12 +32,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$version = $input->getArgument('version');

$fileHashLookup = new FileHashLookupService();
$staticFileClient = new StaticFileClient(new Client());

$files = FileHashLookupService::getFileNames();

$versionRanges = [];
foreach ($files as $file) {
$fileHash = $this->getFileHash($url, $file);
$fileHash = $staticFileClient->getFileHash($url, $file);
$versionRange = $fileHashLookup->getEtherpadVersionRange($file, $fileHash);
if ($versionRange !== null) {
$versionRanges[] = $versionRange;
Expand Down Expand Up @@ -84,24 +85,4 @@ private function calculateVersion(array $versionRanges): VersionRange

return new VersionRange($minVersion, $maxVersion);
}

private function getFileHash(string $url, string $path): ?string
{
try {
$client = new Client([
'base_uri' => $url,
'timeout' => 5.0,
'verify' => false,
]);
$response = $client->get($path, [
'headers' => ['Accept-Encoding' => 'gzip'],
]);

$body = (string)$response->getBody();
return hash('md5', $body);
} catch (GuzzleException) {
}

return null;
}
}
28 changes: 4 additions & 24 deletions src/Console/GenerateFileHashesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
namespace Gared\EtherScan\Console;

use Gared\EtherScan\Service\FileHashLookupService;
use Gared\EtherScan\Service\StaticFileClient;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -19,7 +19,7 @@
)]
class GenerateFileHashesCommand extends Command
{
protected function configure()
protected function configure(): void
{
$this->addArgument('url', InputArgument::REQUIRED, 'Url to etherpad instance');
}
Expand All @@ -29,13 +29,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$url = $input->getArgument('url');

$fileHashLookup = new FileHashLookupService();
$staticFileClient = new StaticFileClient(new Client());

$files = FileHashLookupService::getFileNames();

$tableRows = [];

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

return self::SUCCESS;
}

private function getFileHash(string $url, string $path): ?string
{
try {
$client = new Client([
'base_uri' => $url,
'timeout' => 5.0,
'verify' => false,
]);
$response = $client->get($path, [
'headers' => ['Accept-Encoding' => 'gzip'],
]);

$body = (string)$response->getBody();
return hash('md5', $body);
} catch (GuzzleException $e) {
var_dump($e->getMessage());
}

return null;
}
}
9 changes: 6 additions & 3 deletions src/Console/ScanCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$outputHelper = new ScanCommandOutputHelper(new SymfonyStyle($input, $output), $output);

$scanner = new ScannerService($input->getArgument('url'));
$scanner->scan($outputHelper);
$scanner = new ScannerService();
$scanner->scan(
url: $input->getArgument('url'),
callback: $outputHelper,
);

return self::SUCCESS;
}
}
}
1 change: 0 additions & 1 deletion src/Console/ScanCommandOutputHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

use DateTimeImmutable;
use DateTimeInterface;
use Exception;
use Gared\EtherScan\Service\Scanner\Health\HealthResponseException;
use Gared\EtherScan\Service\ScannerServiceCallbackInterface;
use GuzzleHttp\Exception\GuzzleException;
Expand Down
28 changes: 28 additions & 0 deletions src/Model/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Gared\EtherScan\Model;

class Config
{
public readonly string $padId;
public ?string $pathPrefix = null;

public function __construct(
public string $baseUrl,
public readonly float $timeout = 5.0,
) {
$domain = parse_url($this->baseUrl, PHP_URL_HOST);
if (is_string($domain) === false) {
$domain = $this->baseUrl;
}
$hash = $this->generateShortHash($domain);
$this->padId = 'test' . $hash;
}

private function generateShortHash(string $domain): string
{
$base64 = base64_encode(hash('sha256', $domain, true));
$hashStripped = str_replace(['+', '/'], '', $base64);
return substr($hashStripped, 0, 5);
}
}
55 changes: 55 additions & 0 deletions src/Service/Scanner/AdminScanner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);

namespace Gared\EtherScan\Service\Scanner;

use Gared\EtherScan\Model\Config;
use Gared\EtherScan\Service\ScannerServiceCallbackInterface;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;

readonly class AdminScanner
{
public function __construct(
private Client $client,
) {
}

public function scan(Config $config, ScannerServiceCallbackInterface $callback): void
{
$callback->onScanAdminStart();
$this->getAdmin('admin', 'admin', $config, $callback);
$this->getAdmin('admin', 'changeme1', $config, $callback);
$this->getAdmin('user', 'changeme1', $config, $callback);
}

private function getAdmin(string $user, string $password, Config $config, ScannerServiceCallbackInterface $callback): void
{
try {
$response = $this->client->post($config->baseUrl . 'admin-auth/', [
'auth' => [$user, $password],
'timeout' => $config->timeout,
]);
$body = (string) $response->getBody();
$callback->onScanAdminResult($user, $password, $response->getStatusCode() === 200 && $body === 'Authorized');
return;
} catch (GuzzleException $e) {
if ($e->getCode() === 401 || $e->getCode() === 403) {
$callback->onScanAdminResult($user, $password, false);
return;
}
}

try {
$response = $this->client->get($config->baseUrl . 'admin/', [
'auth' => [$user, $password],
'timeout' => $config->timeout,
]);
$body = (string) $response->getBody();
$callback->onScanAdminResult($user, $password, $response->getStatusCode() === 200 && str_contains($body, 'Plugin manager'));
return;
} catch (GuzzleException) {
$callback->onScanAdminResult($user, $password, false);
}
}
}
8 changes: 5 additions & 3 deletions src/Service/Scanner/ApiEndpointScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Gared\EtherScan\Api\GithubApi;
use Gared\EtherScan\Exception\EtherpadServiceNotFoundException;
use Gared\EtherScan\Model\Config;
use Gared\EtherScan\Service\ApiVersionLookupService;
use Gared\EtherScan\Service\RevisionLookupService;
use Gared\EtherScan\Service\ScannerServiceCallbackInterface;
Expand All @@ -18,18 +19,19 @@
readonly class ApiEndpointScanner
{
public function __construct(
private Client $client,
private VersionRangeService $versionRangeService,
private RevisionLookupService $revisionLookupService,
private ApiVersionLookupService $apiVersionLookupService,
private GithubApi $githubApi,
) {
}

public function scan(Client $client, string $baseUrl, ScannerServiceCallbackInterface $callback): void
public function scan(Config $config, ScannerServiceCallbackInterface $callback): void
{
$callback->onScanApiStart($baseUrl);
$callback->onScanApiStart($config->baseUrl);
try {
$response = $client->get($baseUrl . 'api');
$response = $this->client->get($config->baseUrl . 'api', ['timeout' => $config->timeout]);
$callback->onScanApiResponse($response);

$revision = $this->getRevisionFromHeaders($response);
Expand Down
Loading
Loading