1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Torralbodavid\DuckFunkCore\Http\RCON; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
|
7
|
|
|
class RCONSocket |
8
|
|
|
{ |
9
|
|
|
protected $socket; |
10
|
|
|
protected $command; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* RCONSocket constructor. |
14
|
|
|
* @param array $command |
15
|
|
|
*/ |
16
|
|
|
public function __construct(array $command) |
17
|
|
|
{ |
18
|
|
|
try { |
19
|
|
|
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); |
20
|
|
|
} catch (Exception $exception) { |
21
|
|
|
return response()->json( |
|
|
|
|
22
|
|
|
[ |
23
|
|
|
'connection' => 'failed', |
24
|
|
|
'error' => $exception->getMessage(), |
25
|
|
|
] |
26
|
|
|
); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
if ($socket === false) { |
30
|
|
|
return response()->json( |
|
|
|
|
31
|
|
|
[ |
32
|
|
|
'connection' => 'failed', |
33
|
|
|
'error' => socket_strerror(socket_last_error()), |
34
|
|
|
] |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
try { |
39
|
|
|
$result = socket_connect($socket, config('duck-funk.host_rcon'), config('duck-funk.port_rcon')); |
40
|
|
|
} catch (Exception $exception) { |
41
|
|
|
return response()->json( |
|
|
|
|
42
|
|
|
[ |
43
|
|
|
'connection' => 'failed', |
44
|
|
|
'error' => $exception->getMessage(), |
45
|
|
|
] |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if ($result === false) { |
50
|
|
|
return response()->json( |
|
|
|
|
51
|
|
|
[ |
52
|
|
|
'connection' => 'failed', |
53
|
|
|
'error' => 'The socket connection does not exist', |
54
|
|
|
] |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$command['auth_key'] = config('duck-funk.auth_key_rcon'); |
59
|
|
|
$this->command = json_encode($command); |
60
|
|
|
$this->socket = $socket; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function run() |
64
|
|
|
{ |
65
|
|
|
try { |
66
|
|
|
$this->sendCommand(); |
67
|
|
|
$this->checkCommandReceived(); |
68
|
|
|
$this->destruct(); |
69
|
|
|
} catch (Exception $exception) { |
70
|
|
|
return response()->json( |
|
|
|
|
71
|
|
|
[ |
72
|
|
|
'connection' => 'failed', |
73
|
|
|
'error' => $exception->getMessage(), |
74
|
|
|
] |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return response()->json( |
79
|
|
|
[ |
80
|
|
|
'connection' => 'success', |
81
|
|
|
] |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function sendCommand() |
86
|
|
|
{ |
87
|
|
|
if (socket_write($this->socket, $this->command, strlen($this->command)) === false) { |
88
|
|
|
echo socket_strerror(socket_last_error($this->socket)); |
89
|
|
|
|
90
|
|
|
return false; |
91
|
|
|
} else { |
92
|
|
|
return true; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
private function checkCommandReceived() |
97
|
|
|
{ |
98
|
|
|
return socket_read($this->socket, strlen($this->command)); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
private function destruct() |
102
|
|
|
{ |
103
|
|
|
socket_close($this->socket); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: