51 lines
1.3 KiB
PHP
51 lines
1.3 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Osint;
|
||
|
|
|
||
|
|
use App\Jobs\RunFinding;
|
||
|
|
use App\Models\Source;
|
||
|
|
use App\Models\Target;
|
||
|
|
use App\Models\User;
|
||
|
|
|
||
|
|
class FindingDispatcher
|
||
|
|
{
|
||
|
|
public function __construct(private readonly RunnerRegistry $registry)
|
||
|
|
{
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Queue a lookup for every enabled, registered source that applies to
|
||
|
|
* the target's type and doesn't already have a pending/running finding
|
||
|
|
* for it. Returns how many were queued.
|
||
|
|
*/
|
||
|
|
public function dispatchAllApplicable(Target $target, User $runBy): int
|
||
|
|
{
|
||
|
|
$applicableSources = Source::where('enabled', true)->get()
|
||
|
|
->filter(fn (Source $source) => in_array($target->type, $source->target_types, true)
|
||
|
|
&& $this->registry->has($source->key));
|
||
|
|
|
||
|
|
$busySourceIds = $target->findings()
|
||
|
|
->whereIn('status', ['pending', 'running'])
|
||
|
|
->pluck('source_id');
|
||
|
|
|
||
|
|
$queued = 0;
|
||
|
|
|
||
|
|
foreach ($applicableSources as $source) {
|
||
|
|
if ($busySourceIds->contains($source->id)) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
|
||
|
|
$finding = $target->findings()->create([
|
||
|
|
'source_id' => $source->id,
|
||
|
|
'run_by' => $runBy->id,
|
||
|
|
'status' => 'pending',
|
||
|
|
]);
|
||
|
|
|
||
|
|
RunFinding::dispatch($finding->id);
|
||
|
|
$queued++;
|
||
|
|
}
|
||
|
|
|
||
|
|
return $queued;
|
||
|
|
}
|
||
|
|
}
|