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