2020-10-27 03:27:25 +00:00
|
|
|
use std::{
|
|
|
|
error::Error,
|
|
|
|
sync::Arc,
|
2020-10-27 03:58:50 +00:00
|
|
|
time::Duration,
|
2020-10-27 03:27:25 +00:00
|
|
|
};
|
2020-10-27 02:49:12 +00:00
|
|
|
|
|
|
|
use hyper::{
|
|
|
|
StatusCode,
|
|
|
|
};
|
2020-10-30 22:38:23 +00:00
|
|
|
use reqwest::Client;
|
2020-10-27 03:58:50 +00:00
|
|
|
use tokio::{
|
|
|
|
time::delay_for,
|
|
|
|
};
|
2020-10-27 02:49:12 +00:00
|
|
|
|
2020-10-28 02:29:15 +00:00
|
|
|
use ptth::http_serde;
|
2020-10-28 01:20:06 +00:00
|
|
|
|
2020-10-30 22:43:56 +00:00
|
|
|
const RELAY_URL: &str = "http://127.0.0.1:4000";
|
|
|
|
const SERVER_NAME: &str = "alien_wildlands";
|
2020-10-29 12:45:35 +00:00
|
|
|
|
2020-10-30 22:25:22 +00:00
|
|
|
async fn handle_req_resp (
|
|
|
|
client: &Client,
|
|
|
|
req_resp: reqwest::Response
|
|
|
|
) -> Result <(), Box <dyn Error>> {
|
|
|
|
//println! ("Step 1");
|
|
|
|
|
|
|
|
if req_resp.status () != StatusCode::OK {
|
|
|
|
// TODO: Error handling
|
|
|
|
return Ok (());
|
|
|
|
}
|
|
|
|
|
|
|
|
let body = req_resp.bytes ().await?;
|
|
|
|
let wrapped_req: http_serde::WrappedRequest = match rmp_serde::from_read_ref (&body)
|
|
|
|
{
|
|
|
|
Ok (x) => x,
|
|
|
|
_ => return Ok (()),
|
2020-10-30 22:11:35 +00:00
|
|
|
};
|
|
|
|
|
2020-10-30 22:25:22 +00:00
|
|
|
let (req_id, parts) = (wrapped_req.id, wrapped_req.req);
|
|
|
|
|
2020-10-30 22:38:23 +00:00
|
|
|
let response = ptth::file_server::serve_all (parts).await;
|
2020-10-30 22:25:22 +00:00
|
|
|
|
|
|
|
let mut resp_req = client
|
2020-10-30 22:43:56 +00:00
|
|
|
.post (&format! ("{}/http_response/{}", RELAY_URL, req_id))
|
|
|
|
.header (ptth::PTTH_MAGIC_HEADER, base64::encode (rmp_serde::to_vec (&response.parts).unwrap ()));
|
2020-10-30 22:25:22 +00:00
|
|
|
|
|
|
|
if let Some (body) = response.body {
|
|
|
|
resp_req = resp_req.body (body);
|
|
|
|
}
|
|
|
|
|
|
|
|
//println! ("Step 6");
|
|
|
|
if let Err (e) = resp_req.send ().await {
|
|
|
|
println! ("Err: {:?}", e);
|
|
|
|
}
|
2020-10-29 12:45:35 +00:00
|
|
|
|
|
|
|
Ok (())
|
2020-10-29 12:22:42 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 02:49:12 +00:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main () -> Result <(), Box <dyn Error>> {
|
2020-10-27 03:27:25 +00:00
|
|
|
let client = Arc::new (Client::new ());
|
2020-10-27 02:49:12 +00:00
|
|
|
|
2020-10-27 03:58:50 +00:00
|
|
|
let mut backoff_delay = 0;
|
2020-10-27 02:49:12 +00:00
|
|
|
|
|
|
|
loop {
|
2020-10-27 03:58:50 +00:00
|
|
|
if backoff_delay > 0 {
|
|
|
|
delay_for (Duration::from_millis (backoff_delay)).await;
|
|
|
|
}
|
|
|
|
|
2020-10-30 22:43:56 +00:00
|
|
|
let req_req = client.get (&format! ("{}/http_listen/{}", RELAY_URL, SERVER_NAME));
|
2020-10-27 03:27:25 +00:00
|
|
|
|
|
|
|
let req_resp = match req_req.send ().await {
|
2020-10-27 02:49:12 +00:00
|
|
|
Err (e) => {
|
|
|
|
println! ("Err: {:?}", e);
|
2020-10-27 03:58:50 +00:00
|
|
|
backoff_delay = backoff_delay * 2 + 500;
|
2020-10-27 02:49:12 +00:00
|
|
|
continue;
|
|
|
|
},
|
2020-10-27 03:58:50 +00:00
|
|
|
Ok (r) => {
|
|
|
|
backoff_delay = 0;
|
|
|
|
r
|
|
|
|
},
|
2020-10-27 02:49:12 +00:00
|
|
|
};
|
|
|
|
|
2020-10-30 22:38:23 +00:00
|
|
|
// Spawn another task for each request so we can
|
|
|
|
// immediately listen for the next connection
|
2020-10-28 01:39:02 +00:00
|
|
|
|
2020-10-27 03:27:25 +00:00
|
|
|
let client = client.clone ();
|
|
|
|
tokio::spawn (async move {
|
2020-10-30 22:25:22 +00:00
|
|
|
match handle_req_resp (&client, req_resp).await {
|
2020-10-29 13:19:14 +00:00
|
|
|
_ => (),
|
|
|
|
}
|
2020-10-27 03:27:25 +00:00
|
|
|
});
|
2020-10-27 02:49:12 +00:00
|
|
|
}
|
|
|
|
}
|