Completed
Push — master ( 7c0bc1...fb5480 )
by Antonio Carlos
02:41
created

Firewall::setIp()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6.2163

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 9
cts 11
cp 0.8182
rs 9.2222
c 0
b 0
f 0
cc 6
nc 6
nop 1
crap 6.2163
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
            app()->log->info("FIREWALL: $message");
224
        }
225 16
    }
226
227
    /**
228
     * Remove IP from all lists.
229
     *
230
     * @param $ip
231
     *
232
     * @return bool
233
     */
234 5
    public function remove($ip)
235
    {
236 5
        return $this->dataRepository->remove($ip);
237
    }
238
239
    /**
240
     * Get the list of all IP addresses stored.
241
     *
242
     * @return mixed
243
     */
244 3
    public function report()
245
    {
246 3
        return $this->dataRepository->all();
247
    }
248
249
    /**
250
     * Set the current IP address.
251
     *
252
     * @param $ip
253
     */
254 75
    public function setIp($ip)
255
    {
256 75
        if ($ip) {
257 11
            $this->ip = $ip;
258 75
        } elseif (!$this->ip) {
259 75
            if ($ip = $this->request->server('HTTP_CF_CONNECTING_IP')) {
260
                $this->ip = $ip;
261 75
            } elseif ($ip = $this->request->server->get('HTTP_X_FORWARDED_FOR')) {
262
                $this->ip = $ip;
263 75
            } elseif ($ip = $this->request->getClientIp()) {
264 75
                $this->ip = $ip;
265
            }
266
        }
267 75
    }
268
269
    /**
270
     * Check if a string is a valid country info.
271
     *
272
     * @param $country
273
     *
274
     * @return bool
275
     */
276 1
    public function validCountry($country)
277
    {
278 1
        return $this->dataRepository->validCountry($country);
279
    }
280
281
    /**
282
     * Tell in which list (black/white) an IP address is.
283
     *
284
     * @param $ip
285
     *
286
     * @return bool|string
287
     */
288 47
    public function whichList($ip)
289
    {
290 47
        return $this->dataRepository->whichList($this->getIp($ip));
291
    }
292
293
    /**
294
     * Whitelist an IP address.
295
     *
296
     * @param $ip
297
     * @param bool $force
298
     *
299
     * @return bool
300
     */
301 20
    public function whitelist($ip, $force = false)
302
    {
303 20
        return $this->dataRepository->addToList(true, $ip, $force);
304
    }
305
306
    /**
307
     * Update the GeoIp2 database.
308
     *
309
     * @return bool
310
     */
311 7
    public function updateGeoIp()
312
    {
313 7
        $updater = new GeoIpUpdater();
314
315 7
        $success = $updater->updateGeoIpFiles($this->config->get('geoip_database_path'));
316
317 7
        $this->messageRepository->addMessage($updater->getMessages());
318
319 7
        return $success;
320
    }
321
322
    /**
323
     * Check if the application is receiving some sort of attack.
324
     *
325
     * @param null $ipAddress
326
     *
327
     * @return bool
328
     */
329 8
    public function isBeingAttacked($ipAddress = null)
330
    {
331 8
        return $this->attackBlocker->isBeingAttacked($this->getIp($ipAddress));
332
    }
333
334
    /**
335
     * Get a response to the attack.
336
     *
337
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse|null
338
     */
339 3
    public function responseToAttack()
340
    {
341 3
        return $this->attackBlocker->responseToAttack();
342
    }
343
344
    /**
345
     * Get country code from an IP address.
346
     *
347
     * @param $ip
348
     *
349
     * @return bool|string
350
     */
351 1
    public function getCountryFromIp($ip)
352
    {
353 1
        return $this->dataRepository->getCountryFromIp($ip);
354
    }
355
356
    /**
357
     * Make a country info from a string.
358
     *
359
     * @param $country
360
     *
361
     * @return bool|string
362
     */
363 1
    public function makeCountryFromString($country)
364
    {
365 1
        return $this->dataRepository->makeCountryFromString($country);
366
    }
367
368
    /**
369
     * Get the GeoIP instance.
370
     *
371
     * @return object
372
     */
373 7
    public function getGeoIp()
374
    {
375 7
        return $this->dataRepository->getGeoIp();
376
    }
377
}
378