1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gameap\Repositories; |
4
|
|
|
|
5
|
|
|
use Gameap\Models\DedicatedServer; |
6
|
|
|
use Gameap\Services\Daemon\CertificateService; |
7
|
|
|
use Illuminate\Support\Facades\Storage; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
|
10
|
|
|
class NodeRepository extends Repository |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var \Gameap\Repositories\ClientCertificateRepository |
14
|
|
|
*/ |
15
|
|
|
protected $clientCertificateRepository; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param DedicatedServer $dedicatedServer |
19
|
|
|
* @param \Gameap\Repositories\ClientCertificateRepository $clientCertificateRepository |
20
|
|
|
*/ |
21
|
|
|
public function __construct( |
22
|
|
|
DedicatedServer $dedicatedServer, |
23
|
|
|
ClientCertificateRepository $clientCertificateRepository |
24
|
|
|
) { |
25
|
|
|
$this->model = $dedicatedServer; |
26
|
|
|
$this->clientCertificateRepository = $clientCertificateRepository; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function find() |
30
|
|
|
{ |
31
|
|
|
return DedicatedServer::all(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function findById(int $id): ?DedicatedServer |
35
|
|
|
{ |
36
|
|
|
return DedicatedServer::findOrFail($id); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param int $perPage |
41
|
|
|
* @return mixed |
42
|
|
|
*/ |
43
|
|
|
public function getAll($perPage = 20) |
44
|
|
|
{ |
45
|
|
|
return DedicatedServer::orderBy('id')->withCount('servers')->paginate($perPage); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param int $id |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
|
|
public function getIpList(int $id) |
53
|
|
|
{ |
54
|
|
|
return $this->model->select('ip') |
55
|
|
|
->where('id', '=', $id) |
56
|
|
|
->first() |
57
|
|
|
->ip; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get all busy ports for dedicated servers. Group by ip |
62
|
|
|
* |
63
|
|
|
* @param int $id |
64
|
|
|
* @return \Illuminate\Support\Collection|array |
65
|
|
|
*/ |
66
|
|
|
public function getBusyPorts(int $id) |
67
|
|
|
{ |
68
|
|
|
/** @var DedicatedServer $dedicatedServer */ |
69
|
|
|
$dedicatedServer = $this->model->select('id')->where('id', '=', $id)->first(); |
70
|
|
|
$result = []; |
71
|
|
|
|
72
|
|
|
foreach ($dedicatedServer->servers as $server) { |
73
|
|
|
if (!array_key_exists($server->server_ip, $result)) { |
74
|
|
|
$result[$server->server_ip] = []; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
array_push($result[$server->server_ip], $server->server_port); |
78
|
|
|
array_push($result[$server->server_ip], $server->query_port); |
79
|
|
|
array_push($result[$server->server_ip], $server->rcon_port); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// Unique |
83
|
|
|
foreach ($result as &$ipList) { |
84
|
|
|
$ipList = array_values(array_unique($ipList, SORT_NUMERIC)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $result; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param array $attributes |
92
|
|
|
* @return DedicatedServer |
93
|
|
|
*/ |
94
|
|
|
public function store(array $attributes) |
95
|
|
|
{ |
96
|
|
|
$attributes['ip'] = array_filter($attributes['ip'], function ($value) { |
97
|
|
|
return !empty($value); |
98
|
|
|
}); |
99
|
|
|
|
100
|
|
|
if (empty($attributes['client_certificate_id'])) { |
101
|
|
|
$clientCertificate = $this->clientCertificateRepository->getFirstOrGenerate(); |
102
|
|
|
$attributes['client_certificate_id'] = $clientCertificate->id; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$attributes['gdaemon_api_key'] = Str::random(64); |
106
|
|
|
|
107
|
|
|
$attributes['enabled'] = $attributes['enabled'] ?? 1; |
108
|
|
|
$attributes['os'] = $attributes['os'] ?? 'linux'; |
109
|
|
|
|
110
|
|
|
return DedicatedServer::create($attributes); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param DedicatedServer $dedicatedServer |
115
|
|
|
* @throws \Exception |
116
|
|
|
*/ |
117
|
|
|
public function destroy(DedicatedServer $dedicatedServer): void |
118
|
|
|
{ |
119
|
|
|
if ($dedicatedServer->gdaemon_server_cert != CertificateService::ROOT_CA_CERT && |
120
|
|
|
Storage::disk('local')->exists($dedicatedServer->gdaemon_server_cert) |
121
|
|
|
) { |
122
|
|
|
// TODO: Not working =( |
123
|
|
|
// Storage::disk('local')->delete($dedicatedServer->gdaemon_server_cert); |
124
|
|
|
|
125
|
|
|
$certificateFile = Storage::disk('local') |
126
|
|
|
->getDriver() |
127
|
|
|
->getAdapter() |
128
|
|
|
->applyPathPrefix($dedicatedServer->gdaemon_server_cert); |
129
|
|
|
|
130
|
|
|
if (file_exists($certificateFile)) { |
131
|
|
|
unlink($certificateFile); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$dedicatedServer->delete(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param array $fields |
140
|
|
|
* @param DedicatedServer $dedicatedServer |
141
|
|
|
*/ |
142
|
|
|
public function update(DedicatedServer $dedicatedServer, array $attributes): void |
143
|
|
|
{ |
144
|
|
|
$attributes['ip'] = array_filter($attributes['ip'], function ($value) { |
145
|
|
|
return !empty($value); |
146
|
|
|
}); |
147
|
|
|
|
148
|
|
|
$dedicatedServer->update($attributes); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|