forked from blesta/module-pterodactyl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpterodactyl.php
More file actions
1542 lines (1373 loc) · 60.8 KB
/
pterodactyl.php
File metadata and controls
1542 lines (1373 loc) · 60.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
use Blesta\PterodactylSDK\PterodactylApi;
use Blesta\Core\Util\Validate\Server;
/**
* Pterodactyl Module
*
* @package blesta
* @subpackage blesta.components.modules.pterodactyl
* @copyright Copyright (c) 2019, Phillips Data, Inc.
* @license http://www.blesta.com/license/ The Blesta License Agreement
* @link http://www.blesta.com/ Blesta
*/
class Pterodactyl extends Module
{
/**
* Initializes the module
*/
public function __construct()
{
// Load components required by this module
Loader::loadComponents($this, ['Input']);
// Load the language required by this module
Language::loadLang('pterodactyl', null, dirname(__FILE__) . DS . 'language' . DS);
Language::loadLang('pterodactyl_package', null, dirname(__FILE__) . DS . 'language' . DS);
Language::loadLang('pterodactyl_service', null, dirname(__FILE__) . DS . 'language' . DS);
Language::loadLang('pterodactyl_rule', null, dirname(__FILE__) . DS . 'language' . DS);
// Load configuration required by this module
$this->loadConfig(dirname(__FILE__) . DS . 'config.json');
// Load additional config values
Configure::load('pterodactyl', dirname(__FILE__) . DS . 'config' . DS);
}
/**
* Performs migration of data from $current_version (the current installed version)
* to the given file set version. Sets Input errors on failure, preventing
* the module from being upgraded.
*
* @param string $current_version The current installed version of this module
*/
public function upgrade($current_version)
{
if (version_compare($current_version, '1.1.0', '<')) {
if (!isset($this->ModuleManager)) {
Loader::loadModels($this, ['ModuleManager']);
}
// Update all module rows to set host_name instead of panel_url
$modules = $this->ModuleManager->getByClass('pterodactyl');
foreach ($modules as $module) {
$rows = $this->ModuleManager->getRows($module->id);
foreach ($rows as $row) {
$meta = (array)$row->meta;
if (isset($meta['panel_url'])) {
$meta['host_name'] = $meta['panel_url'];
unset($meta['panel_url']);
$this->ModuleManager->editRow($row->id, $meta);
}
}
}
}
}
/**
* Loads a library class
*
* @param string $command The filename of the class to load
*/
private function loadLib($command) {
Loader::load(dirname(__FILE__) . DS . 'lib' . DS . $command . '.php');
}
/**
* Returns an array of available service deligation order methods. The module
* will determine how each method is defined. For example, the method "first"
* may be implemented such that it returns the module row with the least number
* of services assigned to it.
*
* @return array An array of order methods in key/value paris where the key is
* the type to be stored for the group and value is the name for that option
* @see Module::selectModuleRow()
*/
public function getGroupOrderOptions()
{
return [
'first' => Language::_('Pterodactyl.order_options.first', true)
];
}
/**
* Determines which module row should be attempted when a service is provisioned
* for the given group based upon the order method set for that group.
*
* @return int The module row ID to attempt to add the service with
* @see Module::getGroupOrderOptions()
*/
public function selectModuleRow($module_group_id)
{
if (!isset($this->ModuleManager)) {
Loader::loadModels($this, ['ModuleManager']);
}
$group = $this->ModuleManager->getGroup($module_group_id);
if ($group) {
switch ($group->add_order) {
default:
case 'first':
foreach ($group->rows as $row) {
return $row->id;
}
break;
}
}
return 0;
}
/**
* Attempts to validate service info. This is the top-level error checking method. Sets Input errors on failure.
*
* @param stdClass $package A stdClass object representing the selected package
* @param array $vars An array of user supplied info to satisfy the request (optional)
* @return bool True if the service validates, false otherwise. Sets Input errors when false.
*/
public function validateService($package, array $vars = null)
{
return $this->getServiceRules($vars, $package);
}
/**
* Attempts to validate an existing service against a set of service info updates. Sets Input errors on failure.
*
* @param stdClass $service A stdClass object representing the service to validate for editing
* @param array $vars An array of user-supplied info to satisfy the request (optional)
* @return bool True if the service update validates or false otherwise. Sets Input errors when false.
*/
public function validateServiceEdit($service, array $vars = null)
{
$package = isset($service->package) ? $service->package : null;
return $this->getServiceRules($vars, $package, true);
}
/**
* Returns the rule set for adding/editing a service
*
* @param array $vars A list of input vars (optional)
* @param stdClass $package A stdClass object representing the selected package (optional)
* @param bool $edit True to get the edit rules, false for the add rules (optional)
* @return array Service rules
*/
private function getServiceRules(array $vars = null, $package = null, $edit = false)
{
// Get the service helper
$this->loadLib('pterodactyl_service');
$service_helper = new PterodactylService();
if ($package) {
// Get and set the module row to use for API calls
if ($package->module_group) {
$this->setModuleRow($this->getModuleRow($this->selectModuleRow($package->module_group)));
} else {
$this->setModuleRow($this->getModuleRow($package->module_row));
}
// Load egg
$pterodactyl_egg = $this->apiRequest(
'Nests',
'eggsGet',
['nest_id' => $package->meta->nest_id, 'egg_id' => $package->meta->egg_id]
);
$errors = $this->Input->errors();
if (!empty($errors)) {
$pterodactyl_egg = null;
} else {
// Set egg variables from service, package, or config options
$egg_variables = $service_helper->getEnvironmentVariables($vars, $package, $pterodactyl_egg);
$vars = array_merge($vars, array_change_key_case($egg_variables));
}
}
$this->Input->setRules($service_helper->getServiceRules($vars, $package, $edit, $pterodactyl_egg));
return $this->Input->validates($vars);
}
/**
* Adds the service to the remote server. Sets Input errors on failure,
* preventing the service from being added.
*
* @param stdClass $package A stdClass object representing the selected package
* @param array $vars An array of user supplied info to satisfy the request (optional)
* @param stdClass $parent_package A stdClass object representing the parent
* service's selected package (if the current service is an addon service) (optional)
* @param stdClass $parent_service A stdClass object representing the parent
* service of the service being added (if the current service is an addon service
* service and parent service has already been provisioned) (optional)
* @param string $status The status of the service being added. (optional) These include:
*
* - active
* - canceled
* - pending
* - suspended
* @return array A numerically indexed array of meta fields to be stored for this service containing:
*
* - key The key for this meta field
* - value The value for this key
* - encrypted Whether or not this field should be encrypted (default 0, not encrypted)
* @see Module::getModule()
* @see Module::getModuleRow()
*/
public function addService(
$package,
array $vars = null,
$parent_package = null,
$parent_service = null,
$status = 'pending'
) {
Loader::loadModels($this, ['ModuleClientMeta', 'Clients']);
$meta = [];
// Set configurable options
$package = $this->getConfigurableOptions($vars, $package);
// Load egg
$pterodactyl_egg = $this->apiRequest(
'Nests',
'eggsGet',
['nest_id' => $package->meta->nest_id, 'egg_id' => $package->meta->egg_id]
);
if ($this->Input->errors()) {
return;
}
$this->validateService($package, $vars);
if ($this->Input->errors()) {
return;
}
// Get the service helper
$this->loadLib('pterodactyl_service');
$service_helper = new PterodactylService();
if ($vars['use_module'] == 'true') {
// Load module
$module = $this->getModule();
// Fetch client
$client = $this->Clients->get($vars['client_id'] ?? null);
// Check if a user already exists with the client's email
$pterodactyl_user = $this->apiRequest('Users', 'getByEmail', [$client->email]);
$pterodactyl_user = !empty($pterodactyl_user->data) ? reset($pterodactyl_user->data) : null;
if ($this->Input->errors()) {
$this->Input->setErrors([]);
}
// Check if a user already exists with the current external id
if (empty($pterodactyl_user)) {
$pterodactyl_user = $this->apiRequest('Users', 'getByExternalID', ['bl-' . $vars['client_id']]);
}
if ($this->Input->errors()) {
$this->Input->setErrors([]);
}
// If an account does not exists, create a new one and keep track of the credentials
if (empty($pterodactyl_user)) {
$addParameters = $service_helper->addUserParameters($vars);
$pterodactyl_user = $this->apiRequest('Users', 'add', [$addParameters]);
if ($this->Input->errors()) {
return;
}
// Keep track of the username and password used for this client
$this->ModuleClientMeta->set(
$vars['client_id'],
$module->id,
0,
[
['key' => 'pterodactyl_username', 'value' => $addParameters['username'], 'encrypted' => 0],
['key' => 'pterodactyl_password', 'value' => $addParameters['password'], 'encrypted' => 1]
]
);
}
// Get Pterodactyl credentials
$server_username = $this->ModuleClientMeta->get(
$vars['client_id'],
'pterodactyl_username',
$module->id
);
$server_password = $this->ModuleClientMeta->get(
$vars['client_id'],
'pterodactyl_password',
$module->id
);
$vars['server_username'] = isset($server_username->value) ? $server_username->value : null;
$vars['server_password'] = isset($server_password->value) ? $server_password->value : null;
// Create server
$pterodactyl_server = $this->apiRequest(
'Servers',
'add',
[$service_helper->addServerParameters($vars, $package, $pterodactyl_user, $pterodactyl_egg)]
);
if ($this->Input->errors()) {
// No need to roll back user creation, we'll just use that user for future requests
return;
}
$meta['server_id'] = $pterodactyl_server->attributes->id;
$meta['external_id'] = $pterodactyl_server->attributes->external_id;
if (isset($pterodactyl_server->attributes->relationships)
&& isset($pterodactyl_server->attributes->relationships->allocations)
&& isset($pterodactyl_server->attributes->relationships->allocations->data[0])
) {
$allocation = $pterodactyl_server->attributes->relationships->allocations->data[0];
$meta['server_ip'] = isset($allocation->attributes->ip) ? $allocation->attributes->ip : null;
$meta['server_port'] = isset($allocation->attributes->port) ? $allocation->attributes->port : null;
}
}
$return = [
[
'key' => 'server_id',
'value' => isset($meta['server_id'])
? $meta['server_id'] :
(isset($vars['server_id']) ? $vars['server_id'] : null),
'encrypted' => 0
],
[
'key' => 'external_id',
'value' => !empty($meta['external_id'])
? $meta['external_id']
: (isset($vars['external_id']) ? $vars['external_id'] : null),
'encrypted' => 0
],
[
'key' => 'server_ip',
'value' => isset($meta['server_ip'])
? $meta['server_ip'] :
(isset($vars['server_ip']) ? $vars['server_ip'] : null),
'encrypted' => 0
],
[
'key' => 'server_port',
'value' => isset($meta['server_port'])
? $meta['server_port'] :
(isset($vars['server_port']) ? $vars['server_port'] : null),
'encrypted' => 0
],
[
'key' => 'server_name',
'value' => isset($vars['server_name']) ? $vars['server_name'] : '',
'encrypted' => 0
],
[
'key' => 'server_description',
'value' => isset($vars['server_description']) ? $vars['server_description'] : '',
'encrypted' => 0
],
[
'key' => 'server_username',
'value' => isset($vars['server_username']) ? $vars['server_username'] : '',
'encrypted' => 0
],
[
'key' => 'server_password',
'value' => isset($vars['server_password']) ? $vars['server_password'] : '',
'encrypted' => 1
],
];
$environment_variables = $service_helper->getEnvironmentVariables($vars, $package, $pterodactyl_egg);
foreach ($environment_variables as $environment_variable => $value) {
foreach ($return as $item) {
if ($item['key'] === strtolower($environment_variable)) {
continue 2;
}
}
$return[] = [
'key' => strtolower($environment_variable),
'value' => $value,
'encrypted' => 0
];
}
return $return;
}
/**
* Edits the service on the remote server. Sets Input errors on failure,
* preventing the service from being edited.
*
* @param stdClass $package A stdClass object representing the current package
* @param stdClass $service A stdClass object representing the current service
* @param array $vars An array of user supplied info to satisfy the request (optional)
* @param stdClass $parent_package A stdClass object representing the parent
* service's selected package (if the current service is an addon service) (optional)
* @param stdClass $parent_service A stdClass object representing the parent
* service of the service being edited (if the current service is an addon service) (optional)
* @return array A numerically indexed array of meta fields to be stored for this service containing:
*
* - key The key for this meta field
* - value The value for this key
* - encrypted Whether or not this field should be encrypted (default 0, not encrypted)
* @see Module::getModule()
* @see Module::getModuleRow()
*/
public function editService(
$package,
$service,
array $vars = null,
$parent_package = null,
$parent_service = null
) {
$service_fields = $this->serviceFieldsToObject($service->fields);
$this->validateServiceEdit($service, $vars);
if ($this->Input->errors()) {
return;
}
// Get the service helper
$this->loadLib('pterodactyl_service');
$service_helper = new PterodactylService();
// Set configurable options
$package = $this->getConfigurableOptions($vars, $package);
// Load egg
$pterodactyl_egg = $this->apiRequest(
'Nests',
'eggsGet',
['nest_id' => $package->meta->nest_id, 'egg_id' => $package->meta->egg_id]
);
if ($this->Input->errors()) {
return;
}
if ($vars['use_module'] == 'true') {
// Load user account
$pterodactyl_user = $this->apiRequest('Users', 'getByExternalID', ['bl-' . $service->client_id]);
// Load the server
$pterodactyl_server = $this->getServer($service);
if ($this->Input->errors()) {
return;
}
// Edit server details
$vars['service_id'] = $service->id;
$vars['client_id'] = $service->client_id;
$pterodactyl_server_edited = $this->apiRequest(
'Servers',
'editDetails',
[$pterodactyl_server->attributes->id, $service_helper->editServerParameters($vars, $pterodactyl_user)]
);
if ($this->Input->errors()) {
return;
}
// Set service fields
$vars['server_id'] = $pterodactyl_server_edited->attributes->id;
$vars['external_id'] = $pterodactyl_server_edited->attributes->external_id;
if (isset($pterodactyl_server_edited->attributes->relationships)
&& isset($pterodactyl_server_edited->attributes->relationships->allocations)
&& isset($pterodactyl_server_edited->attributes->relationships->allocations->data[0])
) {
$allocation = $pterodactyl_server_edited->attributes->relationships->allocations->data[0];
$vars['server_ip'] = isset($allocation->attributes->ip) ? $allocation->attributes->ip : null;
$vars['server_port'] = isset($allocation->attributes->port) ? $allocation->attributes->port : null;
}
// Set package fields
$vars['allocation'] = $pterodactyl_server->attributes->allocation;
$this->apiRequest(
'Servers',
'editBuild',
[$pterodactyl_server->attributes->id, $service_helper->editServerBuildParameters($vars, $package)]
);
// Edit startup parameters
$this->apiRequest(
'Servers',
'editStartup',
[
$pterodactyl_server->attributes->id,
$service_helper->editServerStartupParameters($vars, $package, $pterodactyl_egg, $service_fields)
]
);
if ($this->Input->errors()) {
return;
}
}
$return = [
[
'key' => 'server_id',
'value' => !empty($vars['server_id']) ? $vars['server_id'] : $service_fields->server_id,
'encrypted' => 0
],
[
'key' => 'external_id',
'value' => !empty($vars['external_id'])
? $vars['external_id']
: (isset($service_fields->external_id) ? $service_fields->external_id : null),
'encrypted' => 0
],
[
'key' => 'server_ip',
'value' => !empty($vars['server_ip']) ? $vars['server_ip'] : $service_fields->server_ip,
'encrypted' => 0
],
[
'key' => 'server_port',
'value' => !empty($vars['server_port']) ? $vars['server_port'] : $service_fields->server_port,
'encrypted' => 0
],
[
'key' => 'server_name',
'value' => isset($vars['server_name']) ? $vars['server_name'] : $service_fields->server_name,
'encrypted' => 0
],
[
'key' => 'server_description',
'value' => isset($vars['server_description'])
? $vars['server_description']
: $service_fields->server_description,
'encrypted' => 0
],
[
'key' => 'server_username',
'value' => isset($vars['server_username']) ? $vars['server_username'] : $service_fields->server_username,
'encrypted' => 0
],
[
'key' => 'server_password',
'value' => isset($vars['server_password']) ? $vars['server_password'] : $service_fields->server_password,
'encrypted' => 1
],
];
// Add egg variables
$environment_variables = $service_helper->getEnvironmentVariables(
$vars,
$package,
$pterodactyl_egg,
$service_fields
);
foreach ($environment_variables as $environment_variable => $value) {
foreach ($return as $key => $item) {
if ($item['key'] === strtolower($environment_variable)) {
unset($return[$key]);
}
}
$return[] = [
'key' => strtolower($environment_variable),
'value' => $value,
'encrypted' => 0
];
}
return $return;
}
/**
* Cancels the service on the remote server. Sets Input errors on failure,
* preventing the service from being canceled.
*
* @param stdClass $package A stdClass object representing the current package
* @param stdClass $service A stdClass object representing the current service
* @param stdClass $parent_package A stdClass object representing the parent
* service's selected package (if the current service is an addon service) (optional)
* @param stdClass $parent_service A stdClass object representing the parent
* service of the service being canceled (if the current service is an addon service) (optional)
* @return mixed null to maintain the existing meta fields or a numerically
* indexed array of meta fields to be stored for this service containing:
*
* - key The key for this meta field
* - value The value for this key
* - encrypted Whether or not this field should be encrypted (default 0, not encrypted)
* @see Module::getModule()
* @see Module::getModuleRow()
*/
public function cancelService($package, $service, $parent_package = null, $parent_service = null)
{
// Load the server
$pterodactyl_server = $this->getServer($service);
// Delete the server
$this->apiRequest(
'Servers',
'delete',
['server_id' => $pterodactyl_server ? $pterodactyl_server->attributes->id : null]
);
// We do not delete the user, but rather leave it around to be used for any current or future services
return null;
}
/**
* Suspends the service on the remote server. Sets Input errors on failure,
* preventing the service from being suspended.
*
* @param stdClass $package A stdClass object representing the current package
* @param stdClass $service A stdClass object representing the current service
* @param stdClass $parent_package A stdClass object representing the parent
* service's selected package (if the current service is an addon service)
* @param stdClass $parent_service A stdClass object representing the parent
* service of the service being suspended (if the current service is an addon service)
* @return mixed null to maintain the existing meta fields or a numerically
* indexed array of meta fields to be stored for this service containing:
* - key The key for this meta field
* - value The value for this key
* - encrypted Whether or not this field should be encrypted (default 0, not encrypted)
* @see Module::getModule()
* @see Module::getModuleRow()
*/
public function suspendService($package, $service, $parent_package = null, $parent_service = null)
{
// Load the server
$pterodactyl_server = $this->getServer($service);
// Suspend the server
$this->apiRequest(
'Servers',
'suspend',
['server_id' => $pterodactyl_server ? $pterodactyl_server->attributes->id : null]
);
return null;
}
/**
* Unsuspends the service on the remote server. Sets Input errors on failure,
* preventing the service from being unsuspended.
*
* @param stdClass $package A stdClass object representing the current package
* @param stdClass $service A stdClass object representing the current service
* @param stdClass $parent_package A stdClass object representing the parent
* service's selected package (if the current service is an addon service)
* @param stdClass $parent_service A stdClass object representing the parent
* service of the service being unsuspended (if the current service is an addon service)
* @return mixed null to maintain the existing meta fields or a numerically
* indexed array of meta fields to be stored for this service containing:
* - key The key for this meta field
* - value The value for this key
* - encrypted Whether or not this field should be encrypted (default 0, not encrypted)
* @see Module::getModule()
* @see Module::getModuleRow()
*/
public function unsuspendService($package, $service, $parent_package = null, $parent_service = null)
{
// Load the server
$pterodactyl_server = $this->getServer($service);
// Unsuspend the server
$this->apiRequest(
'Servers',
'unsuspend',
['server_id' => $pterodactyl_server ? $pterodactyl_server->attributes->id : null]
);
return null;
}
/**
* Gets a Pterodactyl server for the given service
*
* @param stdClass $service A stdClass object representing the current service
* @return PterodactylResponse An object representing the Pterodactyl server
*/
private function getServer($service)
{
$service_fields = $this->serviceFieldsToObject($service->fields);
// Load server
if (!empty($service_fields->server_id)) {
$pterodactyl_server = $this->apiRequest('Servers', 'get', [$service_fields->server_id]);
} else {
$pterodactyl_server = $this->apiRequest(
'Servers',
'getByExternalID',
[!empty($service_fields->external_id) ? $service_fields->external_id : null]
);
}
return $pterodactyl_server;
}
/**
* Fetches the HTML content to display when viewing the service info in the
* admin interface.
*
* @param stdClass $service A stdClass object representing the service
* @param stdClass $package A stdClass object representing the service's package
* @return string HTML content containing information to display when viewing the service info
*/
public function getAdminServiceInfo($service, $package)
{
Loader::loadModels($this, ['ModuleClientMeta']);
$row = $this->getModuleRow();
// Load the view into this object, so helpers can be automatically added to the view
$this->view = new View('admin_service_info', 'default');
$this->view->base_uri = $this->base_uri;
$this->view->setDefaultView('components' . DS . 'modules' . DS . 'pterodactyl' . DS);
// Load the helpers required for this view
Loader::loadHelpers($this, ['Form', 'Html']);
// Get username and password for the account
$module = $this->getModule();
$username = $this->ModuleClientMeta->get($service->client_id, 'pterodactyl_username', $module->id);
$password = $this->ModuleClientMeta->get($service->client_id, 'pterodactyl_password', $module->id);
// Set view data
$this->view->set('module_row', $row);
$this->view->set('username', $username ? $username->value : '');
$this->view->set('password', $password ? $password->value : '');
return $this->view->fetch();
}
/**
* Fetches the HTML content to display when viewing the service info in the
* client interface.
*
* @param stdClass $service A stdClass object representing the service
* @param stdClass $package A stdClass object representing the service's package
* @return string HTML content containing information to display when viewing the service info
*/
public function getClientServiceInfo($service, $package)
{
Loader::loadModels($this, ['ModuleClientMeta']);
$row = $this->getModuleRow();
// Load the view into this object, so helpers can be automatically added to the view
$this->view = new View('client_service_info', 'default');
$this->view->base_uri = $this->base_uri;
$this->view->setDefaultView('components' . DS . 'modules' . DS . 'pterodactyl' . DS);
// Load the helpers required for this view
Loader::loadHelpers($this, ['Form', 'Html']);
// Get username and password for the account
$module = $this->getModule();
$username = $this->ModuleClientMeta->get($service->client_id, 'pterodactyl_username', $module->id);
$password = $this->ModuleClientMeta->get($service->client_id, 'pterodactyl_password', $module->id);
// Set view data
$this->view->set('module_row', $row);
$this->view->set('username', $username ? $username->value : '');
$this->view->set('password', $password ? $password->value : '');
return $this->view->fetch();
}
/**
* Returns all tabs to display to an admin when managing a service whose
* package uses this module
*
* @param stdClass $package A stdClass object representing the selected package
* @return array An array of tabs in the format of method => title.
* Example: array('methodName' => "Title", 'methodName2' => "Title2")
*/
public function getAdminTabs($package)
{
return [
'tabActions' => Language::_('Pterodactyl.tab_actions', true)
];
}
/**
* Returns all tabs to display to a client when managing a service whose
* package uses this module
*
* @param stdClass $package A stdClass object representing the selected package
* @return array An array of tabs in the format of method => title.
* Example: array('methodName' => "Title", 'methodName2' => "Title2")
*/
public function getClientTabs($package)
{
return [
'tabClientActions' => Language::_('Pterodactyl.tab_client_actions', true)
];
}
/**
* Actions tab (start, stop, restart)
*
* @param stdClass $package A stdClass object representing the current package
* @param stdClass $service A stdClass object representing the current service
* @param array $get Any GET parameters
* @param array $post Any POST parameters
* @param array $files Any FILES parameters
* @return string The string representing the contents of this tab
*/
public function tabActions($package, $service, array $get = null, array $post = null, array $files = null)
{
$this->view = new View('tab_actions', 'default');
return $this->actionsTab($package, $service, false, $get, $post);
}
/**
* Client Actions tab (start, stop, restart)
*
* @param stdClass $package A stdClass object representing the current package
* @param stdClass $service A stdClass object representing the current service
* @param array $get Any GET parameters
* @param array $post Any POST parameters
* @param array $files Any FILES parameters
* @return string The string representing the contents of this tab
*/
public function tabClientActions($package, $service, array $get = null, array $post = null, array $files = null)
{
$this->view = new View('tab_client_actions', 'default');
return $this->actionsTab($package, $service, true, $get, $post);
}
/**
* Handles data for the actions tab in the client and admin interfaces
* @see Pterodactyl::tabActions() and Pterodactyl::tabClientActions()
*
* @param stdClass $package A stdClass object representing the current package
* @param stdClass $service A stdClass object representing the current service
* @param bool $client True if the action is being performed by the client, false otherwise
* @param array $get Any GET parameters
* @param array $post Any POST parameters
* @param array $files Any FILES parameters
*/
private function actionsTab($package, $service, $client = false, array $get = null, array $post = null)
{
$this->view->base_uri = $this->base_uri;
// Load the helpers required for this view
Loader::loadHelpers($this, ['Form', 'Html']);
// Get the service fields
$service_fields = $this->serviceFieldsToObject($service->fields);
// Get server information from the application API
$server = $this->getServer($service);
$server_id = isset($server->attributes->identifier) ? $server->attributes->identifier : null;
// Get the service fields
$get_key = '3';
if ($client) {
$get_key = '2';
}
// Perform actions
if (array_key_exists($get_key, (array)$get)
&& in_array($get[$get_key], ['start', 'stop', 'restart'])
&& isset($server->attributes->identifier)
) {
// Send a power signal
$signal_response = $this->apiRequest(
'Client',
'serverPowerSignal',
[$server->attributes->identifier, $get[$get_key]],
true
);
$errors = $this->Input->errors();
if (empty($errors)) {
$this->setMessage('success', Language::_('Pterodactyl.!success.' . $get[$get_key], true));
}
}
// Fetch the server status from the account API
$this->view->set('server', $this->apiRequest('Client', 'getServerUtilization', [$server_id], true));
$this->view->set('client_id', $service->client_id);
$this->view->set('service_id', $service->id);
$this->view->set('view', $this->view->view);
$this->view->setDefaultView('components' . DS . 'modules' . DS . 'pterodactyl' . DS);
return $this->view->fetch();
}
/**
* Returns an array of package fields, overriding the configurable options
*
* @param array $vars An array of key/value input pairs
* @param stdClass $package A stdClass object representing the package for the service
* @return stdClass The modified package object
*/
private function getConfigurableOptions(array $vars, $package)
{
$fields = [
'location_id', 'egg_id', 'nest_id', 'port_range',
'pack_id', 'memory', 'swap', 'cpu', 'disk', 'io',
'startup', 'image', 'databases', 'allocations', 'backups', 'proxies'
];
// Override package fields, if an equivalent configurable option exists
if (!empty($vars['configoptions'])) {
foreach ($vars['configoptions'] as $field => $value) {
if (in_array($field, $fields)) {
$package->meta->{$field} = $value;
}
}
}
return $package;
}
/**
* Runs a particaluar API requestor method, logs, and reports errors
*
* @param string $requestor The name of the requestor class to use
* @param string $action The name of the requestor method to use
* @param array $data The parameters to submit to the method (optional)
* @return mixed The response from Pterodactyl
*/
private function apiRequest($requestor, $action, array $data = [], $client_api = false)
{
// Fetch the module row
$row = $this->getModuleRow();
if (!$row) {
$this->Input->setErrors(
['module_row' => ['missing' => Language::_('Pterodactyl.!error.module_row.missing', true)]]
);
return;
}
// Fetch the API
$api = $this->getApi(
$row->meta->host_name,
$client_api ? $row->meta->account_api_key : $row->meta->application_api_key,
$row->meta->use_ssl == 'true'
);
// Perform the request
try {
$response = call_user_func_array([$api->{$requestor}, $action], $data);
} catch (Throwable $e) {
// Try executing the request again, without array keys
$response = call_user_func_array([$api->{$requestor}, $action], array_values($data));
}
$errors = $response->errors();
$this->log($requestor . '.' . $action, json_encode($data), 'input', true);
$this->log($requestor . '.' . $action, $response->raw(), 'output', empty($errors));
// Check for request errors
if (!empty($errors)) {
$this->Input->setErrors([$requestor => $errors]);
return;
}
return $response->response();
}
/**
* Validates input data when attempting to add a package, returns the meta
* data to save when adding a package. Performs any action required to add
* the package on the remote server. Sets Input errors on failure,
* preventing the package from being added.
*
* @param array $vars An array of key/value pairs used to add the package (optional)
* @return array A numerically indexed array of meta fields to be stored for this package containing:
*
* - key The key for this meta field
* - value The value for this key
* - encrypted Whether or not this field should be encrypted (default 0, not encrypted)
* @see Module::getModule()
* @see Module::getModuleRow()
*/
public function addPackage(array $vars = null)
{
// Load the package helper
$this->loadLib('pterodactyl_package');
$package_helper = new PterodactylPackage();
// Get package field lists from API
$package_lists = $this->getPackageLists((object)$vars);
// Validate and gather information using the package helper
$meta = $package_helper->add($package_lists, $vars);
if ($package_helper->errors()) {
$this->Input->setErrors($package_helper->errors());
}
return $meta;
}