1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CWP\Core\Control; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Control\HTTPRequest; |
6
|
|
|
use SilverStripe\Control\Middleware\HTTPMiddleware; |
7
|
|
|
use SilverStripe\Core\Config\Configurable; |
8
|
|
|
use SilverStripe\Core\Environment; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Initialises CWP-specific configuration settings, to avoid _config.php. |
12
|
|
|
*/ |
13
|
|
|
class InitialisationMiddleware implements HTTPMiddleware |
14
|
|
|
{ |
15
|
|
|
use Configurable; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Enable egress proxy. This works on the principle of setting http(s)_proxy environment variables, |
19
|
|
|
* which will be automatically picked up by curl. This means RestfulService and raw curl |
20
|
|
|
* requests should work out of the box. Stream-based requests need extra manual configuration. |
21
|
|
|
* Refer to https://www.cwp.govt.nz/guides/core-technical-documentation/common-web-platform-core/en/how-tos/external_http_requests_with_proxy |
22
|
|
|
* |
23
|
|
|
* @config |
24
|
|
|
* @var bool |
25
|
|
|
*/ |
26
|
|
|
private static $egress_proxy_default_enabled = true; |
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Configure the list of domains to bypass proxy by setting the NO_PROXY environment variable. |
30
|
|
|
* 'services.cwp.govt.nz' needs to be present for Solr and Docvert internal CWP integration. |
31
|
|
|
* 'localhost' is necessary for accessing services on the same instance such as tika-server for text extraction. |
32
|
|
|
* |
33
|
|
|
* @config |
34
|
|
|
* @var string[] |
35
|
|
|
*/ |
36
|
|
|
private static $egress_proxy_exclude_domains = [ |
|
|
|
|
37
|
|
|
'services.cwp.govt.nz', |
38
|
|
|
'localhost', |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
public function process(HTTPRequest $request, callable $delegate) |
42
|
|
|
{ |
43
|
|
|
if ($this->config()->get('egress_proxy_default_enabled')) { |
44
|
|
|
$this->configureEgressProxy(); |
45
|
|
|
} |
46
|
|
|
$this->configureProxyDomainExclusions(); |
47
|
|
|
|
48
|
|
|
return $delegate($request); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* If the outbound egress proxy details have been defined in environment variables, configure the proxy |
53
|
|
|
* variables that are used to configure it. |
54
|
|
|
*/ |
55
|
|
|
protected function configureEgressProxy() |
56
|
|
|
{ |
57
|
|
|
if (!Environment::getEnv('SS_OUTBOUND_PROXY') |
58
|
|
|
|| !Environment::getEnv('SS_OUTBOUND_PROXY_PORT') |
59
|
|
|
) { |
60
|
|
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$proxy = Environment::getEnv('SS_OUTBOUND_PROXY'); |
64
|
|
|
$proxyPort = Environment::getEnv('SS_OUTBOUND_PROXY_PORT'); |
65
|
|
|
|
66
|
|
|
Environment::setEnv('http_proxy', $proxy . ':' . $proxyPort); |
67
|
|
|
Environment::setEnv('https_proxy', $proxy . ':' . $proxyPort); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Configure any domains that should be excluded from egress proxy rules and provide them to the environment |
72
|
|
|
*/ |
73
|
|
|
protected function configureProxyDomainExclusions() |
74
|
|
|
{ |
75
|
|
|
$noProxy = $this->config()->get('egress_proxy_exclude_domains'); |
76
|
|
|
if (empty($noProxy)) { |
77
|
|
|
return; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (!is_array($noProxy)) { |
81
|
|
|
$noProxy = [$noProxy]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// Merge with exsiting if needed. |
85
|
|
|
if (Environment::getEnv('NO_PROXY')) { |
86
|
|
|
$noProxy = array_merge(explode(',', Environment::getEnv('NO_PROXY')), $noProxy); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
Environment::setEnv('NO_PROXY', implode(',', array_unique($noProxy))); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|