-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChecker.php
More file actions
199 lines (171 loc) · 4.29 KB
/
Copy pathChecker.php
File metadata and controls
199 lines (171 loc) · 4.29 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?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;
/**
* IP screening decision engine.
*
* - resolve the real client IP (respecting behind_proxy),
* - honour the whitelist,
* - cache decisions keyed by md5(ip|user_agent|referrer),
* - apply the fail mode (default FAIL OPEN = allow) on any error,
* - block by redirect or 403 message.
*/
class Checker
{
/** @var \XF\App */
protected $app;
public function __construct(\XF\App $app)
{
$this->app = $app;
}
/**
* Screen the current request. When the visitor must be blocked this throws a
* reply exception which XenForo turns into the response, halting dispatch.
*
* @param \XF\Mvc\Controller $controller
*
* @throws \XF\Mvc\Reply\Exception
*/
public function guard(\XF\Mvc\Controller $controller)
{
$options = $this->app->options();
if (empty($options->ipBlockEnabled) || $options->ipBlockSiteId === '' || $options->ipBlockApiKey === '')
{
return;
}
$ip = $this->getClientIp();
if ($ip === '')
{
return;
}
// The whitelist is always honoured.
if ($this->isWhitelisted($ip, $options->ipBlockWhitelist))
{
return;
}
$request = $this->app->request();
$userAgent = (string) $request->getServer('HTTP_USER_AGENT', '');
$referrer = (string) $request->getServer('HTTP_REFERER', '');
if ($this->decide($ip, $userAgent, $referrer) === 'block')
{
$this->block($controller);
}
}
/**
* Resolve a decision, using the XenForo cache when enabled.
*
* @return string 'allow' | 'block'
*/
protected function decide($ip, $userAgent, $referrer)
{
$options = $this->app->options();
$ttl = (int) $options->ipBlockCacheTtl;
$cache = $this->app->cache();
$key = 'ipblock_' . md5($ip . '|' . $userAgent . '|' . $referrer);
if ($ttl > 0 && $cache)
{
$cached = $cache->fetch($key);
if ($cached === 'allow' || $cached === 'block')
{
return $cached;
}
}
$result = (new Client($this->app))->check($ip, $userAgent, $referrer);
if ($result === null)
{
// Any error/timeout => apply fail mode (default FAIL OPEN = allow).
$action = !empty($options->ipBlockFailOpen) ? 'allow' : 'block';
}
else
{
$action = $result;
}
if ($ttl > 0 && $cache)
{
$cache->save($key, $action, $ttl);
}
return $action;
}
/**
* Determine the real client IP address.
*
* When behind_proxy is on we trust CF-Connecting-IP first (Cloudflare), then
* the first entry of X-Forwarded-For. Otherwise we use REMOTE_ADDR.
*
* @return string A valid IP, or '' when none could be determined.
*/
protected function getClientIp()
{
$request = $this->app->request();
$candidates = [];
if (!empty($this->app->options()->ipBlockBehindProxy))
{
$cf = trim((string) $request->getServer('HTTP_CF_CONNECTING_IP', ''));
if ($cf !== '')
{
$candidates[] = $cf;
}
$xff = (string) $request->getServer('HTTP_X_FORWARDED_FOR', '');
if ($xff !== '')
{
$parts = explode(',', $xff);
$candidates[] = trim($parts[0]);
}
}
$candidates[] = (string) $request->getServer('REMOTE_ADDR', '');
foreach ($candidates as $candidate)
{
if ($candidate !== '' && filter_var($candidate, FILTER_VALIDATE_IP) !== false)
{
return $candidate;
}
}
return '';
}
/**
* Is the IP explicitly whitelisted? (one IP per line)
*
* @param string $ip
* @param string $whitelist
*
* @return bool
*/
protected function isWhitelisted($ip, $whitelist)
{
$whitelist = (string) $whitelist;
if ($whitelist === '')
{
return false;
}
foreach (preg_split('/\r\n|\r|\n/', $whitelist) as $line)
{
if (trim($line) === $ip)
{
return true;
}
}
return false;
}
/**
* Build and throw the block reply.
*
* @param \XF\Mvc\Controller $controller
*
* @throws \XF\Mvc\Reply\Exception
*/
protected function block(\XF\Mvc\Controller $controller)
{
$options = $this->app->options();
if ($options->ipBlockBlockAction === 'message')
{
$message = $options->ipBlockBlockMessage ?: 'Access to this site has been denied.';
throw $controller->exception($controller->error($message, 403));
}
throw $controller->exception($controller->redirect('https://www.ip-block.com/blocked.php'));
}
}