|
| 1 | + |
| 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/) |
0 commit comments