1 | <?php |
||
13 | class Firewall |
||
14 | { |
||
15 | /** |
||
16 | * The IP address. |
||
17 | * |
||
18 | * @var |
||
19 | */ |
||
20 | private $ip; |
||
21 | |||
22 | /** |
||
23 | * The config object. |
||
24 | * |
||
25 | * @var Config |
||
26 | */ |
||
27 | private $config; |
||
28 | |||
29 | /** |
||
30 | * The data repository object. |
||
31 | * |
||
32 | * @var DataRepository |
||
33 | */ |
||
34 | private $dataRepository; |
||
35 | |||
36 | /** |
||
37 | * The request. |
||
38 | * |
||
39 | * @var Request |
||
40 | */ |
||
41 | private $request; |
||
42 | |||
43 | /** |
||
44 | * The attack blocker. |
||
45 | * |
||
46 | * @var AttackBlocker |
||
47 | */ |
||
48 | private $attackBlocker; |
||
49 | /** |
||
50 | * @var Message |
||
51 | */ |
||
52 | private $messageRepository; |
||
53 | |||
54 | /** |
||
55 | * Initialize Firewall object. |
||
56 | * |
||
57 | * @param Config $config |
||
58 | * @param DataRepository $dataRepository |
||
59 | * @param Request $request |
||
60 | * @param AttackBlocker $attackBlocker |
||
61 | * @param Message $messageRepository |
||
62 | */ |
||
63 | 75 | public function __construct( |
|
82 | |||
83 | /** |
||
84 | * Get all IP addresses. |
||
85 | * |
||
86 | * @return \Illuminate\Support\Collection |
||
87 | */ |
||
88 | 2 | public function all() |
|
92 | |||
93 | /** |
||
94 | * Get all IP addresses by country. |
||
95 | * |
||
96 | * @param $country |
||
97 | * |
||
98 | * @return \Illuminate\Support\Collection |
||
99 | */ |
||
100 | 1 | public function allByCountry($country) |
|
104 | |||
105 | /** |
||
106 | * Blacklist an IP address. |
||
107 | * |
||
108 | * @param $ip |
||
109 | * @param bool $force |
||
110 | * |
||
111 | * @return bool |
||
112 | */ |
||
113 | 33 | public function blacklist($ip, $force = false) |
|
117 | |||
118 | /** |
||
119 | * Create a blocked access response. |
||
120 | * |
||
121 | * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse|null |
||
122 | */ |
||
123 | 4 | public function blockAccess() |
|
129 | |||
130 | /** |
||
131 | * Clear firewall table. |
||
132 | * |
||
133 | * @return mixed |
||
134 | */ |
||
135 | 10 | public function clear() |
|
139 | |||
140 | /** |
||
141 | * Find an IP address. |
||
142 | * |
||
143 | * @param string $ip |
||
144 | * |
||
145 | * @return mixed |
||
146 | */ |
||
147 | 3 | public function find($ip) |
|
151 | |||
152 | /** |
||
153 | * Get the IP address. |
||
154 | * |
||
155 | * @param null $ip |
||
156 | * |
||
157 | * @return null|string |
||
158 | */ |
||
159 | 48 | public function getIp($ip = null) |
|
163 | |||
164 | /** |
||
165 | * Get the messages. |
||
166 | * |
||
167 | * @return \Illuminate\Support\Collection |
||
168 | */ |
||
169 | 6 | public function getMessages() |
|
173 | |||
174 | /** |
||
175 | * Check if IP address is valid. |
||
176 | * |
||
177 | * @param $ip |
||
178 | * |
||
179 | * @return bool |
||
180 | */ |
||
181 | 2 | public function ipIsValid($ip) |
|
185 | |||
186 | /** |
||
187 | * Check if IP is blacklisted. |
||
188 | * |
||
189 | * @param null|string $ip |
||
190 | * |
||
191 | * @return bool |
||
192 | */ |
||
193 | 31 | public function isBlacklisted($ip = null) |
|
200 | |||
201 | /** |
||
202 | * Check if IP address is whitelisted. |
||
203 | * |
||
204 | * @param null|string $ip |
||
205 | * |
||
206 | * @return bool |
||
207 | */ |
||
208 | 23 | public function isWhitelisted($ip = null) |
|
212 | |||
213 | /** |
||
214 | * Register messages in log. |
||
215 | * |
||
216 | * @param $message |
||
217 | * |
||
218 | * @return void |
||
219 | */ |
||
220 | 16 | public function log($message) |
|
226 | |||
227 | 16 | public function getLogger() |
|
235 | |||
236 | /** |
||
237 | * Remove IP from all lists. |
||
238 | * |
||
239 | * @param $ip |
||
240 | * |
||
241 | * @return bool |
||
242 | */ |
||
243 | 5 | public function remove($ip) |
|
247 | |||
248 | /** |
||
249 | * Get the list of all IP addresses stored. |
||
250 | * |
||
251 | * @return mixed |
||
252 | */ |
||
253 | 3 | public function report() |
|
257 | |||
258 | /** |
||
259 | * Set the current IP address. |
||
260 | * |
||
261 | * @param $ip |
||
262 | */ |
||
263 | 75 | public function setIp($ip) |
|
277 | |||
278 | /** |
||
279 | * Check if a string is a valid country info. |
||
280 | * |
||
281 | * @param $country |
||
282 | * |
||
283 | * @return bool |
||
284 | */ |
||
285 | 1 | public function validCountry($country) |
|
289 | |||
290 | /** |
||
291 | * Tell in which list (black/white) an IP address is. |
||
292 | * |
||
293 | * @param $ip |
||
294 | * |
||
295 | * @return bool|string |
||
296 | */ |
||
297 | 47 | public function whichList($ip) |
|
301 | |||
302 | /** |
||
303 | * Whitelist an IP address. |
||
304 | * |
||
305 | * @param $ip |
||
306 | * @param bool $force |
||
307 | * |
||
308 | * @return bool |
||
309 | */ |
||
310 | 20 | public function whitelist($ip, $force = false) |
|
314 | |||
315 | /** |
||
316 | * Update the GeoIp2 database. |
||
317 | * |
||
318 | * @return bool |
||
319 | */ |
||
320 | 7 | public function updateGeoIp() |
|
330 | |||
331 | /** |
||
332 | * Check if the application is receiving some sort of attack. |
||
333 | * |
||
334 | * @param null $ipAddress |
||
335 | * |
||
336 | * @return bool |
||
337 | */ |
||
338 | 8 | public function isBeingAttacked($ipAddress = null) |
|
342 | |||
343 | /** |
||
344 | * Get a response to the attack. |
||
345 | * |
||
346 | * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse|null |
||
347 | */ |
||
348 | 3 | public function responseToAttack() |
|
352 | |||
353 | /** |
||
354 | * Get country code from an IP address. |
||
355 | * |
||
356 | * @param $ip |
||
357 | * |
||
358 | * @return bool|string |
||
359 | */ |
||
360 | 1 | public function getCountryFromIp($ip) |
|
364 | |||
365 | /** |
||
366 | * Make a country info from a string. |
||
367 | * |
||
368 | * @param $country |
||
369 | * |
||
370 | * @return bool|string |
||
371 | */ |
||
372 | 1 | public function makeCountryFromString($country) |
|
376 | |||
377 | /** |
||
378 | * Get the GeoIP instance. |
||
379 | * |
||
380 | * @return object |
||
381 | */ |
||
382 | 7 | public function getGeoIp() |
|
386 | } |
||
387 |