Skip to content

Commit

Permalink
allow clients to send stateless gateway requests without prior regist…
Browse files Browse the repository at this point in the history
…ration
  • Loading branch information
jstuczyn committed Sep 12, 2024
1 parent 60917ec commit a4b47ef
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 215 deletions.
27 changes: 20 additions & 7 deletions common/client-libs/gateway-client/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ impl<C, St> GatewayClient<C, St> {
// as we need to be able to write the request and read the subsequent response
async fn send_websocket_message(
&mut self,
msg: Message,
msg: impl Into<Message>,
) -> Result<ServerResponse, GatewayClientError> {
let should_restart_mixnet_listener = if self.connection.is_partially_delegated() {
self.recover_socket_connection().await?;
Expand All @@ -307,7 +307,7 @@ impl<C, St> GatewayClient<C, St> {
SocketState::NotConnected => return Err(GatewayClientError::ConnectionNotEstablished),
_ => return Err(GatewayClientError::ConnectionInInvalidState),
};
conn.send(msg).await?;
conn.send(msg.into()).await?;
let response = self.read_control_response().await;

if should_restart_mixnet_listener {
Expand Down Expand Up @@ -481,8 +481,7 @@ impl<C, St> GatewayClient<C, St> {
encrypted_address,
iv,
self.cfg.bandwidth.require_tickets,
)
.into();
);

match self.send_websocket_message(msg).await? {
ServerResponse::Authenticate {
Expand Down Expand Up @@ -532,6 +531,21 @@ impl<C, St> GatewayClient<C, St> {
}
}

pub async fn get_gateway_protocol(&mut self) -> Result<u8, GatewayClientError> {
if !self.connection.is_established() {
return Err(GatewayClientError::ConnectionNotEstablished);
}

match self
.send_websocket_message(ClientControlRequest::SupportedProtocol {})
.await?
{
ServerResponse::SupportedProtocol { version } => Ok(version),
ServerResponse::Error { message } => Err(GatewayClientError::GatewayError(message)),
_ => Err(GatewayClientError::UnexpectedResponse),
}
}

async fn claim_ecash_bandwidth(
&mut self,
credential: CredentialSpendingData,
Expand All @@ -543,8 +557,7 @@ impl<C, St> GatewayClient<C, St> {
credential,
self.shared_key.as_ref().unwrap(),
iv,
)
.into();
);
let bandwidth_remaining = match self.send_websocket_message(msg).await? {
ServerResponse::Bandwidth { available_total } => Ok(available_total),
ServerResponse::Error { message } => Err(GatewayClientError::GatewayError(message)),
Expand All @@ -562,7 +575,7 @@ impl<C, St> GatewayClient<C, St> {
}

async fn try_claim_testnet_bandwidth(&mut self) -> Result<(), GatewayClientError> {
let msg = ClientControlRequest::ClaimFreeTestnetBandwidth.into();
let msg = ClientControlRequest::ClaimFreeTestnetBandwidth;
let bandwidth_remaining = match self.send_websocket_message(msg).await? {
ServerResponse::Bandwidth { available_total } => Ok(available_total),
ServerResponse::Error { message } => Err(GatewayClientError::GatewayError(message)),
Expand Down
16 changes: 15 additions & 1 deletion common/gateway-requests/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ pub enum ClientControlRequest {
iv: Vec<u8>,
},
ClaimFreeTestnetBandwidth,
SupportedProtocol {},
}

impl ClientControlRequest {
Expand Down Expand Up @@ -229,6 +230,7 @@ impl ClientControlRequest {
ClientControlRequest::ClaimFreeTestnetBandwidth => {
"ClaimFreeTestnetBandwidth".to_string()
}
ClientControlRequest::SupportedProtocol { .. } => "SupportedProtocol".to_string(),
}
}

Expand Down Expand Up @@ -272,7 +274,15 @@ impl TryFrom<String> for ClientControlRequest {
type Error = serde_json::Error;

fn try_from(msg: String) -> Result<Self, Self::Error> {
serde_json::from_str(&msg)
msg.parse()
}
}

impl FromStr for ClientControlRequest {
type Err = serde_json::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
serde_json::from_str(s)
}
}

Expand Down Expand Up @@ -304,6 +314,9 @@ pub enum ServerResponse {
Send {
remaining_bandwidth: i64,
},
SupportedProtocol {
version: u8,
},
// Generic error
Error {
message: String,
Expand All @@ -324,6 +337,7 @@ impl ServerResponse {
ServerResponse::Send { .. } => "Send".to_string(),
ServerResponse::Error { .. } => "Error".to_string(),
ServerResponse::TypedError { .. } => "TypedError".to_string(),
ServerResponse::SupportedProtocol { .. } => "SupportedProtocol".to_string(),
}
}
pub fn new_error<S: Into<String>>(msg: S) -> Self {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ impl IntoWSMessage for Result<ServerResponse, RequestHandlingError> {
}
}

impl IntoWSMessage for ServerResponse {
fn into_ws_message(self) -> Message {
self.into()
}
}

pub(crate) struct AuthenticatedHandler<R, S, St> {
inner: FreshHandler<R, S, St>,
bandwidth_storage_manager: BandwidthStorageManager<St>,
Expand Down Expand Up @@ -322,13 +328,28 @@ where
.await
.map_err(|e| e.into())
.into_ws_message(),
other => RequestHandlingError::IllegalRequest {
additional_context: format!(
"received illegal message of type {} in an authenticated client",
other.name()
),
ClientControlRequest::SupportedProtocol { .. } => self
.inner
.handle_supported_protocol_request()
.into_ws_message(),
other @ ClientControlRequest::Authenticate { .. } => {
RequestHandlingError::IllegalRequest {
additional_context: format!(
"received illegal message of type {} in an authenticated client",
other.name()
),
}
.into_error_message()
}
other @ ClientControlRequest::RegisterHandshakeInitRequest { .. } => {
RequestHandlingError::IllegalRequest {
additional_context: format!(
"received illegal message of type {} in an authenticated client",
other.name()
),
}
.into_error_message()
}
.into_error_message(),
},
}
}
Expand Down
Loading

0 comments on commit a4b47ef

Please sign in to comment.