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;
@ -191,8 +192,16 @@ class Cryptography
* @throws CryptographyException If the public key is invalid or the encryption fails. * @throws CryptographyException If the public key is invalid or the encryption fails.
*/ */
public static function encryptContent(string $content, string $publicKey): string public static function encryptContent(string $content, string $publicKey): string
{
try
{ {
$publicKey = openssl_pkey_get_public(self::derToPem(Utilities::base64decode($publicKey), self::PEM_PUBLIC_HEADER)); $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,8 +212,15 @@ class Cryptography
throw new CryptographyException('Failed to encrypt content: ' . openssl_error_string()); throw new CryptographyException('Failed to encrypt content: ' . openssl_error_string());
} }
try
{
return base64_encode($encrypted); return base64_encode($encrypted);
} }
catch(Exception $e)
{
throw new CryptographyException('Failed to encode encrypted content: ' . $e->getMessage());
}
}
/** /**
* Decrypts the provided content using the specified private key. * Decrypts the provided content using the specified private key.

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
try
{
$resolvedServer = ServerResolver::resolveDomain($this->peerAddress->getDomain(), false); $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,15 +1,15 @@
<?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 * Thrown as a standard error, with a message and a code
* *
@ -31,4 +31,4 @@ class StandardException extends Exception
{ {
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,26 +1,37 @@
<?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 $id;
private string $error;
private StandardError $code; private StandardError $code;
private string $error;
/** /**
* Constructs the RPC error object. * Constructs the RPC error object.
* *
* @param string $id The ID of the RPC request * @param string $id The ID of the RPC request
* @param StandardError $code The error code * @param StandardError|int $code The error code
* @param string|null $error The error message * @param string|null $error The error message
*/ */
public function __construct(string $id, StandardError $code, ?string $error) public function __construct(string $id, StandardError|int $code, ?string $error)
{ {
$this->id = $id; $this->id = $id;
if(is_int($code))
{
$code = StandardError::tryFrom($code);
if($code === null)
{
$code = StandardError::UNKNOWN;
}
}
$this->code = $code; $this->code = $code;
if($error === null) if($error === null)
@ -44,16 +55,6 @@ class RpcError implements SerializableInterface
return $this->id; return $this->id;
} }
/**
* Returns the error message.
*
* @return string The error message.
*/
public function getError(): string
{
return $this->error;
}
/** /**
* Returns the error code. * Returns the error code.
* *
@ -64,6 +65,16 @@ class RpcError implements SerializableInterface
return $this->code; 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. * Returns an array representation of the object.
* *
@ -73,11 +84,21 @@ class RpcError implements SerializableInterface
{ {
return [ return [
'id' => $this->id, 'id' => $this->id,
'error' => $this->error, 'code' => $this->code->value,
'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. * Returns the RPC error object from an array of data.
* *
@ -86,13 +107,6 @@ class RpcError implements SerializableInterface
*/ */
public static function fromArray(array $data): RpcError public static function fromArray(array $data): RpcError
{ {
$errorCode = StandardError::tryFrom($data['code']); return new RpcError($data['id'], $data['code'], $data['error']);
if($errorCode == null)
{
$errorCode = StandardError::UNKNOWN;
} }
return new RpcError($data['id'], $data['error'], $errorCode);
} }
}

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,11 +1,11 @@
<?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 string $id;
private mixed $result; private mixed $result;
@ -81,4 +81,4 @@ class RpcResponse implements SerializableInterface
{ {
return new RpcResponse($data['id'], $data['result']); 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()
{ {