Repository navigation
Expand file tree
/
Copy pathProxy.php
More file actions
414 lines (362 loc) · 11.8 KB
/
Copy pathProxy.php
File metadata and controls
414 lines (362 loc) · 11.8 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
<?php
/*
* This file is part of Berlioz framework.
*
* @license https://opensource.org/licenses/MIT MIT License
* @copyright 2026 Ronan GIRON
* @author Ronan GIRON <https://github.com/ElGigi>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code, to the root.
*/
declare(strict_types=1);
namespace Berlioz\Http\Client;
use Berlioz\Http\Client\Exception\RequestException;
use Psr\Http\Message\RequestInterface;
use SensitiveParameter;
/**
* Class Proxy.
*
* Validated proxy configuration, shared by the transport adapters.
*
* @internal
*/
final readonly class Proxy
{
public const SCHEME_HTTP = 'http';
public const SCHEME_HTTPS = 'https';
private const SOCKS_SCHEMES = ['socks4', 'socks4a', 'socks5', 'socks5h'];
public function __construct(
public string $scheme,
public string $host,
public int $port,
public ?string $user = null,
#[SensitiveParameter] public ?string $password = null,
) {
}
/**
* __debugInfo() magic method.
*
* @return array
*/
public function __debugInfo(): array
{
return [
'scheme' => $this->scheme,
'host' => $this->host,
'port' => $this->port,
'user' => $this->user,
'password' => null !== $this->password ? '*** HIDDEN ***' : null,
];
}
/**
* Resolve proxy of request.
*
* The proxy of the HTTP context is used if configured, no proxy if disabled (false),
* else the proxy of environment.
*
* @param HttpContext|null $context
* @param RequestInterface $request
*
* @return self|null
* @throws RequestException
*/
public static function resolve(?HttpContext $context, RequestInterface $request): ?self
{
return match ($context?->proxy) {
false => null,
null => self::fromEnvironment($request),
default => self::fromContext($context, $request),
};
}
/**
* Create proxy from HTTP context.
*
* Only "http" and "https" proxies are supported, a proxy without scheme is an "http" proxy.
* Any other configuration is rejected before any I/O.
*
* @param HttpContext|null $context
* @param RequestInterface $request
*
* @return self|null Null if no proxy is configured
* @throws RequestException
*/
public static function fromContext(?HttpContext $context, RequestInterface $request): ?self
{
if (!is_string($context?->proxy)) {
return null;
}
return self::fromString($context->proxy, $request);
}
/**
* Create proxy from environment variables.
*
* Variables "https_proxy" (or "HTTPS_PROXY") for HTTPS requests, "http_proxy" for HTTP requests,
* and "all_proxy" (or "ALL_PROXY") for both; hosts of "no_proxy" (or "NO_PROXY") are excluded.
* The uppercase "HTTP_PROXY" is never used: it can be set by the "Proxy" request header in CGI context (httpoxy).
*
* @param RequestInterface $request
*
* @return self|null
* @throws RequestException
*/
public static function fromEnvironment(RequestInterface $request): ?self
{
if (null === ($env = self::getEnvironmentProxy($request))) {
return null;
}
try {
return self::fromString($env[1], $request);
} catch (RequestException $exception) {
throw new RequestException(
sprintf('Environment variable "%s": %s', $env[0], $exception->getMessage()),
$request,
previous: $exception,
);
}
}
/**
* Get the SOCKS proxy URL of environment variables.
*
* SOCKS proxies are not supported by this class, but the cURL adapter hands them over to libcurl,
* which used them before the proxy was resolved by the adapters.
*
* @param RequestInterface $request
*
* @return string|null Null if the proxy of environment is not a SOCKS proxy, or if there is none
*/
public static function socksFromEnvironment(RequestInterface $request): ?string
{
if (null === ($env = self::getEnvironmentProxy($request))) {
return null;
}
$scheme = strtolower(strstr($env[1], '://', true) ?: '');
return in_array($scheme, self::SOCKS_SCHEMES, true) ? $env[1] : null;
}
/**
* Get the proxy environment variable of request, unless the host is excluded by "no_proxy".
*
* @param RequestInterface $request
*
* @return array{string, string}|null Name and value of variable
*/
private static function getEnvironmentProxy(RequestInterface $request): ?array
{
$uri = $request->getUri();
$secure = 'https' === strtolower($uri->getScheme());
$env = match ($secure) {
true => self::getEnv('https_proxy', 'HTTPS_PROXY'),
false => self::getHttpProxyEnv(),
};
$env ??= self::getEnv('all_proxy', 'ALL_PROXY');
if (null === $env) {
return null;
}
$noProxy = self::getEnv('no_proxy', 'NO_PROXY')[1] ?? null;
if (null !== $noProxy && self::isExcluded($uri->getHost(), $uri->getPort() ?? ($secure ? 443 : 80), $noProxy)) {
return null;
}
return $env;
}
/**
* Get the first non-empty environment variable.
*
* @param string ...$names
*
* @return array{string, string}|null Name and value of variable
*/
private static function getEnv(string ...$names): ?array
{
foreach ($names as $name) {
$value = getenv($name);
if (is_string($value) && '' !== trim($value)) {
return [$name, trim($value)];
}
}
return null;
}
/**
* Get "http_proxy" environment variable.
*
* Environment variables are case-insensitive on Windows: "http_proxy" may be the "HTTP_PROXY" one,
* set by the "Proxy" request header outside of CLI.
*
* @return array{string, string}|null Name and value of variable
*/
private static function getHttpProxyEnv(): ?array
{
if ('Windows' === PHP_OS_FAMILY && 'cli' !== PHP_SAPI) {
return null;
}
return self::getEnv('http_proxy');
}
/**
* Is host excluded by a "no_proxy" list?
*
* Entries are separated by commas: "*" for all hosts, host names (also matching their sub-domains,
* with or without leading dot), IP addresses or CIDR ranges, with an optional port.
* An IP address host only matches IP address and CIDR range entries.
*
* @param string $host
* @param int $port
* @param string $noProxy
*
* @return bool
*/
public static function isExcluded(string $host, int $port, string $noProxy): bool
{
$host = rtrim(strtolower(trim($host, '[]')), '.');
$hostIsIp = false !== @inet_pton($host);
foreach (explode(',', $noProxy) as $entry) {
$entry = strtolower(trim($entry));
if ('' === $entry) {
continue;
}
if ('*' === $entry) {
return true;
}
// Optional port: "host:port", "[ipv6]:port"
$entryPort = null;
if (1 === preg_match('/^(\[[^]]+]|[^:]+):(\d+)$/', $entry, $matches)) {
[, $entry, $entryPort] = $matches;
}
if (null !== $entryPort && (int)$entryPort !== $port) {
continue;
}
$entry = trim($entry, '[]');
if ($hostIsIp) {
if (self::matchIp($host, $entry)) {
return true;
}
continue;
}
$entry = rtrim(ltrim($entry, '*.'), '.');
if ('' !== $entry && ($host === $entry || str_ends_with($host, '.' . $entry))) {
return true;
}
}
return false;
}
/**
* Match IP address with an IP address or CIDR range.
*
* @param string $host
* @param string $entry
*
* @return bool
*/
private static function matchIp(string $host, string $entry): bool
{
if (false === ($hostIp = @inet_pton($host))) {
return false;
}
$bits = null;
if (str_contains($entry, '/')) {
[$entry, $bits] = explode('/', $entry, 2);
if (!ctype_digit($bits)) {
return false;
}
$bits = (int)$bits;
}
if (false === ($entryIp = @inet_pton($entry)) || strlen($entryIp) !== strlen($hostIp)) {
return false;
}
$bits ??= strlen($hostIp) * 8;
if ($bits > strlen($hostIp) * 8) {
return false;
}
$bytes = intdiv($bits, 8);
if (substr($hostIp, 0, $bytes) !== substr($entryIp, 0, $bytes)) {
return false;
}
if (0 === ($remaining = $bits % 8)) {
return true;
}
$mask = (0xFF << (8 - $remaining)) & 0xFF;
return (ord($hostIp[$bytes]) & $mask) === (ord($entryIp[$bytes]) & $mask);
}
/**
* Create proxy from string.
*
* @param string $value
* @param RequestInterface $request
*
* @return self
* @throws RequestException
*/
private static function fromString(string $value, RequestInterface $request): self
{
$value = trim($value);
if (!str_contains($value, '://')) {
$value = self::SCHEME_HTTP . '://' . $value;
}
$parsed = parse_url($value);
if (false === $parsed) {
throw new RequestException('Invalid proxy configuration', $request);
}
$scheme = strtolower($parsed['scheme'] ?? '');
if (!in_array($scheme, [self::SCHEME_HTTP, self::SCHEME_HTTPS], true)) {
throw new RequestException(
sprintf('Unsupported proxy scheme "%s", only "http" and "https" are supported', $scheme),
$request
);
}
if (empty($parsed['host'])) {
throw new RequestException('Invalid proxy configuration: missing host', $request);
}
if (!in_array($parsed['path'] ?? '', ['', '/'], true) ||
isset($parsed['query']) ||
isset($parsed['fragment'])) {
throw new RequestException(
'Invalid proxy configuration: path, query and fragment are not allowed',
$request
);
}
return new self(
scheme: $scheme,
host: $parsed['host'],
port: $parsed['port'] ?? ($scheme === self::SCHEME_HTTPS ? 443 : 80),
user: isset($parsed['user']) ? rawurldecode($parsed['user']) : null,
password: isset($parsed['pass']) ? rawurldecode($parsed['pass']) : null,
);
}
/**
* Is a TLS connection to the proxy?
*
* @return bool
*/
public function isSecure(): bool
{
return self::SCHEME_HTTPS === $this->scheme;
}
/**
* Get authority (host and port), without credentials.
*
* @return string
*/
public function getAuthority(): string
{
return sprintf('%s:%d', $this->host, $this->port);
}
/**
* Get URL, without credentials.
*
* @return string
*/
public function getUrl(): string
{
return sprintf('%s://%s', $this->scheme, $this->getAuthority());
}
/**
* Get value of the "Proxy-Authorization" header.
*
* @return string|null
*/
public function getAuthorization(): ?string
{
if (null === $this->user) {
return null;
}
return 'Basic ' . base64_encode($this->user . ':' . ($this->password ?? ''));
}
}