From de2153395fae330c7278b9da8629acb3412462de Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sun, 1 Nov 2020 21:29:44 +0100 Subject: [PATCH] Continued: - also allow senderAddress, not just senderId - WIP: still a mismatching hash is generated - $traceArray[args] might not be set MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../package/class_NetworkPackageHandler.php | 24 ++++++++++++++----- .../hub/classes/tools/hub/class_HubTools.php | 21 ++++++++++++++-- application/hub/exceptions.php | 13 ++++++---- 3 files changed, 45 insertions(+), 13 deletions(-) diff --git a/application/hub/classes/handler/package/class_NetworkPackageHandler.php b/application/hub/classes/handler/package/class_NetworkPackageHandler.php index 548078ff5..b5dcc5c14 100644 --- a/application/hub/classes/handler/package/class_NetworkPackageHandler.php +++ b/application/hub/classes/handler/package/class_NetworkPackageHandler.php @@ -405,19 +405,31 @@ class NetworkPackageHandler extends BaseHubHandler implements Deliverable, Recei * @param $packageInstance An instance of a DeliverablePackage class * @return $hash Private key's hash * @throws InvalidPrivateKeyHashException If the private key's hash is not valid + * @throws InvalidArgumentException If $packageInstance does not contain senderId/senderAddress */ private function determineSenderPrivateKeyHash (DeliverablePackage $packageInstance) { // Get DHT instance $dhtInstance = DhtObjectFactory::createDhtInstance('node'); - // Ask DHT for session id - /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: Finding local node data for packageInstance->senderId=%s ...', $packageInstance->getSenderId())); - $senderData = $dhtInstance->findNodeLocalBySessionId($packageInstance->getSenderId()); - - // Debugging: - //* DEBUG-DIE: */ die(sprintf('[%s:%d]: senderData=%s', __METHOD__, __LINE__, print_r($senderData, TRUE))); + // Init sender data array + $senderData = array(); + + // Ask DHT for sender's id or address (both session id) + if ($packageInstance->getSenderId() != '') { + // Field senderId is given + /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: Finding local node data for packageInstance->senderId=%s ...', $packageInstance->getSenderId())); + $senderData = $dhtInstance->findNodeLocalBySessionId($packageInstance->getSenderId()); + } elseif ($packageInstance->getSenderAddress() != '') { + // Field senderAddress is given + /* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: Finding local node data for packageInstance->senderAddress=%s ...', $packageInstance->getSenderAddress())); + $senderData = $dhtInstance->findNodeLocalBySessionId($packageInstance->getSenderAddress()); + } else { + // Invalid $packageInstance + throw new InvalidArgumentException('packageInstance does not contain senderId/senderAddress'); + } // Is an entry found? + //* DEBUG-DIE: */ die(sprintf('[%s:%d]: senderData=%s', __METHOD__, __LINE__, print_r($senderData, TRUE))); if (count($senderData) > 0) { // Make sure the element 'private_key_hash' is there //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: senderData=' . print_r($senderData, TRUE)); diff --git a/application/hub/classes/tools/hub/class_HubTools.php b/application/hub/classes/tools/hub/class_HubTools.php index 3e15261f1..8f48342b9 100644 --- a/application/hub/classes/tools/hub/class_HubTools.php +++ b/application/hub/classes/tools/hub/class_HubTools.php @@ -14,6 +14,9 @@ use Org\Shipsimu\Hub\Node\Data\InvalidSessionIdException; use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap; use Org\Mxchange\CoreFramework\Factory\ObjectFactory; +// Import SPL stuff +use \InvalidArgumentException; + /** * This class contains static helper functions for our hub * @@ -100,10 +103,17 @@ class HubTools extends BaseHubSystem { * * @param $sessionId A valid session id * @return $recipientUniversalNodeLocator Recipient as Universal Node Locator + * @throws InvalidArgumentException If $sessionId is not valid */ protected function resolveUniversalNodeLocatorBySessionId ($sessionId) { - // Init variable + // Validate parameter /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HUB-TOOLS: sessionId=%s - CALLED!', $sessionId)); + if (empty($sessionId)) { + // Throw exception + throw new InvalidArgumentException('Parameter "sessionId" is empty'); + } + + // Init variable $recipientUniversalNodeLocator = 'invalid://invalid:invalid'; // And ask it for Universal Node Locator by given session id @@ -136,10 +146,17 @@ class HubTools extends BaseHubSystem { * * @param $sessionId Session id * @return $nodeId Node id + * @throws InvalidArgumentException If $sessionId is not valid */ public static function resolveNodeIdBySessionId ($sessionId) { - // Get an own instance + // Validate parameter /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HUB-TOOLS: sessionId=%s - CALLED!', $sessionId)); + if (empty($sessionId)) { + // Throw exception + throw new InvalidArgumentException('Parameter "sessionId" is empty'); + } + + // Get an own instance $selfInstance = self::getSelfInstance(); // And ask it for session id by given Universal Node Locator diff --git a/application/hub/exceptions.php b/application/hub/exceptions.php index a343589d8..8051930bc 100644 --- a/application/hub/exceptions.php +++ b/application/hub/exceptions.php @@ -39,11 +39,14 @@ function hub_exception_handler ($exceptionInstance) { // Init argument string $argsString = ''; - // Convert arguments type into human-readable - foreach ($traceArray['args'] as $arg) { - $argsString .= ', ' . gettype($arg); - } // END - foreach - $argsString = substr($argsString, 2); + // Arguments given? + if (isset($traceArray['args'])) { + // Convert arguments type into human-readable + foreach ($traceArray['args'] as $arg) { + $argsString .= ', ' . gettype($arg); + } // END - foreach + $argsString = substr($argsString, 2); + } // Set missing file/line if (!isset($traceArray['file'])) $traceArray['file'] = 'unknown'; -- 2.39.5