Skip to content

Commit ebaf946

Browse files
Merge pull request #99 from julienloizelet/feat/curl
Feat/curl
2 parents d91e45b + 8a20610 commit ebaf946

24 files changed

+375
-96
lines changed

.github/workflows/test-suite.yml

+45-5
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,18 @@ jobs:
9797
run: |
9898
ddev exec BOUNCER_KEY=${{ env.BOUNCER_KEY }} LAPI_URL=http://crowdsec:8080 MEMCACHED_DSN=memcached://memcached:11211 REDIS_DSN=redis://redis:6379 /usr/bin/php ./${{env.EXTENSION_PATH}}/vendor/bin/phpunit --testdox --colors --exclude-group ignore ./${{env.EXTENSION_PATH}}/tests/Integration/IpVerificationTest.php
9999
100+
- name: Run PHP UNIT tests (IP verification with cURL)
101+
run: |
102+
ddev exec BOUNCER_KEY=${{ env.BOUNCER_KEY }} USE_CURL=1 LAPI_URL=http://crowdsec:8080 MEMCACHED_DSN=memcached://memcached:11211 REDIS_DSN=redis://redis:6379 /usr/bin/php ./${{env.EXTENSION_PATH}}/vendor/bin/phpunit --testdox --colors --exclude-group ignore ./${{env.EXTENSION_PATH}}/tests/Integration/IpVerificationTest.php
103+
100104
- name: Run PHP UNIT tests (Geolocation)
101105
run: |
102106
ddev exec BOUNCER_KEY=${{ env.BOUNCER_KEY }} LAPI_URL=http://crowdsec:8080 /usr/bin/php ./${{env.EXTENSION_PATH}}/vendor/bin/phpunit --testdox --colors --exclude-group ignore ./${{env.EXTENSION_PATH}}/tests/Integration/GeolocationTest.php
103107
108+
- name: Run PHP UNIT tests (Geolocation with cURL)
109+
run: |
110+
ddev exec BOUNCER_KEY=${{ env.BOUNCER_KEY }} USE_CURL=1 LAPI_URL=http://crowdsec:8080 /usr/bin/php ./${{env.EXTENSION_PATH}}/vendor/bin/phpunit --testdox --colors --exclude-group ignore ./${{env.EXTENSION_PATH}}/tests/Integration/GeolocationTest.php
111+
104112
- name: Prepare Standalone Bouncer end-to-end tests
105113
run: |
106114
ddev create-watcher
@@ -135,38 +143,70 @@ jobs:
135143
exit 1
136144
fi
137145
138-
- name: Run Standalone Bouncer end-to-end test (live mode without geolocation)
146+
- name: Run "live mode with file_get_contents and without geolocation" test
139147
run: |
140148
cd ${{ github.workspace }}/${{env.EXTENSION_PATH}}
141149
cat scripts/auto-prepend/settings.php
142150
cd ${{ github.workspace }}/${{env.EXTENSION_PATH}}/tests/end-to-end/__scripts__
143151
./run-tests.sh ci "./__tests__/1-live-mode.js"
144152
145-
- name: Run Standalone Bouncer end-to-end test (live mode with geolocation)
153+
- name: Run "live mode with cURL and without geolocation" test
154+
run: |
155+
cd ${{ github.workspace }}/${{env.EXTENSION_PATH}}
156+
sed -i 's/\x27use_curl\x27 => false/\x27use_curl\x27 => true/g' scripts/auto-prepend/settings.php
157+
cat scripts/auto-prepend/settings.php
158+
cd ${{ github.workspace }}/${{env.EXTENSION_PATH}}/tests/end-to-end/__scripts__
159+
./run-tests.sh ci "./__tests__/1-live-mode.js"
160+
161+
- name: Run "live mode with file_get_contents and with geolocation" test
146162
run: |
147163
cd ${{ github.workspace }}/${{env.EXTENSION_PATH}}
164+
sed -i 's/\x27use_curl\x27 => true/\x27use_curl\x27 => false/g' scripts/auto-prepend/settings.php
148165
sed -i 's/\x27enabled\x27 => false/\x27enabled\x27 => true/g' scripts/auto-prepend/settings.php
149166
sed -i 's/\x27forced_test_forwarded_ip\x27 => \x27\x27/\x27forced_test_forwarded_ip\x27 => \x27${{env.JP_TEST_IP}}\x27/g' scripts/auto-prepend/settings.php
150167
cat scripts/auto-prepend/settings.php
151168
cd ${{ github.workspace }}/${{env.EXTENSION_PATH}}/tests/end-to-end/__scripts__
152169
./run-tests.sh ci "./__tests__/2-live-mode-with-geolocation.js"
153170
154-
- name: Run Standalone Bouncer end-to-end test (stream mode without geolocation)
171+
- name: Run "live mode with cURL and with geolocation" test
172+
run: |
173+
cd ${{ github.workspace }}/${{env.EXTENSION_PATH}}
174+
sed -i 's/\x27use_curl\x27 => false/\x27use_curl\x27 => true/g' scripts/auto-prepend/settings.php
175+
sed -i 's/\x27use_curl\x27 => true/\x27use_curl\x27 => false/g' scripts/auto-prepend/settings.php
176+
sed -i 's/\x27enabled\x27 => false/\x27enabled\x27 => true/g' scripts/auto-prepend/settings.php
177+
sed -i 's/\x27forced_test_forwarded_ip\x27 => \x27\x27/\x27forced_test_forwarded_ip\x27 => \x27${{env.JP_TEST_IP}}\x27/g' scripts/auto-prepend/settings.php
178+
cat scripts/auto-prepend/settings.php
179+
cd ${{ github.workspace }}/${{env.EXTENSION_PATH}}/tests/end-to-end/__scripts__
180+
./run-tests.sh ci "./__tests__/2-live-mode-with-geolocation.js"
181+
182+
- name: Run "stream mode with file_get_contents and without geolocation" test
155183
run: |
156184
cd ${{ github.workspace }}/${{env.EXTENSION_PATH}}
185+
sed -i 's/\x27use_curl\x27 => true/\x27use_curl\x27 => false/g' scripts/auto-prepend/settings.php
157186
sed -i 's/\x27enabled\x27 => true/\x27enabled\x27 => false/g' scripts/auto-prepend/settings.php
158187
sed -i 's/\x27forced_test_forwarded_ip\x27 => \x27${{env.JP_TEST_IP}}\x27/\x27forced_test_forwarded_ip\x27 => \x27\x27/g' scripts/auto-prepend/settings.php
159188
sed -i 's/\x27stream_mode\x27 => false/\x27stream_mode\x27 => true/g' scripts/auto-prepend/settings.php
160189
cat scripts/auto-prepend/settings.php
161190
cd ${{ github.workspace }}/${{env.EXTENSION_PATH}}/tests/end-to-end/__scripts__
162191
./run-tests.sh ci "./__tests__/3-stream-mode.js"
163192
164-
- name: Run Standalone Bouncer end-to-end test (standalone geolocation)
193+
- name: Run "stream mode with cURL and without geolocation" test
194+
run: |
195+
cd ${{ github.workspace }}/${{env.EXTENSION_PATH}}
196+
sed -i 's/\x27use_curl\x27 => false/\x27use_curl\x27 => true/g' scripts/auto-prepend/settings.php
197+
sed -i 's/\x27enabled\x27 => true/\x27enabled\x27 => false/g' scripts/auto-prepend/settings.php
198+
sed -i 's/\x27forced_test_forwarded_ip\x27 => \x27${{env.JP_TEST_IP}}\x27/\x27forced_test_forwarded_ip\x27 => \x27\x27/g' scripts/auto-prepend/settings.php
199+
sed -i 's/\x27stream_mode\x27 => false/\x27stream_mode\x27 => true/g' scripts/auto-prepend/settings.php
200+
cat scripts/auto-prepend/settings.php
201+
cd ${{ github.workspace }}/${{env.EXTENSION_PATH}}/tests/end-to-end/__scripts__
202+
./run-tests.sh ci "./__tests__/3-stream-mode.js"
203+
204+
- name: Run "standalone geolocation" test
165205
run: |
166206
cd ${{ github.workspace }}/${{env.EXTENSION_PATH}}/tests/end-to-end/__scripts__
167207
./run-tests.sh ci "./__tests__/4-geolocation.js"
168208
169-
- name: Run Standalone Bouncer end-to-end test (live mode with IPv6)
209+
- name: Run "live mode with IPv6" test
170210
run: |
171211
cd ${{ github.workspace }}/${{env.EXTENSION_PATH}}
172212
sed -i 's/\x27forced_test_forwarded_ip\x27 => \x27\x27/\x27forced_test_forwarded_ip\x27 => \x27${{env.IPV6_TEST_IP}}\x27/g' scripts/auto-prepend/settings.php

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

