1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Shieldon package. |
4
|
|
|
* |
5
|
|
|
* (c) Terry L. <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace Shieldon\Firewall\Firewall; |
14
|
|
|
|
15
|
|
|
use Shieldon\Firewall\Firewall\Driver\DriverFactory; |
16
|
|
|
use Shieldon\Firewall\Firewall\Captcha\CaptchaFactory; |
17
|
|
|
|
18
|
|
|
/* |
19
|
|
|
* Main Trait for Firwall class. |
20
|
|
|
*/ |
21
|
|
|
trait MainTrait |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Fetch value from configuration. |
25
|
|
|
* |
26
|
|
|
* @param string $option |
27
|
|
|
* @param string $section |
28
|
|
|
* |
29
|
|
|
* @return mixed |
30
|
|
|
*/ |
31
|
|
|
abstract function getOption(string $option, string $section = ''); |
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Set a data driver for the use of Shiedon Firewall. |
35
|
|
|
* Currently supports File, Redis, MySQL and SQLite. |
36
|
|
|
* |
37
|
|
|
* @return void |
38
|
|
|
*/ |
39
|
|
|
protected function setDriver(): void |
40
|
|
|
{ |
41
|
|
|
$driverType = $this->getOption('driver_type'); |
42
|
|
|
$driverSetting = $this->getOption($driverType, 'drivers'); |
43
|
|
|
|
44
|
|
|
if (isset($driverSetting['directory_path'])) { |
45
|
|
|
$driverSetting['directory_path'] = $driverSetting['directory_path'] ?: $this->directory; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$driverInstance = DriverFactory::getInstance($driverType, $driverSetting); |
49
|
|
|
|
50
|
|
|
$this->status = false; |
|
|
|
|
51
|
|
|
if ($driverInstance !== null) { |
52
|
|
|
$this->kernel->add($driverInstance); |
53
|
|
|
$this->status = true; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Filters |
59
|
|
|
* |
60
|
|
|
* (1) Session. |
61
|
|
|
* (2) Cookie generated by JavaScript code. |
62
|
|
|
* (3) HTTP referrer information. |
63
|
|
|
* (4) Pageview frequency. |
64
|
|
|
* |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
|
|
protected function setFilters(): void |
68
|
|
|
{ |
69
|
|
|
$sessionSetting = $this->getOption('session', 'filters'); |
70
|
|
|
$cookieSetting = $this->getOption('cookie', 'filters'); |
71
|
|
|
$refererSetting = $this->getOption('referer', 'filters'); |
72
|
|
|
$frequencySetting = $this->getOption('frequency', 'filters'); |
73
|
|
|
|
74
|
|
|
$filterConfig = [ |
75
|
|
|
'session' => $sessionSetting['enable'], |
76
|
|
|
'cookie' => $cookieSetting['enable'], |
77
|
|
|
'referer' => $refererSetting['enable'], |
78
|
|
|
'frequency' => $frequencySetting['enable'], |
79
|
|
|
]; |
80
|
|
|
|
81
|
|
|
$this->kernel->setFilters($filterConfig); |
82
|
|
|
|
83
|
|
|
$this->kernel->setProperty('limit_unusual_behavior', [ |
84
|
|
|
'session' => $sessionSetting['config']['quota'] ?? 5, |
85
|
|
|
'cookie' => $cookieSetting['config']['quota'] ?? 5, |
86
|
|
|
'referer' => $refererSetting['config']['quota'] ?? 5, |
87
|
|
|
]); |
88
|
|
|
|
89
|
|
|
// if ($frequencySetting['enable']) { |
90
|
|
|
$frequencyQuota = [ |
91
|
|
|
's' => $frequencySetting['config']['quota_s'] ?? 2, |
92
|
|
|
'm' => $frequencySetting['config']['quota_m'] ?? 10, |
93
|
|
|
'h' => $frequencySetting['config']['quota_h'] ?? 30, |
94
|
|
|
'd' => $frequencySetting['config']['quota_d'] ?? 60, |
95
|
|
|
]; |
96
|
|
|
|
97
|
|
|
$this->kernel->setProperty('time_unit_quota', $frequencyQuota); |
98
|
|
|
|
99
|
|
|
// if ($cookieSetting['enable']) { |
100
|
|
|
$cookieName = $cookieSetting['config']['cookie_name'] ?? 'ssjd'; |
101
|
|
|
$cookieDomain = $cookieSetting['config']['cookie_domain'] ?? ''; |
102
|
|
|
$cookieValue = $cookieSetting['config']['cookie_value'] ?? '1'; |
103
|
|
|
|
104
|
|
|
$this->kernel->setProperty('cookie_name', $cookieName); |
105
|
|
|
$this->kernel->setProperty('cookie_domain', $cookieDomain); |
106
|
|
|
$this->kernel->setProperty('cookie_value', $cookieValue); |
107
|
|
|
|
108
|
|
|
// if ($refererSetting['enable']) { |
109
|
|
|
$this->kernel->setProperty('interval_check_referer', $refererSetting['config']['time_buffer']); |
110
|
|
|
|
111
|
|
|
// if ($sessionSetting['enable']) { |
112
|
|
|
$this->kernel->setProperty('interval_check_session', $sessionSetting['config']['time_buffer']); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Components |
117
|
|
|
* |
118
|
|
|
* (1) Ip |
119
|
|
|
* (2) Rdns |
120
|
|
|
* (3) Header |
121
|
|
|
* (4) User-agent |
122
|
|
|
* (5) Trusted bot |
123
|
|
|
* |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
|
|
protected function setComponents(): void |
127
|
|
|
{ |
128
|
|
|
$componentConfig = [ |
129
|
|
|
'Ip' => $this->getOption('ip', 'components'), |
130
|
|
|
'Rdns' => $this->getOption('rdns', 'components'), |
131
|
|
|
'Header' => $this->getOption('header', 'components'), |
132
|
|
|
'UserAgent' => $this->getOption('user_agent', 'components'), |
133
|
|
|
'TrustedBot' => $this->getOption('trusted_bot', 'components'), |
134
|
|
|
]; |
135
|
|
|
|
136
|
|
|
foreach ($componentConfig as $className => $config) { |
137
|
|
|
$class = 'Shieldon\Firewall\Component\\' . $className; |
138
|
|
|
|
139
|
|
|
if ($config['enable']) { |
140
|
|
|
$componentInstance = new $class(); |
141
|
|
|
|
142
|
|
|
if ($className === 'Ip') { |
143
|
|
|
$this->kernel->add($componentInstance); |
144
|
|
|
|
145
|
|
|
// Need Ip component to be loaded before calling this method. |
146
|
|
|
$this->applyComponentIpManager(); |
|
|
|
|
147
|
|
|
|
148
|
|
|
} elseif ($config['strict_mode']) { |
149
|
|
|
$componentInstance->setStrict(true); |
150
|
|
|
$this->kernel->add($componentInstance); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Captcha modules. |
158
|
|
|
* |
159
|
|
|
* (1) Google ReCaptcha |
160
|
|
|
* (2) Simple image captcha. |
161
|
|
|
* |
162
|
|
|
* @return void |
163
|
|
|
*/ |
164
|
|
|
protected function setCaptchas(): void |
165
|
|
|
{ |
166
|
|
|
$captchaList = [ |
167
|
|
|
'recaptcha', |
168
|
|
|
'image', |
169
|
|
|
]; |
170
|
|
|
|
171
|
|
|
foreach ($captchaList as $captcha) { |
172
|
|
|
$setting = $this->getOption($captcha, 'captcha_modules'); |
173
|
|
|
|
174
|
|
|
if (is_array($setting)) { |
175
|
|
|
|
176
|
|
|
// Initialize messenger instances from the factory/ |
177
|
|
|
if (CaptchaFactory::check($captcha, $setting)) { |
178
|
|
|
|
179
|
|
|
$this->kernel->add( |
180
|
|
|
CaptchaFactory::getInstance( |
181
|
|
|
// The ID of the captcha module in the configuration. |
182
|
|
|
$captcha, |
183
|
|
|
// The settings of the captcha module in the configuration. |
184
|
|
|
$setting |
185
|
|
|
) |
186
|
|
|
); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
unset($setting); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.