Compare commits

...

2 Commits

Author SHA1 Message Date
_ cffb888ac8 add download subcommand 2022-03-25 00:44:43 +00:00
_ c852efb343 add subcommand to generate a random ULID 2022-03-24 23:24:23 +00:00
8 changed files with 184 additions and 6 deletions

15
Cargo.lock generated
View File

@ -584,6 +584,12 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "hex"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]] [[package]]
name = "http" name = "http"
version = "0.2.5" version = "0.2.5"
@ -1203,9 +1209,14 @@ version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"ctrlc", "ctrlc",
"futures-util",
"hex",
"ptth_file_server", "ptth_file_server",
"ptth_server", "ptth_server",
"quic_demo", "quic_demo",
"reqwest",
"rusty_ulid",
"sha2",
"tokio", "tokio",
"tracing", "tracing",
"tracing-subscriber", "tracing-subscriber",
@ -1774,9 +1785,9 @@ dependencies = [
[[package]] [[package]]
name = "sha2" name = "sha2"
version = "0.9.8" version = "0.9.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b69f9a4c9740d74c5baa3fd2e547f9525fa8088a8a958e0ca2409a514e33f5fa" checksum = "4d58a1e1bf39749807d89cf2d98ac2dfa0ff1cb3faa38fbb64dd88ac8013d800"
dependencies = [ dependencies = [
"block-buffer 0.9.0", "block-buffer 0.9.0",
"cfg-if", "cfg-if",

View File

@ -6,11 +6,26 @@ edition = "2018"
license = "AGPL-3.0" license = "AGPL-3.0"
[dependencies] [dependencies]
# Cookie 01FYZ3SDP2XABT7W19ACQVYKXT
# Dependencies should be in sync because ptth_multi_call_server intentionally
# tries to re-use as much code as possible between all of its subcommands,
# including ptth_server and ptth_file_server.
anyhow = "1.0.38" anyhow = "1.0.38"
ctrlc = "3.2.1" ctrlc = "3.2.1"
futures-util = "0.3.9"
hex = "0.4.3"
ptth_file_server = { path = "../ptth_file_server_bin" } ptth_file_server = { path = "../ptth_file_server_bin" }
ptth_server = { path = "../ptth_server" } ptth_server = { path = "../ptth_server" }
quic_demo = { path = "../../prototypes/quic_demo" } quic_demo = { path = "../../prototypes/quic_demo" }
rusty_ulid = "0.10.1"
sha2 = "0.9.8"
tokio = { version = "1.8.1", features = ["full"] } tokio = { version = "1.8.1", features = ["full"] }
tracing-subscriber = "0.2.16" tracing-subscriber = "0.2.16"
tracing = "0.1.25" tracing = "0.1.25"
[dependencies.reqwest]
version = "0.11.1"
default-features = false
features = ["stream", "rustls-tls", "hyper-rustls"]

View File

@ -0,0 +1,122 @@
use std::{
ffi::OsString,
io::{
self,
Write,
},
time::Duration,
};
use anyhow::{
anyhow,
bail,
};
use futures_util::StreamExt;
use reqwest::{
StatusCode,
};
use sha2::{
Digest,
Sha512,
};
use tokio::{
sync::mpsc,
task::{
spawn,
spawn_blocking,
},
};
pub async fn main (args: &[OsString]) -> anyhow::Result <()> {
let mut url = None;
let mut expected_sha512 = None;
let mut args = args [1..].into_iter ();
loop {
let arg = match args.next () {
None => break,
Some (x) => x,
};
match arg.to_str ().ok_or_else (|| anyhow! ("All arguments must be valid UTF-8"))?
{
"--help" => println! ("For now, just look at the source code"),
"--expect-sha512" => {
let expected = args.next ().ok_or_else (|| anyhow! ("--expect-sha512 needs an argument"))?;
expected_sha512 = Some (expected.to_str ().ok_or_else (|| anyhow! ("--expect-sha512's argument must be valid Unicode"))?);
}
arg => {
url = Some (arg);
break;
},
}
}
let url = match url {
None => bail! ("URL argument is required"),
Some (x) => x,
};
// Cookie 01FYZ3W64SM6KYNP48J6EWSCEF
// Try to keep the Clients similar here
let client = reqwest::Client::builder ()
.connect_timeout (Duration::from_secs (30))
.build ()?;
let resp = client.get (url)
.send ().await?;
if resp.status () != StatusCode::OK {
bail! ("Expected 200 OK, got {}", resp.status ());
}
let mut resp_stream = resp.bytes_stream ();
// The hasher is owned by a task because it makes ownership simpler
let (mut hash_tx, mut hash_rx) = mpsc::channel (1);
let hasher_task = spawn_blocking (move || {
let mut hasher = Sha512::new ();
while let Some (chunk) = tokio::runtime::Handle::current ().block_on (hash_rx.recv ()) {
hasher.update (&chunk);
}
anyhow::Result::<_>::Ok (hasher.finalize ())
});
while let Some (chunk) = resp_stream.next ().await {
let chunk = chunk?;
hash_tx.send (chunk.clone ()).await?;
{
let chunk = chunk.clone ();
spawn_blocking (move || {
io::stdout ().write_all (&chunk)?;
anyhow::Result::<_>::Ok (())
}).await??;
}
}
drop (hash_tx);
let hash = hasher_task.await??;
let actual_sha512 = hex::encode (&hash);
match expected_sha512 {
None => eprintln! ("Actual SHA512 = {}", actual_sha512),
Some (expected) => if ! actual_sha512.starts_with (&expected) {
bail! ("Expected SHA512 prefix {}, actual SHA512 {}", expected, actual_sha512);
},
}
Ok (())
}

View File

@ -5,24 +5,32 @@ use std::{
use tokio::sync::watch; use tokio::sync::watch;
mod download;
mod ulid;
#[derive (Clone, Copy, Debug, PartialEq)] #[derive (Clone, Copy, Debug, PartialEq)]
enum Subcommand { enum Subcommand {
Download,
PtthServer, PtthServer,
PtthFileServer, PtthFileServer,
PtthQuicEndServer, PtthQuicEndServer,
Ulid,
} }
#[tokio::main] #[tokio::main]
async fn main () -> anyhow::Result <()> { async fn main () -> anyhow::Result <()> {
use Subcommand::*;
tracing_subscriber::fmt::init (); tracing_subscriber::fmt::init ();
let args = Vec::from_iter (std::env::args_os ()); let args = Vec::from_iter (std::env::args_os ());
let (subcommand, args) = parse_args (&args)?; let (subcommand, args) = parse_args (&args)?;
match subcommand { match subcommand {
Subcommand::PtthServer => ptth_server::executable::main (&args).await, Download => download::main (&args).await,
Subcommand::PtthFileServer => ptth_file_server::main (&args).await, PtthServer => ptth_server::executable::main (&args).await,
Subcommand::PtthQuicEndServer => { PtthFileServer => ptth_file_server::main (&args).await,
PtthQuicEndServer => {
let (shutdown_tx, shutdown_rx) = watch::channel (false); let (shutdown_tx, shutdown_rx) = watch::channel (false);
ctrlc::set_handler (move || { ctrlc::set_handler (move || {
@ -33,6 +41,7 @@ async fn main () -> anyhow::Result <()> {
Ok (()) Ok (())
} }
Ulid => ulid::main (&args).await,
} }
} }
@ -41,9 +50,11 @@ fn parse_subcommand (arg: &str) -> Option <Subcommand>
use Subcommand::*; use Subcommand::*;
let map = vec! [ let map = vec! [
("download", Download),
("ptth_server", PtthServer), ("ptth_server", PtthServer),
("ptth_file_server", PtthFileServer), ("ptth_file_server", PtthFileServer),
("ptth_quic_end_server", PtthQuicEndServer), ("ptth_quic_end_server", PtthQuicEndServer),
("ulid", Ulid),
]; ];
let arg = arg.strip_suffix (".exe").unwrap_or (arg); let arg = arg.strip_suffix (".exe").unwrap_or (arg);

View File

@ -0,0 +1,12 @@
use std::{
ffi::OsString,
};
use anyhow::Result;
pub async fn main (args: &[OsString]) -> Result <()>
{
println! ("{}", rusty_ulid::generate_ulid_string ());
Ok (())
}

View File

@ -13,6 +13,11 @@ default-run = "ptth_server"
[dependencies] [dependencies]
# Cookie 01FYZ3SDP2XABT7W19ACQVYKXT
# Dependencies should be in sync because ptth_multi_call_server intentionally
# tries to re-use as much code as possible between all of its subcommands,
# including ptth_server and ptth_file_server.
aho-corasick = "0.7.15" aho-corasick = "0.7.15"
anyhow = "1.0.38" anyhow = "1.0.38"
arc-swap = "1.2.0" arc-swap = "1.2.0"

View File

@ -361,6 +361,9 @@ impl State {
let mut headers = reqwest::header::HeaderMap::new (); let mut headers = reqwest::header::HeaderMap::new ();
headers.insert ("X-ApiKey", config_file.api_key.try_into ().map_err (ServerError::ApiKeyInvalid)?); headers.insert ("X-ApiKey", config_file.api_key.try_into ().map_err (ServerError::ApiKeyInvalid)?);
// Cookie 01FYZ3W64SM6KYNP48J6EWSCEF
// Try to keep the Clients similar here
let client = Client::builder () let client = Client::builder ()
.default_headers (headers) .default_headers (headers)
.connect_timeout (Duration::from_secs (30)) .connect_timeout (Duration::from_secs (30))

View File

@ -1 +0,0 @@
1.55.0