forked from middlewares/client-ip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientIp.php
194 lines (162 loc) · 5.09 KB
/
ClientIp.php
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
<?php
declare(strict_types = 1);
namespace Middlewares;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
class ClientIp implements MiddlewareInterface
{
/**
* @var string The attribute name
*/
private $attribute = 'client-ip';
/**
* @var array The trusted proxy headers
*/
private $proxyHeaders = [];
/**
* @var array The trusted proxy ips
*/
private $proxyIps = [];
/**
* Configure the proxy.
*/
public function proxy(
array $ips = [],
array $headers = [
'Forwarded',
'Forwarded-For',
'X-Forwarded',
'X-Forwarded-For',
'X-Cluster-Client-Ip',
'Client-Ip',
]
): self {
$this->proxyIps = $ips;
$this->proxyHeaders = $headers;
return $this;
}
/**
* Set the attribute name to store client's IP address.
*/
public function attribute(string $attribute): self
{
$this->attribute = $attribute;
return $this;
}
/**
* Process a server request and return a response.
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$ip = $this->getIp($request);
return $handler->handle($request->withAttribute($this->attribute, $ip));
}
/**
* Detect and return the ip.
*/
private function getIp(ServerRequestInterface $request): ?string
{
$localIp = $this->getLocalIp($request);
if (!empty($this->proxyIps) && !$this->isInProxiedIps($localIp)) {
// Local IP address does not point at a known proxy, do not attempt
// to read proxied IP address.
return $localIp;
}
$proxiedIp = $this->getProxiedIp($request);
if (!empty($proxiedIp)) {
// Found IP address via proxy-defined headers.
return $proxiedIp;
}
return $localIp;
}
/**
* checks if the given ip address is in the list of proxied ips provided
*/
private function isInProxiedIps(string $ip): bool
{
foreach ($this->proxyIps as $proxyIp) {
if ($ip === $proxyIp || self::isInCIDR($ip, $proxyIp)) {
return true;
}
}
return false;
}
private static function isInCIDR(string $ip, string $cidr): bool
{
$tokens = explode('/', $cidr);
if (count($tokens) !== 2 || !self::isValid($ip) || !self::isValid($tokens[0]) || !is_numeric($tokens[1])) {
return false;
}
$cidr_base = ip2long($tokens[0]);
$ip_long = ip2long($ip);
$mask = (0xffffffff << intval($tokens[1])) & 0xffffffff;
return ($cidr_base & $mask) === ($ip_long & $mask);
}
/**
* Returns the first valid proxied IP found.
*/
private function getProxiedIp(ServerRequestInterface $request): ?string
{
foreach ($this->proxyHeaders as $name) {
if ($request->hasHeader($name)) {
if (substr($name, -9) === 'Forwarded') {
$ip = $this->getForwardedHeaderIp($request->getHeaderLine($name));
} else {
$ip = $this->getHeaderIp($request->getHeaderLine($name));
}
if ($ip !== null) {
return $ip;
}
}
}
return null;
}
/**
* Returns the remote address of the request, if valid.
*/
private function getLocalIp(ServerRequestInterface $request): ?string
{
$server = $request->getServerParams();
$ip = trim($server['REMOTE_ADDR'] ?? '', '[]');
return self::isValid($ip) ? $ip : null;
}
/**
* Returns the first valid ip found in the Forwarded or X-Forwarded header.
*/
private function getForwardedHeaderIp(string $header): ?string
{
foreach (array_reverse(array_map('trim', explode(',', strtolower($header)))) as $values) {
foreach (array_reverse(array_map('trim', explode(';', $values))) as $directive) {
if (strpos($directive, 'for=') !== 0) {
continue;
}
$ip = trim(substr($directive, 4));
if (self::isValid($ip) && !$this->isInProxiedIps($ip)) {
return $ip;
}
}
}
return null;
}
/**
* Returns the first valid ip found in the header.
*/
private function getHeaderIp(string $header): ?string
{
foreach (array_reverse(array_map('trim', explode(',', $header))) as $ip) {
if (self::isValid($ip) && !$this->isInProxiedIps($ip)) {
return $ip;
}
}
return null;
}
/**
* Check that a given string is a valid IP address.
*/
private static function isValid(string $ip): bool
{
return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6) !== false;
}
}