Completed
Push — master ( fb5480...789cd5 )
by Antonio Carlos
02:45
created

Firewall::allByCountry()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace PragmaRX\Firewall;
4
5
use Illuminate\Http\Request;
6
use PragmaRX\Firewall\Repositories\DataRepository;
7
use PragmaRX\Firewall\Repositories\Message;
8
use PragmaRX\Firewall\Support\AttackBlocker;
9
use PragmaRX\Firewall\Support\Responder;
10
use PragmaRX\Support\Config;
11
use PragmaRX\Support\GeoIp\Updater as GeoIpUpdater;
12
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(
64
        Config $config,
65
        DataRepository $dataRepository,
66
        Request $request,
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
67
        AttackBlocker $attackBlocker,
68
        Message $messageRepository
69
    ) {
70 75
        $this->config = $config;
71
72 75
        $this->dataRepository = $dataRepository;
73
74 75
        $this->request = $request;
75
76 75
        $this->attackBlocker = $attackBlocker;
77
78 75
        $this->messageRepository = $messageRepository;
79
80 75
        $this->setIp(null);
81 75
    }
82
83
    /**
84
     * Get all IP addresses.
85
     *
86
     * @return \Illuminate\Support\Collection
87
     */
88 2
    public function all()
89
    {
90 2
        return $this->dataRepository->all();
91
    }
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)
101
    {
102 1
        return $this->dataRepository->allByCountry($country);
103
    }
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)
114
    {
115 33
        return $this->dataRepository->addToList(false, $ip, $force);
116
    }
117
118
    /**
119
     * Create a blocked access response.
120
     *
121
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse|null
122
     */
123 4
    public function blockAccess()
124
    {
125 4
        return (new Responder())->respond(
126 4
            $this->config->get('responses.blacklist')
127
        );
128
    }
129
130
    /**
131
     * Clear firewall table.
132
     *
133
     * @return mixed
134
     */
135 10
    public function clear()
136
    {
137 10
        return $this->dataRepository->clear();
138
    }
139
140
    /**
141
     * Find an IP address.
142
     *
143
     * @param string $ip
144
     *
145
     * @return mixed
146
     */
147 3
    public function find($ip)
148
    {
149 3
        return $this->dataRepository->find($ip);
150
    }
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)
160
    {
161 48
        return $ip ?: $this->ip;
162
    }
163
164
    /**
165
     * Get the messages.
166
     *
167
     * @return \Illuminate\Support\Collection
168
     */
169 6
    public function getMessages()
170
    {
171 6
        return $this->messageRepository->getMessages();
172
    }
173
174
    /**
175
     * Check if IP address is valid.
176
     *
177
     * @param $ip
178
     *
179
     * @return bool
180
     */
181 2
    public function ipIsValid($ip)
182
    {
183 2
        return $this->dataRepository->ipIsValid($ip);
184
    }
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)
194
    {
195 31
        $list = $this->whichList($ip);
196
197 31
        return !($list == 'whitelist') &&
198 31
                $list == 'blacklist';
199
    }
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)
209
    {
210 23
        return $this->whichList($ip) == 'whitelist';
211
    }
212
213
    /**
214
     * Register messages in log.
215
     *
216
     * @param $message
217
     *
218
     * @return void
219
     */
220 16
    public function log($message)
221
    {
222 16
        if ($this->config->get('enable_log')) {
223 16
            $this->getLogger()->info("FIREWALL: $message");
224
        }
225 16
    }
226
227 16
    public function getLogger()
228
    {
229 16
        if ($stack = $this->config->get('log_stack')) {
230
            return app()->log->stack($stack);
231
        }
232
233 16
        return app()->log;
234
    }
235
236
    /**
237
     * Remove IP from all lists.
238
     *
239
     * @param $ip
240
     *
241
     * @return bool
242
     */
243 5
    public function remove($ip)
244
    {
245 5
        return $this->dataRepository->remove($ip);
246
    }
247
248
    /**
249
     * Get the list of all IP addresses stored.
250
     *
251
     * @return mixed
252
     */
253 3
    public function report()
254
    {
255 3
        return $this->dataRepository->all();
256
    }
257
258
    /**
259
     * Set the current IP address.
260
     *
261
     * @param $ip
262
     */
263 75
    public function setIp($ip)
264
    {
265 75
        if ($ip) {
266 11
            $this->ip = $ip;
267 75
        } elseif (!$this->ip) {
268 75
            if ($ip = $this->request->server('HTTP_CF_CONNECTING_IP')) {
269
                $this->ip = $ip;
270 75
            } elseif ($ip = $this->request->server->get('HTTP_X_FORWARDED_FOR')) {
271
                $this->ip = $ip;
272 75
            } elseif ($ip = $this->request->getClientIp()) {
273 75
                $this->ip = $ip;
274
            }
275
        }
276 75
    }
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)
286
    {
287 1
        return $this->dataRepository->validCountry($country);
288
    }
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)
298
    {
299 47
        return $this->dataRepository->whichList($this->getIp($ip));
300
    }
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)
311
    {
312 20
        return $this->dataRepository->addToList(true, $ip, $force);
313
    }
314
315
    /**
316
     * Update the GeoIp2 database.
317
     *
318
     * @return bool
319
     */
320 7
    public function updateGeoIp()
321
    {
322 7
        $updater = new GeoIpUpdater();
323
324 7
        $success = $updater->updateGeoIpFiles($this->config->get('geoip_database_path'));
325
326 7
        $this->messageRepository->addMessage($updater->getMessages());
327
328 7
        return $success;
329
    }
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)
339
    {
340 8
        return $this->attackBlocker->isBeingAttacked($this->getIp($ipAddress));
341
    }
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()
349
    {
350 3
        return $this->attackBlocker->responseToAttack();
351
    }
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)
361
    {
362 1
        return $this->dataRepository->getCountryFromIp($ip);
363
    }
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)
373
    {
374 1
        return $this->dataRepository->makeCountryFromString($country);
375
    }
376
377
    /**
378
     * Get the GeoIP instance.
379
     *
380
     * @return object
381
     */
382 7
    public function getGeoIp()
383
    {
384 7
        return $this->dataRepository->getGeoIp();
385
    }
386
}
387