-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathClientIp.php
163 lines (141 loc) · 3.61 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
<?php
namespace Middlewares;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Interop\Http\ServerMiddleware\DelegateInterface;
class ClientIp implements MiddlewareInterface
{
/**
* @var bool
*/
private $remote = false;
/**
* @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.
*
* @param array $ips
* @param array $headers
*
* @return self
*/
public function proxy(
array $ips = [],
array $headers = [
'Forwarded',
'Forwarded-For',
'X-Forwarded',
'X-Forwarded-For',
'X-Cluster-Client-Ip',
'Client-Ip'
]
) {
$this->proxyIps = $ips;
$this->proxyHeaders = $headers;
return $this;
}
/**
* To get the ip from a remote service.
* Useful for testing purposes on localhost.
*
* @param bool $remote
*
* @return self
*/
public function remote($remote = true)
{
$this->remote = $remote;
return $this;
}
/**
* Set the attribute name to store client's IP address.
*
* @param string $attribute
*
* @return self
*/
public function attribute($attribute)
{
$this->attribute = $attribute;
return $this;
}
/**
* Process a server request and return a response.
*
* @param ServerRequestInterface $request
* @param DelegateInterface $delegate
*
* @return ResponseInterface
*/
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
{
$ip = $this->getIp($request);
return $delegate->process($request->withAttribute($this->attribute, $ip));
}
/**
* Detect and return the ip.
*
* @param ServerRequestInterface $request
*
* @return string|null
*/
private function getIp(ServerRequestInterface $request)
{
if ($this->remote) {
$ip = file_get_contents('http://ipecho.net/plain');
if (self::isValid($ip)) {
return $ip;
}
}
$ip = null;
$server = $request->getServerParams();
if (!empty($server['REMOTE_ADDR']) && self::isValid($server['REMOTE_ADDR'])) {
$ip = $server['REMOTE_ADDR'];
}
if (empty($this->proxyHeaders) || (!empty($this->proxyIps) && !in_array($ip, $this->proxyIps))) {
return $ip;
}
foreach ($this->proxyHeaders as $name) {
if ($request->hasHeader($name) && ($ip = self::getHeaderIp($request->getHeaderLine($name))) !== null) {
return $ip;
}
}
}
/**
* Returns the first valid ip found in the header.
*
* @param string $header
*
* @return string|null
*/
private static function getHeaderIp($header)
{
foreach (array_map('trim', explode(',', $header)) as $ip) {
if (self::isValid($ip)) {
return $ip;
}
}
}
/**
* Check that a given string is a valid IP address.
*
* @param string $ip
*
* @return bool
*/
private static function isValid($ip)
{
return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6) !== false;
}
}