Skip to content

Commit

Permalink
πŸ”¨ WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Owen Melbourne committed Aug 19, 2019
1 parent 6a17ccc commit 6d90db7
Show file tree
Hide file tree
Showing 19 changed files with 623 additions and 40 deletions.
37 changes: 35 additions & 2 deletions app/Checkers/Certificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@
use App\Website;
use App\CertificateScan;
use VisualAppeal\SslLabs;
use App\Notifications\CertificateIsWeak;
use Spatie\SslCertificate\SslCertificate;
use App\Notifications\CertificateIsInvalid;
use App\Notifications\CertificateWillExpire;
use App\Notifications\CertificateHasExpired;
use App\Notifications\CertificateIsExpiring;

class Certificate
{
private $website;

private $scan;

public function __construct(Website $website)
{
$this->website = $website;
Expand Down Expand Up @@ -52,17 +59,43 @@ private function fetch()
if ($endpoint->statusMessage === 'Ready') {
$scan->grade = $endpoint->grade;
$this->website->certificates()->save($scan);
$this->scan = $scan;
break;
}
}

if (app()->runningInConsole()) {
dump($scan->exists ? 'Cert Updated' : 'Pending...');
if (!$scan->exists) {
dump('Scan still in progress...');
dump($result);
}
}
}

private function notify()
private function notify($notification = null)
{
$this->scan = $this->website->certificates()->latest()->first();

if (!$this->scan) {
return null;
}

if (!$this->scan->was_valid) {
$notification = new CertificateIsInvalid($this->website, $this->scan);
} elseif ($this->scan->did_expire) {
$notification = new CertificateHasExpired($this->website, $this->scan);
} elseif (now()->diffInHours($this->scan->valid_to) <= 24) {
$notification = new CertificateIsExpiring($this->website, $this->scan);
} elseif (now()->diffInDays($this->scan->valid_to) <= 7) {
$notification = new CertificateWillExpire($this->website, $this->scan);
} elseif (in_array($this->scan->grade, ['C', 'D', 'E', 'F'])) {
$notification = new CertificateIsWeak($this->website, $this->scan);
}

if (!$notification) {
return null;
}

$this->website->user->notify($notification);
}
}
36 changes: 18 additions & 18 deletions app/Checkers/Dns.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,24 @@ public function run()

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);
$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()
Expand Down
81 changes: 81 additions & 0 deletions app/Notifications/CertificateHasExpired.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace App\Notifications;

use App\Website;
use App\CertificateScan;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class CertificateHasExpired extends Notification
{
use Queueable;

/**
* @var Website
*/
private $website;

/**
* @var CertificateScan
*/
private $scan;

/**
* Create a new notification instance.
*
* @param Website $website
* @param CertificateScan $scan
*/
public function __construct(Website $website, CertificateScan $scan)
{
$this->website = $website;
$this->scan = $scan;
}

/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['database', 'mail'];
}

/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->subject('πŸ”’ SSL has expired on: ' . $this->website->url)
->markdown('mail.ssl-expired', [
'website' => $this->website,
'scan' => $this->scan,
]);
}

/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
'website_id' => $this->website->id,
'website' => $this->website->certificate_hostname,
'was_valid' => $this->scan->was_valid,
'did_expire' => $this->scan->did_expire,
'grade' => $this->scan->grade,
];
}
}
81 changes: 81 additions & 0 deletions app/Notifications/CertificateIsExpiring.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace App\Notifications;

use App\Website;
use App\CertificateScan;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class CertificateIsExpiring extends Notification
{
use Queueable;

/**
* @var Website
*/
private $website;

/**
* @var CertificateScan
*/
private $scan;

/**
* Create a new notification instance.
*
* @param Website $website
* @param CertificateScan $scan
*/
public function __construct(Website $website, CertificateScan $scan)
{
$this->website = $website;
$this->scan = $scan;
}

/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['database', 'mail'];
}

/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->subject('πŸ”’ SSL expires today on: ' . $this->website->url)
->markdown('mail.ssl-expiring', [
'website' => $this->website,
'scan' => $this->scan,
]);
}

/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
'website_id' => $this->website->id,
'website' => $this->website->certificate_hostname,
'was_valid' => $this->scan->was_valid,
'did_expire' => $this->scan->did_expire,
'grade' => $this->scan->grade,
];
}
}
81 changes: 81 additions & 0 deletions app/Notifications/CertificateIsInvalid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace App\Notifications;

use App\Website;
use App\CertificateScan;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class CertificateIsInvalid extends Notification
{
use Queueable;

/**
* @var Website
*/
private $website;

/**
* @var CertificateScan
*/
private $scan;

/**
* Create a new notification instance.
*
* @param Website $website
* @param CertificateScan $scan
*/
public function __construct(Website $website, CertificateScan $scan)
{
$this->website = $website;
$this->scan = $scan;
}

/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['database', 'mail'];
}

/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->subject('πŸ”’ SSL is invalid on: ' . $this->website->url)
->markdown('mail.ssl-invalid', [
'website' => $this->website,
'scan' => $this->scan,
]);
}

/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
'website_id' => $this->website->id,
'website' => $this->website->certificate_hostname,
'was_valid' => $this->scan->was_valid,
'did_expire' => $this->scan->did_expire,
'grade' => $this->scan->grade,
];
}
}
Loading

0 comments on commit 6d90db7

Please sign in to comment.