-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigurationReader.php
More file actions
238 lines (206 loc) · 5.34 KB
/
Copy pathConfigurationReader.php
File metadata and controls
238 lines (206 loc) · 5.34 KB
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
<?php
declare(strict_types=1);
namespace WebSocket;
use Cake\Core\Configure;
use Cake\Utility\Hash;
use RuntimeException;
use WebSocket\Enum\WebSocketProtocol;
use WebSocket\Server\Timer;
/**
* Class ConfigurationReader
* Created by allancarvalho in dezembro 16, 2021
*/
class ConfigurationReader
{
/**
* @var \WebSocket\ConfigurationReader
*/
protected static ConfigurationReader $instance;
/**
* @var \WebSocket\Enum\WebSocketProtocol
*/
protected WebSocketProtocol $webSocketProtocol;
/**
* @var string|false
*/
protected string|false $proxy;
/**
* When proxy is setted:
* If true will use the proxy every time
* If false will use only when protocol is WSS
*
* @var bool
*/
protected bool $forceProxy;
/**
* @var string
*/
protected string $host;
/**
* @var string
*/
protected string $httpHost;
/**
* @var int
*/
protected int $port;
/**
* @var int
*/
protected int $maxClients;
/**
* @var int
*/
protected int $maxConnectionsPerIp;
/**
* @var bool
*/
protected bool $checkOrigin;
/**
* @var array
*/
protected array $allowedOrigins;
/**
* @var \WebSocket\Server\Timer[]
*/
protected array $timers;
/**
* Construct method
*/
public function __construct()
{
$defaultHttpHost = Hash::get($_SERVER, 'HTTP_HOST', '127.0.0.1');
$defaultWebSocketProtocol = Hash::get($_SERVER, 'REQUEST_SCHEME', '') === 'https' ?
WebSocketProtocol::WSS : WebSocketProtocol::WS;
/** @var array $configuration */
$configuration = Configure::read('WebSocket', []);
$this->webSocketProtocol = Hash::get($configuration, 'webSocketProtocol', $defaultWebSocketProtocol);
$this->proxy = Hash::get($configuration, 'proxy', false);
$this->forceProxy = Hash::get($configuration, 'forceProxy', false);
$this->host = Hash::get($configuration, 'host', '127.0.0.1');
$this->httpHost = Hash::get($configuration, 'httpHost', $defaultHttpHost);
$this->port = Hash::get($configuration, 'port', 8000);
$this->maxClients = Hash::get($configuration, 'maxClients', 30);
$this->maxConnectionsPerIp = Hash::get($configuration, 'maxConnectionsPerIp', 5);
$this->checkOrigin = Hash::get($configuration, 'checkOrigin', false);
$allowedOrigins = Hash::get($configuration, 'allowedOrigins', []);
$this->allowedOrigins = [];
foreach ($allowedOrigins as $domain) {
$domain = str_replace(['https://', 'http://', 'www.'], '', $domain);
/** @phpstan-ignore-next-line */
$domain = str_contains($domain, '/') ? substr($domain, 0, strpos($domain, '/')) : $domain;
if (empty($domain)) {
continue;
}
/** @var string $domain */
$this->allowedOrigins[$domain] = true;
}
$timers = Hash::get($configuration, 'timers', []);
foreach ($timers as $timer) {
if (!class_exists($timer)) {
throw new RuntimeException(sprintf(
'All timers must to be a FQN of a class that extends from "%s"!',
Timer::class
));
}
if (!is_subclass_of($timer, Timer::class)) {
throw new RuntimeException(sprintf('All timers must to be a extension from "%s"!', Timer::class));
}
}
$this->timers = $timers;
}
/**
* @return void
*/
public static function reloadInstance(): void
{
self::$instance = new ConfigurationReader();
}
/**
* @return self
*/
public static function getInstance(): ConfigurationReader
{
if (empty(self::$instance)) {
self::$instance = new ConfigurationReader();
}
return self::$instance;
}
/**
* @return \WebSocket\Enum\WebSocketProtocol
*/
public function getWebSocketProtocol(): WebSocketProtocol
{
return $this->webSocketProtocol;
}
/**
* @return false|string
*/
public function getProxy(): bool|string
{
return $this->proxy;
}
/**
* @return bool
*/
public function isForceProxy(): mixed
{
return $this->forceProxy;
}
/**
* @return string
*/
public function getHost(): string
{
return $this->host;
}
/**
* @return string
*/
public function getHttpHost(): string
{
return $this->httpHost;
}
/**
* @return int
*/
public function getPort(): int
{
return $this->port;
}
/**
* @return int
*/
public function getMaxClients(): int
{
return $this->maxClients;
}
/**
* @return int
*/
public function getMaxConnectionsPerIp(): int
{
return $this->maxConnectionsPerIp;
}
/**
* @return bool
*/
public function isCheckOrigin(): bool
{
return $this->checkOrigin;
}
/**
* @return array
*/
public function getAllowedOrigins(): array
{
return $this->allowedOrigins;
}
/**
* @return \WebSocket\Server\Timer[]
*/
public function getTimers(): mixed
{
return $this->timers;
}
}