|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* php-guard/curl <https://github.com/php-guard/curl> |
|
4
|
|
|
* Copyright (C) ${YEAR} by Alexandre Le Borgne <[email protected]>. |
|
5
|
|
|
* |
|
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
7
|
|
|
* it under the terms of the GNU General Public License as published by |
|
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
9
|
|
|
* (at your option) any later version. |
|
10
|
|
|
* |
|
11
|
|
|
* This program is distributed in the hope that it will be useful, |
|
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14
|
|
|
* GNU General Public License for more details. |
|
15
|
|
|
* |
|
16
|
|
|
* You should have received a copy of the GNU General Public License |
|
17
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace PhpGuard\Curl\RequestModifier; |
|
21
|
|
|
|
|
22
|
|
|
use PhpGuard\Curl\CurlRequest; |
|
23
|
|
|
|
|
24
|
|
|
class ProxyRequestModifier implements RequestModifierInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
private $proxy; |
|
30
|
|
|
/** |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
private $proxyIgnoredHosts; |
|
34
|
|
|
/** |
|
35
|
|
|
* @var int |
|
36
|
|
|
*/ |
|
37
|
|
|
private $port; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct(string $proxy, ?int $port = null, array $proxyIgnoredHosts = []) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->proxy = $proxy; |
|
42
|
|
|
$this->proxyIgnoredHosts = $proxyIgnoredHosts; |
|
43
|
|
|
$this->port = $port; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Handles a request and produces a response. |
|
48
|
|
|
* |
|
49
|
|
|
* May call other collaborating code to generate the response. |
|
50
|
|
|
* |
|
51
|
|
|
* @param CurlRequest $request |
|
52
|
|
|
* |
|
53
|
|
|
* @return CurlRequest |
|
54
|
|
|
*/ |
|
55
|
|
|
public function modify(CurlRequest $request): CurlRequest |
|
56
|
|
|
{ |
|
57
|
|
|
if (!in_array(parse_url($request->getUrl(), PHP_URL_HOST), $this->proxyIgnoredHosts)) { |
|
58
|
|
|
$request->getCurlOptions()[CURLOPT_HTTPPROXYTUNNEL] = true; |
|
59
|
|
|
$request->getCurlOptions()[CURLOPT_PROXY] = $this->proxy; |
|
60
|
|
|
if ($this->port) { |
|
61
|
|
|
$request->getCurlOptions()[CURLOPT_PROXYPORT] = $this->port; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $request; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|