2020-11-29 23:24:25 +00:00
|
|
|
use std::{
|
|
|
|
sync::Arc,
|
|
|
|
time::Duration,
|
|
|
|
};
|
|
|
|
|
|
|
|
use chrono::Utc;
|
|
|
|
use futures::{
|
|
|
|
FutureExt,
|
|
|
|
stream::StreamExt,
|
|
|
|
};
|
|
|
|
use hyper::{
|
|
|
|
Body,
|
|
|
|
Response,
|
|
|
|
Request,
|
|
|
|
StatusCode,
|
|
|
|
};
|
|
|
|
use tokio::{
|
|
|
|
spawn,
|
|
|
|
sync::{
|
|
|
|
mpsc,
|
|
|
|
oneshot,
|
|
|
|
},
|
|
|
|
};
|
2021-03-06 21:15:41 +00:00
|
|
|
use tokio_stream::wrappers::ReceiverStream;
|
2020-11-29 23:24:25 +00:00
|
|
|
|
|
|
|
use ptth_core::{
|
|
|
|
http_serde,
|
|
|
|
prelude::*,
|
|
|
|
};
|
|
|
|
|
|
|
|
use super::{
|
|
|
|
error_reply,
|
|
|
|
errors::{
|
|
|
|
RequestError,
|
|
|
|
ShuttingDownError,
|
|
|
|
},
|
|
|
|
HandleHttpResponseError,
|
|
|
|
ok_reply,
|
|
|
|
RelayState,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Servers will come here and either handle queued requests from parked clients,
|
|
|
|
// or park themselves until a request comes in.
|
|
|
|
// Step 1
|
|
|
|
|
|
|
|
pub async fn handle_listen (
|
|
|
|
state: Arc <RelayState>,
|
|
|
|
watcher_code: String,
|
|
|
|
api_key: &[u8],
|
|
|
|
)
|
|
|
|
-> Result <Response <Body>, RequestError>
|
|
|
|
{
|
|
|
|
use super::RequestRendezvous::*;
|
|
|
|
|
|
|
|
let trip_error = || Ok (error_reply (StatusCode::UNAUTHORIZED, "Bad X-ApiKey")?);
|
|
|
|
|
2021-03-15 19:11:57 +00:00
|
|
|
let now = Utc::now ();
|
|
|
|
let actual_tripcode = blake3::hash (api_key);
|
|
|
|
|
2020-11-29 23:24:25 +00:00
|
|
|
let expected_tripcode = {
|
|
|
|
let config = state.config.read ().await;
|
|
|
|
|
|
|
|
match config.servers.get (&watcher_code) {
|
|
|
|
None => {
|
|
|
|
error! ("Denied http_listen for non-existent server name {}", watcher_code);
|
2021-04-03 03:30:22 +00:00
|
|
|
state.unregistered_servers.push (crate::RejectedServer {
|
2021-03-15 19:11:57 +00:00
|
|
|
name: watcher_code,
|
|
|
|
tripcode: actual_tripcode,
|
|
|
|
seen: now,
|
2021-04-03 03:30:22 +00:00
|
|
|
}).await;
|
2021-03-15 19:11:57 +00:00
|
|
|
|
2020-11-29 23:24:25 +00:00
|
|
|
return trip_error ();
|
|
|
|
},
|
2020-12-12 15:57:22 +00:00
|
|
|
Some (x) => *(*x).tripcode,
|
2020-11-29 23:24:25 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if expected_tripcode != actual_tripcode {
|
|
|
|
error! ("Denied http_listen for bad tripcode {}", base64::encode (actual_tripcode.as_bytes ()));
|
|
|
|
return trip_error ();
|
|
|
|
}
|
|
|
|
|
|
|
|
// End of early returns
|
|
|
|
|
|
|
|
{
|
2020-12-13 03:29:54 +00:00
|
|
|
// TODO: Move into relay_state.rs
|
|
|
|
|
2020-11-29 23:24:25 +00:00
|
|
|
let mut server_status = state.server_status.lock ().await;
|
|
|
|
|
|
|
|
let mut status = server_status.entry (watcher_code.clone ()).or_insert_with (Default::default);
|
|
|
|
|
2021-03-15 19:11:57 +00:00
|
|
|
status.last_seen = now;
|
2020-11-29 23:24:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let (tx, rx) = oneshot::channel ();
|
|
|
|
|
2021-03-21 03:09:44 +00:00
|
|
|
let listen_id = rusty_ulid::generate_ulid_string ();
|
2021-03-16 15:15:16 +00:00
|
|
|
|
2020-11-29 23:24:25 +00:00
|
|
|
{
|
|
|
|
let mut request_rendezvous = state.request_rendezvous.lock ().await;
|
|
|
|
|
|
|
|
if let Some (ParkedClients (v)) = request_rendezvous.remove (&watcher_code)
|
|
|
|
{
|
|
|
|
if ! v.is_empty () {
|
|
|
|
// 1 or more clients were parked - Make the server
|
|
|
|
// handle them immediately
|
|
|
|
|
|
|
|
debug! ("Sending {} parked requests to server {}", v.len (), watcher_code);
|
|
|
|
return Ok (ok_reply (rmp_serde::to_vec (&v)?)?);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-16 15:15:16 +00:00
|
|
|
debug! ("Parking server {}, listen id {}", watcher_code, listen_id);
|
2020-11-29 23:24:25 +00:00
|
|
|
request_rendezvous.insert (watcher_code.clone (), ParkedServer (tx));
|
|
|
|
}
|
|
|
|
|
|
|
|
// No clients were parked - make the server long-poll
|
|
|
|
|
|
|
|
futures::select! {
|
|
|
|
x = rx.fuse () => match x {
|
|
|
|
Ok (Ok (one_req)) => {
|
2020-12-17 01:06:15 +00:00
|
|
|
trace! ("Unparking server {}", watcher_code);
|
2020-11-29 23:24:25 +00:00
|
|
|
Ok (ok_reply (rmp_serde::to_vec (&vec! [one_req])?)?)
|
|
|
|
},
|
|
|
|
Ok (Err (ShuttingDownError::ShuttingDown)) => Ok (error_reply (StatusCode::SERVICE_UNAVAILABLE, "Server is shutting down, try again soon")?),
|
2021-03-16 14:39:15 +00:00
|
|
|
Err (e) => {
|
2021-03-16 15:15:16 +00:00
|
|
|
error! ("{} {} {}", watcher_code, listen_id, e);
|
2021-03-16 14:39:15 +00:00
|
|
|
Ok (error_reply (StatusCode::INTERNAL_SERVER_ERROR, "Server error")?)
|
|
|
|
},
|
2020-11-29 23:24:25 +00:00
|
|
|
},
|
2021-04-03 01:58:19 +00:00
|
|
|
_ = tokio::time::sleep (Duration::from_secs (25)).fuse () => {
|
2021-03-16 15:15:16 +00:00
|
|
|
debug! ("Timed out http_listen for server {} {}", watcher_code, listen_id);
|
2020-11-29 23:24:25 +00:00
|
|
|
return Ok (error_reply (StatusCode::NO_CONTENT, "No requests now, long-poll again")?)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Servers will come here to stream responses to clients
|
|
|
|
// Step 5
|
|
|
|
|
|
|
|
pub async fn handle_response (
|
|
|
|
req: Request <Body>,
|
|
|
|
state: Arc <RelayState>,
|
|
|
|
req_id: String,
|
|
|
|
)
|
|
|
|
-> Result <Response <Body>, HandleHttpResponseError>
|
|
|
|
{
|
|
|
|
#[derive (Debug)]
|
|
|
|
enum BodyFinishedReason {
|
|
|
|
StreamFinished,
|
|
|
|
ClientDisconnected,
|
|
|
|
}
|
|
|
|
use BodyFinishedReason::*;
|
|
|
|
use HandleHttpResponseError::*;
|
|
|
|
|
|
|
|
let (parts, mut body) = req.into_parts ();
|
|
|
|
|
|
|
|
let magic_header = parts.headers.get (ptth_core::PTTH_MAGIC_HEADER).ok_or (MissingPtthMagicHeader)?;
|
|
|
|
|
|
|
|
let magic_header = base64::decode (magic_header).map_err (PtthMagicHeaderNotBase64)?;
|
|
|
|
|
|
|
|
let resp_parts: http_serde::ResponseParts = rmp_serde::from_read_ref (&magic_header).map_err (PtthMagicHeaderNotMsgPack)?;
|
|
|
|
|
|
|
|
// Intercept the body packets here so we can check when the stream
|
|
|
|
// ends or errors out
|
|
|
|
|
2021-03-06 21:15:41 +00:00
|
|
|
let (body_tx, body_rx) = mpsc::channel (2);
|
2020-11-29 23:24:25 +00:00
|
|
|
let (body_finished_tx, body_finished_rx) = oneshot::channel ();
|
|
|
|
let mut shutdown_watch_rx = state.shutdown_watch_rx.clone ();
|
|
|
|
|
|
|
|
let relay_task = spawn (async move {
|
2021-03-21 03:34:47 +00:00
|
|
|
if *shutdown_watch_rx.borrow () {
|
|
|
|
debug! ("Can't relay bytes, relay is shutting down");
|
|
|
|
}
|
|
|
|
else {
|
2020-11-29 23:24:25 +00:00
|
|
|
loop {
|
|
|
|
let item = body.next ().await;
|
|
|
|
|
|
|
|
if let Some (item) = item {
|
|
|
|
if let Ok (bytes) = &item {
|
|
|
|
trace! ("Relaying {} bytes", bytes.len ());
|
|
|
|
}
|
|
|
|
|
|
|
|
futures::select! {
|
2021-03-21 03:34:47 +00:00
|
|
|
x = body_tx.send (item).fuse () => if x.is_err () {
|
2020-11-29 23:24:25 +00:00
|
|
|
info! ("Body closed while relaying. (Client hung up?)");
|
|
|
|
body_finished_tx.send (ClientDisconnected).map_err (|_| LostServer)?;
|
|
|
|
break;
|
|
|
|
},
|
2021-03-06 21:15:41 +00:00
|
|
|
_ = shutdown_watch_rx.changed ().fuse () => {
|
2020-11-29 23:24:25 +00:00
|
|
|
debug! ("Closing stream: relay is shutting down");
|
|
|
|
break;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2020-12-17 01:06:15 +00:00
|
|
|
trace! ("Finished relaying bytes");
|
2020-11-29 23:24:25 +00:00
|
|
|
body_finished_tx.send (StreamFinished).map_err (|_| LostServer)?;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok::<(), HandleHttpResponseError> (())
|
|
|
|
});
|
|
|
|
|
2021-03-06 21:15:41 +00:00
|
|
|
let body = Body::wrap_stream (ReceiverStream::new (body_rx));
|
2020-11-29 23:24:25 +00:00
|
|
|
|
|
|
|
let tx = {
|
|
|
|
let response_rendezvous = state.response_rendezvous.read ().await;
|
|
|
|
match response_rendezvous.remove (&req_id) {
|
|
|
|
None => {
|
|
|
|
error! ("Server tried to respond to non-existent request");
|
|
|
|
return Ok (error_reply (StatusCode::BAD_REQUEST, "Request ID not found in response_rendezvous")?);
|
|
|
|
},
|
|
|
|
Some ((_, x)) => x,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// UKAUFFY4 (Send half)
|
|
|
|
if tx.send (Ok ((resp_parts, body))).is_err () {
|
|
|
|
let msg = "Failed to connect to client";
|
|
|
|
error! (msg);
|
|
|
|
return Ok (error_reply (StatusCode::BAD_GATEWAY, msg)?);
|
|
|
|
}
|
|
|
|
|
|
|
|
relay_task.await??;
|
|
|
|
|
2020-12-17 01:06:15 +00:00
|
|
|
trace! ("Connected server to client for streaming.");
|
2020-11-29 23:24:25 +00:00
|
|
|
match body_finished_rx.await {
|
|
|
|
Ok (StreamFinished) => {
|
|
|
|
Ok (error_reply (StatusCode::OK, "StreamFinished")?)
|
|
|
|
},
|
|
|
|
Ok (ClientDisconnected) => {
|
|
|
|
Ok (error_reply (StatusCode::OK, "ClientDisconnected")?)
|
|
|
|
},
|
|
|
|
Err (e) => {
|
|
|
|
debug! ("body_finished_rx {}", e);
|
|
|
|
Ok (error_reply (StatusCode::OK, "body_finished_rx Err")?)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|