Skip to content

Commit

Permalink
πŸ”¨ WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
OwenMelbz committed Aug 19, 2019
1 parent d39c716 commit b4a6661
Show file tree
Hide file tree
Showing 32 changed files with 2,054 additions and 89 deletions.
27 changes: 27 additions & 0 deletions app/CertificateScan.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class CertificateScan extends Model
{
protected $guarded = [];

protected $casts = [
'additional_domains' => 'array',
'valid_from' => 'datetime',
'valid_to' => 'datetime',
'was_valid' => 'boolean',
'did_expire' => 'boolean',
];

protected $appends = [
'expires_in',
];

public function getExpiresInAttribute()
{
return now()->diffAsCarbonInterval($this->valid_to)->forHumans(['join' => true]);
}
}
66 changes: 66 additions & 0 deletions app/Checkers/Certificate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace App\Checkers;

use App\Website;
use Andyftw\SSLLabs\Api;
use App\CertificateScan;
use Spatie\SslCertificate\SslCertificate;

class Certificate
{
private $website;

public function __construct(Website $website)
{
$this->website = $website;
}

public function run()
{
$this->fetch();
$this->notify();
}

private function fetch()
{
$certificate = SslCertificate::createForHostName($this->website->certificate_hostname);

$scan = new CertificateScan([
'issuer' => $certificate->getIssuer(),
'domain' => $certificate->getDomain(),
'additional_domains' => $certificate->getAdditionalDomains(),
'valid_from' => $certificate->validFromDate(),
'valid_to' => $certificate->expirationDate(),
'was_valid' => $certificate->isValid(),
'did_expire' => $certificate->isExpired(),
]);

$labs = new \VisualAppeal\SslLabs();

$result = $labs->analyze(
$this->website->certificate_hostname,
$publish = false,
$startNew = false,
$fromCache = true,
$maxAge = 12,
$all = null,
$ignoreMismatch = false
);

foreach ($result->endpoints ?? [] as $endpoint) {
if ($endpoint->statusMessage === 'Ready') {
$scan->grade = $endpoint->grade;
$this->website->certificates()->save($scan);
break;
}
}

dump($scan->exists ? 'Cert Updated' : 'Pending...');
}

private function notify()
{

}
}
78 changes: 78 additions & 0 deletions app/Checkers/Dns.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace App\Checkers;

use App\DnsScan;
use App\Website;
use Whoisdoma\DNSParser\DNSParser;
use SebastianBergmann\Diff\Differ;
use Spatie\Dns\Dns as DnsLookup;

class Dns
{
private $website;

public function __construct(Website $website)
{
$this->website = $website;
}

public function run()
{
$this->fetch();
$this->compare();
$this->notify();
}

private function fetch()
{
$response = (new DNSParser('array'))->lookup($this->website->dns_hostname);

$flat = collect($response['records'])->transform(function ($item) {
return sprintf(
'%s %s %s ttl:%d',
$item['host'],
$item['type'],
$item['ip'] ?? $item['target'] ?? $item['mname'] ?? $item['txt'] ?? $item['ipv6'] ?? '',
$item['ttl']
);
})->sort()->values()->implode("\n");

$scan = new DnsScan([
'records' => $response['records'],
'flat' => $flat,
]);

$this->website->dns()->save($scan);
}

private function compare()
{
$scans = $this->website->last_dns_scans;

if ($scans->isEmpty() || $scans->count() === 1) {
return;
}

$diff = (new Differ)->diff(
$scans->last()->flat,
$scans->first()->flat
);

$placeholder = '--- Original
+++ New
';

if ($diff === $placeholder) {
$diff = null;
}

$scans->first()->diff = $diff;
$scans->first()->save();
}

private function notify()
{

}
}
8 changes: 8 additions & 0 deletions app/Checkers/Robots.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ private function compare()

$diff = (new Differ)->diff($scans->last()->txt, $scans->first()->txt);

$placeholder = '--- Original
+++ New
';

if ($diff === $placeholder) {
$diff = null;
}

$scans->first()->diff = $diff;
$scans->first()->save();
}
Expand Down
29 changes: 13 additions & 16 deletions app/Checkers/Uptime.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

namespace App\Checkers;

use App\RobotScan;
use App\Website;
use App\UptimeScan;
use GuzzleHttp\Client;
use App\Website;
use GuzzleHttp\TransferStats;
use Illuminate\Support\Str;
use SebastianBergmann\Diff\Differ;

Expand All @@ -23,7 +21,7 @@ public function __construct(Website $website)
public function run()
{
$this->fetch();
// $this->compare();
$this->notify();
}

private function fetch()
Expand All @@ -43,26 +41,25 @@ private function fetch()
],
]);

$keywordFound = Str::contains($response->getBody(), $this->website->uptime_keyword);

if (!$keywordFound && $response->getStatusCode() == '200') {
$reason = sprintf('Keyword: %s not found (%d)', $this->website->uptime_keyword, 200);
} else {
$reason = sprintf('%s (%d)', $response->getReasonPhrase(), $response->getStatusCode());
}

$scan = new UptimeScan([
'response_status' => sprintf('%s (%d)', $response->getReasonPhrase(), $response->getStatusCode()),
'response_status' => $reason,
'response_time' => $response_time,
'was_online' => Str::contains($response->getBody(), $this->website->uptime_keyword)
'was_online' => $keywordFound,
]);

$this->website->uptimes()->save($scan);
}

private function compare()
private function notify()
{
$scans = $this->website->last_robot_scans;

if ($scans->isEmpty() || $scans->count() === 1) {
return;
}

$diff = (new Differ)->diff($scans->last()->txt, $scans->first()->txt);

$scans->first()->diff = $diff;
$scans->first()->save();
}
}
53 changes: 53 additions & 0 deletions app/Console/Commands/AllCheckersCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace App\Console\Commands;

use App\Jobs\CertificateCheck;
use App\Jobs\DnsCheck;
use App\Jobs\RobotsCheck;
use App\Jobs\UptimeCheck;
use App\Website;
use Illuminate\Console\Command;

class AllCheckersCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'check:all {website}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$websiteId = $this->argument('website');
$website = Website::findOrFail($websiteId);

DnsCheck::dispatchNow($website);
RobotsCheck::dispatchNow($website);
UptimeCheck::dispatchNow($website);
CertificateCheck::dispatchNow($website);
}
}
48 changes: 48 additions & 0 deletions app/Console/Commands/CertificateCheckCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace App\Console\Commands;

use App\Website;
use App\Jobs\CertificateCheck;
use Illuminate\Console\Command;

class CertificateCheckCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'check:ssl {website}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$websiteId = $this->argument('website');

CertificateCheck::dispatchNow(
Website::findOrFail($websiteId)
);
}
}
Loading

0 comments on commit b4a6661

Please sign in to comment.