1 | <?php |
||
11 | class TrustedProxyMiddleware implements HTTPMiddleware |
||
12 | { |
||
13 | /** |
||
14 | * Comma-separated list of IP ranges that are trusted to provide proxy headers. |
||
15 | * Can also be 'none' or '*' (all) |
||
16 | * |
||
17 | * @var string |
||
18 | */ |
||
19 | private $trustedProxyIPs = null; |
||
20 | |||
21 | /** |
||
22 | * Array of headers from which to lookup the hostname |
||
23 | * |
||
24 | * @var array |
||
25 | */ |
||
26 | private $proxyHostHeaders = [ |
||
27 | 'X-Forwarded-Host' |
||
28 | ]; |
||
29 | |||
30 | /** |
||
31 | * Array of headers from which to lookup the client IP |
||
32 | * |
||
33 | * @var array |
||
34 | */ |
||
35 | private $proxyIPHeaders = [ |
||
36 | 'Client-IP', |
||
37 | 'X-Forwarded-For' |
||
38 | ]; |
||
39 | |||
40 | /** |
||
41 | * Array of headers from which to lookup the client scheme (http/https) |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | private $proxySchemeHeaders = [ |
||
46 | 'X-Forwarded-Protocol', |
||
47 | 'X-Forwarded-Proto', |
||
48 | ]; |
||
49 | |||
50 | /** |
||
51 | * Return the comma-separated list of IP ranges that are trusted to provide proxy headers |
||
52 | * Can also be 'none' or '*' (all) |
||
53 | * |
||
54 | * @return string |
||
55 | */ |
||
56 | public function getTrustedProxyIPs() |
||
60 | |||
61 | /** |
||
62 | * Set the comma-separated list of IP ranges that are trusted to provide proxy headers |
||
63 | * Can also be 'none' or '*' (all) |
||
64 | * |
||
65 | * @param string $trustedProxyIPs |
||
66 | * @return $this |
||
67 | */ |
||
68 | public function setTrustedProxyIPs($trustedProxyIPs) |
||
73 | |||
74 | /** |
||
75 | * Return the array of headers from which to lookup the hostname |
||
76 | * |
||
77 | * @return array |
||
78 | */ |
||
79 | public function getProxyHostHeaders() |
||
83 | |||
84 | /** |
||
85 | * Set the array of headers from which to lookup the hostname. |
||
86 | * |
||
87 | * @param array $proxyHostHeaders |
||
88 | * @return $this |
||
89 | */ |
||
90 | public function setProxyHostHeaders($proxyHostHeaders) |
||
95 | |||
96 | /** |
||
97 | * Return the array of headers from which to lookup the client IP |
||
98 | * |
||
99 | * @return array |
||
100 | */ |
||
101 | public function getProxyIPHeaders() |
||
105 | |||
106 | /** |
||
107 | * Set the array of headers from which to lookup the client IP. |
||
108 | * |
||
109 | * @param array $proxyIPHeaders |
||
110 | * @return $this |
||
111 | */ |
||
112 | public function setProxyIPHeaders($proxyIPHeaders) |
||
117 | |||
118 | /** |
||
119 | * Return the array of headers from which to lookup the client scheme (http/https) |
||
120 | * |
||
121 | * @return array |
||
122 | */ |
||
123 | public function getProxySchemeHeaders() |
||
127 | |||
128 | /** |
||
129 | * Set array of headers from which to lookup the client scheme (http/https) |
||
130 | * Can also specify comma-separated list as a single string. |
||
131 | * |
||
132 | * @param array $proxySchemeHeaders |
||
133 | * @return $this |
||
134 | */ |
||
135 | public function setProxySchemeHeaders($proxySchemeHeaders) |
||
140 | |||
141 | public function process(HTTPRequest $request, callable $delegate) |
||
178 | |||
179 | /** |
||
180 | * Determine if the current request is coming from a trusted proxy |
||
181 | * |
||
182 | * @param HTTPRequest $request |
||
183 | * @return bool True if the request's source IP is a trusted proxy |
||
184 | */ |
||
185 | protected function isTrustedProxy(HTTPRequest $request) |
||
207 | |||
208 | /** |
||
209 | * Extract an IP address from a header value that has been obtained. |
||
210 | * Accepts single IP or comma separated string of IPs |
||
211 | * |
||
212 | * @param string $headerValue The value from a trusted header |
||
213 | * @return string The IP address |
||
214 | */ |
||
215 | protected function getIPFromHeaderValue($headerValue) |
||
237 | } |
||
238 |