Completed
Pull Request — master (#166)
by Talha Zekeriya
15:11
created

Firewall::setIp()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 8.4275

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 9
cts 13
cp 0.6923
rs 8.8333
c 0
b 0
f 0
cc 7
nc 7
nop 1
crap 8.4275
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
                if ($first_ip_in_list = stristr($ip, ',', true)) {
273
                    $this->ip = $first_ip_in_list;
274
                }
275 75
            } elseif ($ip = $this->request->getClientIp()) {
276 75
                $this->ip = $ip;
277
            }
278
        }
279 75
    }
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)
289
    {
290 1
        return $this->dataRepository->validCountry($country);
291
    }
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)
301
    {
302 47
        return $this->dataRepository->whichList($this->getIp($ip));
303
    }
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)
314
    {
315 20
        return $this->dataRepository->addToList(true, $ip, $force);
316
    }
317
318
    /**
319
     * Update the GeoIp2 database.
320
     *
321
     * @return bool
322
     */
323 7
    public function updateGeoIp()
324
    {
325 7
        $updater = new GeoIpUpdater();
326
327 7
        $success = $updater->updateGeoIpFiles($this->config->get('geoip_database_path'));
328
329 7
        $this->messageRepository->addMessage($updater->getMessages());
330
331 7
        return $success;
332
    }
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)
342
    {
343 8
        return $this->attackBlocker->isBeingAttacked($this->getIp($ipAddress));
344
    }
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()
352
    {
353 3
        return $this->attackBlocker->responseToAttack();
354
    }
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)
364
    {
365 1
        return $this->dataRepository->getCountryFromIp($ip);
366
    }
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)
376
    {
377 1
        return $this->dataRepository->makeCountryFromString($country);
378
    }
379
380
    /**
381
     * Get the GeoIP instance.
382
     *
383
     * @return object
384
     */
385 7
    public function getGeoIp()
386
    {
387 7
        return $this->dataRepository->getGeoIp();
388
    }
389
}
390