Skip to content

Commit 63058d8

Browse files
committed
Super early base implementation of notifications from daemon
1 parent c989dd0 commit 63058d8

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

app/Http/Controllers/Remote/RemoteController.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
use Pterodactyl\Models;
2727
use Pterodactyl\Exceptions\DisplayException;
28+
use Pterodactyl\Services\NotificationService;
2829

2930
use Pterodactyl\Http\Controllers\Controller;
3031
use Illuminate\Http\Request;
@@ -82,4 +83,29 @@ public function postInstall(Request $request)
8283
], 200);
8384
}
8485

86+
public function event(Request $request)
87+
{
88+
$server = Models\Server::where('uuid', $request->input('server'))->first();
89+
if (!$server) {
90+
return response()->json([
91+
'error' => 'No server by that ID was found on the system.'
92+
], 422);
93+
}
94+
95+
$node = Models\Node::findOrFail($server->node);
96+
97+
$hmac = $request->input('signed');
98+
if (base64_decode($hmac) !== hash_hmac('sha256', $server->uuid, $node->daemonSecret, true)) {
99+
return response()->json([
100+
'error' => 'Signed HMAC was invalid.'
101+
], 403);
102+
}
103+
104+
// Passes Validation, Setup Notifications
105+
$notify = new NotificationService($server);
106+
$notify->pass($request->input('notification'));
107+
108+
return response('', 201);
109+
}
110+
85111
}

app/Http/Routes/RemoteRoutes.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ public function map(Router $router) {
4040
'as' => 'remote.install',
4141
'uses' => 'Remote\RemoteController@postInstall'
4242
]);
43+
44+
$router->post('event', [
45+
'as' => 'remote.event',
46+
'uses' => 'Remote\RemoteController@event'
47+
]);
4348
});
4449
}
4550

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com>
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
namespace Pterodactyl\Services;
25+
26+
use Pterodactyl\Models\Server;
27+
use Pterodactyl\Models\User;
28+
29+
use Pterodactyl\Notifications\Daemon;
30+
31+
class NotificationService {
32+
33+
protected $server;
34+
35+
protected $user;
36+
37+
/**
38+
* Daemon will pass an event name, this matches that event name with the notification to send.
39+
* @var array
40+
*/
41+
protected $types = [
42+
// 'crashed' => 'CrashNotification',
43+
// 'started' => 'StartNotification',
44+
// 'stopped' => 'StopNotification',
45+
// 'rebuild' => 'RebuildNotification'
46+
];
47+
48+
public function __construct(Server $server)
49+
{
50+
$this->server = $server;
51+
$this->user = User::findOrFail($server->owner);
52+
}
53+
54+
public function pass(array $notification)
55+
{
56+
if (!$notification->type) {
57+
return;
58+
}
59+
60+
if (class_exists($this->types[$notification->event]::class)) {
61+
$user->notify(new $this->types[$notification->type]($notification->event));
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)