Skip to content

Commit 4ba1c9b

Browse files
docs(ddev): Add check-ip script documentation for ddev usage
1 parent 0e5529b commit 4ba1c9b

File tree

1 file changed

+85
-1
lines changed

1 file changed

+85
-1
lines changed

docs/ddev.md

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,88 @@ Finally, run
121121

122122
```
123123
ddev exec BOUNCER_KEY=your-bouncer-key LAPI_URL=http://crowdsec:8080 MEMCACHED_DSN=memcached://memcached:11211 REDIS_DSN=redis://redis:6379 /usr/bin/php ./my-own-modules/crowdsec-php-lib/vendor/bin/phpunit --testdox --colors --exclude-group ignore ./my-own-modules/crowdsec-php-lib/tests/IpVerificationTest.php
124-
```
124+
```
125+
126+
### Use a `check-ip` php script for test
127+
128+
129+
Create this short `check-ip.php` script in your root folder:
130+
131+
```php
132+
<?php
133+
134+
require __DIR__ . '/my-own-modules/crowdsec-php-lib/vendor/autoload.php';
135+
136+
use CrowdSecBouncer\Bouncer;
137+
use Symfony\Component\Cache\Adapter\PhpFilesAdapter;
138+
use Monolog\Formatter\LineFormatter;
139+
use Monolog\Handler\RotatingFileHandler;
140+
use Monolog\Handler\StreamHandler;
141+
use Monolog\Logger;
142+
143+
// Init cache adapter
144+
145+
$cacheAdapter = new PhpFilesAdapter('', 0, __DIR__.'/.cache');
146+
147+
// Parse argument
148+
149+
$requestedIp = $argv[1];
150+
$bouncerKey = $argv[2];
151+
if (!$requestedIp || !$bouncerKey) {
152+
die('Usage: php check-ip.php <IP> <BOUNCER_KEY>');
153+
}
154+
// Instantiate the Stream logger with info level(optional)
155+
$logger = new Logger('example');
156+
157+
// Display logs with INFO verbosity
158+
$streamHandler = new StreamHandler('php://stdout', Logger::DEBUG);
159+
$streamHandler->setFormatter(new LineFormatter("[%datetime%] %message% %context%\n"));
160+
$logger->pushHandler($streamHandler);
161+
162+
// Store logs with WARNING verbosity
163+
$fileHandler = new RotatingFileHandler(__DIR__.'/crowdsec.log', 0, Logger::DEBUG);
164+
$logger->pushHandler($fileHandler);
165+
166+
// Init
167+
$bouncer = new Bouncer($cacheAdapter, $logger);
168+
$bouncer->configure([
169+
'api_key' => $bouncerKey,
170+
'api_url' => 'http://crowdsec:8080'
171+
]
172+
);
173+
174+
// Ask remediation to LAPI
175+
176+
echo "\nVerify $requestedIp...\n";
177+
$remediation = $bouncer->getRemediationForIp($requestedIp);
178+
echo "\nResult: $remediation\n\n"; // "ban", "captcha" or "bypass"
179+
```
180+
181+
To run this script, you have to know your bouncer key `<BOUNCER_KEY>` and run
182+
```command
183+
ddev exec php check-ip.php <IP> <BOUNCER_KEY>
184+
```
185+
186+
As a reminder, your bouncer key is returned by the `ddev create-bouncer` command.
187+
188+
For example, run the php script:
189+
190+
```bash
191+
ddev exec php check-ip.php 1.2.3.4 <BOUNCER_KEY>
192+
```
193+
194+
As your CrowdSec instance contains no decisions, you received the result "bypass".
195+
196+
Let's now add a new decision in CrowdSec, for example we will ban the 1.2.3.4/30 for 4h:
197+
198+
```bash
199+
ddev exec -s crowdsec cscli decisions add --range 1.2.3.4/30 --duration 4h --type ban
200+
```
201+
202+
Now, if you run the php script against the `1.2.3.4` IP:
203+
204+
```bash
205+
ddev exec php check-ip.php 1.2.3.4 <BOUNCER_KEY>
206+
```
207+
208+
LAPI will advise you to ban this IP as it's within the 1.2.3.4/30 range.

0 commit comments

Comments
 (0)