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
|
|
|
* php version 7.1.0 |
11
|
|
|
* |
12
|
|
|
* @category Web-security |
13
|
|
|
* @package Shieldon |
14
|
|
|
* @author Terry Lin <[email protected]> |
15
|
|
|
* @copyright 2019 terrylinooo |
16
|
|
|
* @license https://github.com/terrylinooo/shieldon/blob/2.x/LICENSE MIT |
17
|
|
|
* @link https://github.com/terrylinooo/shieldon |
18
|
|
|
* @see https://shieldon.io |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
declare(strict_types=1); |
22
|
|
|
|
23
|
|
|
namespace Shieldon\Firewall\Kernel; |
24
|
|
|
|
25
|
|
|
use Shieldon\Firewall\Component\ComponentInterface; |
26
|
|
|
use Shieldon\Firewall\Component\ComponentProvider; |
27
|
|
|
|
28
|
|
|
/* |
29
|
|
|
* @since 1.0.0 |
30
|
|
|
*/ |
31
|
|
|
trait ComponentTrait |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* Container for Shieldon components. |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
public $component = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Set a commponent. |
42
|
|
|
* |
43
|
|
|
* @param ComponentProvider $instance The component instance. |
44
|
|
|
* |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
|
|
public function setComponent(ComponentProvider $instance): void |
48
|
|
|
{ |
49
|
|
|
$class = $this->getClassName($instance); |
|
|
|
|
50
|
|
|
$this->component[$class] = $instance; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get a component instance from component's container. |
55
|
|
|
* |
56
|
|
|
* @param string $name The component's class name. |
57
|
|
|
* |
58
|
|
|
* @return ComponentInterface|null |
59
|
|
|
*/ |
60
|
|
|
public function getComponent(string $name) |
61
|
|
|
{ |
62
|
|
|
if (!isset($this->component[$name])) { |
63
|
|
|
return null; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $this->component[$name]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Disable components. |
71
|
|
|
* |
72
|
|
|
* @return void |
73
|
|
|
*/ |
74
|
|
|
public function disableComponents(): void |
75
|
|
|
{ |
76
|
|
|
$this->component = []; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/* |
80
|
|
|
|-------------------------------------------------------------------------- |
81
|
|
|
| Stage in Kernel |
82
|
|
|
|-------------------------------------------------------------------------- |
83
|
|
|
| The below methods are used in "process" method in Kernel. |
84
|
|
|
*/ |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Initialize components. |
88
|
|
|
* |
89
|
|
|
* @return void |
90
|
|
|
*/ |
91
|
|
|
protected function initComponents() |
92
|
|
|
{ |
93
|
|
|
foreach (array_keys($this->component) as $name) { |
94
|
|
|
$this->component[$name]->setIp($this->ip); |
95
|
|
|
$this->component[$name]->setRdns($this->rdns); |
96
|
|
|
|
97
|
|
|
// Apply global strict mode to all components by `strictMode()` if nesscessary. |
98
|
|
|
if (isset($this->strictMode)) { |
99
|
|
|
$this->component[$name]->setStrict($this->strictMode); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Check if current IP is trusted or not. |
106
|
|
|
* |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
|
|
protected function isTrustedBot() |
110
|
|
|
{ |
111
|
|
|
if ($this->getComponent('TrustedBot')) { |
112
|
|
|
|
113
|
|
|
// We want to put all the allowed robot into the rule list, so that the checking of IP's resolved hostname |
114
|
|
|
// is no more needed for that IP. |
115
|
|
|
if ($this->getComponent('TrustedBot')->isAllowed()) { |
|
|
|
|
116
|
|
|
|
117
|
|
|
if ($this->getComponent('TrustedBot')->isGoogle()) { |
|
|
|
|
118
|
|
|
// Add current IP into allowed list, because it is from real Google domain. |
119
|
|
|
$this->action( |
|
|
|
|
120
|
|
|
self::ACTION_ALLOW, |
|
|
|
|
121
|
|
|
self::REASON_IS_GOOGLE |
|
|
|
|
122
|
|
|
); |
123
|
|
|
|
124
|
|
|
} elseif ($this->getComponent('TrustedBot')->isBing()) { |
|
|
|
|
125
|
|
|
// Add current IP into allowed list, because it is from real Bing domain. |
126
|
|
|
$this->action( |
127
|
|
|
self::ACTION_ALLOW, |
128
|
|
|
self::REASON_IS_BING |
|
|
|
|
129
|
|
|
); |
130
|
|
|
|
131
|
|
|
} elseif ($this->getComponent('TrustedBot')->isYahoo()) { |
|
|
|
|
132
|
|
|
// Add current IP into allowed list, because it is from real Yahoo domain. |
133
|
|
|
$this->action( |
134
|
|
|
self::ACTION_ALLOW, |
135
|
|
|
self::REASON_IS_YAHOO |
|
|
|
|
136
|
|
|
); |
137
|
|
|
|
138
|
|
|
} else { |
139
|
|
|
// Add current IP into allowed list, because you trust it. |
140
|
|
|
// You have already defined it in the settings. |
141
|
|
|
$this->action( |
142
|
|
|
self::ACTION_ALLOW, |
143
|
|
|
self::REASON_IS_SEARCH_ENGINE |
|
|
|
|
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
// Allowed robots not join to our traffic handler. |
147
|
|
|
$this->result = self::RESPONSE_ALLOW; |
|
|
|
|
148
|
|
|
return true; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
return false; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Check whether the IP is fake search engine or not. |
156
|
|
|
* The method "isTrustedBot()" must be executed before this method. |
157
|
|
|
* |
158
|
|
|
* @return bool |
159
|
|
|
*/ |
160
|
|
|
protected function isFakeRobot(): bool |
161
|
|
|
{ |
162
|
|
|
if ($this->getComponent('TrustedBot')) { |
163
|
|
|
if ($this->getComponent('TrustedBot')->isFakeRobot()) { |
|
|
|
|
164
|
|
|
$this->action( |
165
|
|
|
self::ACTION_DENY, |
|
|
|
|
166
|
|
|
self::REASON_COMPONENT_TRUSTED_ROBOT |
|
|
|
|
167
|
|
|
); |
168
|
|
|
$this->result = self::RESPONSE_DENY; |
|
|
|
|
169
|
|
|
return true; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
return false; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Check whether the IP is handled by IP compoent or not. |
177
|
|
|
* |
178
|
|
|
* @return bool |
179
|
|
|
*/ |
180
|
|
|
protected function isIpComponent(): bool |
181
|
|
|
{ |
182
|
|
|
if ($this->getComponent('Ip')) { |
183
|
|
|
|
184
|
|
|
$result = $this->getComponent('Ip')->check($this->ip); |
|
|
|
|
185
|
|
|
|
186
|
|
|
$actionCode = self::ACTION_DENY; |
|
|
|
|
187
|
|
|
|
188
|
|
|
if (!empty($result)) { |
189
|
|
|
|
190
|
|
|
switch ($result['status']) { |
191
|
|
|
|
192
|
|
|
case 'allow': |
193
|
|
|
$actionCode = self::ACTION_ALLOW; |
|
|
|
|
194
|
|
|
$reasonCode = $result['code']; |
195
|
|
|
break; |
196
|
|
|
|
197
|
|
|
case 'deny': |
198
|
|
|
$actionCode = self::ACTION_DENY; |
199
|
|
|
$reasonCode = $result['code']; |
200
|
|
|
break; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
$this->action($actionCode, $reasonCode); |
|
|
|
|
204
|
|
|
|
205
|
|
|
// $resultCode = $actionCode |
206
|
|
|
$this->result = $this->sessionHandler($actionCode); |
|
|
|
|
207
|
|
|
return true; |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
return false; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Check other compoents. |
215
|
|
|
* |
216
|
|
|
* @return bool |
217
|
|
|
*/ |
218
|
|
|
protected function isComponents() |
219
|
|
|
{ |
220
|
|
|
foreach ($this->component as $component) { |
221
|
|
|
|
222
|
|
|
if ($component->isDenied()) { |
223
|
|
|
|
224
|
|
|
$this->action( |
225
|
|
|
self::ACTION_DENY, |
|
|
|
|
226
|
|
|
$component->getDenyStatusCode() |
227
|
|
|
); |
228
|
|
|
|
229
|
|
|
$this->result = self::RESPONSE_DENY; |
|
|
|
|
230
|
|
|
return true; |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
return false; |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|