-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathService.php
More file actions
398 lines (354 loc) · 12.8 KB
/
Service.php
File metadata and controls
398 lines (354 loc) · 12.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
<?php
namespace App\Services\AMPTEST;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Cache;
use App\Services\ServiceInterface;
use Illuminate\Http\Request;
use App\Models\Settings
use App\Models\Package;
use App\Models\Order;
class Service implements ServiceInterface
{
/**
* Unique key used to store settings
* for this service.
*
* @return string
*/
public static $key = 'amptest';
public function __construct(Order $order)
{
$this->order = $order;
}
/**
* Returns the meta data about this Server/Service
*
* @return object
*/
public static function metaData(): object
{
return (object)
[
'display_name' => 'AMPTEST',
'author' => 'Pay2Win',
'version' => '1.0.0',
'wemx_version' => ['dev', '>=1.8.0'],
];
}
/**
* Define the default configuration values required to setup this service
* i.e host, api key, or other values. Use Laravel validation rules for
*
* Laravel validation rules: https://laravel.com/docs/10.x/validation
*
* @return array
*/
public static function setConfig(): array
{
// Check if the URL ends with a slash
$doesNotEndWithSlash = function ($attribute, $value, $fail) {
if (preg_match('/\/$/', $value)) {
return $fail('AMP Panel URL must not end with a slash "/".');
}
};
return [
[
"col" => "col-12",
"key" => "amptest::hostname",
"name" => "Hostname",
"description" => "Hostname of the AMP instance",
"type" => "url",
"rules" => ['required', 'active_url', $doesNotEndWithSlash], // laravel validation rules
],
[
"key" => "amptest::username",
"name" => "Username",
"description" => "Username of an administrator on AMP Panel",
"type" => "text",
"rules" => ['required'], // laravel validation rules
],
[
"key" => "encrypted::amptest::password",
"name" => "User Password",
"description" => "Password of an administrator on AMP Panel",
"type" => "password",
"rules" => ['required'], // laravel validation rules
],
];
}
/**
* Define the default package configuration values required when creatig
* new packages. i.e maximum ram usage, allowed databases and backups etc.
*
* Laravel validation rules: https://laravel.com/docs/10.x/validation
*
* @return array
*/
public static function setPackageConfig(Package $package): array
{
$templates = Service::api('/ADSModule/GetDeploymentTemplates', [])->collect()->mapWithKeys(function ($item) {
if(!isset($item['Id']) OR !isset($item['Name'])) {
throw new \Exception("Could not retrieve a list of deployable templates, create a template on AMP first.");
}
return [$item['Id'] => $item['Name']];
});
return [
[
"col" => "col-12",
"key" => "template",
"name" => "Template ",
"description" => "Select the template to deploy for this package",
"type" => "select",
"options" => $templates->toArray(),
"save_on_change" => true,
"rules" => ['required'],
],
[
"col" => "col-12",
"key" => "post_create_action",
"name" => "Post Create Action ",
"description" => "Choose what the application does inside the instance",
"type" => "select",
"options" => [
0 => 'Do Nothing',
1 => 'Update Once',
2 => 'Update Always',
3 => 'Update and Start Once',
4 => 'Update and Start Always',
5 => 'Start Always',
],
"save_on_change" => true,
"rules" => ['required'],
]
];
}
/**
* Define the checkout config that is required at checkout and is fillable by
* the client. Its important to properly sanatize all inputted data with rules
*
* Laravel validation rules: https://laravel.com/docs/10.x/validation
*
* @return array
*/
public static function setCheckoutConfig(Package $package): array
{
return [];
}
/**
* Define buttons shown at order management page
*
* @return array
*/
public static function setServiceButtons(Order $order): array
{
return [
[
"name" => "Login to Panel",
"color" => "primary",
"href" => settings('amptest::hostname'),
"target" => "_blank", // optional
],
];
}
/**
* Change the AMP password
*/
public function changePassword(Order $order, string $newPassword)
{
try {
$ampUser = $order->getExternalUser();
$response = Service::api('/Core/ResetUserPassword', [
'Username' => $ampUser->username,
'NewPassword' => $newPassword,
]);
if($response->failed())
{
throw new \Exception("AMP failed to reset password. Please try again.");
}
$order->updateExternalPassword($newPassword);
} catch (\Exception $error) {
return redirect()->back()->withError("Something went wrong, please try again.");
}
return redirect()->back()->withSuccess("Password has been changed");
}
/**
* This function is responsible for creating an instance of the
* service. This can be anything such as a server, vps or any other instance.
*
* @return void
*/
public function create(array $data = [])
{
// define the order, user and package
$order = $this->order;
$user = $order->user;
$package = $order->package;
// define user data
$externalId = 'WMX'.$order->id;
$username = $user->username . rand(1, 1000);
$password = str_random(12);
// if AMP user already exists, set the username
$externalUser = $order->getExternalUser();
if($externalUser) {
$username = $externalUser->username;
}
$isampuser = Service::api('/Core/GetUserInfo', [
'UID' => $user->username;
])
if($isampuser->failed())
{
throw new \Exception("[amptest] Failed to find user in isampuser function $isampuser->failed()");
}
$server = Service::api('/ADSModule/DeployTemplate', [
'TemplateID' => $package->data('template'),
'NewUsername' => $username,
'NewPassword' => $password,
'NewEmail' => $user->email,
'Tag' => $externalId,
'FriendlyName' => $package->name,
'Secret' => 'secretwemx'. $order->id,
'PostCreate' => $package->data('post_create_action', 0),
'RequiredTags' => [],
'ExtraProvisionSettings' => [],
]);
if($server->failed())
{
throw new \Exception("[amptest] Failed to create instance");
}
$order->setExternalId((string) $externalId);
if(!$externalUser) {
// create the external user
$order->createExternalUser([
'username' => $username,
'password' => $password,
]);
// finally, lets email the user their login details
$user->email([
'subject' => 'Game Panel Account',
'content' => "Your account has been created on the game panel. You can login using the following details: <br><br> Username: {$username} <br> Password: {$password} <br><br><br> test output $isampuser",
'button' => [
'name' => 'Game Panel',
'url' => settings('amptest::hostname'),
],
]);
}
}
/**
* Handle the callback from the AMP server
*/
public function callback(Request $request)
{
ErrorLog('amptest:callback', json_encode($request->all()));
return response()->json(['success' => true], 200);
}
/**
* This function is responsible for suspending an instance of the
* service. This method is called when a order is expired or
* suspended by an admin
*
* @return void
*/
public function suspend(array $data = [])
{
$order = $this->order;
$server = Service::api('/ADSModule/SetInstanceSuspended', [
'InstanceName' => $order->external_id,
'Suspended' => true,
]);
}
/**
* This function is responsible for unsuspending an instance of the
* service. This method is called when a order is activated or
* unsuspended by an admin
*
* @return void
*/
public function unsuspend(array $data = [])
{
$order = $this->order;
$server = Service::api('/ADSModule/SetInstanceSuspended', [
'InstanceName' => $order->external_id,
'Suspended' => false,
]);
}
/**
* This function is responsible for deleting an instance of the
* service. This can be anything such as a server, vps or any other instance.
*
* @return void
*/
public function terminate(array $data = [])
{
$order = $this->order;
$server = Service::api('/ADSModule/DeleteInstance', [
'InstanceName' => $order->external_id,
]);
}
/**
* Init connection with API
*/
public static function api($endpoint, $data = [])
{
// retrieve the session ID
$method = 'post';
$sessionID = Cache::get('amptest::SessionID');
if(!$sessionID) {
$session = Http::withHeaders(['Accept' => 'application/json', 'Content-Type' => 'application/json'])->post(settings('amptest::hostname'). "/API/Core/Login",
[
'username' => settings('amptest::username'),
'password' => settings('encrypted::amptest::password'),
'token' => '',
'rememberMe' => false,
]);
if($session->failed())
{
throw new \Exception("[amptest] Failed to retrieve session ID. Ensure the API details and hostname are valid.");
}
$sessionID = $session['sessionID'];
if(!isset($sessionID))
{
throw new \Exception("[amptest] Failed to retrieve session ID. Ensure the API details and hostname are valid.");
}
Cache::put('amptest::SessionID', $sessionID, 240);
}
// define the URL and data
$url = settings('amptest::hostname'). "/API{$endpoint}";
$data['SESSIONID'] = $sessionID;
// make the request
$response = Http::withHeaders([
'Accept' => 'application/json',
'Content-Type' => 'application/json',
])->$method($url, $data);
if($response->failed())
{
if($response->unauthorized() OR $response->forbidden()) {
throw new \Exception("[amptest] This action is unauthorized! Confirm that API token has the right permissions");
}
if($response->serverError()) {
throw new \Exception("[amptest] Internal Server Error: {$response->status()}");
}
throw new \Exception("[amptest] Failed to connect to the API. Ensure the API details and hostname are valid.");
}
return $response;
}
/**
* Test API connection
*/
public static function testConnection()
{
try {
// try to get list of packages through API request
$templates = Service::api('/ADSModule/GetDeploymentTemplates', [])->collect()->mapWithKeys(function ($item) {
if(!isset($item['Id']) OR !isset($item['Name'])) {
throw new \Exception("Could not retrieve a list of deployable templates, create a template on AMP first.");
}
return [$item['Id'] => $item['Name']];
});
} catch(\Exception $error) {
// if try-catch fails, return the error with details
return redirect()->back()->withError("Failed to connect to AMP. <br><br>[amptest] {$error->getMessage()}");
}
// if no errors are logged, return a success message
return redirect()->back()->withSuccess("Successfully connected with AMP");
}
}