Total Complexity | 44 |
Total Lines | 389 |
Duplicated Lines | 0 % |
Changes | 6 | ||
Bugs | 0 | Features | 0 |
Complex classes like Firewall often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Firewall, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
52 | class Firewall |
||
53 | { |
||
54 | use FirewallTrait; |
||
55 | use XssProtectionTrait; |
||
56 | use MessengerTrait; |
||
57 | |||
58 | /** |
||
59 | * Collection of PSR-7 or PSR-15 middlewares. |
||
60 | * |
||
61 | * @var array |
||
62 | */ |
||
63 | protected $middlewares = []; |
||
64 | |||
65 | /** |
||
66 | * Constructor. |
||
67 | */ |
||
68 | public function __construct(?ServerRequestInterface $request = null, ?ResponseInterface $response = null) |
||
69 | { |
||
70 | Container::set('firewall', $this); |
||
71 | |||
72 | $this->kernel = new Kernel($request, $response); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Set up the path of the configuration file. |
||
77 | * |
||
78 | * @param string $source The path. |
||
79 | * @param string $type The type. |
||
80 | * |
||
81 | * @return void |
||
82 | */ |
||
83 | public function configure(string $source, string $type = 'json') |
||
84 | { |
||
85 | if ($type === 'json') { |
||
86 | $this->directory = rtrim($source, '\\/'); |
||
87 | $configFilePath = $this->directory . '/' . $this->filename; |
||
88 | |||
89 | if (file_exists($configFilePath)) { |
||
90 | $jsonString = file_get_contents($configFilePath); |
||
91 | |||
92 | } else { |
||
93 | $jsonString = file_get_contents(__DIR__ . '/../../config.json'); |
||
94 | |||
95 | if (defined('PHP_UNIT_TEST')) { |
||
96 | $jsonString = file_get_contents(__DIR__ . '/../../tests/config.json'); |
||
97 | } |
||
98 | } |
||
99 | |||
100 | $this->configuration = json_decode($jsonString, true); |
||
101 | $this->kernel->managedBy('managed'); |
||
102 | |||
103 | } elseif ($type === 'php') { |
||
104 | $this->configuration = require $source; |
||
105 | $this->kernel->managedBy('config'); |
||
106 | } |
||
107 | |||
108 | $this->setup(); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Add middlewares and use them before going into Shieldon kernal. |
||
113 | * |
||
114 | * @param MiddlewareInterface $middleware A PSR-15 middlewares. |
||
115 | * |
||
116 | * @return void |
||
117 | */ |
||
118 | public function add(MiddlewareInterface $middleware) |
||
119 | { |
||
120 | $this->middlewares[] = $middleware; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Setup everything we need. |
||
125 | * |
||
126 | * @return void |
||
127 | */ |
||
128 | public function setup(): void |
||
129 | { |
||
130 | $this->status = $this->getOption('daemon'); |
||
131 | |||
132 | $this->setDriver(); |
||
133 | |||
134 | $this->setChannel(); |
||
135 | |||
136 | $this->setIpSource(); |
||
137 | |||
138 | $this->setLogger(); |
||
139 | |||
140 | $this->setCoreModules(); |
||
141 | |||
142 | $this->setSessionLimit(); |
||
143 | |||
144 | $this->setCronJob(); |
||
145 | |||
146 | $this->setExcludedUrls(); |
||
147 | |||
148 | $this->setXssProtection(); |
||
149 | |||
150 | $this->setAuthentication(); |
||
151 | |||
152 | $this->setDialogUI(); |
||
153 | |||
154 | $this->setMessengers(); |
||
155 | |||
156 | $this->setMessageEvents(); |
||
157 | |||
158 | $this->setDenyAttempts(); |
||
159 | |||
160 | $this->setIptablesWatchingFolder(); |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Just, run! |
||
165 | * |
||
166 | * @return ResponseInterface |
||
167 | */ |
||
168 | public function run(): ResponseInterface |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Set the channel ID. |
||
209 | * |
||
210 | * @return void |
||
211 | */ |
||
212 | protected function setChannel(): void |
||
213 | { |
||
214 | $channelId = $this->getOption('channel_id'); |
||
215 | |||
216 | if ($channelId) { |
||
217 | $this->kernel->setChannel($channelId); |
||
218 | } |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Set up the action logger. |
||
223 | * |
||
224 | * @return void |
||
225 | */ |
||
226 | protected function setLogger(): void |
||
227 | { |
||
228 | $loggerSetting = $this->getOption('action', 'loggers'); |
||
229 | |||
230 | if ($loggerSetting['enable']) { |
||
231 | if (!empty($loggerSetting['config']['directory_path'])) { |
||
232 | $this->kernel->add(new ActionLogger($loggerSetting['config']['directory_path'])); |
||
233 | } |
||
234 | } |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * If you use CDN, please choose the real IP source. |
||
239 | * |
||
240 | * @return void |
||
241 | */ |
||
242 | protected function setIpSource(): void |
||
243 | { |
||
244 | $ipSourceType = $this->getOption('ip_variable_source'); |
||
245 | $serverParams = get_request()->getServerParams(); |
||
246 | |||
247 | /** |
||
248 | * REMOTE_ADDR: general |
||
249 | * HTTP_CF_CONNECTING_IP: Cloudflare |
||
250 | * HTTP_X_FORWARDED_FOR: Google Cloud CDN, Google Load-balancer, AWS. |
||
251 | * HTTP_X_FORWARDED_HOST: KeyCDN, or other CDN providers not listed here. |
||
252 | * |
||
253 | */ |
||
254 | $key = array_search(true, $ipSourceType); |
||
255 | $ip = $serverParams[$key]; |
||
256 | |||
257 | if (empty($ip)) { |
||
258 | // @codeCoverageIgnoreStart |
||
259 | throw new RuntimeException('IP source is not set correctly.'); |
||
260 | // @codeCoverageIgnoreEnd |
||
261 | } |
||
262 | |||
263 | $this->kernel->setIp($ip); |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * Set deny attempts. |
||
268 | * |
||
269 | * @return void |
||
270 | */ |
||
271 | protected function setDenyAttempts(): void |
||
272 | { |
||
273 | $setting = $this->getOption('failed_attempts_in_a_row', 'events'); |
||
274 | |||
275 | $enableDataCircle = $setting['data_circle']['enable'] ?: false; |
||
276 | $enableSystemFirewall = $setting['system_firewall']['enable'] ?: false; |
||
277 | |||
278 | $this->kernel->setProperty('deny_attempt_enable', [ |
||
279 | 'data_circle' => $enableDataCircle, |
||
280 | 'system_firewall' => $enableSystemFirewall, |
||
281 | ]); |
||
282 | |||
283 | $this->kernel->setProperty('deny_attempt_buffer', [ |
||
284 | 'data_circle' => $setting['data_circle']['buffer'] ?? 10, |
||
285 | 'system_firewall' => $setting['data_circle']['buffer'] ?? 10, |
||
286 | ]); |
||
287 | |||
288 | // Check the time of the last failed attempt. @since 0.2.0 |
||
289 | $recordAttempt = $this->getOption('record_attempt'); |
||
290 | |||
291 | $detectionPeriod = $recordAttempt['detection_period'] ?? 5; |
||
292 | $timeToReset = $recordAttempt['time_to_reset'] ?? 1800; |
||
293 | |||
294 | $this->kernel->setProperty('record_attempt_detection_period', $detectionPeriod); |
||
295 | $this->kernel->setProperty('reset_attempt_counter', $timeToReset); |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Set iptables working folder. |
||
300 | * |
||
301 | * @return void |
||
302 | */ |
||
303 | protected function setIptablesWatchingFolder(): void |
||
304 | { |
||
305 | $iptablesSetting = $this->getOption('config', 'iptables'); |
||
306 | $this->kernel->setProperty('iptables_watching_folder', $iptablesSetting['watching_folder']); |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * Set the online session limit. |
||
311 | * |
||
312 | * @return void |
||
313 | */ |
||
314 | protected function setSessionLimit(): void |
||
315 | { |
||
316 | $sessionLimitSetting = $this->getOption('online_session_limit'); |
||
317 | |||
318 | if ($sessionLimitSetting['enable']) { |
||
319 | |||
320 | $onlineUsers = $sessionLimitSetting['config']['count'] ?? 100; |
||
321 | $alivePeriod = $sessionLimitSetting['config']['period'] ?? 300; |
||
322 | |||
323 | $this->kernel->limitSession($onlineUsers, $alivePeriod); |
||
324 | } |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * Set the cron job. |
||
329 | * This is triggered by the pageviews, not system cron job. |
||
330 | * |
||
331 | * @return void |
||
332 | */ |
||
333 | protected function setCronJob(): void |
||
334 | { |
||
335 | if (!$this->status) { |
||
336 | return; |
||
337 | } |
||
338 | |||
339 | $cronjobSetting = $this->getOption('reset_circle', 'cronjob'); |
||
340 | |||
341 | if ($cronjobSetting['enable']) { |
||
342 | |||
343 | $nowTime = time(); |
||
344 | |||
345 | $lastResetTime = $cronjobSetting['config']['last_update']; |
||
346 | |||
347 | if (!empty($lastResetTime) ) { |
||
348 | $lastResetTime = strtotime($lastResetTime); |
||
349 | } else { |
||
350 | // @codeCoverageIgnoreStart |
||
351 | $lastResetTime = strtotime(date('Y-m-d 00:00:00')); |
||
352 | // @codeCoverageIgnoreEnd |
||
353 | } |
||
354 | |||
355 | if (($nowTime - $lastResetTime) > $cronjobSetting['config']['period']) { |
||
356 | |||
357 | $updateResetTime = date('Y-m-d 00:00:00'); |
||
358 | |||
359 | // Update new reset time. |
||
360 | $this->setConfig('cronjob.reset_circle.config.last_update', $updateResetTime); |
||
361 | $this->updateConfig(); |
||
362 | |||
363 | // Remove all logs. |
||
364 | $this->kernel->driver->rebuild(); |
||
365 | } |
||
366 | } |
||
367 | } |
||
368 | |||
369 | /** |
||
370 | * Set the URLs that want to be excluded from Shieldon protection. |
||
371 | * |
||
372 | * @return void |
||
373 | */ |
||
374 | protected function setExcludedUrls(): void |
||
375 | { |
||
376 | $excludedUrls = $this->getOption('excluded_urls'); |
||
377 | |||
378 | if (!empty($excludedUrls)) { |
||
379 | $list = array_column($excludedUrls, 'url'); |
||
380 | |||
381 | $this->kernel->setExcludedUrls($list); |
||
382 | } |
||
383 | } |
||
384 | |||
385 | /** |
||
386 | * WWW-Athentication. |
||
387 | * |
||
388 | * @return void |
||
389 | */ |
||
390 | protected function setAuthentication(): void |
||
391 | { |
||
392 | $authenticateList = $this->getOption('www_authenticate'); |
||
393 | |||
394 | if (is_array($authenticateList)) { |
||
395 | $this->add(new Middleware\httpAuthentication($authenticateList)); |
||
396 | } |
||
397 | } |
||
398 | |||
399 | /** |
||
400 | * Apply the denied list and the allowed list to Ip Component. |
||
401 | */ |
||
402 | protected function applyComponentIpManager() |
||
403 | { |
||
404 | $ipList = $this->getOption('ip_manager'); |
||
405 | |||
406 | $allowedList = []; |
||
407 | $deniedList = []; |
||
408 | |||
409 | if (is_array($ipList)) { |
||
410 | foreach ($ipList as $ip) { |
||
411 | |||
412 | if (0 === strpos($this->kernel->getCurrentUrl(), $ip['url']) ) { |
||
413 | |||
414 | if ('allow' === $ip['rule']) { |
||
415 | $allowedList[] = $ip['ip']; |
||
416 | } |
||
417 | |||
418 | if ('deny' === $ip['rule']) { |
||
419 | $deniedList[] = $ip['ip']; |
||
420 | } |
||
421 | } |
||
422 | } |
||
423 | } |
||
424 | |||
425 | $this->kernel->component['Ip']->setAllowedItems($allowedList); |
||
426 | $this->kernel->component['Ip']->setDeniedItems($deniedList); |
||
427 | } |
||
428 | |||
429 | /** |
||
430 | * Set dialog UI. |
||
431 | * |
||
432 | * @return void |
||
433 | */ |
||
434 | protected function setDialogUI() |
||
441 | } |
||
442 | } |
||
443 | } |
||
444 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths