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