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
|
58 |
|
private function getFirewallModel() |
51
|
|
|
{ |
52
|
58 |
|
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
|
57 |
|
return new $firewallModel(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get the root directory for this ServiceProvider. |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
72 |
|
public function getRootDirectory() |
65
|
|
|
{ |
66
|
72 |
|
return __DIR__.'/../..'; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Register the service provider. |
71
|
|
|
* |
72
|
|
|
* @return void |
73
|
|
|
*/ |
74
|
72 |
|
public function register() |
75
|
|
|
{ |
76
|
72 |
|
parent::register(); |
77
|
|
|
|
78
|
72 |
|
if (!$this->getConfig('enabled')) { |
79
|
1 |
|
return; |
80
|
|
|
} |
81
|
|
|
|
82
|
71 |
|
$this->registerMigrations(); |
83
|
|
|
|
84
|
71 |
|
$this->registerFileSystem(); |
85
|
|
|
|
86
|
71 |
|
$this->registerCache(); |
87
|
|
|
|
88
|
71 |
|
$this->registerFirewall(); |
89
|
|
|
|
90
|
71 |
|
$this->registerDataRepository(); |
91
|
|
|
|
92
|
71 |
|
$this->registerMessageRepository(); |
93
|
|
|
|
94
|
71 |
|
$this->registerIpList(); |
95
|
|
|
|
96
|
71 |
|
$this->registerIpAddress(); |
97
|
|
|
|
98
|
71 |
|
$this->registerGeoIp(); |
99
|
|
|
|
100
|
71 |
|
$this->registerAttackBlocker(); |
101
|
|
|
|
102
|
71 |
|
$this->registerReportCommand(); |
103
|
|
|
|
104
|
71 |
|
$this->registerCountriesRepository(); |
105
|
|
|
|
106
|
71 |
|
if ($this->getConfig('use_database')) { |
107
|
23 |
|
$this->registerWhitelistCommand(); |
108
|
23 |
|
$this->registerBlacklistCommand(); |
109
|
23 |
|
$this->registerRemoveCommand(); |
110
|
23 |
|
$this->registerClearCommand(); |
111
|
|
|
} |
112
|
|
|
|
113
|
71 |
|
$this->registerUpdateGeoIpCommand(); |
114
|
|
|
|
115
|
71 |
|
$this->registerMiddleware(); |
116
|
|
|
|
117
|
71 |
|
$this->registerEventListeners(); |
118
|
71 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Register the attack blocker. |
122
|
|
|
*/ |
123
|
|
|
private function registerAttackBlocker() |
124
|
|
|
{ |
125
|
71 |
|
$this->app->singleton('firewall.attackBlocker', function () { |
126
|
71 |
|
return new AttackBlocker(); |
127
|
71 |
|
}); |
128
|
71 |
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Register the countries repository. |
132
|
|
|
*/ |
133
|
|
|
private function registerCountriesRepository() |
134
|
|
|
{ |
135
|
71 |
|
$this->app->singleton('firewall.countries', function () { |
136
|
35 |
|
return new Countries(); |
137
|
71 |
|
}); |
138
|
71 |
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Register the Blacklist Artisan command. |
142
|
|
|
* |
143
|
|
|
* @return void |
144
|
|
|
*/ |
145
|
|
|
private function registerBlacklistCommand() |
146
|
|
|
{ |
147
|
23 |
|
$this->app->singleton('firewall.blacklist.command', function () { |
148
|
23 |
|
return new BlacklistCommand(); |
149
|
23 |
|
}); |
150
|
|
|
|
151
|
23 |
|
$this->commands('firewall.blacklist.command'); |
152
|
23 |
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Register the Cache driver used by Firewall. |
156
|
|
|
* |
157
|
|
|
* @return void |
158
|
|
|
*/ |
159
|
|
|
private function registerCache() |
160
|
|
|
{ |
161
|
71 |
|
$this->app->singleton('firewall.cache', function () { |
162
|
71 |
|
return new Cache(app('cache')); |
163
|
71 |
|
}); |
164
|
71 |
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Register the List Artisan command. |
168
|
|
|
* |
169
|
|
|
* @return void |
170
|
|
|
*/ |
171
|
|
|
private function registerClearCommand() |
172
|
|
|
{ |
173
|
23 |
|
$this->app->singleton('firewall.clear.command', function () { |
174
|
23 |
|
return new ClearCommand(); |
175
|
23 |
|
}); |
176
|
|
|
|
177
|
23 |
|
$this->commands('firewall.clear.command'); |
178
|
23 |
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Register the Data Repository driver used by Firewall. |
182
|
|
|
* |
183
|
|
|
* @return void |
184
|
|
|
*/ |
185
|
|
|
private function registerDataRepository() |
186
|
|
|
{ |
187
|
71 |
|
$this->app->singleton('firewall.datarepository', function () { |
188
|
71 |
|
return new DataRepository(); |
189
|
71 |
|
}); |
190
|
71 |
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Register event listeners. |
194
|
|
|
*/ |
195
|
71 |
|
private function registerEventListeners() |
196
|
|
|
{ |
197
|
71 |
|
Event::listen(AttackDetected::class, NotifyAdmins::class); |
198
|
71 |
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Register the Filesystem driver used by Firewall. |
202
|
|
|
* |
203
|
|
|
* @return void |
204
|
|
|
*/ |
205
|
|
|
private function registerFileSystem() |
|
|
|
|
206
|
|
|
{ |
207
|
71 |
|
$this->app->singleton('firewall.filesystem', function () { |
208
|
1 |
|
return new Filesystem(); |
209
|
71 |
|
}); |
210
|
71 |
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Takes all the components of Firewall and glues them |
214
|
|
|
* together to create Firewall. |
215
|
|
|
* |
216
|
|
|
* @return void |
217
|
|
|
*/ |
218
|
|
|
private function registerFirewall() |
219
|
|
|
{ |
220
|
71 |
|
$this->app->singleton('firewall', function ($app) { |
221
|
71 |
|
$app['firewall.loaded'] = true; |
222
|
|
|
|
223
|
71 |
|
$this->firewall = new Firewall( |
224
|
71 |
|
$app['firewall.config'], |
225
|
71 |
|
$app['firewall.datarepository'], |
226
|
71 |
|
$app['request'], |
227
|
71 |
|
$attackBlocker = $app['firewall.attackBlocker'], |
228
|
71 |
|
$app['firewall.messages'] |
229
|
|
|
); |
230
|
|
|
|
231
|
71 |
|
$attackBlocker->setFirewall($this->firewall); |
232
|
|
|
|
233
|
71 |
|
return $this->firewall; |
234
|
71 |
|
}); |
235
|
71 |
|
} |
236
|
|
|
|
237
|
|
|
private function registerIpAddress() |
238
|
|
|
{ |
239
|
71 |
|
$this->app->singleton('firewall.ipaddress', function () { |
240
|
57 |
|
return new IpAddress(); |
241
|
71 |
|
}); |
242
|
71 |
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Register the ip list repository. |
246
|
|
|
*/ |
247
|
|
|
private function registerIpList() |
248
|
|
|
{ |
249
|
71 |
|
$this->app->singleton('firewall.iplist', function () { |
250
|
58 |
|
return new IpList($this->getFirewallModel()); |
|
|
|
|
251
|
71 |
|
}); |
252
|
71 |
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Register the message repository. |
256
|
|
|
*/ |
257
|
|
|
private function registerMessageRepository() |
258
|
|
|
{ |
259
|
71 |
|
$this->app->singleton('firewall.messages', function () { |
260
|
71 |
|
return new Message(); |
261
|
71 |
|
}); |
262
|
71 |
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Register blocking and unblocking Middleware. |
266
|
|
|
* |
267
|
|
|
* @return void |
268
|
|
|
*/ |
269
|
|
|
private function registerMiddleware() |
270
|
|
|
{ |
271
|
71 |
|
$this->app->singleton('firewall.middleware.blacklist', function () { |
272
|
|
|
return new FirewallBlacklist(new Blacklist()); |
273
|
71 |
|
}); |
274
|
|
|
|
275
|
71 |
|
$this->app->singleton('firewall.middleware.whitelist', function () { |
276
|
|
|
return new FirewallWhitelist(new Whitelist()); |
277
|
71 |
|
}); |
278
|
71 |
|
} |
279
|
|
|
|
280
|
71 |
|
private function registerMigrations() |
281
|
|
|
{ |
282
|
71 |
|
$this->loadMigrationsFrom(__DIR__.'/../../migrations'); |
283
|
71 |
|
} |
284
|
|
|
|
285
|
|
|
private function registerGeoIp() |
286
|
|
|
{ |
287
|
71 |
|
$this->app->singleton('firewall.geoip', function () { |
288
|
28 |
|
return new GeoIp($this->getConfig('geoip_database_path')); |
289
|
71 |
|
}); |
290
|
71 |
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Register the List Artisan command. |
294
|
|
|
* |
295
|
|
|
* @return void |
296
|
|
|
*/ |
297
|
|
|
private function registerRemoveCommand() |
298
|
|
|
{ |
299
|
23 |
|
$this->app->singleton('firewall.remove.command', function () { |
300
|
23 |
|
return new RemoveCommand(); |
301
|
23 |
|
}); |
302
|
|
|
|
303
|
23 |
|
$this->commands('firewall.remove.command'); |
304
|
23 |
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Register the List Artisan command. |
308
|
|
|
* |
309
|
|
|
* @return void |
310
|
|
|
*/ |
311
|
|
|
private function registerReportCommand() |
312
|
|
|
{ |
313
|
71 |
|
$this->app->singleton('firewall.list.command', function () { |
314
|
71 |
|
return new ReportCommand(); |
315
|
71 |
|
}); |
316
|
|
|
|
317
|
71 |
|
$this->commands('firewall.list.command'); |
318
|
71 |
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Register the updategeoip command. |
322
|
|
|
*/ |
323
|
|
|
private function registerUpdateGeoIpCommand() |
324
|
|
|
{ |
325
|
71 |
|
$this->app->singleton('firewall.updategeoip.command', function () { |
326
|
71 |
|
return new UpdateGeoIpCommand(); |
327
|
71 |
|
}); |
328
|
|
|
|
329
|
71 |
|
$this->commands('firewall.updategeoip.command'); |
330
|
71 |
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Register the Whitelist Artisan command. |
334
|
|
|
* |
335
|
|
|
* @return void |
336
|
|
|
*/ |
337
|
|
|
private function registerWhitelistCommand() |
338
|
|
|
{ |
339
|
23 |
|
$this->app->singleton('firewall.whitelist.command', function () { |
340
|
23 |
|
return new WhitelistCommand(); |
341
|
23 |
|
}); |
342
|
|
|
|
343
|
|
|
$this->commands('firewall.whitelist.command'); |
344
|
|
|
} |
345
|
|
|
} |
346
|
|
|
|
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.