-
Notifications
You must be signed in to change notification settings - Fork 1
/
gtmp
425 lines (382 loc) · 13.1 KB
/
gtmp
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
#!/usr/bin/php
<?php
/* _____ _______ __ __ _____
/ ____|__ __| | \/ | __ \
| | __ | |______| \ / | |__) |
| | |_ | | |______| |\/| | ___/
| |__| | | | | | | | |
\_____| |_| |_| |_|_| Administrator Script
Author: Hamza Akiour (www.hamza.es)
This project is licensed under the GNU (General Public License)
*/
//General vars - CONFIGURE ON FILE 'gtmp_config'
$GLOBALS['path'] = "";
$GLOBALS['instance_name'] = "";
$GLOBALS['executable'] = "";
$GLOBALS['savelog'] = "";
$GLOBALS['alwaysSafeStart'] = "";
$GLOBALS['timestampOnLog'] = "";
$GLOBALS['alwaysSafeStop'] = "";
//Check if we cast the program with minimum one argument
if(count($argv) < 2)
castError("000");
$selected_option = $argv[1];
//Check if the configuration file exist, if not, generate a new one
if(!file_exists("gtmp_config")){
echo "'gtmp_config' not found, generating new 'gtmp_config'\n";
generateNewConfigurationFile();
}
//Get the parameters and values from configuration file
$configuration_file = fopen('gtmp_config','r');
if(!$configuration_file)
castError("008");
while ($line = fgets($configuration_file)) {
if(substr($line,0,1) == "$"){
$parameter_pos = strpos($line , "=");
if ($parameter_pos)
{
$parameter_name = trim(substr($line , 0 ,$parameter_pos - 1));
$parameter_value = trim(substr($line , $parameter_pos + 1));
if(trim($parameter_value) <> ""){
switch ($parameter_name) {
case '$path':
$GLOBALS['path'] = $parameter_value;
break;
case '$instance_name':
$GLOBALS['instance_name'] = $parameter_value;
break;
case '$executable':
$GLOBALS['executable'] = $parameter_value;
break;
case '$savelog':
$GLOBALS['savelog'] = $parameter_value;
break;
case '$timestampOnLog':
$GLOBALS['timestampOnLog'] = $parameter_value;
break;
case '$alwaysSafeStart':
$GLOBALS['alwaysSafeStart'] = $parameter_value;
break;
case '$alwaysSafeStop':
$GLOBALS['alwaysSafeStop'] = $parameter_value;
break;
}
}
}
}
}
fclose($configuration_file);
//Check if we could load parameters from configuration file
if (trim($GLOBALS['path']) == "" || trim($GLOBALS['instance_name']) == "" || trim($GLOBALS['executable']) == "")
castError("007");
//Check if the executable exist
if (!file_exists("{$GLOBALS['path']}/{$GLOBALS['executable']}"))
castError("001");
//Procces the given option
$selected_option = strtolower($selected_option);
if($selected_option == "start" && ($GLOBALS['alwaysSafeStart'] == "y" || existThisParameter("-f", $argv))){
if ($GLOBALS['alwaysSafeStart'] == "y")
echo "safestart option activated on 'gtmp_config' file\n";
else
echo "forcing safestart\n";
$selected_option = "safestart";
}
if($selected_option == "stop" && ($GLOBALS['alwaysSafeStop'] == "y") || existThisParameter("-f", $argv)){
if ($GLOBALS['alwaysSafeStop'] == "y")
echo "safestop option activated on 'gtmp_config' file\n";
else
echo "forcing safestop\n";
$selected_option = "safestop";
}
$status = processStatus("[m]ono {$GLOBALS['executable']}");
switch ($selected_option) {
case "start":
if ($status)
castError("004");
if (!startServer($argv))
castError("005");
echo "Server started.\n";
break;
case "stop":
if (!$status)
castError("003");
if (!stopServer())
castError("006");
echo "Server stopped.\n";
break;
case "restart":
if (!$status)
castError("003");
echo "Restarting...\n";
if (!stopServer())
castError("006");
sleep(2);
if (!startServer($argv))
castError("005");
echo "Server restarted.\n";
break;
case "newconf":
if(file_exists("gtmp_config")){
`mv gtmp_config gtmp_config.old`;
}
generateNewConfigurationFile();
break;
case "safestart":
if ($status)
stopServer();
if (existScreenSession())
killAllScreenSessions();
if (!startServer($argv,"forceStart"))
castError("005");
break;
case "safestop":
if ($status)
stopServer();
if (existScreenSession())
killAllScreenSessions();
if (!isServerOn(true))
castError("006");
break;
case "-l": //In this case, we don't cast any error but keep the program runing
break;
default:
castError("000");
}
//Folow log
if(existThisParameter("-l", $argv))
{
echo "Live option detected (-l)\n";
if(file_exists("{$GLOBALS['path']}gtmp-server.log")){
echo "Showing the log...\n";
$logfile = popen("tail -f {$GLOBALS['path']}gtmp-server.log 2>&1", 'r');
while(!feof($logfile)) {
$buffer = fgets($logfile);
echo "$buffer";
}
pclose($logfile);
//`tail -f {$GLOBALS['path']}gtmp-server.log`;
}
else
castError("009");
}
/*
*Functions
*/
function existThisParameter($parameter, $argv)
{
for ($index = 0; $index < sizeof($argv); $index++)
{
if(strtolower($argv[$index]) == strtolower($parameter))
return true;
}
return false;
}
function getParameterFromUser($message,$defaultValue)
{
$answer = readline("{$message}");
$answer = trim($answer);
if($answer == ""){
$answer = $defaultValue;
}
return $answer;
}
function isYesOrNo($parameter, $default){
$parameter = strtolower(substr($parameter,0,1));
if($parameter <> "y" && $parameter <> "n"){
echo "Error, only can put 'y' or 'n', setting default value ('{$default}')\n";
$parameter = $default;
}
return $parameter;
}
function generateNewConfigurationFile()
{
echo "Leave empty the parameters for take automatically default value.\n";
do{
echo "-------------------------------------------------------\n";
$GLOBALS['path'] = getParameterFromUser("Introduce the path of your gt-mp folder (where you have the executable server file (Ex: /home/hakiour/gt-mp/)), recommended to no leave empty: ", getcwd());
if(substr($GLOBALS['path'], -1) <> "/")
$GLOBALS['path'] .= "/";
$GLOBALS['executable'] = getParameterFromUser("Introduce the name of your .exe (default: GrandTheftMultiplayer.Server.exe): ", "GrandTheftMultiplayer.Server.exe");
if (!file_exists("{$GLOBALS['path']}/{$GLOBALS['executable']}"))
{
echo "ERROR: the path ({$GLOBALS['path']}) or the executable ({$GLOBALS['executable']}) is incorrect.\n";
continue;
}
$GLOBALS['instance_name'] = getParameterFromUser("The name of the screen instance (if you don't know it, don't put anything): ", "gtav");
$GLOBALS['savelog'] = isYesOrNo(getParameterFromUser("Save logs for SCREEN sessions (recommended) (y/n): ", "y"), "y");
$GLOBALS['timestampOnLog'] = isYesOrNo(getParameterFromUser("Print timestamp on SCREEN sessions logs (y/n): ", "n"), "n");
$GLOBALS['alwaysSafeStart'] = isYesOrNo(getParameterFromUser("Always start on safemode (this kill the actual SCREEN sessions and start a new one): ", "y"), "y");
$GLOBALS['alwaysSafeStop'] = isYesOrNo(getParameterFromUser("Always stop on safemode (this kill all the gtmp SCREEN sessions): ", "y"), "y");
$answer = isYesOrNo(getParameterFromUser("Your path is: '{$GLOBALS['path']}', inside the path you have the executable server: '{$GLOBALS['executable']}', this run on the SCREEN instance '{$GLOBALS['instance_name']}', continue(y/n): ", "n"),"n");
if ($answer == "y")
break;
}while (true);
writeOnFile("gtmp_config",'$path = '.$GLOBALS['path']."\n".'$instance_name = '.$GLOBALS['instance_name']."\n".'$executable = '.$GLOBALS['executable']."\n".'$savelog = '.$GLOBALS['savelog']."\n".'$timestampOnLog = '.$GLOBALS['timestampOnLog']."\n".'$alwaysSafeStart = '.$GLOBALS['alwaysSafeStart']."\n".'$alwaysSafeStop = '.$GLOBALS['alwaysSafeStop']);
}
function launchScreenSession(){
if($GLOBALS['savelog'] == "y"){
$name= "gtmp-server";
if($GLOBALS['timestampOnLog'] == "y"){
$config= "logfile {$GLOBALS['path']}/{$name}.log\nlogfile flush 1\nlog on\nlogtstamp after 1\nlogtstamp string \"\\012-------%Y-%m-%d %c:%s-------\\012\"\nlogtstamp on";
}else{
$config= "logfile {$GLOBALS['path']}/{$name}.log\nlogfile flush 1\nlog on\nlogtstamp off";
}
writeOnFile("log.conf",$config,"/tmp/");
`screen -S {$GLOBALS["instance_name"]} -c /tmp/log.conf -dmSL`;
if (`rm -f /tmp/log.conf`)//If it's true, means we don't have powers for delete the file (or create it)
castError("010", "/tmp/log.conf");
}else
`screen -S {$GLOBALS["instance_name"]} -dm`;
}
function killAllScreenSessions()
{
//Generate the /tmp/ScreenSessions file
TotalScreenSessions();
//Get the ScreenID's
$screenSessionsFile = fopen('/tmp/ScreenSessions','r');
if(!$screenSessionsFile)
{
//Try to generate the ScreenSessions File
$screenSessionsFileGenerated = False;
for ($index = 0; $index < 10; $index++)
{
sleep(1);
TotalScreenSessions();
$screenSessionsFile = fopen('/tmp/ScreenSessions','r');
if($screenSessionsFile){
$screenSessionsFileGenerated = true;
break;
}
}
if (!$screenSessionsFileGenerated)
return false;
}
//Kill each screen session
while ($line = fgets($screenSessionsFile)) {
$screen_pos = strpos($line , $GLOBALS['instance_name']);
$screenID = trim(substr($line , 0 ,$screen_pos - 1));
stopScreenSession($screenID);
}
}
function TotalScreenSessions()
{
`rm -f /tmp/ScreenSessions 2> /dev/null`;
$screenLines = `screen -ls | grep -w "{$GLOBALS['instance_name']}"`;
writeOnFile("ScreenSessions",$screenLines,"/tmp/");
return `wc -l /tmp/ScreenSessions`;
}
function stopScreenSession($instanceID = "")
{
if ($instanceID == "")
$instanceID = $GLOBALS['instanceID'];
`screen -X -S {$instanceID} quit`;
}
function writeOnFile($file,$text,$path = ""){
$thisFile = @fopen($path.$file, "w") or castError("010", $path.$file);
fwrite($thisFile, $text);
fclose($thisFile);
}
function processStatus($grepThisProcess){
return `ps aux | grep "{$grepThisProcess}"` ? true : false;
}
function stopServer() {
echo "Shutting down the server...\n";
turnOffServer();
echo "Waiting while the server finish shutthing down.\n";
return isServerOn(false);
}
function existScreenSession()
{
return trim(`screen -ls | grep -w "{$GLOBALS['instance_name']}"`) <> "" ? true : false;
}
function startServer($argv,$extra = "") {
echo "Starting the server...\n";
sleep(1);//This sleep is because if we launch the server so fast the system can't take it
if (!existScreenSession())
{
if(existThisParameter("-f", $argv) || $extra == "forceStart")
{
launchScreenSession();
}
else
castError("002");
}
turnOnServer();
return isServerOn();
}
function turnOnServer(){
//For one bug on GT-MP start exe, first we need to move to the executable folder.
`screen -S gtav -p 0 -X stuff "cd {$GLOBALS['path']}\015"`;
`screen -S gtav -p 0 -X stuff "mono {$GLOBALS['executable']}\015"`;
}
function turnOffServer(){
`screen -S {$GLOBALS['instance_name']} -p 0 -X stuff ^C`;
}
function isServerOn($WannaSeeIfServerIsPowerOff = false){
$count = 0;
do {
$serverStatus = `ps aux | grep "[m]ono "{$GLOBALS['executable']}`;
switch ($WannaSeeIfServerIsPowerOff) {
case false:
if ($serverStatus)
{
return true;
}
break;
case true:
if (!$serverStatus)
{
return true;
}
break;
}
sleep(1);
$count++;
}while ($count <= 7);
return false;
}
function castError($error_code, $extra = ""){
//cast and error and exit the program
$messageError = "";
switch($error_code)
{
case "000":
$messageError = "Usage: {start|stop|restart|safestart|newconf}";
break;
case "001":
$messageError = "Error 001 - {$GLOBALS['executable']} not found, check the path route on 'gtmp_config' file.";
break;
case "002":
$messageError = "Error 002 - Can't find the SCREEN SESSION where the server is executed, if you want to start a new session call the command with the -f parameter ('gtmp start -f').";
break;
case "003":
$messageError = "Error 003 - The server isn't startet. Use 'gtmp start' for start the server.";
break;
case "004":
$messageError = "Error 004 - The server is already started.";
break;
case "005":
$messageError = "Error 005 - The server could not be started.";
break;
case "006":
$messageError = "Error 006 - The server could not be shut down.";
break;
case "007":
$messageError = "Error 007 - Bad configuration on 'gtmp_config' file.";
break;
case "008":
$messageError = "Error 008 - Can't read 'gtmp_config', permission denied).";
break;
case "009":
$messageError = "Error 009 - {$GLOBALS['path']}gtmp-Server.log file not found, check the configuration file ('gtmp_config').";
break;
case "010":
$messageError = "Error 010 - Can't access to {$extra}, insufficient permission. Please delete the file or change the permissions.";
break;
default:
$messageError = "Error 011 - Undefined error.";
break;
}
exit("{$messageError}\n");
}
?>