-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.php
More file actions
92 lines (79 loc) · 2.17 KB
/
Copy pathClient.php
File metadata and controls
92 lines (79 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
/**
* IP Block Protection - a XenForo 2 add-on.
*
* @copyright 2026 ip-block.com
* @license GNU General Public License v2.0 only
*/
namespace IpBlock\Protection;
use GuzzleHttp\Client as GuzzleClient;
/**
* HTTP client for the ip-block.com screening API.
*
* Shared API contract:
* POST https://api.ip-block.com/v1/check
* Header: Content-Type: application/json
* Body: {"api_key","site_id","ip","user_agent","referrer"}
* Reply: {"action":"allow"} | {"action":"block"}
* Timeout: 1 second. Blocked ONLY when action === "block".
*/
class Client
{
/** @var \XF\App */
protected $app;
public function __construct(\XF\App $app)
{
$this->app = $app;
}
/**
* Ask the API whether an IP should be allowed or blocked.
*
* @param string $ip
* @param string $userAgent
* @param string $referrer
*
* @return string|null 'allow', 'block', or null on ANY error/timeout so the
* caller can apply its fail mode.
*/
public function check($ip, $userAgent, $referrer)
{
$options = $this->app->options();
$apiUrl = $options->ipBlockApiUrl ?: 'https://api.ip-block.com/v1/check';
$payload = [
'api_key' => (string) $options->ipBlockApiKey,
'site_id' => (string) $options->ipBlockSiteId,
'ip' => (string) $ip,
'user_agent' => (string) $userAgent,
'referrer' => (string) $referrer,
];
try
{
/** @var GuzzleClient $http */
$http = $this->app->http()->client();
$response = $http->post($apiUrl, [
'headers' => ['Content-Type' => 'application/json'],
'json' => $payload,
'timeout' => 1,
'connect_timeout' => 1,
'http_errors' => false,
]);
$status = $response->getStatusCode();
if ($status < 200 || $status >= 300)
{
return null;
}
$data = @json_decode((string) $response->getBody(), true);
if (!is_array($data) || !isset($data['action']))
{
return null;
}
// Blocked ONLY on an explicit "block" action.
return ($data['action'] === 'block') ? 'block' : 'allow';
}
catch (\Throwable $e)
{
// Timeout, DNS failure, connection reset, etc. => let the caller fail open/closed.
return null;
}
}
}