77

8+
## [0.25.0] - 2022-07-22
9+
10+
### Added
11+
- Add a `use_curl` setting to make LAPI rest requests with `cURL` instead of `file_get_contents`
12+
813
## [0.24.0] - 2022-07-08
914

1015
### Added

composer.json

+7-2
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,14 @@
4545
"monolog/monolog": "^1.17 || ^2.1",
4646
"gregwar/captcha": "^1.1",
4747
"mlocati/ip-lib": "^1.18",
48-
"geoip2/geoip2": "^2.12.2"
48+
"geoip2/geoip2": "^2.12.2",
49+
"ext-json": "*"
4950
},
5051
"require-dev": {
51-
"phpunit/phpunit": "8.5.27"
52+
"phpunit/phpunit": "8.5.27",
53+
"ext-curl": "*"
54+
},
55+
"suggest": {
56+
"ext-curl": "*"
5257
}
5358
}

docs/INSTALLATION_GUIDE.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
## Requirements
2626

2727
- PHP >= 7.2
28-
- required php extensions : `ext-json`, `ext-gd`
28+
- required php extensions: `ext-json`, `ext-gd`
29+
- suggested php extension: `ext-curl`
2930

3031
## Installation
3132

docs/TECHNICAL_NOTES.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,15 @@ an empty array.
5050

