Skip to content

Commit

Permalink
feat(timeout): Add config for timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
julienloizelet committed Dec 19, 2022
1 parent 31a4d9b commit 9634ca8
Show file tree
Hide file tree
Showing 17 changed files with 50 additions and 40 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ jobs:
fi
- name: Create Tag ${{ github.event.inputs.tag_name }}
uses: actions/github-script@v3
uses: actions/github-script@v6
with:
github-token: ${{ github.token }}
script: |
github.git.createRef({
github.rest.git.createRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: "refs/tags/${{ github.event.inputs.tag_name }}",
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
}
},
"require": {
"crowdsec/bouncer": "0.34.0",
"crowdsec/bouncer": "0.35.0",
"symfony/polyfill-mbstring": "1.20.0",
"symfony/service-contracts": "2.4.1"
},
Expand Down
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion inc/Bounce.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function getBouncerInstance(array $settings): Bouncer
'api_url' => $this->escape($this->getStringSettings('crowdsec_api_url')),
'use_curl' => $this->getBoolSettings('crowdsec_use_curl'),
'api_user_agent' => Constants::CROWDSEC_BOUNCER_USER_AGENT,
'api_timeout' => Constants::API_TIMEOUT,
'api_timeout' => $this->getIntegerSettings('crowdsec_api_timeout')?:Constants::API_TIMEOUT,
// Debug
'debug_mode' => $this->getBoolSettings('crowdsec_debug_mode'),
'disable_prod_log' => $this->getBoolSettings('crowdsec_disable_prod_log'),
Expand Down
13 changes: 13 additions & 0 deletions inc/admin/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@ function adminSettings()
'crowdsec_settings', 'crowdsec_admin_connection', function () {}, function () {}, '<p>If checked, calls to Local API will be done with <i>cURL</i> (be sure to have <i>cURL</i> enabled on your system before enabling).
<br>If not checked, calls are done with <i>file_get_contents</i> method (<i>allow_url_fopen</i> is required for this).</p>');

// Field "timeout"
addFieldString('crowdsec_api_timeout', 'Local Api request timeout', 'crowdsec_plugin_settings', 'crowdsec_settings',
'crowdsec_admin_connection', function ($input) {
if ((int) $input === 0) {
add_settings_error('Local API timeout', 'crowdsec_error', 'Local API timeout: Must be different than 0.');

return Constants::API_TIMEOUT;
}

return (int) $input !== 0 ? (int) $input : Constants::API_TIMEOUT ;
}, ' seconds. <p>Authorized timeout for a Local API request.<br> Set a negative value to allow unlimited timeout.<br> Default to' . Constants::API_TIMEOUT .'.',
Constants::API_TIMEOUT, 'width: 115px;', 'number');

