Skip to content

Commit 191ba39

Browse files
committed
First Commit with complete code
0 parents  commit 191ba39

25 files changed

+1644
-0
lines changed

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Neelkanth Kaushik <me.neelkanth@gmail.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 280 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,280 @@
1+
![Laravel Surveillance Logo](https://github.com/neelkanthk/repo_logos/blob/master/surveillance_small.png?raw=true)
2+
3+
# Surveillance
4+
5+
A Laravel package to put malicious users, IP addresses and anonymous browser fingerprints under surveillance, write surveillance logs and block malicious ones from accessing the app.
6+
7+
#### NOTE: This package does not provide a client side library for browser fingerprinting. [FingerprintJS Open Source](https://github.com/fingerprintjs/fingerprintjs) is a good library to use for client side browser fingerprinting.
8+
9+
__This package provides__:
10+
11+
_1. A middleware to be used on routes._
12+
13+
_2. A command line interface to enable/disable surveillance and block/unblock access._
14+
15+
_3. A fluent API to programmatically enable/disable surveillance, block/unblock access and log the requests at runtime._
16+
17+
_4. By default the package used MySQL database as storage but the package can be extended to use virtually any storage technology._
18+
19+
### Minimum Requirements
20+
21+
#### 1. Laravel 6.0
22+
#### 2. PHP 7.2
23+
24+
## Installation
25+
26+
#### 1. Install the package via composer:
27+
28+
```bash
29+
composer require neelkanthk/laravel-surveillance
30+
```
31+
32+
#### 2.1. Publish the migration files:
33+
```bash
34+
php artisan vendor:publish --provider="Neelkanth\Laravel\Surveillance\Providers\SurveillanceServiceProvider" --tag="migrations"
35+
```
36+
37+
#### 2.2. Publish language files:
38+
```bash
39+
php artisan vendor:publish --provider="Neelkanth\Laravel\Surveillance\Providers\SurveillanceServiceProvider" --tag="lang"
40+
```
41+
42+
#### 3. Run the migrations
43+
```bash
44+
php artisan migrate
45+
```
46+
47+
#### 4. After migrations have been run two tables will be created in the database namely `surveillance_managers` and `surveillance_logs`
48+
49+
#### 5. You can publish the config file with:
50+
```bash
51+
php artisan vendor:publish --provider="Neelkanth\Laravel\Surveillance\Providers\SurveillanceServiceProvider" --tag="config"
52+
```
53+
54+
This is the contents of the file that will be published at `config/surveillance.php`:
55+
56+
57+
```php
58+
return [
59+
60+
/*
61+
* The name of the header to be used for browser fingerprint
62+
*/
63+
"fingerprint-header-key" => "fingerprint",
64+
65+
/*
66+
* This class is responsible enabling, disabling, blocking and unblocking.
67+
* To override the default functionality extend the below class and provide its name here.
68+
*/
69+
"manager-repository" => 'Neelkanth\Laravel\Surveillance\Implementations\SurveillanceManagerRepository',
70+
71+
/*
72+
* This class is responsible for logging the surveillance enabled requests
73+
* To override the default functionality extend the below class and provide its name here.
74+
*/
75+
"log-repository" => 'Neelkanth\Laravel\Surveillance\Implementations\SurveillanceLogRepository',
76+
77+
/*
78+
* The types which are allowed currently.
79+
* DO NOT MODIFY THESE
80+
*/
81+
"allowed-types" => ["userid", "ip", "fingerprint"]
82+
];
83+
```
84+
85+
## CLI Usage
86+
87+
#### Enable surveillance for an IP Address
88+
```bash
89+
php artisan surveillance:enable ip 192.1.2.4
90+
```
91+
92+
#### Disable surveillance for an IP Address
93+
```bash
94+
php artisan surveillance:disable ip 192.1.2.4
95+
```
96+
97+
#### Enable surveillance for a User ID
98+
```bash
99+
php artisan surveillance:enable userid 1234
100+
```
101+
102+
#### Disable surveillance for a User ID
103+
```bash
104+
php artisan surveillance:disable userid 1234
105+
```
106+
107+
#### Enable surveillance for Browser Fingerprint
108+
```bash
109+
php artisan surveillance:enable fingerprint hjP0tLyIUy7SXaSY6gyb
110+
```
111+
112+
#### Disable surveillance for Browser Fingerprint
113+
```bash
114+
php artisan surveillance:disable fingerprint hjP0tLyIUy7SXaSY6gyb
115+
```
116+
117+
#### Block an IP Address
118+
```bash
119+
php artisan surveillance:block ip 192.1.2.4
120+
```
121+
122+
#### UnBlock an IP Address
123+
```bash
124+
php artisan surveillance:unblock ip 192.1.2.4
125+
```
126+
127+
#### Block a User ID
128+
```bash
129+
php artisan surveillance:block userid 1234
130+
```
131+
132+
#### UnBlock a User ID
133+
```bash
134+
php artisan surveillance:unblock userid 1234
135+
```
136+
137+
#### Block a Browser Fingerprint
138+
```bash
139+
php artisan surveillance:block fingerprint hjP0tLyIUy7SXaSY6gyb
140+
```
141+
142+
#### UnBlock a Browser Fingerprint
143+
```bash
144+
php artisan surveillance:unblock fingerprint hjP0tLyIUy7SXaSY6gyb
145+
```
146+
147+
#### Remove a Surveillance record from Database
148+
```bash
149+
php artisan surveillance:remove ip 192.5.4.3
150+
```
151+
152+
## Middleware Usage
153+
154+
#### You can use the 'surveillance' middleware on any route or route group just like any other middleware.
155+
156+
```php
157+
Route::middleware(["surveillance"])->get('/', function () {
158+
159+
});
160+
```
161+
162+
## Programmatic Usage
163+
164+
#### Enable Surveillance
165+
166+
```php
167+
use Neelkanth\Laravel\Surveillance\Services\Surveillance;
168+
Surveillance::manager()->type("ip")->value("192.5.4.1")->enableSurveillance();
169+
```
170+
171+
#### Block Access
172+
173+
```php
174+
use Neelkanth\Laravel\Surveillance\Services\Surveillance;
175+
Surveillance::manager()->type("userid")->value(2121)->blockAccess();
176+
```
177+
178+
#### Logging a Request (Works when surveillance in enabled on User ID, IP Address or Browser Fingerprint)
179+
180+
```php
181+
use Neelkanth\Laravel\Surveillance\Services\Surveillance;
182+
Surveillance::logger()->writeLog();
183+
```
184+
185+
## Allowed Types
186+
187+
#### Currently only userid, ip and fingerprint types are allowed.
188+
189+
## Customizing and Overriding the defaults
190+
191+
### To override the default surveillance management funtionality
192+
193+
#### Step 1: Extend the `SurveillanceManagerRepository` Class and override all of its methods
194+
195+
```php
196+
//Example repository to use MongoDB instead of MySQL
197+
namespace App;
198+
199+
use Neelkanth\Laravel\Surveillance\Implementations\SurveillanceManagerRepository;
200+
use Illuminate\Support\Carbon;
201+
202+
class SurveillanceManagerMongoDbRepository extends SurveillanceManagerRepository
203+
{
204+
public function enableSurveillance()
205+
{
206+
$surveillance = $this->getRecord();
207+
if (is_null($surveillance)) {
208+
$surveillance["type"] = $this->getType();
209+
$surveillance["value"] = $this->getValue();
210+
}
211+
$surveillance["surveillance_enabled"] = 1;
212+
$surveillance["surveillance_enabled_at"] = Carbon::now()->toDateTimeString();
213+
$collection = (new \MongoDB\Client)->surveillance->manager;
214+
$insertOneResult = $collection->insertOne($surveillance);
215+
return $insertOneResult;
216+
}
217+
}
218+
```
219+
220+
#### Step 2: Provide the custom class in the `config/surveillance.php` file's `manager-repository` key
221+
222+
```php
223+
/*
224+
* This class is responsible enabling, disabling, blocking and unblocking.
225+
* To override the default functionality extend the below class and provide its name here.
226+
*/
227+
"manager-repository" => 'App\SurveillanceManagerMongoDbRepository',
228+
```
229+
230+
### To override the default logging funtionality
231+
232+
#### Step 1: Extend the `SurveillanceLogRepository` Class and override all of its methods
233+
234+
```php
235+
236+
//Example repository to write Logs in MongoDB instead of MySQL
237+
namespace App;
238+
239+
use Neelkanth\Laravel\Surveillance\Implementations\SurveillanceLogRepository;
240+
241+
class SurveillanceLogMongoDbRepository extends SurveillanceLogRepository
242+
{
243+
public function writeLog($dataToLog = null)
244+
{
245+
if (!is_null($dataToLog)) {
246+
$this->setLogToWrite($dataToLog);
247+
}
248+
$log = $this->getLogToWrite();
249+
if (!empty($log) && is_array($log)) {
250+
$collection = (new \MongoDB\Client)->surveillance->logs;
251+
$insertOneResult = $collection->insertOne($log);
252+
}
253+
}
254+
}
255+
```
256+
257+
#### Step 2: Provide the custom class in the `config/surveillance.php` file's `log-repository` key
258+
259+
```php
260+
/*
261+
* This class is responsible for logging the surveillance enabled requests
262+
* To override the default functionality extend the below class and provide its name here.
263+
*/
264+
"log-repository" => 'App\SurveillanceLogMongoDbRepository',
265+
```
266+
267+
## Contributing
268+
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
269+
270+
## Security
271+
If you discover any security-related issues, please email me.neelkanth@gmail.com instead of using the issue tracker.
272+
273+
## Credits
274+
275+
- [Neelkanth Kaushik](https://github.com/neelkanthk)
276+
- [All Contributors](../../contributors)
277+
- [CCTV Icon](https://pixabay.com/vectors/image-sign-warning-icon-cctv-3042333)
278+
279+
## License
280+
[MIT](https://choosealicense.com/licenses/mit/)

composer.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "neelkanthk/laravel-surveillance",
3+
"description": "Put users, IP addresses and anonymous browser fingerprints on surveillance and block malicious ones.",
4+
"keywords": ["laravel", "laravel-package", "laravel-security", "php", "fingerprintjs", "fingeprintjs2", "access-control"],
5+
"type": "laravel-package",
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Neelkanth Kaushik",
10+
"email": "me.neelkanth@gmail.com"
11+
}
12+
],
13+
"require": {
14+
"php": "^7.2",
15+
"laravel/framework": "^6.0"
16+
},
17+
"autoload": {
18+
"psr-4": {
19+
"Neelkanth\\Laravel\\Surveillance\\": "src"
20+
}
21+
},
22+
"extra": {
23+
"laravel": {
24+
"providers": [
25+
"Neelkanth\\Laravel\\Surveillance\\Providers\\SurveillanceServiceProvider"
26+
],
27+
"aliases": {
28+
"Surveillance": "Neelkanth\\Laravel\\Surveillance\\Facades\\Surveillance"
29+
}
30+
}
31+
}
32+
}

config/surveillance.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
return [
4+
5+
/*
6+
* The name of the header to be used for browser fingerprint
7+
*/
8+
"fingerprint-header-key" => "fingerprint",
9+
10+
/*
11+
* This class is responsible enabling, disabling, blocking and unblocking.
12+
* To override the default functionality extend the below class and provide its name here.
13+
*/
14+
"manager-repository" => 'Neelkanth\Laravel\Surveillance\Implementations\SurveillanceManagerRepository',
15+
16+
/*
17+
* This class is responsible for logging the surveillance enabled requests
18+
* To override the default functionality extend the below class and provide its name here.
19+
*/
20+
"log-repository" => 'Neelkanth\Laravel\Surveillance\Implementations\SurveillanceLogRepository',
21+
22+
/*
23+
* The types which are allowed currently.
24+
* DO NOT MODIFY THESE
25+
*/
26+
"allowed-types" => ["userid", "ip", "fingerprint"]
27+
];

0 commit comments

Comments
 (0)