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