/************************************
** Section "Bouncing refinements" **
***********************************/
Expand Down
2 changes: 1 addition & 1 deletion inc/bouncer-instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function getDatabaseSettings(): array
'api_url' => esc_attr(get_option('crowdsec_api_url')),
'use_curl' => !empty(get_option('crowdsec_use_curl')),
'api_user_agent' => Constants::CROWDSEC_BOUNCER_USER_AGENT,
'api_timeout' => Constants::API_TIMEOUT,
'api_timeout' => (int)get_option('crowdsec_api_timeout') ?:Constants::API_TIMEOUT,
// Debug
'debug_mode' => !empty(get_option('crowdsec_debug_mode')),
'log_directory_path' => Constants::CROWDSEC_LOG_BASE_PATH,
Expand Down
1 change: 1 addition & 0 deletions inc/options-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ function getCrowdSecOptionsConfig()
['name' => 'crowdsec_tls_ca_cert_path', 'default' => '', 'autoInit' => true],
['name' => 'crowdsec_api_key', 'default' => '', 'autoInit' => true],
['name' => 'crowdsec_use_curl', 'default' => '', 'autoInit' => true],
['name' => 'crowdsec_api_timeout', 'default' => Constants::API_TIMEOUT, 'autoInit' => true],
['name' => 'crowdsec_bouncing_level', 'default' => Constants::BOUNCING_LEVEL_DISABLED, 'autoInit' => true],
['name' => 'crowdsec_public_website_only', 'default' => 'on', 'autoInit' => true],
['name' => 'crowdsec_stream_mode', 'default' => '', 'autoInit' => true],
Expand Down
2 changes: 1 addition & 1 deletion inc/templates/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function updateTlsDisplay () {
?>
</form>
<h2><?php echo __("Test your settings");?></h2>
<p><?php echo __("Here you can check if your saved settings are correct.");?></p>
<p><?php echo __("Here you can check if your <b>saved</b> settings are correct.");?></p>
<p><?php echo __("Click the 'Test bouncing' button and the bouncer will try to get the remediation for the following IP:");?></p>
<form action="admin-post.php" method="post" id="crowdsec_action_test_connection">
<input type="hidden" name="action" value="crowdsec_test_connection"/>
Expand Down
14 changes: 7 additions & 7 deletions vendor/composer/installed.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,17 @@
},
{
"name": "crowdsec/bouncer",
"version": "v0.34.0",
"version_normalized": "0.34.0.0",
"version": "v0.35.0",
"version_normalized": "0.35.0.0",
"source": {
"type": "git",
"url": "https://github.com/crowdsecurity/php-cs-bouncer.git",
"reference": "2334a9ca93cad280641659cdb5077601c75980c2"
"reference": "ed00473fe3ca065062b836ffb7fa939baecafcc3"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/crowdsecurity/php-cs-bouncer/zipball/2334a9ca93cad280641659cdb5077601c75980c2",
"reference": "2334a9ca93cad280641659cdb5077601c75980c2",
"url": "https://api.github.com/repos/crowdsecurity/php-cs-bouncer/zipball/ed00473fe3ca065062b836ffb7fa939baecafcc3",
"reference": "ed00473fe3ca065062b836ffb7fa939baecafcc3",
"shasum": ""
},
"require": {
Expand All @@ -112,7 +112,7 @@
"suggest": {
"ext-curl": "*"
},
"time": "2022-11-24T04:29:36+00:00",
"time": "2022-12-16T09:50:47+00:00",
"type": "library",
"installation-source": "dist",
"autoload": {
Expand Down Expand Up @@ -151,7 +151,7 @@
],
"support": {
"issues": "https://github.com/crowdsecurity/php-cs-bouncer/issues",
"source": "https://github.com/crowdsecurity/php-cs-bouncer/tree/v0.34.0"
"source": "https://github.com/crowdsecurity/php-cs-bouncer/tree/v0.35.0"
},
"install-path": "../crowdsec/bouncer"
},
Expand Down
10 changes: 5 additions & 5 deletions vendor/composer/installed.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'name' => 'crowdsec/wordpress-bouncer',
'pretty_version' => 'dev-main',
'version' => 'dev-main',
'reference' => '7145a1b0f7e850c6bf780d2073b0400e2a72d317',
'reference' => '31a4d9b3cc01cae0fb981c3b405e1d69fbccbb7a',
'type' => 'project',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
Expand All @@ -20,9 +20,9 @@
'dev_requirement' => false,
),
'crowdsec/bouncer' => array(
'pretty_version' => 'v0.34.0',
'version' => '0.34.0.0',
'reference' => '2334a9ca93cad280641659cdb5077601c75980c2',
'pretty_version' => 'v0.35.0',
'version' => '0.35.0.0',
'reference' => 'ed00473fe3ca065062b836ffb7fa939baecafcc3',
'type' => 'library',
'install_path' => __DIR__ . '/../crowdsec/bouncer',
'aliases' => array(),
Expand All @@ -31,7 +31,7 @@
'crowdsec/wordpress-bouncer' => array(
'pretty_version' => 'dev-main',
'version' => 'dev-main',
'reference' => '7145a1b0f7e850c6bf780d2073b0400e2a72d317',
'reference' => '31a4d9b3cc01cae0fb981c3b405e1d69fbccbb7a',
'type' => 'project',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
Expand Down
2 changes: 1 addition & 1 deletion vendor/crowdsec/bouncer/src/AbstractBounce.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ protected function prepareBouncerConfigs(): array
'api_key' => $this->getStringSettings('api_key'),
'api_url' => $this->getStringSettings('api_url'),
'api_user_agent' => $this->getStringSettings('api_user_agent'),
'api_timeout' => $apiTimeout > 0 ? $apiTimeout : Constants::API_TIMEOUT,
'api_timeout' => $apiTimeout !== 0 ? $apiTimeout : Constants::API_TIMEOUT,
'use_curl' => $this->getBoolSettings('use_curl'),
// Debug
'debug_mode' => $this->getBoolSettings('debug_mode'),
Expand Down
2 changes: 1 addition & 1 deletion vendor/crowdsec/bouncer/src/AbstractCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,9 +396,9 @@ protected function hit(string $ip): string
$remediations = $this->adapter->getItem(base64_encode($ip))->get();

// We apply array values first because keys are ids.
/** @var array $firstRemediation */
$firstRemediation = array_values($remediations)[0];

/** @var string */
return $firstRemediation[0];
}

Expand Down
2 changes: 1 addition & 1 deletion vendor/crowdsec/bouncer/src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private function addConnectionNodes($rootNode)
->info('Absolute path to the CA used to process TLS handshake')->defaultValue('')
->end()
->booleanNode('tls_verify_peer')->defaultValue(false)->end()
->integerNode('api_timeout')->min(Constants::API_TIMEOUT)->defaultValue(Constants::API_TIMEOUT)->end()
->integerNode('api_timeout')->defaultValue(Constants::API_TIMEOUT)->end()
->booleanNode('use_curl')->defaultValue(false)->end()
->end();
}
Expand Down
4 changes: 2 additions & 2 deletions vendor/crowdsec/bouncer/src/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class Constants
{
/** @var int The timeout when calling LAPI */
public const API_TIMEOUT = 1;
public const API_TIMEOUT = 120;
/** @var string The API-KEY auth type */
public const AUTH_KEY = 'api_key';
/** @var string The TLS auth type */
Expand Down Expand Up @@ -75,7 +75,7 @@ class Constants
/** @var string Path for html templates folder (e.g. ban and captcha wall) */
public const TEMPLATES_DIR = __DIR__ . "/templates";
/** @var string The last version of this library */
public const VERSION = 'v0.34.0';
public const VERSION = 'v0.35.0';
/** @var string The "disabled" x-forwarded-for setting */
public const X_FORWARDED_DISABLED = 'no_forward';
}
3 changes: 1 addition & 2 deletions vendor/crowdsec/bouncer/src/RestClient/AbstractClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ abstract public function request(
array $queryParams = null,
array $bodyParams = null,
string $method = 'GET',
array $headers = null,
int $timeout = null
array $headers = null
): ?array;
}
3 changes: 1 addition & 2 deletions vendor/crowdsec/bouncer/src/RestClient/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ public function request(
array $queryParams = null,
array $bodyParams = null,
string $method = 'GET',
array $headers = null,
int $timeout = null
array $headers = null
): ?array {
$handle = curl_init();
$curlOptions = $this->createOptions($endpoint, $queryParams, $bodyParams, $method, $headers ?: $this->headers);
Expand Down
10 changes: 4 additions & 6 deletions vendor/crowdsec/bouncer/src/RestClient/FileGetContents.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@ public function request(
array $queryParams = null,
array $bodyParams = null,
string $method = 'GET',
array $headers = null,
int $timeout = null
array $headers = null
): ?array {
if ($queryParams) {
$endpoint .= '?' . http_build_query($queryParams);
}

$config = $this->createConfig($bodyParams, $method, $headers, $timeout);
$config = $this->createConfig($bodyParams, $method, $headers);
$context = stream_context_create($config);

$this->logger->debug('', [
Expand Down Expand Up @@ -81,15 +80,14 @@ private function convertHeadersToString(array $headers): string
private function createConfig(
array $bodyParams = null,
string $method = 'GET',
array $headers = null,
int $timeout = null
array $headers = null
): array {
$header = $headers ? $this->convertHeadersToString($headers) : $this->headerString;
$config = [
'http' => [
'method' => $method,
'header' => $header,
'timeout' => $timeout ?: $this->timeout,
'timeout' => $this->timeout,
'ignore_errors' => true,
],
];
Expand Down

0 comments on commit 9634ca8

Please sign in to comment.