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