Skip to content

Commit e438df5

Browse files
feat(*): Use crowdsec/remediation-engine 2.0.0 and crowdsec/common
1 parent 71d10a8 commit e438df5

File tree

14 files changed

+122
-62
lines changed

14 files changed

+122
-62
lines changed

CHANGELOG.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,31 @@
22
All notable changes to this project will be documented in this file.
33

44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/)
5-
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
5+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6+
7+
## Public API
8+
9+
The purpose of this section is to declare the public API of this library as required by [item 1 of semantic versioning specification](https://semver.org/spec/v2.0.0.html#spec-item-1).
10+
11+
The public API of this library consists of all public or protected methods, properties and constants belonging to
12+
the `src` folder.
13+
14+
---
15+
16+
## [1.0.0](https://github.com/crowdsecurity/php-cs-bouncer/releases/tag/v1.0.0) - 2023-??-??
17+
[_Compare with previous release_](https://github.com/crowdsecurity/php-cs-bouncer/compare/v0.36.0...v1.0.0)
18+
19+
### Changed
20+
- Change version to `1.0.0`: first stable release
21+
- Use `crowdsec/common` package
22+
23+
### Added
24+
25+
- Add public API declaration
26+
27+
28+
29+
---
630

731

832
## [0.36.0](https://github.com/crowdsecurity/php-cs-bouncer/releases/tag/v0.36.0) - 2023-01-26

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
],
4141
"require": {
4242
"php": ">=7.2.5",
43-
"crowdsec/remediation-engine": "0.6.1",
43+
"crowdsec/remediation-engine": "^2.0.0",
44+
"crowdsec/common": "^1.2.0",
4445
"symfony/config": "^4.4.27 || ^5.2 || ^6.0",
4546
"twig/twig": "^3.4.2",
4647
"gregwar/captcha": "^1.1",

docs/USER_GUIDE.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,26 @@ Once you set up your server as below, every browser access to a php script will
8888

8989
You will have to :
9090

91+
- copy sources of the lib in some `/path/to/the/crowdsec-lib` folder
92+
9193
- give the correct permission for the folder that contains the lib
9294

9395
- copy the `scripts/auto-prepend/settings.example.php` to a `scripts/auto-prepend/settings.php` file
9496

97+
- run the composer installation process to retrieve all necessary dependencies
98+
9599
- set an `auto_prepend_file` directive in your PHP setup.
96100

101+
- Optionally, if you want to use the standalone bouncer in stream mode, you wil have to set a cron task to refresh
102+
cache periodically.
103+
104+
### Copy sources
105+
106+
Create a folder `crowdsec-lib` and clone the sources:
107+
108+
```
109+
mkdir -p /path/to/the/crowdsec-lib && git clone git@github.com:crowdsecurity/php-cs-bouncer.git /path/to/the/crowdsec-lib
110+
```
97111

98112
### Files permission
99113

@@ -105,6 +119,14 @@ You can achieve it by running command like:
105119
sudo chown www-data /path/to/the/crowdsec-lib
106120
```
107121

122+
### Composer
123+
124+
You should run the composer installation process:
125+
126+
```
127+
cd /path/to/the/crowdsec-lib && composer install
128+
```
129+
108130
### Settings file
109131

110132
Please copy the `scripts/auto-prepend/settings.example.php` to a `scripts/auto-prepend/settings.php`
@@ -154,6 +176,26 @@ or modify your `Virtual Host` accordingly:
154176
```
155177

156178

179+
### Stream mode cron task
180+
181+
To use the stream mode, you first have to set the `stream_mode` setting value to `true` in your
182+
`scripts/auto-prepend/settings.php` file.
183+
184+
Then, you could edit the webserver user (e.g. `www-data`) crontab:
185+
186+
```
187+
sudo -u www-data crontab -e
188+
189+
```
190+
191+
and add the following line
192+
193+
```
194+
* * * * * /usr/bin/php /absolute/path/to/scripts/auto-prepend/refresh-cache.php
195+
```
196+
197+
In this example, cache is refreshed every minute, but you can modify the cron expression depending on your needs.
198+
157199
## Create your own bouncer
158200

159201
### Implementation

scripts/auto-prepend/bounce.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919
} catch (\Throwable $e) {
2020
$displayErrors = $crowdSecStandaloneBouncerConfig['display_errors'] ?? false;
2121
if (true === $displayErrors) {
22-
throw new BouncerException($e->getMessage(), $e->getCode(), $e);
22+
throw new BouncerException($e->getMessage(), (int)$e->getCode(), $e);
2323
}
2424
}

scripts/auto-prepend/refresh-cache.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,3 @@
1313

1414
$bouncer = new StandaloneBouncer($crowdSecStandaloneBouncerConfig);
1515
$bouncer->refreshBlocklistCache();
16-
echo 'Cache has been refreshed (if stream mode is enabled)' . \PHP_EOL;

scripts/auto-prepend/settings.example.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@
164164
'api_url' => Constants::DEFAULT_LAPI_URL,
165165

166166
// In seconds. The timeout when calling LAPI. Must be greater or equal than 1. Defaults to 1 sec.
167-
'api_timeout' => 1,
167+
'api_timeout' => Constants::API_TIMEOUT,
168168

169169
// ============================================================================#
170170
// Remediation engine configs

src/AbstractBouncer.php

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
namespace CrowdSecBouncer;
66

77
use CrowdSec\LapiClient\Bouncer as BouncerClient;
8-
use CrowdSec\LapiClient\RequestHandler\Curl;
9-
use CrowdSec\LapiClient\RequestHandler\FileGetContents;
8+
use CrowdSec\Common\Client\RequestHandler\Curl;
9+
use CrowdSec\Common\Client\RequestHandler\FileGetContents;
1010
use CrowdSec\RemediationEngine\AbstractRemediation;
1111
use CrowdSec\RemediationEngine\CacheStorage\AbstractCache;
1212
use CrowdSec\RemediationEngine\CacheStorage\CacheStorageException;
@@ -94,10 +94,15 @@ public function bounceCurrentIp(): void
9494
* @return bool If the cache has been successfully cleared or not
9595
*
9696
*
97+
* @throws BouncerException
9798
*/
9899
public function clearCache(): bool
99100
{
100-
return $this->getRemediationEngine()->clearCache();
101+
try {
102+
return $this->getRemediationEngine()->clearCache();
103+
} catch (\Exception $e) {
104+
throw new BouncerException($e->getMessage(), (int)$e->getCode(), $e);
105+
}
101106
}
102107

103108
/**
@@ -180,12 +185,33 @@ abstract public function getRequestUri(): string;
180185
* This method prune the cache: it removes all the expired cache items.
181186
*
182187
* @return bool If the cache has been successfully pruned or not
183-
* @throws CacheStorageException
184188
*
189+
* @throws BouncerException
185190
*/
186191
public function pruneCache(): bool
187192
{
188-
return $this->getRemediationEngine()->pruneCache();
193+
try {
194+
return $this->getRemediationEngine()->pruneCache();
195+
} catch (\Exception $e) {
196+
throw new BouncerException($e->getMessage(), (int)$e->getCode(), $e);
197+
}
198+
}
199+
200+
/**
201+
* Process a simple cache test
202+
*
203+
* @return void
204+
* @throws BouncerException
205+
* @throws InvalidArgumentException
206+
*/
207+
public function testCacheConnection(): void
208+
{
209+
try {
210+
$cache = $this->getRemediationEngine()->getCacheStorage();
211+
$cache->getItem(AbstractCache::CONFIG);
212+
} catch (\Exception $e) {
213+
throw new BouncerException($e->getMessage(), (int)$e->getCode(), $e);
214+
}
189215
}
190216

191217
/**
@@ -194,11 +220,15 @@ public function pruneCache(): bool
194220
*
195221
* @return array Number of deleted and new decisions
196222
*
197-
*
223+
* @throws BouncerException
198224
*/
199225
public function refreshBlocklistCache(): array
200226
{
201-
return $this->getRemediationEngine()->refreshDecisions();
227+
try {
228+
return $this->getRemediationEngine()->refreshDecisions();
229+
} catch (\Exception $e) {
230+
throw new BouncerException($e->getMessage(), (int)$e->getCode(), $e);
231+
}
202232
}
203233

204234
/**
@@ -227,7 +257,7 @@ public function run(): bool
227257
'line' => $e->getLine(),
228258
]);
229259
if (true === $this->getConfig('display_errors')) {
230-
throw new BouncerException($e->getMessage(), $e->getCode(), $e);
260+
throw new BouncerException($e->getMessage(), (int)$e->getCode(), $e);
231261
}
232262
}
233263

src/BouncerException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace CrowdSecBouncer;
66

7-
use RuntimeException;
7+
use CrowdSec\Common\Exception;
88

99
/**
1010
* Exception interface for all exceptions thrown by CrowdSec Bouncer.
@@ -16,6 +16,6 @@
1616
* @copyright Copyright (c) 2020+ CrowdSec
1717
* @license MIT License
1818
*/
19-
class BouncerException extends RuntimeException
19+
class BouncerException extends Exception
2020
{
2121
}

src/Configuration.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
namespace CrowdSecBouncer;
66

7+
use CrowdSec\Common\Configuration\AbstractConfiguration;
78
use InvalidArgumentException;
89
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
910
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
1011
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
11-
use Symfony\Component\Config\Definition\ConfigurationInterface;
1212

1313
/**
1414
* The Library configuration. You'll find here all configuration possible. Used when instantiating the library.
@@ -20,7 +20,7 @@
2020
* @copyright Copyright (c) 2020+ CrowdSec
2121
* @license MIT License
2222
*/
23-
class Configuration implements ConfigurationInterface
23+
class Configuration extends AbstractConfiguration
2424
{
2525
/**
2626
* @var string[]
@@ -44,16 +44,6 @@ class Configuration implements ConfigurationInterface
4444
'text',
4545
];
4646

47-
/**
48-
* Keep only necessary configs
49-
* @param array $configs
50-
* @return array
51-
*/
52-
public function cleanConfigs(array $configs): array
53-
{
54-
return array_intersect_key($configs, array_flip($this->keys));
55-
}
56-
5747
/**
5848
* {@inheritdoc}
5949
* @throws InvalidArgumentException

src/Constants.php

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace CrowdSecBouncer;
66

7+
use CrowdSec\RemediationEngine\Constants as RemConstants;
8+
79
/**
810
* Every constant of the library are set here.
911
*
@@ -14,28 +16,16 @@
1416
* @copyright Copyright (c) 2020+ CrowdSec
1517
* @license MIT License
1618
*/
17-
class Constants
19+
class Constants extends RemConstants
1820
{
19-
/** @var int The timeout when calling LAPI */
20-
public const API_TIMEOUT = 120;
21-
/** @var string The API-KEY auth type */
22-
public const AUTH_KEY = 'api_key';
23-
/** @var string The TLS auth type */
24-
public const AUTH_TLS = 'tls';
2521
/** @var string The "disabled" bouncing level */
2622
public const BOUNCING_LEVEL_DISABLED = 'bouncing_disabled';
2723
/** @var string The "flex" bouncing level */
2824
public const BOUNCING_LEVEL_FLEX = 'flex_bouncing';
2925
/** @var string The "normal" bouncing level */
3026
public const BOUNCING_LEVEL_NORMAL = 'normal_bouncing';
31-
/** @var int The duration we keep a bad IP in cache */
32-
public const CACHE_EXPIRATION_FOR_BAD_IP = 20;
3327
/** @var int The duration we keep a captcha flow in cache */
3428
public const CACHE_EXPIRATION_FOR_CAPTCHA = 86400;
35-
/** @var int The duration we keep a clean IP in cache */
36-
public const CACHE_EXPIRATION_FOR_CLEAN_IP = 5;
37-
/** @var int The duration we keep a geolocation result in cache */
38-
public const CACHE_EXPIRATION_FOR_GEO = 86400;
3929
/** @var string The "MEMCACHED" cache system */
4030
public const CACHE_SYSTEM_MEMCACHED = 'memcached';
4131
/** @var string The "PHPFS" cache system */
@@ -46,26 +36,10 @@ class Constants
4636
public const CACHE_TAG_CAPTCHA = 'captcha';
4737
/** @var string The Default URL of the CrowdSec LAPI */
4838
public const DEFAULT_LAPI_URL = 'http://localhost:8080';
49-
/** @var string The "MaxMind" geolocation type */
50-
public const GEOLOCATION_TYPE_MAXMIND = 'maxmind';
51-
/** @var string The Maxmind "Country" database type */
52-
public const MAXMIND_COUNTRY = 'country';
53-
/** @var string The ban remediation */
54-
public const REMEDIATION_BAN = 'ban';
55-
/** @var string The bypass remediation */
56-
public const REMEDIATION_BYPASS = 'bypass';
57-
/** @var string The captcha remediation */
58-
public const REMEDIATION_CAPTCHA = 'captcha';
59-
/** @var string The CrowdSec country scope for decisions */
60-
public const SCOPE_COUNTRY = 'country';
61-
/** @var string The CrowdSec Ip scope for decisions */
62-
public const SCOPE_IP = 'ip';
63-
/** @var string The CrowdSec Range scope for decisions */
64-
public const SCOPE_RANGE = 'range';
6539
/** @var string Path for html templates folder (e.g. ban and captcha wall) */
6640
public const TEMPLATES_DIR = __DIR__ . "/templates";
6741
/** @var string The last version of this library */
68-
public const VERSION = 'v0.36.0';
42+
public const VERSION = 'v1.0.0';
6943
/** @var string The "disabled" x-forwarded-for setting */
7044
public const X_FORWARDED_DISABLED = 'no_forward';
7145
}

0 commit comments

Comments
 (0)