5151
### Why not PHP 5.6?
5252

53-
Because this PHP version is no more supported since December 2018 (not even a security fix!).
53+
Because this PHP version is no more supported since December 2018 (not even a security fix).
5454
Also, a lot of libraries are no more compatible with this version.
5555
We don't want to use an older version of these libraries because Composer can only install one version of each extension/package.
5656
So, being compatible with this old PHP version means to be not compatible with projects using a new version of these libraries.
5757

5858
### Why not 7.0.x nor 7.1.x ?
5959

60-
These PHP versions are not anymore maintained for security fixes since 2019! We encourage you a lot to upgrade your PHP version. You can view the [full list of PHP versions lifecycle](https://www.php.net/supported-versions.php).
60+
These PHP versions are not anymore maintained for security fixes since 2019. We encourage you a lot to upgrade your
61+
PHP version. You can view the [full list of PHP versions lifecycle](https://www.php.net/supported-versions.php).
6162

6263
To get a robust library and not provide security bug unmaintained, we use [components](https://packagist.org/packages/symfony/cache#v3.4.47) under [LTS versioning](https://symfony.com/releases/3.4).
6364

docs/USER_GUIDE.md

+2
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ Here is the list of available settings:
125125

126126
- `api_timeout`: In seconds. The timeout when calling LAPI. Must be greater or equal than 1. Defaults to 1 sec.
127127
- `api_user_agent`: HTTP user agent used to call CLAPI. Default to this library name/current version.
128+
- `use_curl`: By default, this lib call the REST LAPI using `file_get_contents` method (`allow_url_fopen` is required).
129+
You can set `use_curl` to `true` in order to use `cURL` request instead (`curl` is in then required)
128130

129131
##### Debug
130132
- `debug_mode`:true to enable verbose debug log.

scripts/auto-prepend/settings.example.php

+7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
// In seconds. The timeout when calling LAPI. Must be greater or equal than 1. Defaults to 1 sec.
1919
'api_timeout'=> 1,
2020

21+
/**
22+
* By default, the lib call the REST LAPI using file_get_contents method (allow_url_fopen is required).
23+
* Set 'use_curl' to true in order to use cURL request instead (curl is in then required)
24+
*
25+
*/
26+
'use_curl' => false,
27+
2128
// HTTP user agent used to call LAPI. Default to this library name/current version.
2229
'api_user_agent'=> 'CrowdSec PHP Library/x.x.x',
2330

src/AbstractBounce.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ protected function handleForwardedFor(string $ip, array $configs): string
130130
$ipList = array_map('trim', array_values(array_filter(explode(',', $XForwardedForHeader))));
131131
$forwardedIp = end($ipList);
132132
}
133-
} else if ($configs['forced_test_forwarded_ip'] === Constants::X_FORWARDED_DISABLED) {
133+
} elseif ($configs['forced_test_forwarded_ip'] === Constants::X_FORWARDED_DISABLED) {
134134
$this->logger->debug('', [
135135
'type' => 'DISABLED_X_FORWARDED_FOR_USAGE',
136136
'original_ip' => $ip,
@@ -207,7 +207,7 @@ protected function shouldTrustXforwardedFor(string $ip): bool
207207
}
208208

209209
/**
210-
* @throws InvalidArgumentException
210+
* @throws InvalidArgumentException|BouncerException
211211
*/
212212
protected function displayCaptchaWall(string $ip): void
213213
{
@@ -237,7 +237,7 @@ protected function handleBanRemediation(): void
237237
* @param string $ip
238238
* @return void
239239
* @throws InvalidArgumentException
240-
* @throws CacheException
240+
* @throws CacheException|BouncerException
241241
*/
242242
protected function handleCaptchaResolutionForm(string $ip)
243243
{
@@ -321,7 +321,7 @@ protected function handleCaptchaResolutionForm(string $ip)
321321
*
322322
* @return void
323323
* @throws InvalidArgumentException
324-
* @throws CacheException
324+
* @throws CacheException|BouncerException
325325
*/
326326
protected function handleCaptchaRemediation(string $ip)
327327
{
@@ -362,7 +362,7 @@ protected function handleCaptchaRemediation(string $ip)
362362
* @param string $ip
363363
* @return void
364364
* @throws InvalidArgumentException
365-
* @throws CacheException
365+
* @throws CacheException|BouncerException
366366
*/
367367
protected function handleRemediation(string $remediation, string $ip)
368368
{
@@ -385,7 +385,7 @@ protected function handleRemediation(string $remediation, string $ip)
385385
* @param array $names
386386
* @param string $ip
387387
* @return array
388-
* @throws InvalidArgumentException
388+
* @throws InvalidArgumentException|BouncerException
389389
*/
390390
public function getIpVariables(string $cacheTag, array $names, string $ip): array
391391
{
@@ -405,7 +405,7 @@ public function getIpVariables(string $cacheTag, array $names, string $ip): arra
405405
* @param string $ip
406406
* @return void
407407
* @throws InvalidArgumentException
408-
* @throws CacheException
408+
* @throws CacheException|BouncerException
409409
*/
410410
public function setIpVariables(string $cacheTag, array $pairs, string $ip): void
411411
{
@@ -424,7 +424,7 @@ public function setIpVariables(string $cacheTag, array $pairs, string $ip): void
424424
* @param string $ip
425425
* @return void
426426
* @throws InvalidArgumentException
427-
* @throws CacheException
427+
* @throws CacheException|BouncerException
428428
*/
429429
public function unsetIpVariables(string $cacheTag, array $names, string $ip): void
430430
{

src/ApiCache.php

+13-7
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ private function removeDecisionFromRemediationItem(string $cacheKey, int $decisi
286286

287287
/**
288288
* Parse "duration" entries returned from API to a number of seconds.
289+
* @throws BouncerException
289290
*/
290291
private static function parseDurationToSeconds(string $duration): int
291292
{
@@ -317,6 +318,7 @@ private static function parseDurationToSeconds(string $duration): int
317318
/**
318319
* Format a remediation item of a cache item.
319320
* This format use a minimal amount of data allowing less cache data consumption.
321+
* @throws BouncerException
320322
*/
321323
private function formatRemediationFromDecision(?array $decision): array
322324
{
@@ -364,6 +366,7 @@ private function defferUpdateCacheConfig(array $config): void
364366
* Update the cached remediation of the specified cacheKey from these new decisions.
365367
*
366368
* @throws InvalidArgumentException|\Psr\Cache\CacheException
369+
* @throws BouncerException
367370
*/
368371
private function saveRemediationsForCacheKey(array $decisions, string $cacheKey): string
369372
{
@@ -401,6 +404,7 @@ private function saveRemediationsForCacheKey(array $decisions, string $cacheKey)
401404
* @param string $scope
402405
* @param string $value
403406
* @return string
407+
* @throws BouncerException
404408
*/
405409
private function getCacheKey(string $scope, string $value): string
406410
{
@@ -615,7 +619,7 @@ private function removeRemediations(array $decisions): array
615619
}
616620

617621
/**
618-
* @throws InvalidArgumentException
622+
* @throws InvalidArgumentException|BouncerException
619623
*/
620624
public function clear(): bool
621625
{
@@ -640,7 +644,7 @@ public function clear(): bool
640644
*
641645
* @return array "count": number of decisions added, "errors": decisions not added
642646
*
643-
* @throws InvalidArgumentException|\Psr\Cache\CacheException
647+
* @throws InvalidArgumentException|\Psr\Cache\CacheException|BouncerException
644648
*/
645649
public function warmUp(): array
646650
{
@@ -682,7 +686,7 @@ public function warmUp(): array
682686
*
683687
* @return array number of deleted and new decisions, and errors when processing decisions
684688
*
685-
* @throws InvalidArgumentException|\Psr\Cache\CacheException
689+
* @throws InvalidArgumentException|\Psr\Cache\CacheException|BouncerException
686690
*/
687691
public function pullUpdates(): array
688692
{
@@ -782,7 +786,7 @@ private function hit(string $ip): string
782786
*
783787
* @return string
784788
* @throws InvalidArgumentException
785-
* @throws \Psr\Cache\CacheException
789+
* @throws \Psr\Cache\CacheException|BouncerException
786790
*/
787791
private function handleCacheRemediation(string $cacheScope, string $value): string
788792
{
@@ -870,6 +874,7 @@ public function get(AddressInterface $address): string
870874

871875
/**
872876
* Prune the cache (only when using PHP File System cache).
877+
* @throws BouncerException
873878
*/
874879
public function prune(): bool
875880
{
@@ -886,6 +891,7 @@ public function prune(): bool
886891
/**
887892
* When Memcached connection fail, it throws an unhandled warning.
888893
* To catch this warning as a clean exception we have to temporarily change the error handler.
894+
* @throws BouncerException
889895
*/
890896
private function setCustomErrorHandler(): void
891897
{
@@ -946,7 +952,7 @@ public function testConnection(): void
946952
*
947953
* @return array|mixed
948954
*
949-
* @throws InvalidArgumentException
955+
* @throws InvalidArgumentException|BouncerException
950956
*/
951957
private function getIpCachedVariables(string $cacheTag, string $ip)
952958
{
@@ -968,7 +974,7 @@ private function getIpCachedVariables(string $cacheTag, string $ip)
968974
* @param string $ip
969975
*
970976
* @return array
971-
* @throws InvalidArgumentException
977+
* @throws InvalidArgumentException|BouncerException
972978
*/
973979
public function getIpVariables(string $cacheTag, array $names, string $ip): array
974980
{
@@ -1011,7 +1017,7 @@ public function setIpVariables(string $cacheTag, array $pairs, string $ip)
10111017
* @param string $ip
10121018
* @return void
10131019
* @throws InvalidArgumentException
1014-
* @throws \Psr\Cache\CacheException
1020+
* @throws \Psr\Cache\CacheException|BouncerException
10151021
*/
10161022
public function unsetIpVariables(string $cacheTag, array $pairs, string $ip)
10171023
{

0 commit comments

Comments
 (0)