1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gameap\Http\Controllers\GdaemonAPI; |
4
|
|
|
|
5
|
|
|
use Gameap\Exceptions\GameapException; |
6
|
|
|
use Gameap\Repositories\NodeRepository; |
7
|
|
|
use Gameap\Services\Daemon\CertificateService; |
8
|
|
|
use Illuminate\Contracts\Filesystem\FileNotFoundException; |
9
|
|
|
use Illuminate\Http\Request; |
10
|
|
|
use Illuminate\Routing\Controller as BaseController; |
11
|
|
|
use Illuminate\Support\Facades\Cache; |
12
|
|
|
use Illuminate\Support\Facades\Storage; |
13
|
|
|
use Illuminate\Support\Str; |
14
|
|
|
|
15
|
|
|
class SetupController extends BaseController |
16
|
|
|
{ |
17
|
|
|
private const CREATE_TOKEN_LENGTH = 24; |
18
|
|
|
|
19
|
|
|
private const CREATE_TOKEN_TTL_IN_SECONDS = 3600; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The DedicatedServersRepository instance. |
23
|
|
|
* |
24
|
|
|
* @var \Gameap\Repositories\NodeRepository |
25
|
|
|
*/ |
26
|
|
|
public $repository; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* DedicatedServersController constructor. |
30
|
|
|
* @param NodeRepository $repository |
31
|
|
|
*/ |
32
|
|
|
public function __construct(NodeRepository $repository) |
33
|
|
|
{ |
34
|
|
|
$this->repository = $repository; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Return path to daemon script setup |
39
|
|
|
*/ |
40
|
|
|
public function setup(string $token) |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
if (app()->has('debugbar')) { |
43
|
|
|
app('debugbar')->disable(); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
Cache::forget('gdaemonAutoSetupToken'); |
47
|
|
|
|
48
|
|
|
$gdaemonCreateToken = Str::random(self::CREATE_TOKEN_LENGTH); |
49
|
|
|
Cache::put('gdaemonAutoCreateToken', $gdaemonCreateToken, self::CREATE_TOKEN_TTL_IN_SECONDS); |
50
|
|
|
|
51
|
|
|
return "export createToken={$gdaemonCreateToken}; |
52
|
|
|
export panelHost=" . url('/') . '; |
53
|
|
|
curl -sL https://raw.githubusercontent.com/gameap/auto-install-scripts/master/install-gdaemon.sh | bash --'; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Creating a new Dedicated server. Uploading certificate |
58
|
|
|
* |
59
|
|
|
* Gets dedicated server attributes and server certificate |
60
|
|
|
* Return signed client certificate and signed server certificate |
61
|
|
|
* |
62
|
|
|
* @param string $token |
63
|
|
|
* @param Request $request |
64
|
|
|
* @return string |
65
|
|
|
* |
66
|
|
|
* @throws GameapException |
67
|
|
|
* @throws FileNotFoundException |
68
|
|
|
*/ |
69
|
|
|
public function create(string $token, Request $request) |
70
|
|
|
{ |
71
|
|
|
if (app()->has('debugbar')) { |
72
|
|
|
app('debugbar')->disable(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$attributes = $request->all(); |
76
|
|
|
|
77
|
|
|
if ($request->hasFile('gdaemon_server_cert')) { |
78
|
|
|
$csr = $request->file('gdaemon_server_cert')->get(); |
79
|
|
|
$serverSignedCertificate = CertificateService::signCsr($csr); |
80
|
|
|
} else { |
81
|
|
|
return 'Error Empty GDdaemon server certificate'; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if (empty($attributes['location'])) { |
85
|
|
|
$attributes['location'] = 'Unknown'; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$attributes['gdaemon_server_cert'] = CertificateService::ROOT_CA_CERT; |
89
|
|
|
|
90
|
|
|
$dedicatedServer = $this->repository->store($attributes); |
91
|
|
|
$certificate = CertificateService::getRootCert(); |
92
|
|
|
|
93
|
|
|
Cache::forget('gdaemonCreateToken'); |
94
|
|
|
|
95
|
|
|
return "Success {$dedicatedServer->id} {$dedicatedServer->gdaemon_api_key}\n{$certificate}\n\n{$serverSignedCertificate}"; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.