|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Gameap\Services; |
|
4
|
|
|
|
|
5
|
|
|
use GameQ\GameQ; |
|
6
|
|
|
use Gameap\Models\Server; |
|
7
|
|
|
use Knik\Gameap\GdaemonCommands; |
|
8
|
|
|
use Html; |
|
9
|
|
|
use Gameap\Exceptions\Services\InvalidCommandException; |
|
10
|
|
|
use Gameap\Exceptions\Services\ServerInactiveException; |
|
11
|
|
|
use Storage; |
|
12
|
|
|
|
|
13
|
|
|
class ServerService |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var GameQ |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $gameq; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var GdaemonCommands |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $gdaemonCommands; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $storageDisk = 'local'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* ServerService constructor. |
|
32
|
|
|
* |
|
33
|
|
|
* @param GameQ $gameq |
|
34
|
|
|
* @param GdaemonCommands $gdaemonCommands |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(GameQ $gameq, GdaemonCommands $gdaemonCommands) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->gameq = $gameq; |
|
39
|
|
|
$this->gdaemonCommands = $gdaemonCommands; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param Server $server |
|
44
|
|
|
* @return array |
|
45
|
|
|
*/ |
|
46
|
|
|
public function query(Server $server) |
|
47
|
|
|
{ |
|
48
|
|
|
$query = $this->gameq->setOption('timeout', 5) |
|
49
|
|
|
->addServer([ |
|
50
|
|
|
'type' => $server->game->engine, |
|
51
|
|
|
'host' => "{$server->server_ip}:{$server->query_port}", |
|
52
|
|
|
]) |
|
53
|
|
|
->process(); |
|
54
|
|
|
|
|
55
|
|
|
$serverResult = $query["{$server->server_ip}:{$server->query_port}"]; |
|
56
|
|
|
|
|
57
|
|
|
if ($serverResult['gq_online']) { |
|
58
|
|
|
$result = [ |
|
59
|
|
|
'status' => $serverResult['gq_online'] ? 'online' : 'offline', |
|
60
|
|
|
'hostname' => $serverResult['gq_hostname'], |
|
61
|
|
|
'map' => $serverResult['gq_mapname'], |
|
62
|
|
|
'players' => $serverResult['gq_numplayers'] . '/' . $serverResult['gq_maxplayers'], |
|
63
|
|
|
'version' => isset($serverResult['version']) ? $serverResult['version'] : null, |
|
64
|
|
|
'password' => $serverResult['gq_password'] ? 'yes' : 'no', |
|
65
|
|
|
'joinlink' => $serverResult['gq_joinlink'], |
|
66
|
|
|
]; |
|
67
|
|
|
} else { |
|
68
|
|
|
$result = [ |
|
69
|
|
|
'status' => 'offline', |
|
70
|
|
|
]; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $result; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param Server $server |
|
78
|
|
|
* @param string $command |
|
79
|
|
|
* @param array $extraData |
|
80
|
|
|
* @return string |
|
81
|
|
|
*/ |
|
82
|
|
|
public function replaceShortCodes(Server $server, string $command, array $extraData = []) |
|
83
|
|
|
{ |
|
84
|
|
|
foreach ($extraData as $key => $value) { |
|
85
|
|
|
$command = str_replace('{' . $key . '}', $value, $command); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$replaceArray = [ |
|
89
|
|
|
'host' => $server->server_ip, |
|
90
|
|
|
'port' => $server->server_port, |
|
91
|
|
|
'query_port' => $server->query_port, |
|
92
|
|
|
'rcon_port' => $server->rcon_port, |
|
93
|
|
|
'dir' => $server->full_path, |
|
94
|
|
|
'uuid' => $server->uuid, |
|
95
|
|
|
'uuid_short' => $server->uuid_short, |
|
96
|
|
|
'game' => $server->game_id, |
|
97
|
|
|
'user' => $server->su_user, |
|
98
|
|
|
]; |
|
99
|
|
|
|
|
100
|
|
|
foreach ($replaceArray as $key => $value) { |
|
101
|
|
|
$command = str_replace('{' . $key . '}', $value, $command); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $command; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param Server $server |
|
109
|
|
|
* @param string $command |
|
110
|
|
|
* @param array $extraData |
|
111
|
|
|
* @return string |
|
112
|
|
|
* |
|
113
|
|
|
* @throws InvalidCommandException |
|
114
|
|
|
*/ |
|
115
|
|
|
public function getCommand(Server $server, string $command, array $extraData = []) |
|
116
|
|
|
{ |
|
117
|
|
|
$property = 'script_' . $command; |
|
118
|
|
|
if (isset($server->dedicatedServer->$property)) { |
|
119
|
|
|
$script = $server->dedicatedServer->$property; |
|
120
|
|
|
return $this->replaceShortCodes($server, $script, $extraData); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
throw new InvalidCommandException(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param Server $server |
|
128
|
|
|
* @return string |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getConsoleLog(Server $server) |
|
131
|
|
|
{ |
|
132
|
|
|
$this->checkServer($server); |
|
133
|
|
|
$this->configureGdaemon($server); |
|
134
|
|
|
|
|
135
|
|
|
$command = $this->getCommand($server, 'get_console'); |
|
136
|
|
|
$result = $this->gdaemonCommands->exec($command, $exitCode); |
|
137
|
|
|
|
|
138
|
|
|
return $result; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @param Server $server |
|
143
|
|
|
* @param string $command |
|
144
|
|
|
* @return string |
|
145
|
|
|
*/ |
|
146
|
|
|
public function sendConsoleCommand(Server $server, string $command) |
|
147
|
|
|
{ |
|
148
|
|
|
$this->checkServer($server); |
|
149
|
|
|
$this->configureGdaemon($server); |
|
150
|
|
|
|
|
151
|
|
|
$command = $this->getCommand($server, 'send_command', ['command' => $command]); |
|
152
|
|
|
$result = $this->gdaemonCommands->exec($command, $exitCode); |
|
|
|
|
|
|
153
|
|
|
|
|
154
|
|
|
return $exitCode == 0 ? true: false; |
|
|
|
|
|
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Setting up gdaemon commands configuration |
|
159
|
|
|
* |
|
160
|
|
|
* @param Server $server |
|
161
|
|
|
*/ |
|
162
|
|
|
private function configureGdaemon(Server $server) |
|
163
|
|
|
{ |
|
164
|
|
|
$this->gdaemonCommands->setConfig([ |
|
165
|
|
|
'host' => $server->dedicatedServer->gdaemon_host, |
|
166
|
|
|
'port' => $server->dedicatedServer->gdaemon_port, |
|
167
|
|
|
'username' => $server->dedicatedServer->gdaemon_login, |
|
168
|
|
|
'password' => $server->dedicatedServer->gdaemon_password, |
|
169
|
|
|
|
|
170
|
|
|
'serverCertificate' => Storage::disk($this->storageDisk) |
|
171
|
|
|
->getDriver() |
|
172
|
|
|
->getAdapter() |
|
173
|
|
|
->applyPathPrefix($server->dedicatedServer->gdaemon_server_cert), |
|
174
|
|
|
|
|
175
|
|
|
'localCertificate' => Storage::disk($this->storageDisk) |
|
176
|
|
|
->getDriver() |
|
177
|
|
|
->getAdapter() |
|
178
|
|
|
->applyPathPrefix($server->dedicatedServer->clientCertificate->certificate), |
|
179
|
|
|
|
|
180
|
|
|
'privateKey' => Storage::disk($this->storageDisk) |
|
181
|
|
|
->getDriver() |
|
182
|
|
|
->getAdapter() |
|
183
|
|
|
->applyPathPrefix($server->dedicatedServer->clientCertificate->private_key), |
|
184
|
|
|
|
|
185
|
|
|
'privateKeyPass' => $server->dedicatedServer->clientCertificate->private_key_pass, |
|
186
|
|
|
'workDir' => $server->dedicatedServer->work_path, |
|
187
|
|
|
'timeout' => 10, |
|
188
|
|
|
]); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @param Server $server |
|
193
|
|
|
* @throws ServerInactiveException |
|
194
|
|
|
*/ |
|
195
|
|
|
private function checkServer(Server $server) |
|
196
|
|
|
{ |
|
197
|
|
|
if ($server->processActive() == false) { |
|
|
|
|
|
|
198
|
|
|
throw new ServerInactiveException('Server is down'); |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
} |