2021-03-06 18:55:05 +00:00
|
|
|
use std::{
|
|
|
|
net::SocketAddr,
|
|
|
|
path::PathBuf,
|
|
|
|
sync::Arc,
|
|
|
|
time::Duration,
|
|
|
|
};
|
|
|
|
|
|
|
|
use reqwest::Client;
|
|
|
|
use tokio::{
|
|
|
|
spawn,
|
|
|
|
sync::oneshot,
|
|
|
|
};
|
|
|
|
use tracing::{
|
|
|
|
debug,
|
|
|
|
info,
|
|
|
|
};
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main () -> anyhow::Result <()> {
|
|
|
|
use ptth_relay::key_validity::BlakeHashWrapper;
|
|
|
|
|
|
|
|
tracing_subscriber::fmt ().try_init ().ok ();
|
|
|
|
|
|
|
|
let relay_port = 4002;
|
|
|
|
let proxy_port = 11510;
|
|
|
|
|
|
|
|
// Start relay
|
|
|
|
|
|
|
|
let server_name = "aliens_wildland";
|
|
|
|
let api_key = "AnacondaHardcoverGrannyUnlatchLankinessMutate";
|
|
|
|
let tripcode = BlakeHashWrapper::from_key (api_key.as_bytes ());
|
|
|
|
debug! ("Relay is expecting tripcode {}", tripcode.encode_base64 ());
|
|
|
|
|
2021-04-27 20:05:27 +00:00
|
|
|
let relay_state = ptth_relay::Relay::build ()
|
|
|
|
.port (relay_port)
|
|
|
|
.server (ptth_relay::config::file::Server {
|
|
|
|
name: server_name.to_string (),
|
|
|
|
tripcode,
|
|
|
|
display_name: None,
|
|
|
|
})
|
|
|
|
.build ().expect ("Can't create relay state");
|
2021-03-06 18:55:05 +00:00
|
|
|
|
2021-04-27 20:05:27 +00:00
|
|
|
let relay_state = Arc::new (relay_state);
|
2021-03-06 18:55:05 +00:00
|
|
|
|
|
|
|
let (stop_relay_tx, stop_relay_rx) = oneshot::channel ();
|
|
|
|
let task_relay = spawn ({
|
|
|
|
let relay_state = relay_state.clone ();
|
|
|
|
async move {
|
|
|
|
ptth_relay::run_relay (
|
|
|
|
relay_state,
|
2021-04-18 13:28:07 +00:00
|
|
|
&PathBuf::new (),
|
2021-03-06 18:55:05 +00:00
|
|
|
stop_relay_rx,
|
|
|
|
None
|
|
|
|
).await
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
assert! (relay_state.list_servers ().await.is_empty ());
|
|
|
|
|
|
|
|
// Start proxy
|
|
|
|
|
2021-03-06 19:17:42 +00:00
|
|
|
let (stop_proxy_tx, stop_proxy_rx) = oneshot::channel ();
|
2021-03-06 18:55:05 +00:00
|
|
|
let task_proxy = spawn (async move {
|
|
|
|
debug_proxy::run_proxy (SocketAddr::from (([0, 0, 0, 0], proxy_port)), format! ("127.0.0.1:{}", relay_port), stop_proxy_rx).await
|
|
|
|
});
|
|
|
|
|
|
|
|
// Start server
|
|
|
|
|
|
|
|
let relay_url = format! ("http://127.0.0.1:{}", proxy_port);
|
|
|
|
|
2021-03-21 02:49:44 +00:00
|
|
|
let config_file = ptth_server::ConfigFile::new (
|
|
|
|
server_name.into (),
|
|
|
|
api_key.into (),
|
|
|
|
format! ("{}/7ZSFUKGV", relay_url),
|
|
|
|
);
|
2021-03-06 18:55:05 +00:00
|
|
|
|
|
|
|
let (stop_server_tx, stop_server_rx) = oneshot::channel ();
|
|
|
|
let task_server = {
|
|
|
|
spawn (async move {
|
|
|
|
ptth_server::run_server (config_file, stop_server_rx, None, None).await
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2021-03-06 21:15:41 +00:00
|
|
|
tokio::time::sleep (Duration::from_millis (1000)).await;
|
2021-03-06 18:55:05 +00:00
|
|
|
|
|
|
|
assert_eq! (relay_state.list_servers ().await, vec! [
|
|
|
|
server_name.to_string (),
|
|
|
|
]);
|
|
|
|
|
|
|
|
let client = Client::builder ()
|
|
|
|
.timeout (Duration::from_secs (2))
|
|
|
|
.build ().expect ("Couldn't build HTTP client");
|
|
|
|
|
|
|
|
let resp = client.get (&format! ("{}/frontend/relay_up_check", relay_url))
|
|
|
|
.send ().await.expect ("Couldn't check if relay is up").bytes ().await.expect ("Couldn't check if relay is up");
|
|
|
|
|
|
|
|
assert_eq! (resp, "Relay is up\n");
|
|
|
|
|
|
|
|
let resp = client.get (&format! ("{}/frontend/servers/{}/files/COPYING", relay_url, server_name))
|
|
|
|
.send ().await.expect ("Couldn't find license").bytes ().await.expect ("Couldn't find license");
|
|
|
|
|
|
|
|
if blake3::hash (&resp) != blake3::Hash::from ([
|
|
|
|
0xca, 0x02, 0x92, 0x78,
|
|
|
|
0x9c, 0x0a, 0x0e, 0xcb,
|
|
|
|
0xa7, 0x06, 0xf4, 0xb3,
|
|
|
|
0xf3, 0x49, 0x30, 0x07,
|
|
|
|
|
|
|
|
0xa9, 0x95, 0x17, 0x31,
|
|
|
|
0xc1, 0xd4, 0x32, 0xc5,
|
|
|
|
0x2c, 0x4a, 0xac, 0x1f,
|
|
|
|
0x1a, 0xbb, 0xa8, 0xef,
|
|
|
|
]) {
|
|
|
|
panic! ("{}", String::from_utf8 (resp.to_vec ()).expect ("???"));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Requesting a file from a server that isn't registered
|
|
|
|
// will error out
|
|
|
|
|
|
|
|
let resp = client.get (&format! ("{}/frontend/servers/obviously_this_server_does_not_exist/files/COPYING", relay_url))
|
|
|
|
.send ().await.expect ("Couldn't send request to bogus server");
|
|
|
|
|
|
|
|
assert_eq! (resp.status (), reqwest::StatusCode::NOT_FOUND);
|
|
|
|
|
2021-03-06 21:15:41 +00:00
|
|
|
tokio::time::sleep (Duration::from_secs (60)).await;
|
2021-03-06 18:55:05 +00:00
|
|
|
|
|
|
|
info! ("Shutting down end-to-end test");
|
|
|
|
|
|
|
|
stop_server_tx.send (()).expect ("Couldn't shut down server");
|
2021-03-06 19:17:42 +00:00
|
|
|
task_server.await.expect ("Couldn't join server").expect ("Server error");
|
|
|
|
info! ("Server stopped");
|
2021-03-06 18:55:05 +00:00
|
|
|
|
2021-03-06 19:17:42 +00:00
|
|
|
stop_proxy_tx.send (()).expect ("Couldn't shut down proxy");
|
|
|
|
task_proxy.await.expect ("Couldn't join proxy").expect ("Proxy error");
|
|
|
|
info! ("Proxy stopped");
|
2021-03-06 18:55:05 +00:00
|
|
|
|
2021-03-06 19:17:42 +00:00
|
|
|
stop_relay_tx.send (()).expect ("Couldn't shut down relay");
|
2021-03-06 18:55:05 +00:00
|
|
|
task_relay.await.expect ("Couldn't join relay").expect ("Relay error");
|
|
|
|
info! ("Relay stopped");
|
|
|
|
|
|
|
|
Ok (())
|
|
|
|
}
|