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
|
53 |
|
private function getFirewallModel() |
51
|
|
|
{ |
52
|
53 |
|
if (!$firewallModel = $this->getConfig('firewall_model')) { |
53
|
1 |
|
throw new ConfigurationOptionNotAvailable('Config option "firewall_model" is not available, please publish/check your configuration.'); |
54
|
|
|
} |
55
|
|
|
|
56
|
52 |
|
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
|
66 |
|
public function getRootDirectory() |
75
|
|
|
{ |
76
|
66 |
|
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
|
66 |
|
public function register() |
95
|
|
|
{ |
96
|
66 |
|
parent::register(); |
97
|
|
|
|
98
|
66 |
|
if (!$this->getConfig('enabled')) { |
99
|
1 |
|
return; |
100
|
|
|
} |
101
|
|
|
|
102
|
65 |
|
$this->registerMigrations(); |
103
|
|
|
|
104
|
65 |
|
$this->registerFileSystem(); |
105
|
|
|
|
106
|
65 |
|
$this->registerCache(); |
107
|
|
|
|
108
|
65 |
|
$this->registerFirewall(); |
109
|
|
|
|
110
|
65 |
|
$this->registerDataRepository(); |
111
|
|
|
|
112
|
65 |
|
$this->registerMessageRepository(); |
113
|
|
|
|
114
|
65 |
|
$this->registerIpList(); |
115
|
|
|
|
116
|
65 |
|
$this->registerIpAddress(); |
117
|
|
|
|
118
|
65 |
|
$this->registerGeoIp(); |
119
|
|
|
|
120
|
65 |
|
$this->registerAttackBlocker(); |
121
|
|
|
|
122
|
65 |
|
$this->registerReportCommand(); |
123
|
|
|
|
124
|
65 |
|
$this->registerCountriesRepository(); |
125
|
|
|
|
126
|
65 |
|
if ($this->getConfig('use_database')) { |
127
|
24 |
|
$this->registerWhitelistCommand(); |
128
|
24 |
|
$this->registerBlacklistCommand(); |
129
|
24 |
|
$this->registerRemoveCommand(); |
130
|
24 |
|
$this->registerClearCommand(); |
131
|
|
|
} |
132
|
|
|
|
133
|
65 |
|
$this->registerUpdateGeoIpCommand(); |
134
|
|
|
|
135
|
65 |
|
$this->registerMiddleware(); |
136
|
|
|
|
137
|
65 |
|
$this->registerEventListeners(); |
138
|
65 |
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Register the attack blocker. |
142
|
|
|
*/ |
143
|
|
|
private function registerAttackBlocker() |
144
|
|
|
{ |
145
|
65 |
|
$this->app->singleton('firewall.attackBlocker', function () { |
146
|
65 |
|
return new AttackBlocker(); |
147
|
65 |
|
}); |
148
|
65 |
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Register the countries repository. |
152
|
|
|
*/ |
153
|
|
|
private function registerCountriesRepository() |
154
|
|
|
{ |
155
|
65 |
|
$this->app->singleton('firewall.countries', function () { |
156
|
30 |
|
return new Countries(); |
157
|
65 |
|
}); |
158
|
65 |
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Register the Blacklist Artisan command. |
162
|
|
|
* |
163
|
|
|
* @return void |
164
|
|
|
*/ |
165
|
|
|
private function registerBlacklistCommand() |
166
|
|
|
{ |
167
|
24 |
|
$this->app->singleton('firewall.blacklist.command', function () { |
168
|
24 |
|
return new BlacklistCommand(); |
169
|
24 |
|
}); |
170
|
|
|
|
171
|
24 |
|
$this->commands('firewall.blacklist.command'); |
172
|
24 |
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Register the Cache driver used by Firewall. |
176
|
|
|
* |
177
|
|
|
* @return void |
178
|
|
|
*/ |
179
|
|
|
private function registerCache() |
180
|
|
|
{ |
181
|
65 |
|
$this->app->singleton('firewall.cache', function () { |
182
|
49 |
|
return new Cache(app('cache')); |
183
|
65 |
|
}); |
184
|
65 |
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Register the List Artisan command. |
188
|
|
|
* |
189
|
|
|
* @return void |
190
|
|
|
*/ |
191
|
|
|
private function registerClearCommand() |
192
|
|
|
{ |
193
|
24 |
|
$this->app->singleton('firewall.clear.command', function () { |
194
|
24 |
|
return new ClearCommand(); |
195
|
24 |
|
}); |
196
|
|
|
|
197
|
24 |
|
$this->commands('firewall.clear.command'); |
198
|
24 |
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Register the Data Repository driver used by Firewall. |
202
|
|
|
* |
203
|
|
|
* @return void |
204
|
|
|
*/ |
205
|
|
|
private function registerDataRepository() |
206
|
|
|
{ |
207
|
65 |
|
$this->app->singleton('firewall.datarepository', function () { |
208
|
65 |
|
return new DataRepository(); |
209
|
65 |
|
}); |
210
|
65 |
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Register event listeners. |
214
|
|
|
*/ |
215
|
65 |
|
private function registerEventListeners() |
216
|
|
|
{ |
217
|
65 |
|
Event::listen(AttackDetected::class, NotifyAdmins::class); |
218
|
65 |
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Register the Filesystem driver used by Firewall. |
222
|
|
|
* |
223
|
|
|
* @return void |
224
|
|
|
*/ |
225
|
|
|
private function registerFileSystem() |
|
|
|
|
226
|
|
|
{ |
227
|
65 |
|
$this->app->singleton('firewall.filesystem', function () { |
228
|
1 |
|
return new Filesystem(); |
229
|
65 |
|
}); |
230
|
65 |
|
} |
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
|
65 |
|
$this->app->singleton('firewall', function ($app) { |
241
|
65 |
|
$app['firewall.loaded'] = true; |
242
|
|
|
|
243
|
65 |
|
$this->firewall = new Firewall( |
244
|
65 |
|
$app['firewall.config'], |
245
|
65 |
|
$app['firewall.datarepository'], |
246
|
65 |
|
$app['request'], |
247
|
65 |
|
$attackBlocker = $app['firewall.attackBlocker'], |
248
|
65 |
|
$app['firewall.messages'] |
249
|
|
|
); |
250
|
|
|
|
251
|
65 |
|
$attackBlocker->setFirewall($this->firewall); |
252
|
|
|
|
253
|
65 |
|
return $this->firewall; |
254
|
65 |
|
}); |
255
|
65 |
|
} |
256
|
|
|
|
257
|
|
|
private function registerIpAddress() |
258
|
|
|
{ |
259
|
65 |
|
$this->app->singleton('firewall.ipaddress', function () { |
260
|
52 |
|
return new IpAddress(); |
261
|
65 |
|
}); |
262
|
65 |
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Register the ip list repository. |
266
|
|
|
*/ |
267
|
|
|
private function registerIpList() |
268
|
|
|
{ |
269
|
65 |
|
$this->app->singleton('firewall.iplist', function () { |
270
|
53 |
|
return new IpList($this->getFirewallModel()); |
|
|
|
|
271
|
65 |
|
}); |
272
|
65 |
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Register the message repository. |
276
|
|
|
*/ |
277
|
|
|
private function registerMessageRepository() |
278
|
|
|
{ |
279
|
65 |
|
$this->app->singleton('firewall.messages', function () { |
280
|
65 |
|
return new Message(); |
281
|
65 |
|
}); |
282
|
65 |
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Register blocking and unblocking Middleware. |
286
|
|
|
* |
287
|
|
|
* @return void |
288
|
|
|
*/ |
289
|
|
|
private function registerMiddleware() |
290
|
|
|
{ |
291
|
65 |
|
$this->app->singleton('firewall.middleware.blacklist', function () { |
292
|
|
|
return new FirewallBlacklist(new Blacklist()); |
293
|
65 |
|
}); |
294
|
|
|
|
295
|
65 |
|
$this->app->singleton('firewall.middleware.whitelist', function () { |
296
|
|
|
return new FirewallWhitelist(new Whitelist()); |
297
|
65 |
|
}); |
298
|
65 |
|
} |
299
|
|
|
|
300
|
65 |
|
private function registerMigrations() |
301
|
|
|
{ |
302
|
65 |
|
$this->loadMigrationsFrom(__DIR__.'/../../migrations'); |
303
|
65 |
|
} |
304
|
|
|
|
305
|
|
|
private function registerGeoIp() |
306
|
|
|
{ |
307
|
65 |
|
$this->app->singleton('firewall.geoip', function () { |
308
|
23 |
|
return new GeoIp($this->getConfig('geoip_database_path')); |
309
|
65 |
|
}); |
310
|
65 |
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Register the List Artisan command. |
314
|
|
|
* |
315
|
|
|
* @return void |
316
|
|
|
*/ |
317
|
|
|
private function registerRemoveCommand() |
318
|
|
|
{ |
319
|
24 |
|
$this->app->singleton('firewall.remove.command', function () { |
320
|
24 |
|
return new RemoveCommand(); |
321
|
24 |
|
}); |
322
|
|
|
|
323
|
24 |
|
$this->commands('firewall.remove.command'); |
324
|
24 |
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Register the List Artisan command. |
328
|
|
|
* |
329
|
|
|
* @return void |
330
|
|
|
*/ |
331
|
|
|
private function registerReportCommand() |
332
|
|
|
{ |
333
|
65 |
|
$this->app->singleton('firewall.list.command', function () { |
334
|
65 |
|
return new ReportCommand(); |
335
|
65 |
|
}); |
336
|
|
|
|
337
|
65 |
|
$this->commands('firewall.list.command'); |
338
|
65 |
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* Register the updategeoip command. |
342
|
|
|
*/ |
343
|
|
|
private function registerUpdateGeoIpCommand() |
344
|
|
|
{ |
345
|
65 |
|
$this->app->singleton('firewall.updategeoip.command', function () { |
346
|
65 |
|
return new UpdateGeoIpCommand(); |
347
|
65 |
|
}); |
348
|
|
|
|
349
|
65 |
|
$this->commands('firewall.updategeoip.command'); |
350
|
65 |
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Register the Whitelist Artisan command. |
354
|
|
|
* |
355
|
|
|
* @return void |
356
|
|
|
*/ |
357
|
|
|
private function registerWhitelistCommand() |
358
|
|
|
{ |
359
|
24 |
|
$this->app->singleton('firewall.whitelist.command', function () { |
360
|
24 |
|
return new WhitelistCommand(); |
361
|
24 |
|
}); |
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.