|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pterodactyl\Http\Controllers\Api\Remote; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use Carbon\Carbon; |
| 7 | +use Pterodactyl\Models\User; |
| 8 | +use Webmozart\Assert\Assert; |
| 9 | +use Pterodactyl\Models\Server; |
| 10 | +use Pterodactyl\Models\ActivityLog; |
| 11 | +use Illuminate\Support\Facades\Log; |
| 12 | +use Pterodactyl\Models\ActivityLogSubject; |
| 13 | +use Pterodactyl\Http\Controllers\Controller; |
| 14 | +use Pterodactyl\Http\Requests\Api\Remote\ActivityEventRequest; |
| 15 | + |
| 16 | +class ActivityProcessingController extends Controller |
| 17 | +{ |
| 18 | + public function __invoke(ActivityEventRequest $request) |
| 19 | + { |
| 20 | + $tz = Carbon::now()->getTimezone(); |
| 21 | + |
| 22 | + /** @var \Pterodactyl\Models\Node $node */ |
| 23 | + $node = $request->attributes->get('node'); |
| 24 | + |
| 25 | + $servers = $node->servers()->whereIn('uuid', $request->servers())->get()->keyBy('uuid'); |
| 26 | + $users = User::query()->whereIn('uuid', $request->users())->get()->keyBy('uuid'); |
| 27 | + |
| 28 | + clock()->log($request->input('data')); |
| 29 | + |
| 30 | + $logs = []; |
| 31 | + foreach ($request->input('data') as $datum) { |
| 32 | + /** @var \Pterodactyl\Models\Server|null $server */ |
| 33 | + $server = $servers->get($datum['server']); |
| 34 | + if (is_null($server) || is_null($event = $this->event($datum['event']))) { |
| 35 | + continue; |
| 36 | + } |
| 37 | + |
| 38 | + try { |
| 39 | + $when = Carbon::createFromFormat( |
| 40 | + Carbon::RFC3339, |
| 41 | + preg_replace('/(\.\d+)Z$/', 'Z', $datum['timestamp']), |
| 42 | + 'UTC' |
| 43 | + ); |
| 44 | + } catch (Exception $exception) { |
| 45 | + Log::warning($exception, ['timestamp' => $datum['timestamp']]); |
| 46 | + |
| 47 | + // If we cannot parse the value for some reason don't blow up this request, just go ahead |
| 48 | + // and log the event with the current time, and set the metadata value to have the original |
| 49 | + // timestamp that was provided. |
| 50 | + $when = Carbon::now(); |
| 51 | + $datum['metadata'] = array_merge($datum['metadata'] ?? [], ['original_timestamp' => $datum['timestamp']]); |
| 52 | + } |
| 53 | + |
| 54 | + $log = [ |
| 55 | + 'ip' => empty($datum['ip']) ? '127.0.0.1' : $datum['ip'], |
| 56 | + 'event' => $event, |
| 57 | + 'properties' => json_encode($datum['metadata'] ?? []), |
| 58 | + // We have to change the time to the current timezone due to the way Laravel is handling |
| 59 | + // the date casting internally. If we just leave it in UTC it ends up getting double-cast |
| 60 | + // and the time is way off. |
| 61 | + 'timestamp' => $when->setTimezone($tz), |
| 62 | + ]; |
| 63 | + |
| 64 | + if ($user = $users->get($datum['user'])) { |
| 65 | + $log['actor_id'] = $user->id; |
| 66 | + $log['actor_type'] = $user->getMorphClass(); |
| 67 | + } |
| 68 | + |
| 69 | + if (!isset($logs[$datum['server']])) { |
| 70 | + $logs[$datum['server']] = []; |
| 71 | + } |
| 72 | + |
| 73 | + $logs[$datum['server']][] = $log; |
| 74 | + } |
| 75 | + |
| 76 | + foreach ($logs as $key => $data) { |
| 77 | + Assert::isInstanceOf($server = $servers->get($key), Server::class); |
| 78 | + |
| 79 | + $batch = []; |
| 80 | + foreach ($data as $datum) { |
| 81 | + $id = ActivityLog::insertGetId($datum); |
| 82 | + $batch[] = [ |
| 83 | + 'activity_log_id' => $id, |
| 84 | + 'subject_id' => $server->id, |
| 85 | + 'subject_type' => $server->getMorphClass(), |
| 86 | + ]; |
| 87 | + } |
| 88 | + |
| 89 | + ActivityLogSubject::insert($batch); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Takes an event from Wings and converts it into the expected event type on |
| 95 | + * the Panel. If no matching event type can be deduced, null is returned and |
| 96 | + * the event won't be logged. |
| 97 | + */ |
| 98 | + protected function event(string $input): ?string |
| 99 | + { |
| 100 | + switch ($input) { |
| 101 | + case 'console_command': |
| 102 | + return 'server:console.command'; |
| 103 | + case 'power_start': |
| 104 | + return 'server:power.start'; |
| 105 | + case 'power_stop': |
| 106 | + return 'server:power.stop'; |
| 107 | + case 'power_restart': |
| 108 | + return 'server:power.restart'; |
| 109 | + case 'power_kill': |
| 110 | + return 'server:power.kill'; |
| 111 | + default: |
| 112 | + return null; |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments