From e1740e677260d7902c6091806087ebe784591d7f Mon Sep 17 00:00:00 2001 From: Peter Burnett Date: Wed, 17 Jun 2020 11:49:34 +1000 Subject: [PATCH] Added proxy settings for Curl and soap requests --- classes/rest/api.php | 23 +++++++++++++++++++++++ classes/soap/api.php | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/classes/rest/api.php b/classes/rest/api.php index 399bb93..f908686 100644 --- a/classes/rest/api.php +++ b/classes/rest/api.php @@ -292,6 +292,29 @@ public function rest_call($verb, $resourcepath, requestoptions $requestoptions, if (!empty($headers)) { curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); } + // Add moodle proxy curlopts. + if (!empty($CFG->proxyhost)) { + if (empty($CFG->proxyport)) { + $proxyhost = $CFG->proxyhost; + } else { + $proxyhost = $CFG->proxyhost.':'.$CFG->proxyport; + } + if (!empty($CFG->proxyuser) and !empty($CFG->proxypassword)) { + $proxyauth = $CFG->proxyuser.':'.$CFG->proxypassword; + curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC | CURLAUTH_NTLM); + curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth); + } + if (!empty($CFG->proxytype)) { + if ($CFG->proxytype == 'SOCKS5') { + $proxytype = CURLPROXY_SOCKS5; + } else { + $proxytype = CURLPROXY_HTTP; + curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, false); + } + curl_setopt($ch, CURLOPT_PROXYTYPE, $proxytype); + } + curl_setopt($ch, CURLOPT_PROXY, $proxyhost); + } $jsonstr = curl_exec($ch); $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $this->logger->info('response', ['httpcode' => $httpcode]); diff --git a/classes/soap/api.php b/classes/soap/api.php index 2de6975..2b185d8 100644 --- a/classes/soap/api.php +++ b/classes/soap/api.php @@ -70,6 +70,7 @@ class api extends generated\SASDefaultAdapter implements api_session, api_attend * @param stdClass $config - custom config passed in on construct. */ public function __construct(array $options = array(), $wsdl = null, stdClass $config = null) { + global $CFG; $this->setup($config); @@ -94,6 +95,18 @@ public function __construct(array $options = array(), $wsdl = null, stdClass $co $options['trace'] = 1; } + // Add moodle proxy soap options. + if (!empty($CFG->proxyhost)) { + $options['proxy_host'] = $CFG->proxyhost; + if (!empty($CFG->proxyport)) { + $options['proxy_port'] = $CFG->proxyport; + } + if (!empty($CFG->proxyuser) and !empty($CFG->proxypassword)) { + $options['proxy_login'] = $CFG->proxyuser; + $options['proxy_password'] = $CFG->proxypassword; + } + } + $serviceok = $this->test_service_reachable($options['location']); if (!$serviceok) { $this->usable = false; @@ -161,6 +174,7 @@ public function is_usable() { * @return bool */ protected function test_service_reachable($serviceuri) { + global $CFG; $ch = curl_init(); $this->logger->info('Testing service availability: '.$serviceuri); curl_setopt($ch, CURLOPT_URL, $serviceuri); @@ -169,6 +183,29 @@ protected function test_service_reachable($serviceuri) { curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); + // Add moodle proxy curlopts. + if (!empty($CFG->proxyhost)) { + if (empty($CFG->proxyport)) { + $proxyhost = $CFG->proxyhost; + } else { + $proxyhost = $CFG->proxyhost.':'.$CFG->proxyport; + } + if (!empty($CFG->proxyuser) and !empty($CFG->proxypassword)) { + $proxyauth = $CFG->proxyuser.':'.$CFG->proxypassword; + curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC | CURLAUTH_NTLM); + curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth); + } + if (!empty($CFG->proxytype)) { + if ($CFG->proxytype == 'SOCKS5') { + $proxytype = CURLPROXY_SOCKS5; + } else { + $proxytype = CURLPROXY_HTTP; + curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, false); + } + curl_setopt($ch, CURLOPT_PROXYTYPE, $proxytype); + } + curl_setopt($ch, CURLOPT_PROXY, $proxyhost); + } $body = curl_exec($ch); if (stripos($body, '