Refactor RPC framework and enhance error handling.

This commit is contained in:
netkas 2024-12-19 15:09:22 -05:00
parent 42ba7013f7
commit ef3b10b286
10 changed files with 383 additions and 245 deletions

View file

@ -2,6 +2,7 @@
namespace Socialbox\Classes; namespace Socialbox\Classes;
use Exception;
use InvalidArgumentException; use InvalidArgumentException;
use Random\RandomException; use Random\RandomException;
use Socialbox\Exceptions\CryptographyException; use Socialbox\Exceptions\CryptographyException;
@ -192,7 +193,15 @@ class Cryptography
*/ */
public static function encryptContent(string $content, string $publicKey): string public static function encryptContent(string $content, string $publicKey): string
{ {
$publicKey = openssl_pkey_get_public(self::derToPem(Utilities::base64decode($publicKey), self::PEM_PUBLIC_HEADER)); try
{
$publicKey = openssl_pkey_get_public(self::derToPem(Utilities::base64decode($publicKey), self::PEM_PUBLIC_HEADER));
}
catch(Exception $e)
{
throw new CryptographyException('Failed to decode public key: ' . $e->getMessage());
}
if (!$publicKey) if (!$publicKey)
{ {
throw new CryptographyException('Invalid public key: ' . openssl_error_string()); throw new CryptographyException('Invalid public key: ' . openssl_error_string());
@ -203,7 +212,14 @@ class Cryptography
throw new CryptographyException('Failed to encrypt content: ' . openssl_error_string()); throw new CryptographyException('Failed to encrypt content: ' . openssl_error_string());
} }
return base64_encode($encrypted); try
{
return base64_encode($encrypted);
}
catch(Exception $e)
{
throw new CryptographyException('Failed to encode encrypted content: ' . $e->getMessage());
}
} }
/** /**

View file

@ -12,7 +12,7 @@
use Socialbox\Objects\KeyPair; use Socialbox\Objects\KeyPair;
use Socialbox\Objects\PeerAddress; use Socialbox\Objects\PeerAddress;
use Socialbox\Objects\RpcRequest; use Socialbox\Objects\RpcRequest;
use Socialbox\Objects\RpcResponse; use Socialbox\Objects\RpcResult;
class RpcClient class RpcClient
{ {
@ -34,7 +34,6 @@
* @param ExportedSession|null $exportedSession Optional. An exported session to be used to re-connect. * @param ExportedSession|null $exportedSession Optional. An exported session to be used to re-connect.
* @throws CryptographyException If there is an error in the cryptographic operations. * @throws CryptographyException If there is an error in the cryptographic operations.
* @throws RpcException If there is an error in the RPC request or if no response is received. * @throws RpcException If there is an error in the RPC request or if no response is received.
* @throws DatabaseOperationException If there is an error in the database operations.
* @throws ResolutionException If there is an error in the resolution process. * @throws ResolutionException If there is an error in the resolution process.
*/ */
public function __construct(string|PeerAddress $peerAddress, ?ExportedSession $exportedSession=null) public function __construct(string|PeerAddress $peerAddress, ?ExportedSession $exportedSession=null)
@ -65,7 +64,15 @@
$this->encryptionKey = Cryptography::generateEncryptionKey(); $this->encryptionKey = Cryptography::generateEncryptionKey();
// Resolve the domain and get the server's Public Key & RPC Endpoint // Resolve the domain and get the server's Public Key & RPC Endpoint
$resolvedServer = ServerResolver::resolveDomain($this->peerAddress->getDomain(), false); try
{
$resolvedServer = ServerResolver::resolveDomain($this->peerAddress->getDomain(), false);
}
catch (DatabaseOperationException $e)
{
throw new ResolutionException('Failed to resolve domain: ' . $e->getMessage(), 0, $e);
}
$this->serverPublicKey = $resolvedServer->getPublicKey(); $this->serverPublicKey = $resolvedServer->getPublicKey();
$this->rpcEndpoint = $resolvedServer->getEndpoint(); $this->rpcEndpoint = $resolvedServer->getEndpoint();
@ -171,7 +178,7 @@
* Sends an RPC request with the given JSON data. * Sends an RPC request with the given JSON data.
* *
* @param string $jsonData The JSON data to be sent in the request. * @param string $jsonData The JSON data to be sent in the request.
* @return array An array of RpcResult objects. * @return RpcResult[] An array of RpcResult objects.
* @throws RpcException If the request fails, the response is invalid, or the decryption/signature verification fails. * @throws RpcException If the request fails, the response is invalid, or the decryption/signature verification fails.
*/ */
public function sendRawRequest(string $jsonData): array public function sendRawRequest(string $jsonData): array
@ -179,7 +186,7 @@
try try
{ {
$encryptedData = Cryptography::encryptTransport($jsonData, $this->encryptionKey); $encryptedData = Cryptography::encryptTransport($jsonData, $this->encryptionKey);
$signature = Cryptography::signContent($jsonData, $this->keyPair->getPrivateKey()); $signature = Cryptography::signContent($jsonData, $this->keyPair->getPrivateKey(), true);
} }
catch (CryptographyException $e) catch (CryptographyException $e)
{ {
@ -187,15 +194,27 @@
} }
$ch = curl_init(); $ch = curl_init();
$headers = [];
curl_setopt($ch, CURLOPT_URL, $this->rpcEndpoint); curl_setopt($ch, CURLOPT_URL, $this->rpcEndpoint);
curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($curl, $header) use (&$headers)
{
$len = strlen($header);
$header = explode(':', $header, 2);
if (count($header) < 2) // ignore invalid headers
{
return $len;
}
$headers[strtolower(trim($header[0]))][] = trim($header[1]);
return $len;
});
curl_setopt($ch, CURLOPT_HTTPHEADER, [ curl_setopt($ch, CURLOPT_HTTPHEADER, [
StandardHeaders::REQUEST_TYPE->value . ': ' . RequestType::RPC->value, StandardHeaders::REQUEST_TYPE->value . ': ' . RequestType::RPC->value,
StandardHeaders::SESSION_UUID->value . ': ' . $this->sessionUuid, StandardHeaders::SESSION_UUID->value . ': ' . $this->sessionUuid,
StandardHeaders::SIGNATURE->value . ': ' . $signature, StandardHeaders::SIGNATURE->value . ': ' . $signature
'Content-Type: application/encrypted-json',
]); ]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encryptedData); curl_setopt($ch, CURLOPT_POSTFIELDS, $encryptedData);
@ -246,7 +265,7 @@
if (!$this->bypassSignatureVerification) if (!$this->bypassSignatureVerification)
{ {
$signature = curl_getinfo($ch, CURLINFO_HEADER_OUT)['Signature'] ?? null; $signature = $headers['signature'][0] ?? null;
if ($signature === null) if ($signature === null)
{ {
throw new RpcException('The server did not provide a signature for the response'); throw new RpcException('The server did not provide a signature for the response');
@ -254,7 +273,7 @@
try try
{ {
if (!Cryptography::verifyContent($decryptedResponse, $signature, $this->serverPublicKey)) if (!Cryptography::verifyContent($decryptedResponse, $signature, $this->serverPublicKey, true))
{ {
throw new RpcException('Failed to verify the response signature'); throw new RpcException('Failed to verify the response signature');
} }
@ -266,41 +285,45 @@
} }
$decoded = json_decode($decryptedResponse, true); $decoded = json_decode($decryptedResponse, true);
if(isset($decoded['id']))
if (is_array($decoded)) {
return [new RpcResult($decoded)];
}
else
{ {
$results = []; $results = [];
foreach ($decoded as $responseMap) foreach ($decoded as $responseMap)
{ {
$results[] = RpcResponse::fromArray($responseMap); $results[] = new RpcResult($responseMap);
} }
return $results; return $results;
} }
if (is_object($decoded))
{
return [RpcResponse::fromArray((array)$decoded)];
}
throw new RpcException('Failed to decode response');
} }
/** /**
* Sends an RPC request and retrieves the corresponding RPC response. * Sends an RPC request and retrieves the corresponding RPC response.
* *
* @param RpcRequest $request The RPC request to be sent. * @param RpcRequest $request The RPC request to be sent.
* @return RpcResponse The received RPC response. * @return RpcResult The received RPC response.
* @throws RpcException If no response is received from the request. * @throws RpcException If no response is received from the request.
*/ */
public function sendRequest(RpcRequest $request): RpcResponse public function sendRequest(RpcRequest $request, bool $throwException=true): RpcResult
{ {
$response = $this->sendRawRequest(json_encode($request)); $response = $this->sendRawRequest(json_encode($request->toArray()));
if (count($response) === 0) if (count($response) === 0)
{ {
throw new RpcException('Failed to send request, no response received'); throw new RpcException('Failed to send request, no response received');
} }
if($throwException)
{
if($response[0]->getError() !== null)
{
throw $response[0]->getError()->toRpcException();
}
}
return $response[0]; return $response[0];
} }
@ -309,7 +332,7 @@
* and handles the response. * and handles the response.
* *
* @param RpcRequest[] $requests An array of RpcRequest objects to be sent to the server. * @param RpcRequest[] $requests An array of RpcRequest objects to be sent to the server.
* @return RpcResponse[] An array of RpcResponse objects received from the server. * @return RpcResult[] An array of RpcResult objects received from the server.
* @throws RpcException If no response is received from the server. * @throws RpcException If no response is received from the server.
*/ */
public function sendRequests(array $requests): array public function sendRequests(array $requests): array
@ -324,7 +347,7 @@
if (count($responses) === 0) if (count($responses) === 0)
{ {
throw new RpcException('Failed to send requests, no response received'); return [];
} }
return $responses; return $responses;

View file

@ -1,34 +1,34 @@
<?php <?php
namespace Socialbox\Exceptions; namespace Socialbox\Exceptions;
use Exception; use Exception;
use Socialbox\Enums\StandardError; use Socialbox\Enums\StandardError;
use Socialbox\Objects\RpcError; use Socialbox\Objects\RpcError;
use Socialbox\Objects\RpcRequest; use Socialbox\Objects\RpcRequest;
use Throwable; use Throwable;
class StandardException extends Exception class StandardException extends Exception
{
/**
* Thrown as a standard error, with a message and a code
*
* @param string $message
* @param StandardError $code
* @param Throwable|null $previous
*/
public function __construct(string $message, StandardError $code, ?Throwable $previous=null)
{ {
parent::__construct($message, $code->value, $previous); /**
} * Thrown as a standard error, with a message and a code
*
* @param string $message
* @param StandardError $code
* @param Throwable|null $previous
*/
public function __construct(string $message, StandardError $code, ?Throwable $previous=null)
{
parent::__construct($message, $code->value, $previous);
}
public function getStandardError(): StandardError public function getStandardError(): StandardError
{ {
return StandardError::from($this->code); return StandardError::from($this->code);
} }
public function produceError(RpcRequest $request): ?RpcError public function produceError(RpcRequest $request): ?RpcError
{ {
return $request->produceError(StandardError::from($this->code), $this->message); return $request->produceError(StandardError::from($this->code), $this->message);
}
} }
}

View file

@ -4,6 +4,7 @@
use InvalidArgumentException; use InvalidArgumentException;
use Socialbox\Classes\Cryptography; use Socialbox\Classes\Cryptography;
use Socialbox\Classes\Logger;
use Socialbox\Classes\Utilities; use Socialbox\Classes\Utilities;
use Socialbox\Enums\SessionState; use Socialbox\Enums\SessionState;
use Socialbox\Enums\StandardHeaders; use Socialbox\Enums\StandardHeaders;
@ -141,7 +142,7 @@
try try
{ {
return Cryptography::verifyContent(hash('sha1', $decryptedContent), $this->getSignature(), $this->getSession()->getPublicKey()); return Cryptography::verifyContent($decryptedContent, $this->getSignature(), $this->getSession()->getPublicKey(), true);
} }
catch(CryptographyException) catch(CryptographyException)
{ {

View file

@ -1,98 +1,112 @@
<?php <?php
namespace Socialbox\Objects; namespace Socialbox\Objects;
use Socialbox\Enums\StandardError; use Socialbox\Enums\StandardError;
use Socialbox\Interfaces\SerializableInterface; use Socialbox\Exceptions\RpcException;
use Socialbox\Interfaces\SerializableInterface;
class RpcError implements SerializableInterface class RpcError implements SerializableInterface
{
private string $id;
private string $error;
private StandardError $code;
/**
* Constructs the RPC error object.
*
* @param string $id The ID of the RPC request
* @param StandardError $code The error code
* @param string|null $error The error message
*/
public function __construct(string $id, StandardError $code, ?string $error)
{ {
$this->id = $id; private string $id;
$this->code = $code; private StandardError $code;
private string $error;
if($error === null) /**
* Constructs the RPC error object.
*
* @param string $id The ID of the RPC request
* @param StandardError|int $code The error code
* @param string|null $error The error message
*/
public function __construct(string $id, StandardError|int $code, ?string $error)
{ {
$this->error = $code->getMessage(); $this->id = $id;
}
else if(is_int($code))
{ {
$this->error = $error; $code = StandardError::tryFrom($code);
if($code === null)
{
$code = StandardError::UNKNOWN;
}
}
$this->code = $code;
if($error === null)
{
$this->error = $code->getMessage();
}
else
{
$this->error = $error;
}
} }
} /**
* Returns the ID of the RPC request.
/** *
* Returns the ID of the RPC request. * @return string The ID of the RPC request.
* */
* @return string The ID of the RPC request. public function getId(): string
*/
public function getId(): string
{
return $this->id;
}
/**
* Returns the error message.
*
* @return string The error message.
*/
public function getError(): string
{
return $this->error;
}
/**
* Returns the error code.
*
* @return StandardError The error code.
*/
public function getCode(): StandardError
{
return $this->code;
}
/**
* Returns an array representation of the object.
*
* @return array The array representation of the object.
*/
public function toArray(): array
{
return [
'id' => $this->id,
'error' => $this->error,
'code' => $this->code->value
];
}
/**
* Returns the RPC error object from an array of data.
*
* @param array $data The data to construct the RPC error from.
* @return RpcError The RPC error object.
*/
public static function fromArray(array $data): RpcError
{
$errorCode = StandardError::tryFrom($data['code']);
if($errorCode == null)
{ {
$errorCode = StandardError::UNKNOWN; return $this->id;
} }
return new RpcError($data['id'], $data['error'], $errorCode); /**
* Returns the error code.
*
* @return StandardError The error code.
*/
public function getCode(): StandardError
{
return $this->code;
}
/**
* Returns the error message.
*
* @return string The error message.
*/
public function getError(): string
{
return $this->error;
}
/**
* Returns an array representation of the object.
*
* @return array The array representation of the object.
*/
public function toArray(): array
{
return [
'id' => $this->id,
'code' => $this->code->value,
'error' => $this->error
];
}
/**
* Converts the current object to an RpcException instance.
*
* @return RpcException The RpcException generated from the current object.
*/
public function toRpcException(): RpcException
{
return new RpcException($this->error, $this->code->value);
}
/**
* Returns the RPC error object from an array of data.
*
* @param array $data The data to construct the RPC error from.
* @return RpcError The RPC error object.
*/
public static function fromArray(array $data): RpcError
{
return new RpcError($data['id'], $data['code'], $data['error']);
}
} }
}

View file

@ -23,7 +23,7 @@
* @param string|null $id The ID of the request. * @param string|null $id The ID of the request.
* @param array|null $parameters The parameters of the request. * @param array|null $parameters The parameters of the request.
*/ */
public function __construct(string $method, ?string $id, ?array $parameters) public function __construct(string $method, ?string $id, ?array $parameters=null)
{ {
$this->method = $method; $this->method = $method;
$this->parameters = $parameters; $this->parameters = $parameters;

View file

@ -1,84 +1,84 @@
<?php <?php
namespace Socialbox\Objects; namespace Socialbox\Objects;
use Socialbox\Interfaces\SerializableInterface; use Socialbox\Interfaces\SerializableInterface;
class RpcResponse implements SerializableInterface class RpcResponse implements SerializableInterface
{
private string $id;
private mixed $result;
/**
* Constructs the response object.
*
* @param string $id The ID of the response.
* @param mixed|null $result The result of the response.
*/
public function __construct(string $id, mixed $result)
{ {
$this->id = $id; private string $id;
$this->result = $result; private mixed $result;
}
/** /**
* Returns the ID of the response. * Constructs the response object.
* *
* @return string The ID of the response. * @param string $id The ID of the response.
*/ * @param mixed|null $result The result of the response.
public function getId(): string */
{ public function __construct(string $id, mixed $result)
return $this->id;
}
/**
* Returns the result of the response.
*
* @return mixed|null The result of the response.
*/
public function getResult(): mixed
{
return $this->result;
}
/**
* Converts the given data to an array.
*
* @param mixed $data The data to be converted. This can be an instance of SerializableInterface, an array, or a scalar value.
* @return mixed The converted data as an array if applicable, or the original data.
*/
private function convertToArray(mixed $data): mixed
{
// If the data is an instance of SerializableInterface, call toArray on it
if ($data instanceof SerializableInterface)
{ {
return $data->toArray(); $this->id = $id;
$this->result = $result;
} }
return $data; /**
} * Returns the ID of the response.
*
* @return string The ID of the response.
*/
public function getId(): string
{
return $this->id;
}
/** /**
* Returns an array representation of the object. * Returns the result of the response.
* *
* @return array The array representation of the object. * @return mixed|null The result of the response.
*/ */
public function toArray(): array public function getResult(): mixed
{ {
return [ return $this->result;
'id' => $this->id, }
'result' => $this->convertToArray($this->result)
];
}
/** /**
* Returns the response object from an array of data. * Converts the given data to an array.
* *
* @param array $data The data to construct the response from. * @param mixed $data The data to be converted. This can be an instance of SerializableInterface, an array, or a scalar value.
* @return RpcResponse The response object. * @return mixed The converted data as an array if applicable, or the original data.
*/ */
public static function fromArray(array $data): RpcResponse private function convertToArray(mixed $data): mixed
{ {
return new RpcResponse($data['id'], $data['result']); // If the data is an instance of SerializableInterface, call toArray on it
if ($data instanceof SerializableInterface)
{
return $data->toArray();
}
return $data;
}
/**
* Returns an array representation of the object.
*
* @return array The array representation of the object.
*/
public function toArray(): array
{
return [
'id' => $this->id,
'result' => $this->convertToArray($this->result)
];
}
/**
* Returns the response object from an array of data.
*
* @param array $data The data to construct the response from.
* @return RpcResponse The response object.
*/
public static function fromArray(array $data): RpcResponse
{
return new RpcResponse($data['id'], $data['result']);
}
} }
}

View file

@ -0,0 +1,95 @@
<?php
namespace Socialbox\Objects;
class RpcResult
{
private ?RpcResponse $response;
private ?RpcError $error;
/**
* Constructor for initializing the instance with a response or error.
*
* @param RpcResponse|RpcError|array $response An instance of RpcResponse, RpcError, or an associative array
* containing error or result data to initialize the class.
* @return void
*/
public function __construct(RpcResponse|RpcError|array $response)
{
if(is_array($response))
{
if(isset($response['error']) && isset($response['code']))
{
$response = RpcError::fromArray($response);
}
elseif(isset($response['result']))
{
$response = RpcResponse::fromArray($response);
}
else
{
$response = null;
}
}
if($response === null)
{
$this->error = null;
$this->response = null;
return;
}
if($response instanceof RpcResponse)
{
$this->response = $response;
$this->error = null;
return;
}
if($response instanceof RpcError)
{
$this->error = $response;
$this->response = null;
}
}
/**
* Checks whether the operation was successful.
*
* @return bool True if there is no error, otherwise false.
*/
public function isSuccess(): bool
{
return $this->error === null;
}
/**
* Checks if the instance contains no error and no response.
*
* @return bool True if both error and response are null, otherwise false.
*/
public function isEmpty(): bool
{
return $this->error === null && $this->response === null;
}
/**
* Retrieves the error associated with the instance, if any.
*
* @return RpcError|null The error object if an error exists, or null if no error is present.
*/
public function getError(): ?RpcError
{
return $this->error;
}
/**
* Retrieves the RPC response if available.
*
* @return RpcResponse|null The response object if set, or null if no response is present.
*/
public function getResponse(): ?RpcResponse
{
return $this->response;
}
}

View file

@ -5,53 +5,42 @@
use Socialbox\Classes\RpcClient; use Socialbox\Classes\RpcClient;
use Socialbox\Classes\Utilities; use Socialbox\Classes\Utilities;
use Socialbox\Exceptions\CryptographyException; use Socialbox\Exceptions\CryptographyException;
use Socialbox\Exceptions\DatabaseOperationException;
use Socialbox\Exceptions\ResolutionException; use Socialbox\Exceptions\ResolutionException;
use Socialbox\Exceptions\RpcException; use Socialbox\Exceptions\RpcException;
use Socialbox\Objects\ExportedSession;
use Socialbox\Objects\KeyPair; use Socialbox\Objects\KeyPair;
use Socialbox\Objects\PeerAddress;
use Socialbox\Objects\RpcError; use Socialbox\Objects\RpcError;
use Socialbox\Objects\RpcRequest; use Socialbox\Objects\RpcRequest;
class SocialClient extends RpcClient class SocialClient extends RpcClient
{ {
/** /**
* Constructs a new instance with the specified domain. * Constructs the object from an array of data.
* *
* @param string $domain The domain to be set for the instance. * @param string|PeerAddress $peerAddress The address of the peer to connect to.
* @throws ResolutionException * @param ExportedSession|null $exportedSession Optional. The exported session to use for communication.
* @throws CryptographyException If the public key is invalid.
* @throws ResolutionException If the domain cannot be resolved.
* @throws RpcException If the RPC request fails.
*/ */
public function __construct(string $domain) public function __construct(string|PeerAddress $peerAddress, ?ExportedSession $exportedSession=null)
{ {
parent::__construct($domain); parent::__construct($peerAddress, $exportedSession);
} }
/** /**
* Creates a new session using the provided key pair. * Sends a ping request to the server and checks the response.
* *
* @param KeyPair $keyPair The key pair to be used for creating the session. * @return true Returns true if the ping request succeeds.
* @return string The UUID of the created session. * @throws RpcException Thrown if the RPC request fails.
* @throws CryptographyException if there is an error in the cryptographic operations.
* @throws RpcException if there is an error in the RPC request or if no response is received.
*/ */
public function createSession(KeyPair $keyPair): string public function ping(): true
{ {
$response = $this->sendRequest(new RpcRequest('createSession', Utilities::randomCrc32(), [ return (bool)$this->sendRequest(
'public_key' => $keyPair->getPublicKey() new RpcRequest('ping', Utilities::randomCrc32())
])); )->getResponse()->getResult();
if($response === null)
{
throw new RpcException('Failed to create the session, no response received');
}
if($response instanceof RpcError)
{
throw RpcException::fromRpcError($response);
}
$this->setSessionUuid($response->getResult());
$this->setPrivateKey($keyPair->getPrivateKey());
return $response->getResult();
} }
} }

View file

@ -3,8 +3,8 @@
require 'ncc'; require 'ncc';
import('net.nosial.socialbox'); import('net.nosial.socialbox');
$client = new \Socialbox\Classes\RpcClient(generateRandomPeer()); $client = new \Socialbox\SocialClient(generateRandomPeer());
var_dump($client->exportSession()); var_dump($client->ping());
function generateRandomPeer() function generateRandomPeer()
{ {