|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PragmaRX\Firewall\Vendor\Laravel; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Event; |
|
6
|
|
|
use PragmaRX\Firewall\Events\AttackDetected; |
|
7
|
|
|
use PragmaRX\Firewall\Exceptions\ConfigurationOptionNotAvailable; |
|
8
|
|
|
use PragmaRX\Firewall\Filters\Blacklist; |
|
9
|
|
|
use PragmaRX\Firewall\Filters\Whitelist; |
|
10
|
|
|
use PragmaRX\Firewall\Firewall; |
|
11
|
|
|
use PragmaRX\Firewall\Listeners\NotifyAdmins; |
|
12
|
|
|
use PragmaRX\Firewall\Middleware\FirewallBlacklist; |
|
13
|
|
|
use PragmaRX\Firewall\Middleware\FirewallWhitelist; |
|
14
|
|
|
use PragmaRX\Firewall\Repositories\Cache\Cache; |
|
15
|
|
|
use PragmaRX\Firewall\Repositories\Countries; |
|
16
|
|
|
use PragmaRX\Firewall\Repositories\DataRepository; |
|
17
|
|
|
use PragmaRX\Firewall\Repositories\IpList; |
|
18
|
|
|
use PragmaRX\Firewall\Repositories\Message; |
|
19
|
|
|
use PragmaRX\Firewall\Support\AttackBlocker; |
|
20
|
|
|
use PragmaRX\Firewall\Support\IpAddress; |
|
21
|
|
|
use PragmaRX\Firewall\Vendor\Laravel\Artisan\Blacklist as BlacklistCommand; |
|
22
|
|
|
use PragmaRX\Firewall\Vendor\Laravel\Artisan\Clear as ClearCommand; |
|
23
|
|
|
use PragmaRX\Firewall\Vendor\Laravel\Artisan\Remove as RemoveCommand; |
|
24
|
|
|
use PragmaRX\Firewall\Vendor\Laravel\Artisan\Report as ReportCommand; |
|
25
|
|
|
use PragmaRX\Firewall\Vendor\Laravel\Artisan\UpdateGeoIp as UpdateGeoIpCommand; |
|
26
|
|
|
use PragmaRX\Firewall\Vendor\Laravel\Artisan\Whitelist as WhitelistCommand; |
|
27
|
|
|
use PragmaRX\Support\Filesystem; |
|
28
|
|
|
use PragmaRX\Support\GeoIp\GeoIp; |
|
29
|
|
|
use PragmaRX\Support\ServiceProvider as PragmaRXServiceProvider; |
|
30
|
|
|
|
|
31
|
|
|
class ServiceProvider extends PragmaRXServiceProvider |
|
32
|
|
|
{ |
|
33
|
|
|
protected $packageVendor = 'pragmarx'; |
|
34
|
|
|
|
|
35
|
|
|
protected $packageVendorCapitalized = 'PragmaRX'; |
|
36
|
|
|
|
|
37
|
|
|
protected $packageName = 'firewall'; |
|
38
|
|
|
|
|
39
|
|
|
protected $packageNameCapitalized = 'Firewall'; |
|
40
|
|
|
|
|
41
|
|
|
private $firewall; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Get the full path of the stub config file. |
|
45
|
|
|
* |
|
46
|
|
|
* @throws ConfigurationOptionNotAvailable |
|
47
|
|
|
* |
|
48
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
|
49
|
|
|
*/ |
|
50
|
25 |
|
private function getFirewallModel() |
|
51
|
|
|
{ |
|
52
|
25 |
|
if (!$firewallModel = $this->getConfig('firewall_model')) { |
|
53
|
|
|
throw new ConfigurationOptionNotAvailable('Config option "firewall_model" is not available, please publish/check your configuration.'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
25 |
|
return new $firewallModel(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Get the current package directory. |
|
61
|
|
|
* |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getPackageDir() |
|
65
|
|
|
{ |
|
66
|
|
|
return __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..'; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Get the root directory for this ServiceProvider. |
|
71
|
|
|
* |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
39 |
|
public function getRootDirectory() |
|
75
|
|
|
{ |
|
76
|
39 |
|
return __DIR__.'/../..'; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Get the services provided by the provider. |
|
81
|
|
|
* |
|
82
|
|
|
* @return array |
|
83
|
|
|
*/ |
|
84
|
|
|
public function provides() |
|
85
|
|
|
{ |
|
86
|
|
|
return ['firewall']; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Register the service provider. |
|
91
|
|
|
* |
|
92
|
|
|
* @return void |
|
93
|
|
|
*/ |
|
94
|
39 |
|
public function register() |
|
95
|
|
|
{ |
|
96
|
39 |
|
parent::register(); |
|
97
|
|
|
|
|
98
|
39 |
|
if (!$this->getConfig('enabled')) { |
|
99
|
1 |
|
return; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
38 |
|
$this->registerMigrations(); |
|
103
|
|
|
|
|
104
|
38 |
|
$this->registerFileSystem(); |
|
105
|
|
|
|
|
106
|
38 |
|
$this->registerCache(); |
|
107
|
|
|
|
|
108
|
38 |
|
$this->registerFirewall(); |
|
109
|
|
|
|
|
110
|
38 |
|
$this->registerDataRepository(); |
|
111
|
|
|
|
|
112
|
38 |
|
$this->registerMessageRepository(); |
|
113
|
|
|
|
|
114
|
38 |
|
$this->registerIpList(); |
|
115
|
|
|
|
|
116
|
38 |
|
$this->registerIpAddress(); |
|
117
|
|
|
|
|
118
|
38 |
|
$this->registerGeoIp(); |
|
119
|
|
|
|
|
120
|
38 |
|
$this->registerAttackBlocker(); |
|
121
|
|
|
|
|
122
|
38 |
|
$this->registerReportCommand(); |
|
123
|
|
|
|
|
124
|
38 |
|
$this->registerCountriesRepository(); |
|
125
|
|
|
|
|
126
|
38 |
|
if ($this->getConfig('use_database')) { |
|
127
|
15 |
|
$this->registerWhitelistCommand(); |
|
128
|
15 |
|
$this->registerBlacklistCommand(); |
|
129
|
15 |
|
$this->registerRemoveCommand(); |
|
130
|
15 |
|
$this->registerClearCommand(); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
38 |
|
$this->registerUpdateGeoIpCommand(); |
|
134
|
|
|
|
|
135
|
38 |
|
$this->registerMiddleware(); |
|
136
|
|
|
|
|
137
|
38 |
|
$this->registerEventListeners(); |
|
138
|
38 |
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Register the attack blocker. |
|
142
|
|
|
*/ |
|
143
|
|
|
private function registerAttackBlocker() |
|
144
|
|
|
{ |
|
145
|
38 |
|
$this->app->singleton('firewall.attackBlocker', function () { |
|
146
|
37 |
|
return new AttackBlocker(); |
|
147
|
38 |
|
}); |
|
148
|
38 |
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Register the countries repository. |
|
152
|
|
|
*/ |
|
153
|
|
|
private function registerCountriesRepository() |
|
154
|
|
|
{ |
|
155
|
38 |
|
$this->app->singleton('firewall.countries', function () { |
|
156
|
11 |
|
return new Countries(); |
|
157
|
38 |
|
}); |
|
158
|
38 |
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Register the Blacklist Artisan command. |
|
162
|
|
|
* |
|
163
|
|
|
* @return void |
|
164
|
|
|
*/ |
|
165
|
|
|
private function registerBlacklistCommand() |
|
166
|
|
|
{ |
|
167
|
15 |
|
$this->app->singleton('firewall.blacklist.command', function () { |
|
168
|
15 |
|
return new BlacklistCommand(); |
|
169
|
15 |
|
}); |
|
170
|
|
|
|
|
171
|
15 |
|
$this->commands('firewall.blacklist.command'); |
|
172
|
15 |
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Register the Cache driver used by Firewall. |
|
176
|
|
|
* |
|
177
|
|
|
* @return void |
|
178
|
|
|
*/ |
|
179
|
|
|
private function registerCache() |
|
180
|
|
|
{ |
|
181
|
38 |
|
$this->app->singleton('firewall.cache', function () { |
|
182
|
23 |
|
return new Cache(app('cache')); |
|
183
|
38 |
|
}); |
|
184
|
38 |
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Register the List Artisan command. |
|
188
|
|
|
* |
|
189
|
|
|
* @return void |
|
190
|
|
|
*/ |
|
191
|
|
|
private function registerClearCommand() |
|
192
|
|
|
{ |
|
193
|
15 |
|
$this->app->singleton('firewall.clear.command', function () { |
|
194
|
15 |
|
return new ClearCommand(); |
|
195
|
15 |
|
}); |
|
196
|
|
|
|
|
197
|
15 |
|
$this->commands('firewall.clear.command'); |
|
198
|
15 |
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Register the Data Repository driver used by Firewall. |
|
202
|
|
|
* |
|
203
|
|
|
* @return void |
|
204
|
|
|
*/ |
|
205
|
|
|
private function registerDataRepository() |
|
206
|
|
|
{ |
|
207
|
38 |
|
$this->app->singleton('firewall.datarepository', function () { |
|
208
|
37 |
|
return new DataRepository(); |
|
209
|
38 |
|
}); |
|
210
|
38 |
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Register event listeners. |
|
214
|
|
|
*/ |
|
215
|
38 |
|
private function registerEventListeners() |
|
216
|
|
|
{ |
|
217
|
38 |
|
Event::listen(AttackDetected::class, NotifyAdmins::class); |
|
218
|
38 |
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Register the Filesystem driver used by Firewall. |
|
222
|
|
|
* |
|
223
|
|
|
* @return void |
|
224
|
|
|
*/ |
|
225
|
|
|
private function registerFileSystem() |
|
|
|
|
|
|
226
|
|
|
{ |
|
227
|
38 |
|
$this->app->singleton('firewall.filesystem', function () { |
|
228
|
|
|
return new Filesystem(); |
|
229
|
38 |
|
}); |
|
230
|
38 |
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Takes all the components of Firewall and glues them |
|
234
|
|
|
* together to create Firewall. |
|
235
|
|
|
* |
|
236
|
|
|
* @return void |
|
237
|
|
|
*/ |
|
238
|
|
|
private function registerFirewall() |
|
239
|
|
|
{ |
|
240
|
38 |
|
$this->app->singleton('firewall', function ($app) { |
|
241
|
37 |
|
$app['firewall.loaded'] = true; |
|
242
|
|
|
|
|
243
|
37 |
|
$this->firewall = new Firewall( |
|
244
|
37 |
|
$app['firewall.config'], |
|
245
|
37 |
|
$app['firewall.datarepository'], |
|
246
|
37 |
|
$app['request'], |
|
247
|
37 |
|
$attackBlocker = $app['firewall.attackBlocker'], |
|
248
|
37 |
|
$app['firewall.messages'] |
|
249
|
|
|
); |
|
250
|
|
|
|
|
251
|
37 |
|
$attackBlocker->setFirewall($this->firewall); |
|
252
|
|
|
|
|
253
|
37 |
|
return $this->firewall; |
|
254
|
38 |
|
}); |
|
255
|
38 |
|
} |
|
256
|
|
|
|
|
257
|
|
|
private function registerIpAddress() |
|
258
|
|
|
{ |
|
259
|
38 |
|
$this->app->singleton('firewall.ipaddress', function () { |
|
260
|
25 |
|
return new IpAddress(); |
|
261
|
38 |
|
}); |
|
262
|
38 |
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* Register the ip list repository. |
|
266
|
|
|
*/ |
|
267
|
|
|
private function registerIpList() |
|
268
|
|
|
{ |
|
269
|
38 |
|
$this->app->singleton('firewall.iplist', function () { |
|
270
|
25 |
|
return new IpList($this->getFirewallModel()); |
|
|
|
|
|
|
271
|
38 |
|
}); |
|
272
|
38 |
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* Register the message repository. |
|
276
|
|
|
*/ |
|
277
|
|
|
private function registerMessageRepository() |
|
278
|
|
|
{ |
|
279
|
38 |
|
$this->app->singleton('firewall.messages', function () { |
|
280
|
37 |
|
return new Message(); |
|
281
|
38 |
|
}); |
|
282
|
38 |
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* Register blocking and unblocking Middleware. |
|
286
|
|
|
* |
|
287
|
|
|
* @return void |
|
288
|
|
|
*/ |
|
289
|
|
|
private function registerMiddleware() |
|
290
|
|
|
{ |
|
291
|
38 |
|
$this->app->singleton('firewall.middleware.blacklist', function () { |
|
292
|
|
|
return new FirewallBlacklist(new Blacklist()); |
|
293
|
38 |
|
}); |
|
294
|
|
|
|
|
295
|
38 |
|
$this->app->singleton('firewall.middleware.whitelist', function () { |
|
296
|
|
|
return new FirewallWhitelist(new Whitelist()); |
|
297
|
38 |
|
}); |
|
298
|
38 |
|
} |
|
299
|
|
|
|
|
300
|
38 |
|
private function registerMigrations() |
|
301
|
|
|
{ |
|
302
|
38 |
|
$this->loadMigrationsFrom(__DIR__.'/../../migrations'); |
|
303
|
38 |
|
} |
|
304
|
|
|
|
|
305
|
|
|
private function registerGeoIp() |
|
306
|
|
|
{ |
|
307
|
38 |
|
$this->app->singleton('firewall.geoip', function () { |
|
308
|
6 |
|
return new GeoIp($this->getConfig('geoip_database_path')); |
|
309
|
38 |
|
}); |
|
310
|
38 |
|
} |
|
311
|
|
|
|
|
312
|
|
|
/** |
|
313
|
|
|
* Register the List Artisan command. |
|
314
|
|
|
* |
|
315
|
|
|
* @return void |
|
316
|
|
|
*/ |
|
317
|
|
|
private function registerRemoveCommand() |
|
318
|
|
|
{ |
|
319
|
15 |
|
$this->app->singleton('firewall.remove.command', function () { |
|
320
|
15 |
|
return new RemoveCommand(); |
|
321
|
15 |
|
}); |
|
322
|
|
|
|
|
323
|
15 |
|
$this->commands('firewall.remove.command'); |
|
324
|
15 |
|
} |
|
325
|
|
|
|
|
326
|
|
|
/** |
|
327
|
|
|
* Register the List Artisan command. |
|
328
|
|
|
* |
|
329
|
|
|
* @return void |
|
330
|
|
|
*/ |
|
331
|
|
|
private function registerReportCommand() |
|
332
|
|
|
{ |
|
333
|
38 |
|
$this->app->singleton('firewall.list.command', function () { |
|
334
|
38 |
|
return new ReportCommand(); |
|
335
|
38 |
|
}); |
|
336
|
|
|
|
|
337
|
38 |
|
$this->commands('firewall.list.command'); |
|
338
|
38 |
|
} |
|
339
|
|
|
|
|
340
|
|
|
/** |
|
341
|
|
|
* Register the updategeoip command. |
|
342
|
|
|
*/ |
|
343
|
|
|
private function registerUpdateGeoIpCommand() |
|
344
|
|
|
{ |
|
345
|
38 |
|
$this->app->singleton('firewall.updategeoip.command', function () { |
|
346
|
38 |
|
return new UpdateGeoIpCommand(); |
|
347
|
38 |
|
}); |
|
348
|
|
|
|
|
349
|
38 |
|
$this->commands('firewall.updategeoip.command'); |
|
350
|
38 |
|
} |
|
351
|
|
|
|
|
352
|
|
|
/** |
|
353
|
|
|
* Register the Whitelist Artisan command. |
|
354
|
|
|
* |
|
355
|
|
|
* @return void |
|
356
|
|
|
*/ |
|
357
|
|
|
private function registerWhitelistCommand() |
|
358
|
|
|
{ |
|
359
|
15 |
|
$this->app->singleton('firewall.whitelist.command', function () { |
|
360
|
15 |
|
return new WhitelistCommand(); |
|
361
|
15 |
|
}); |
|
362
|
|
|
|
|
363
|
|
|
$this->commands('firewall.whitelist.command'); |
|
364
|
|
|
} |
|
365
|
|
|
} |
|
366
|
|
|
|
Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.