1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* AWS helper class. Contains useful functions when interacting with the SDK.
|
|
|
19 |
*
|
|
|
20 |
* @package core
|
|
|
21 |
* @author Peter Burnett <peterburnett@catalyst-au.net>
|
|
|
22 |
* @copyright 2020 Catalyst IT
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
*/
|
|
|
25 |
|
|
|
26 |
namespace core\aws;
|
|
|
27 |
|
|
|
28 |
use Aws\CommandInterface;
|
|
|
29 |
use Aws\AwsClient;
|
|
|
30 |
use Psr\Http\Message\RequestInterface;
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* This class contains functions that help plugins to interact with the AWS SDK.
|
|
|
34 |
*
|
|
|
35 |
* @copyright 2020 Catalyst IT
|
|
|
36 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
37 |
*/
|
|
|
38 |
class aws_helper {
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* This creates a proxy string suitable for use with the AWS SDK.
|
|
|
42 |
*
|
|
|
43 |
* @return string the string to use for proxy settings.
|
|
|
44 |
*/
|
|
|
45 |
public static function get_proxy_string(): string {
|
|
|
46 |
global $CFG;
|
|
|
47 |
$proxy = '';
|
|
|
48 |
if (empty($CFG->proxytype)) {
|
|
|
49 |
return $proxy;
|
|
|
50 |
}
|
|
|
51 |
if ($CFG->proxytype === 'SOCKS5') {
|
|
|
52 |
// If it is a SOCKS proxy, append the protocol info.
|
|
|
53 |
$protocol = 'socks5://';
|
|
|
54 |
} else {
|
|
|
55 |
$protocol = '';
|
|
|
56 |
}
|
|
|
57 |
if (!empty($CFG->proxyhost)) {
|
|
|
58 |
$proxy = $CFG->proxyhost;
|
|
|
59 |
if (!empty($CFG->proxyport)) {
|
|
|
60 |
$proxy .= ':'. $CFG->proxyport;
|
|
|
61 |
}
|
|
|
62 |
if (!empty($CFG->proxyuser) && !empty($CFG->proxypassword)) {
|
|
|
63 |
$proxy = $protocol . $CFG->proxyuser . ':' . $CFG->proxypassword . '@' . $proxy;
|
|
|
64 |
}
|
|
|
65 |
}
|
|
|
66 |
return $proxy;
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
* Configure the provided AWS client to route traffic via the moodle proxy for any hosts not excluded.
|
|
|
71 |
*
|
|
|
72 |
* @param AwsClient $client
|
|
|
73 |
* @return AwsClient
|
|
|
74 |
*/
|
|
|
75 |
public static function configure_client_proxy(AwsClient $client): AwsClient {
|
|
|
76 |
$client->getHandlerList()->appendBuild(self::add_proxy_when_required(), 'proxy_bypass');
|
|
|
77 |
return $client;
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
* Generate a middleware higher order function to wrap the handler and append proxy configuration based on target.
|
|
|
82 |
*
|
|
|
83 |
* @return callable Middleware high order callable.
|
|
|
84 |
*/
|
|
|
85 |
protected static function add_proxy_when_required(): callable {
|
|
|
86 |
return function (callable $fn) {
|
|
|
87 |
return function (CommandInterface $command, ?RequestInterface $request = null) use ($fn) {
|
|
|
88 |
if (isset($request)) {
|
|
|
89 |
$target = (string) $request->getUri();
|
|
|
90 |
if (!is_proxybypass($target)) {
|
|
|
91 |
$command['@http']['proxy'] = self::get_proxy_string();
|
|
|
92 |
}
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
$promise = $fn($command, $request);
|
|
|
96 |
return $promise;
|
|
|
97 |
};
|
|
|
98 |
};
|
|
|
99 |
}
|
|
|
100 |
}
|