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) |
|
280 | |||
281 | /** |
||
282 | * Check if a string is a valid country info. |
||
283 | * |
||
284 | * @param $country |
||
285 | * |
||
286 | * @return bool |
||
287 | */ |
||
288 | 1 | public function validCountry($country) |
|
292 | |||
293 | /** |
||
294 | * Tell in which list (black/white) an IP address is. |
||
295 | * |
||
296 | * @param $ip |
||
297 | * |
||
298 | * @return bool|string |
||
299 | */ |
||
300 | 47 | public function whichList($ip) |
|
304 | |||
305 | /** |
||
306 | * Whitelist an IP address. |
||
307 | * |
||
308 | * @param $ip |
||
309 | * @param bool $force |
||
310 | * |
||
311 | * @return bool |
||
312 | */ |
||
313 | 20 | public function whitelist($ip, $force = false) |
|
317 | |||
318 | /** |
||
319 | * Update the GeoIp2 database. |
||
320 | * |
||
321 | * @return bool |
||
322 | */ |
||
323 | 7 | public function updateGeoIp() |
|
333 | |||
334 | /** |
||
335 | * Check if the application is receiving some sort of attack. |
||
336 | * |
||
337 | * @param null $ipAddress |
||
338 | * |
||
339 | * @return bool |
||
340 | */ |
||
341 | 8 | public function isBeingAttacked($ipAddress = null) |
|
345 | |||
346 | /** |
||
347 | * Get a response to the attack. |
||
348 | * |
||
349 | * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse|null |
||
350 | */ |
||
351 | 3 | public function responseToAttack() |
|
355 | |||
356 | /** |
||
357 | * Get country code from an IP address. |
||
358 | * |
||
359 | * @param $ip |
||
360 | * |
||
361 | * @return bool|string |
||
362 | */ |
||
363 | 1 | public function getCountryFromIp($ip) |
|
367 | |||
368 | /** |
||
369 | * Make a country info from a string. |
||
370 | * |
||
371 | * @param $country |
||
372 | * |
||
373 | * @return bool|string |
||
374 | */ |
||
375 | 1 | public function makeCountryFromString($country) |
|
379 | |||
380 | /** |
||
381 | * Get the GeoIP instance. |
||
382 | * |
||
383 | * @return object |
||
384 | */ |
||
385 | 7 | public function getGeoIp() |
|
389 | } |
||
390 |