|
|
@ -6,6 +6,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use async_std::task;
|
|
|
|
use rand::{thread_rng, RngCore};
|
|
|
|
use rand::{thread_rng, RngCore};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use x25519_dalek::PublicKey;
|
|
|
|
use x25519_dalek::PublicKey;
|
|
|
@ -137,13 +138,21 @@ impl VentedServer {
|
|
|
|
let connections = Arc::clone(&connections);
|
|
|
|
let connections = Arc::clone(&connections);
|
|
|
|
Box::pin(async move {
|
|
|
|
Box::pin(async move {
|
|
|
|
let payload = event.get_payload::<RedirectPayload>().ok()?;
|
|
|
|
let payload = event.get_payload::<RedirectPayload>().ok()?;
|
|
|
|
|
|
|
|
|
|
|
|
if payload.source == event.origin? {
|
|
|
|
if payload.source == event.origin? {
|
|
|
|
|
|
|
|
log::trace!(
|
|
|
|
|
|
|
|
"Handling redirect from {} via {} to {}",
|
|
|
|
|
|
|
|
payload.source,
|
|
|
|
|
|
|
|
payload.proxy,
|
|
|
|
|
|
|
|
payload.target
|
|
|
|
|
|
|
|
);
|
|
|
|
let opt_stream = connections.lock().get(&payload.target).cloned();
|
|
|
|
let opt_stream = connections.lock().get(&payload.target).cloned();
|
|
|
|
if let Some(mut stream) = opt_stream {
|
|
|
|
if let Some(mut stream) = opt_stream {
|
|
|
|
if let Ok(_) = stream
|
|
|
|
if let Ok(_) = stream
|
|
|
|
.send(Event::with_payload(REDIRECT_REDIRECTED_EVENT, &payload))
|
|
|
|
.send(Event::with_payload(REDIRECT_REDIRECTED_EVENT, &payload))
|
|
|
|
.await
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
log::trace!("Redirect succeeded");
|
|
|
|
return Some(Event::with_payload(
|
|
|
|
return Some(Event::with_payload(
|
|
|
|
REDIRECT_CONFIRM_EVENT,
|
|
|
|
REDIRECT_CONFIRM_EVENT,
|
|
|
|
&RedirectResponsePayload { id: payload.id },
|
|
|
|
&RedirectResponsePayload { id: payload.id },
|
|
|
@ -152,6 +161,7 @@ impl VentedServer {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log::trace!("Redirect failed");
|
|
|
|
Some(Event::with_payload(
|
|
|
|
Some(Event::with_payload(
|
|
|
|
REDIRECT_FAIL_EVENT,
|
|
|
|
REDIRECT_FAIL_EVENT,
|
|
|
|
&RedirectResponsePayload { id: payload.id },
|
|
|
|
&RedirectResponsePayload { id: payload.id },
|
|
|
@ -168,32 +178,44 @@ impl VentedServer {
|
|
|
|
let mut event_handler = event_handler.clone();
|
|
|
|
let mut event_handler = event_handler.clone();
|
|
|
|
Box::pin(async move {
|
|
|
|
Box::pin(async move {
|
|
|
|
let payload = event.get_payload::<RedirectPayload>().ok()?;
|
|
|
|
let payload = event.get_payload::<RedirectPayload>().ok()?;
|
|
|
|
let event = Event::from(&mut &payload.content[..]).ok()?;
|
|
|
|
|
|
|
|
let origin = event.origin.clone()?;
|
|
|
|
let origin = event.origin.clone()?;
|
|
|
|
|
|
|
|
let event = Event::from(&mut &payload.content[..]).ok()?;
|
|
|
|
|
|
|
|
|
|
|
|
let responses = event_handler.handle_event(event).await;
|
|
|
|
log::trace!("Spawning task to handle redirect responses");
|
|
|
|
let responses = responses
|
|
|
|
task::spawn(async move {
|
|
|
|
.iter()
|
|
|
|
log::trace!("Emitting redirected event...");
|
|
|
|
.cloned()
|
|
|
|
let responses = event_handler.handle_event(event).await;
|
|
|
|
.map(|mut value| {
|
|
|
|
log::trace!("Mapping responses...");
|
|
|
|
let payload = payload.clone();
|
|
|
|
let responses = responses
|
|
|
|
Event::with_payload(
|
|
|
|
.iter()
|
|
|
|
REDIRECT_EVENT,
|
|
|
|
.cloned()
|
|
|
|
&RedirectPayload::new(
|
|
|
|
.map(|mut value| {
|
|
|
|
payload.target,
|
|
|
|
let payload = payload.clone();
|
|
|
|
payload.proxy,
|
|
|
|
Event::with_payload(
|
|
|
|
payload.source,
|
|
|
|
REDIRECT_EVENT,
|
|
|
|
value.as_bytes(),
|
|
|
|
&RedirectPayload::new(
|
|
|
|
),
|
|
|
|
payload.target,
|
|
|
|
)
|
|
|
|
payload.proxy,
|
|
|
|
})
|
|
|
|
payload.source,
|
|
|
|
.collect::<Vec<Event>>();
|
|
|
|
value.as_bytes(),
|
|
|
|
let opt_stream = connections.lock().get(&origin).cloned();
|
|
|
|
),
|
|
|
|
if let Some(mut stream) = opt_stream {
|
|
|
|
)
|
|
|
|
for response in responses {
|
|
|
|
})
|
|
|
|
stream.send(response).await.ok()?;
|
|
|
|
.collect::<Vec<Event>>();
|
|
|
|
|
|
|
|
let opt_stream = connections.lock().get(&origin).cloned();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log::trace!("Sending responses...");
|
|
|
|
|
|
|
|
if let Some(mut stream) = opt_stream {
|
|
|
|
|
|
|
|
for response in responses {
|
|
|
|
|
|
|
|
if let Err(e) = stream.send(response).await {
|
|
|
|
|
|
|
|
log::error!("Failed to send response events: {}", e);
|
|
|
|
|
|
|
|
connections.lock().remove(stream.receiver_node());
|
|
|
|
|
|
|
|
stream.shutdown().expect("Failed to shutdown stream");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
log::trace!("Done");
|
|
|
|
|
|
|
|
|
|
|
|
None
|
|
|
|
None
|
|
|
|
})
|
|
|
|
})
|
|
|
|