Completed
Push — master ( 2bd6c2...9d13eb )
by Antonio Carlos
08:09 queued 01:00
created

Firewall::getGeoIp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\Redirectable;
10
use PragmaRX\Firewall\Support\Responder;
11
use PragmaRX\Support\Config;
12
use PragmaRX\Support\GeoIp\Updater as GeoIpUpdater;
13
14
class Firewall
15
{
16
    use Redirectable;
17
18
    /**
19
     * The IP adress.
20
     *
21
     * @var
22
     */
23
    private $ip;
24
25
    /**
26
     * The config object.
27
     *
28
     * @var Config
29
     */
30
    private $config;
31
32
    /**
33
     * The data repository object.
34
     *
35
     * @var DataRepository
36
     */
37
    private $dataRepository;
38
39
    /**
40
     * The request.
41
     *
42
     * @var Request
43
     */
44
    private $request;
45
46
    /**
47
     * The attack blocker.
48
     *
49
     * @var AttackBlocker
50
     */
51
    private $attackBlocker;
52
    /**
53
     * @var Message
54
     */
55
    private $messageRepository;
56
57
    /**
58
     * Initialize Firewall object.
59
     *
60
     * @param Config         $config
61
     * @param DataRepository $dataRepository
62
     * @param Request        $request
63
     * @param AttackBlocker  $attackBlocker
64
     * @param Message        $messageRepository
65
     */
66
    public function __construct(
67
        Config $config,
68
        DataRepository $dataRepository,
69
        Request $request,
70
        AttackBlocker $attackBlocker,
71
        Message $messageRepository
72
    ) {
73
        $this->config = $config;
74
75
        $this->dataRepository = $dataRepository;
76
77
        $this->request = $request;
78
79
        $this->attackBlocker = $attackBlocker;
80
81
        $this->messageRepository = $messageRepository;
82
83
        $this->setIp(null);
84
    }
85
86
    /**
87
     * Get all IP addresses.
88
     *
89
     * @return \Illuminate\Support\Collection
90
     */
91
    public function all()
92
    {
93
        return $this->dataRepository->all();
94
    }
95
96
    /**
97
     * Get all IP addresses by country.
98
     *
99
     * @param $country
100
     *
101
     * @return \Illuminate\Support\Collection
102
     */
103
    public function allByCountry($country)
104
    {
105
        return $this->dataRepository->allByCountry($country);
106
    }
107
108
    /**
109
     * Blacklist an IP adress.
110
     *
111
     * @param $ip
112
     * @param bool $force
113
     *
114
     * @return bool
115
     */
116
    public function blacklist($ip, $force = false)
117
    {
118
        return $this->dataRepository->addToList(false, $ip, $force);
119
    }
120
121
    /**
122
     * Create a blocked access response.
123
     *
124
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse|null
125
     */
126
    public function blockAccess()
127
    {
128
        return (new Responder())->respond(
129
            $this->config->get('response')
130
        );
131
    }
132
133
    /**
134
     * Clear firewall table.
135
     *
136
     * @return mixed
137
     */
138
    public function clear()
139
    {
140
        return $this->dataRepository->clear();
141
    }
142
143
    /**
144
     * Find an IP address.
145
     *
146
     * @param string $ip
147
     *
148
     * @return \Illuminate\Database\Eloquent\Model|null
149
     */
150
    public function find($ip)
151
    {
152
        return $this->dataRepository->find($ip);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->dataRepository->find($ip) also could return the type PragmaRX\Firewall\Reposi...tracts\Cache\Repository which is incompatible with the documented return type null|Illuminate\Database\Eloquent\Model.
Loading history...
153
    }
154
155
    /**
156
     * Get the IP address.
157
     *
158
     * @return string|null
159
     */
160
    public function getIp()
161
    {
162
        return $this->ip;
163
    }
164
165
    /**
166
     * Get the messages.
167
     *
168
     * @return \Illuminate\Support\Collection
169
     */
170
    public function getMessages()
171
    {
172
        return $this->messageRepository->getMessages();
173
    }
174
175
    /**
176
     * Check if IP address is valid.
177
     *
178
     * @param $ip
179
     *
180
     * @return bool
181
     */
182
    public function ipIsValid($ip)
183
    {
184
        return $this->dataRepository->ipIsValid($ip);
185
    }
186
187
    /**
188
     * Check if IP is blacklisted.
189
     *
190
     * @param null|string $ip
191
     *
192
     * @return bool
193
     */
194
    public function isBlacklisted($ip = null)
195
    {
196
        $list = $this->whichList($ip);
197
198
        return !($list == 'whitelist') &&
199
                $list == 'blacklist';
200
    }
201
202
    /**
203
     * Check if IP address is whitelisted.
204
     *
205
     * @param null|string $ip
206
     *
207
     * @return bool
208
     */
209
    public function isWhitelisted($ip = null)
210
    {
211
        return $this->whichList($ip) == 'whitelist';
212
    }
213
214
    /**
215
     * Register messages in log.
216
     *
217
     * @param $message
218
     *
219
     * @return void
220
     */
221
    public function log($message)
222
    {
223
        if ($this->config->get('enable_log')) {
224
            app()->log->info("FIREWALL: $message");
225
        }
226
    }
227
228
    /**
229
     * Remove IP from all lists.
230
     *
231
     * @param $ip
232
     *
233
     * @return bool
234
     */
235
    public function remove($ip)
236
    {
237
        return $this->dataRepository->remove($ip);
238
    }
239
240
    /**
241
     * Get the list of all IP addresses stored.
242
     *
243
     * @return mixed
244
     */
245
    public function report()
246
    {
247
        return $this->dataRepository->all();
248
    }
249
250
    /**
251
     * Set the current IP address.
252
     *
253
     * @param $ip
254
     */
255
    public function setIp($ip)
256
    {
257
        $this->ip = $ip
258
            ?: ($this->ip
259
                ?: $this->request->getClientIp());
260
    }
261
262
    /**
263
     * Check if a string is a valid country info.
264
     *
265
     * @param $country
266
     *
267
     * @return bool
268
     */
269
    public function validCountry($country)
270
    {
271
        return $this->dataRepository->validCountry($country);
272
    }
273
274
    /**
275
     * Tell in which list (black/white) an IP address is.
276
     *
277
     * @param $ip_address
278
     *
279
     * @return bool|string
280
     */
281
    public function whichList($ip_address)
282
    {
283
        return $this->dataRepository->whichList($ip_address);
284
    }
285
286
    /**
287
     * Whitelist an IP address.
288
     *
289
     * @param $ip
290
     * @param bool $force
291
     *
292
     * @return bool
293
     */
294
    public function whitelist($ip, $force = false)
295
    {
296
        return $this->dataRepository->addToList(true, $ip, $force);
297
    }
298
299
    /**
300
     * Update the GeoIp2 database.
301
     *
302
     * @return bool
303
     */
304
    public function updateGeoIp()
305
    {
306
        $updater = new GeoIpUpdater();
307
308
        $success = $updater->updateGeoIpFiles($this->config->get('geoip_database_path'));
309
310
        $this->messageRepository->addMessage($updater->getMessages());
311
312
        return $success;
313
    }
314
315
    /**
316
     * Check if the application is receiving some sort of attack.
317
     *
318
     * @return bool
319
     */
320
    public function isBeingAttacked($ipAddress = null)
321
    {
322
        return $this->attackBlocker->isBeingAttacked($ipAddress ?: $this->ip);
323
    }
324
325
    /**
326
     * Get a response to the attack.
327
     *
328
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse|null
329
     */
330
    public function responseToAttack()
331
    {
332
        return $this->attackBlocker->responseToAttack();
333
    }
334
335
    /**
336
     * Get country code from an IP address.
337
     *
338
     * @param $ip_address
339
     *
340
     * @return bool|string
341
     */
342
    public function getCountryFromIp($ip_address)
343
    {
344
        return $this->dataRepository->getCountryFromIp($ip_address);
345
    }
346
347
    /**
348
     * Make a country info from a string.
349
     *
350
     * @param $country
351
     *
352
     * @return bool|string
353
     */
354
    public function makeCountryFromString($country)
355
    {
356
        return $this->dataRepository->makeCountryFromString($country);
357
    }
358
359
    /**
360
     * Get the GeoIP instance.
361
     *
362
     * @return object
363
     */
364
    public function getGeoIp()
365
    {
366
        return $this->dataRepository->getGeoIp();
367
    }
368
}
369