2022-07-09 21:45:38 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Remote;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
2022-07-09 21:53:07 +00:00
|
|
|
use Illuminate\Support\Str;
|
2022-07-09 21:45:38 +00:00
|
|
|
use Pterodactyl\Models\User;
|
|
|
|
use Webmozart\Assert\Assert;
|
|
|
|
use Pterodactyl\Models\Server;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
2022-10-14 16:59:20 +00:00
|
|
|
use Pterodactyl\Models\ActivityLog;
|
2022-07-09 21:45:38 +00:00
|
|
|
use Pterodactyl\Models\ActivityLogSubject;
|
|
|
|
use Pterodactyl\Http\Controllers\Controller;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Remote\ActivityEventRequest;
|
|
|
|
|
|
|
|
class ActivityProcessingController extends Controller
|
|
|
|
{
|
|
|
|
public function __invoke(ActivityEventRequest $request)
|
|
|
|
{
|
|
|
|
$tz = Carbon::now()->getTimezone();
|
|
|
|
|
|
|
|
/** @var \Pterodactyl\Models\Node $node */
|
|
|
|
$node = $request->attributes->get('node');
|
|
|
|
|
|
|
|
$servers = $node->servers()->whereIn('uuid', $request->servers())->get()->keyBy('uuid');
|
|
|
|
$users = User::query()->whereIn('uuid', $request->users())->get()->keyBy('uuid');
|
|
|
|
|
|
|
|
$logs = [];
|
|
|
|
foreach ($request->input('data') as $datum) {
|
|
|
|
/** @var \Pterodactyl\Models\Server|null $server */
|
|
|
|
$server = $servers->get($datum['server']);
|
2022-07-09 21:53:07 +00:00
|
|
|
if (is_null($server) || !Str::startsWith($datum['event'], 'server:')) {
|
2022-07-09 21:45:38 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$when = Carbon::createFromFormat(
|
2022-11-29 17:53:59 +00:00
|
|
|
\DateTimeInterface::RFC3339,
|
2022-07-09 21:45:38 +00:00
|
|
|
preg_replace('/(\.\d+)Z$/', 'Z', $datum['timestamp']),
|
|
|
|
'UTC'
|
|
|
|
);
|
2022-11-29 17:53:59 +00:00
|
|
|
} catch (\Exception $exception) {
|
2022-07-09 21:45:38 +00:00
|
|
|
Log::warning($exception, ['timestamp' => $datum['timestamp']]);
|
|
|
|
|
|
|
|
// If we cannot parse the value for some reason don't blow up this request, just go ahead
|
|
|
|
// and log the event with the current time, and set the metadata value to have the original
|
|
|
|
// timestamp that was provided.
|
|
|
|
$when = Carbon::now();
|
|
|
|
$datum['metadata'] = array_merge($datum['metadata'] ?? [], ['original_timestamp' => $datum['timestamp']]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$log = [
|
|
|
|
'ip' => empty($datum['ip']) ? '127.0.0.1' : $datum['ip'],
|
2022-07-09 21:53:07 +00:00
|
|
|
'event' => $datum['event'],
|
2022-07-09 21:45:38 +00:00
|
|
|
'properties' => json_encode($datum['metadata'] ?? []),
|
|
|
|
// We have to change the time to the current timezone due to the way Laravel is handling
|
|
|
|
// the date casting internally. If we just leave it in UTC it ends up getting double-cast
|
|
|
|
// and the time is way off.
|
|
|
|
'timestamp' => $when->setTimezone($tz),
|
|
|
|
];
|
|
|
|
|
|
|
|
if ($user = $users->get($datum['user'])) {
|
|
|
|
$log['actor_id'] = $user->id;
|
|
|
|
$log['actor_type'] = $user->getMorphClass();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($logs[$datum['server']])) {
|
|
|
|
$logs[$datum['server']] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$logs[$datum['server']][] = $log;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($logs as $key => $data) {
|
|
|
|
Assert::isInstanceOf($server = $servers->get($key), Server::class);
|
|
|
|
|
|
|
|
$batch = [];
|
|
|
|
foreach ($data as $datum) {
|
|
|
|
$id = ActivityLog::insertGetId($datum);
|
|
|
|
$batch[] = [
|
|
|
|
'activity_log_id' => $id,
|
|
|
|
'subject_id' => $server->id,
|
|
|
|
'subject_type' => $server->getMorphClass(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
ActivityLogSubject::insert($batch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|