From 4d970d3638c839230e0430730de28b3b06c9521e Mon Sep 17 00:00:00 2001 From: Daniel Morell Date: Fri, 1 Mar 2024 12:27:08 -0600 Subject: [PATCH 1/3] Fixed #480 IP could not resolve IPv6 --- src/Senders/CurlSender.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Senders/CurlSender.php b/src/Senders/CurlSender.php index 4994827a..54e9af21 100644 --- a/src/Senders/CurlSender.php +++ b/src/Senders/CurlSender.php @@ -26,6 +26,7 @@ class CurlSender implements SenderInterface private $maxBatchRequests = 75; private $batchRequests = array(); private $inflightRequests = array(); + private $ipResolve = CURL_IPRESOLVE_V4; public function __construct($opts) { @@ -54,6 +55,10 @@ public function __construct($opts) if (array_key_exists('ca_cert_path', $opts)) { $this->caCertPath = $opts['ca_cert_path']; } + if (array_key_exists('ip_resolve', $opts) + && in_array($opts['ip_resolve'], [CURL_IPRESOLVE_V4, CURL_IPRESOLVE_V6, CURL_IPRESOLVE_WHATEVER])) { + $this->ipResolve = $opts['ip_resolve']; + } } public function getEndpoint() @@ -151,7 +156,7 @@ public function setCurlOptions(CurlHandle $handle, EncodedPayload $payload, stri curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); curl_setopt($handle, CURLOPT_TIMEOUT, $this->timeout); curl_setopt($handle, CURLOPT_HTTPHEADER, array('X-Rollbar-Access-Token: ' . $accessToken)); - curl_setopt($handle, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); + curl_setopt($handle, CURLOPT_IPRESOLVE, $this->ipResolve); if (!is_null($this->caCertPath)) { curl_setopt($handle, CURLOPT_CAINFO, $this->caCertPath); From 25f4ef643e02df2132f05bd613ccf6335fc4b840 Mon Sep 17 00:00:00 2001 From: Daniel Morell Date: Fri, 1 Mar 2024 14:47:17 -0600 Subject: [PATCH 2/3] Added tests for the `ip_resolve` config. --- tests/CurlSenderTest.php | 42 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/tests/CurlSenderTest.php b/tests/CurlSenderTest.php index 0e084309..120f656c 100644 --- a/tests/CurlSenderTest.php +++ b/tests/CurlSenderTest.php @@ -2,7 +2,10 @@ namespace Rollbar; +use ReflectionClass; +use ReflectionException; use Rollbar\Payload\Level; +use Rollbar\Senders\CurlSender; class CurlSenderTest extends BaseRollbarTest { @@ -26,4 +29,43 @@ public function testCurlError(): void ) ); } + + /** + * This test will fail if the {@see CurlSender::$ipResolve} property is renamed or removed. + */ + public function testIPResolve(): void + { + $sender = new CurlSender([ + 'ip_resolve' => CURL_IPRESOLVE_V4, + ]); + self::assertSame(CURL_IPRESOLVE_V4, self::getPrivateProperty($sender, 'ipResolve')); + + $sender = new CurlSender([]); + self::assertSame(CURL_IPRESOLVE_V4, self::getPrivateProperty($sender, 'ipResolve')); + + $sender = new CurlSender([ + 'ip_resolve' => CURL_IPRESOLVE_V6, + ]); + self::assertSame(CURL_IPRESOLVE_V6, self::getPrivateProperty($sender, 'ipResolve')); + + $sender = new CurlSender([ + 'ip_resolve' => CURL_IPRESOLVE_WHATEVER, + ]); + self::assertSame(CURL_IPRESOLVE_WHATEVER, self::getPrivateProperty($sender, 'ipResolve')); + } + + /** + * Returns the value of a private property of an object. + * + * @param object $object The object from which to get the private property. + * @param string $property The name of the private property to get. + * @return mixed + * @throws ReflectionException If the object or property does not exist. + */ + private static function getPrivateProperty(object $object, string $property): mixed + { + $reflection = new ReflectionClass($object); + $property = $reflection->getProperty($property); + return $property->getValue($object); + } } From 8e03ea45ec7f18a0fecdf34b0f0dc132276f27d2 Mon Sep 17 00:00:00 2001 From: Daniel Morell Date: Fri, 1 Mar 2024 15:23:59 -0600 Subject: [PATCH 3/3] Made property publicly accessible. --- tests/CurlSenderTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/CurlSenderTest.php b/tests/CurlSenderTest.php index 120f656c..891250bb 100644 --- a/tests/CurlSenderTest.php +++ b/tests/CurlSenderTest.php @@ -66,6 +66,7 @@ private static function getPrivateProperty(object $object, string $property): mi { $reflection = new ReflectionClass($object); $property = $reflection->getProperty($property); + $property->setAccessible(true); return $property->getValue($object); } }