**Added**
- support for check-block request

**changed**
- internal refactoring
This commit is contained in:
kristuff 2021-01-08 10:02:09 +01:00
commit 6140da20ec
4 changed files with 125 additions and 58 deletions

View file

@ -10,11 +10,12 @@
Features
--------
- **✓** Single check request
- **✓** Single IP check request
- **✓** Check IP block request
- **✓** Single report request
- **✓** Auto cleaning report comment from sensitive data
- **✓** Blacklist request
- *\[TODO\]* Check block request
- *\[TODO\]* clear address block request
- *\[TODO\]* Bulk report request
Requirements
@ -31,7 +32,7 @@ Deploy with composer:
```json
...
"require": {
"kristuff/abuseipdb": ">=0.9.3-stable"
"kristuff/abuseipdb": ">=0.9.4-stable"
},
```

View file

@ -14,7 +14,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @version 0.9.3
* @version 0.9.4
* @copyright 2020-2021 Kristuff
*/

View file

@ -14,7 +14,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @version 0.9.3
* @version 0.9.4
* @copyright 2020-2021 Kristuff
*/
@ -172,7 +172,7 @@ class ApiHandler extends ApiDefintion
}
// validates categories, clean message
$cats = $this->validateCategories($categories);
$cats = $this->validateReportCategories($categories);
$msg = $this->cleanMessage($message);
// report AbuseIPDB request
@ -189,65 +189,71 @@ class ApiHandler extends ApiDefintion
}
/**
* Check if the category(ies) given is/are valid
* Check for shortname or id, and categories that can't be used alone
* Perform a 'check-block' api request
*
* @access protected
* @param array $categories The report categories list
*
* @return string Formatted string id list ('18,2,3...')
* @throws \InvalidArgumentException
*
* Sample json response for 127.0.0.1/24
*
* {
* "data": {
* "networkAddress": "127.0.0.0",
* "netmask": "255.255.255.0",
* "minAddress": "127.0.0.1",
* "maxAddress": "127.0.0.254",
* "numPossibleHosts": 254,
* "addressSpaceDesc": "Loopback",
* "reportedAddress": [
* {
* "ipAddress": "127.0.0.1",
* "numReports": 631,
* "mostRecentReport": "2019-03-21T16:35:16+00:00",
* "abuseConfidenceScore": 0,
* "countryCode": null
* },
* {
* "ipAddress": "127.0.0.2",
* "numReports": 16,
* "mostRecentReport": "2019-03-12T20:31:17+00:00",
* "abuseConfidenceScore": 0,
* "countryCode": null
* },
* ...
* ]
* }
* }
*
*
* @access public
* @param string $network The network to check
* @param int $maxAge Max age in days
* @param bool $returnArray True to return an indexed array instead of an object. Default is false.
*
* @return object|array
* @throws \InvalidArgumentException when maxAge is less than 1 or greater than 365, or when network value was not set.
*/
protected function validateCategories(string $categories)
public function checkBlock(string $network = null, int $maxAge = 30, bool $returnArray = false)
{
// the return categories string
$catsString = '';
// used when cat that can't be used alone
$needAnother = null;
// parse given categories
$cats = explode(',', $categories);
foreach ($cats as $cat) {
// get index on our array of categories
$catIndex = is_numeric($cat) ? $this->getCategoryIndex($cat, 1) : $this->getCategoryIndex($cat, 0);
// check if found
if ($catIndex === false ){
throw new \InvalidArgumentException('Invalid report category was given : ['. $cat . ']');
}
// get Id
$catId = $this->aipdbApiCategories[$catIndex][1];
// need another ?
if ($needAnother !== false){
// is a standalone cat ?
if ($this->aipdbApiCategories[$catIndex][3] === false) {
$needAnother = true;
} else {
// ok, continue with other at least one given
// no need to reperform this check
$needAnother = false;
}
}
// set or add to cats list
$catsString = ($catsString === '') ? $catId : $catsString .','.$catId;
// max age must be less or equal to 365
if ($maxAge > 365 || $maxAge < 1){
throw new \InvalidArgumentException('maxAge must be at least 1 and less than 365 (' . $maxAge . ' was given)');
}
if ($needAnother !== false){
throw new \InvalidArgumentException('Invalid report category paremeter given: some categories can\'t be used alone');
// ip must be set
if (empty($network)){
throw new \InvalidArgumentException('network argument must be set (null given)');
}
// if here that ok
return $catsString;
// minimal data
$data = [
'network' => $network,
'maxAgeInDays' => $maxAge,
];
$response = $this->apiRequest('check-block', $data, 'GET', $returnArray) ;
return json_decode($response, $returnArray);
}
/**
* Perform a 'check' api request
*
@ -329,6 +335,66 @@ class ApiHandler extends ApiDefintion
return json_decode($response, $returnArray);
}
/**
* Check if the category(ies) given is/are valid
* Check for shortname or id, and categories that can't be used alone
*
* @access protected
* @param array $categories The report categories list
*
* @return string Formatted string id list ('18,2,3...')
* @throws \InvalidArgumentException
*/
protected function validateReportCategories(string $categories)
{
// the return categories string
$catsString = '';
// used when cat that can't be used alone
$needAnother = null;
// parse given categories
$cats = explode(',', $categories);
foreach ($cats as $cat) {
// get index on our array of categories
$catIndex = is_numeric($cat) ? $this->getCategoryIndex($cat, 1) : $this->getCategoryIndex($cat, 0);
// check if found
if ($catIndex === false ){
throw new \InvalidArgumentException('Invalid report category was given : ['. $cat . ']');
}
// get Id
$catId = $this->aipdbApiCategories[$catIndex][1];
// need another ?
if ($needAnother !== false){
// is a standalone cat ?
if ($this->aipdbApiCategories[$catIndex][3] === false) {
$needAnother = true;
} else {
// ok, continue with other at least one given
// no need to reperform this check
$needAnother = false;
}
}
// set or add to cats list
$catsString = ($catsString === '') ? $catId : $catsString .','.$catId;
}
if ($needAnother !== false){
throw new \InvalidArgumentException('Invalid report category paremeter given: some categories can\'t be used alone');
}
// if here that ok
return $catsString;
}
/**
* Perform a cURL request
*

View file

@ -14,7 +14,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @version 0.9.3
* @version 0.9.4
* @copyright 2020-2021 Kristuff
*/