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 fcbbd01 commit 6a17ccc
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 20 deletions.
52 changes: 33 additions & 19 deletions app/Checkers/Dns.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
use App\Website;
use Whoisdoma\DNSParser\DNSParser;
use SebastianBergmann\Diff\Differ;
use Spatie\Dns\Dns as DnsLookup;
use App\Notifications\DnsHasChanged;

class Dns
{
private $website;

private $scan;

public function __construct(Website $website)
{
$this->website = $website;
Expand All @@ -26,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 All @@ -69,10 +71,22 @@ private function compare()

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

$this->scan = $scans->first();
}

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

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

$this->website->user->notify(
new DnsHasChanged($this->website, $this->scan)
);
}
}
80 changes: 80 additions & 0 deletions app/Notifications/DnsHasChanged.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace App\Notifications;

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

class DnsHasChanged extends Notification
{
use Queueable;
/**
* @var Website
*/
private $website;

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

/**
* Create a new notification instance.
*
* @param Website $website
* @param DnsScan $scan
*/
public function __construct(Website $website, DnsScan $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('πŸ“š Change detected on: ' . $this->website->url)
->markdown('mail.dns-changed', [
'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,
'url' => $this->website->dns_hostname,
'changed_on' => $this->scan->created_at,
'flat' => $this->scan->flat,
'diff' => $this->scan->diff,
];
}
}
18 changes: 18 additions & 0 deletions resources/views/mail/dns-changed.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@component('mail::message')
# πŸ“š DNS change detected on: {{ $website->url }}

The public DNS file has changed since the last scan - please check and make any necessary changes.

Please see the diff below:

<div style="padding: 20px; background: #f1f2fb;">
<pre style="margin:0;padding:0;"><code style="margin:0;padding:0;">{{ $scan->diff }}</code></pre>
</div>

@component('mail::button', ['url' => $website->show_link])
Open Monitor
@endcomponent

Thanks,<br>
{{ config('app.name') }}
@endcomponent
2 changes: 1 addition & 1 deletion resources/views/mail/robots-changed.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@component('mail::message')
# πŸ€– Change detected on: {{ $website->url }}
# πŸ€– Robots change detected on: {{ $website->url }}

The robots.txt file has changed since the last scan - please check and make any necessary changes.

Expand Down

0 comments on commit 6a17ccc

Please sign in to comment.