0.2.0: Set up hashing via feature

Signed-off-by: Ivan Bushchik <ivabus@ivabus.dev>
This commit is contained in:
Ivan Bushchik 2023-12-16 10:11:33 +03:00
parent 5672a605bc
commit 5518879b36
No known key found for this signature in database
GPG key ID: 2F16FBF3262E090C
4 changed files with 35 additions and 17 deletions

View file

@ -1,6 +1,6 @@
[package] [package]
name = "binhost" name = "binhost"
version = "0.1.0" version = "0.2.0"
edition = "2021" edition = "2021"
license = "MIT" license = "MIT"
repository = "https://github.com/ivabus/binhost" repository = "https://github.com/ivabus/binhost"
@ -11,4 +11,8 @@ clap = { version = "4.4.11", features = ["derive"] }
rocket = "0.5.0" rocket = "0.5.0"
serde = { version = "1.0.193", features = ["derive"] } serde = { version = "1.0.193", features = ["derive"] }
once_cell = "1.19.0" once_cell = "1.19.0"
sha2 = "0.10.8" sha2 = { version = "0.10.8", optional = true }
[features]
default = [ "sha256" ]
sha256 = [ "dep:sha2" ]

View file

@ -67,6 +67,8 @@ GET /bin/<BIN>/<PLATFORM>/<ARCH> HTTP/1.1
### Get sha256 hash of binary for specific platform ### Get sha256 hash of binary for specific platform
Only with "sha256" feature (recalculates hash on each request, may be bad on large files or lots of requests)
#### Request #### Request
```http request ```http request

View file

@ -12,14 +12,25 @@ extern crate rocket;
use rocket::http::Status; use rocket::http::Status;
use rocket::response::content::RawText; use rocket::response::content::RawText;
use rocket::tokio::io::AsyncReadExt;
use sha2::digest::FixedOutput;
use sha2::Digest;
static mut BINS: Option<(HashMap<String, Bin>, Instant)> = None; static mut BINS: Option<(HashMap<String, Bin>, Instant)> = None;
static WEB_SH: &str = include_str!("../web.sh"); static WEB_SH: &str = include_str!("../web.sh");
#[cfg(feature = "sha256")]
static HASH_CALCULATION_SH: &str = r#"
if ! which sha256sum > /dev/null; then
echo "No \`sha256sum\` command found, continuing without checking" 1>&2
else
echo ":: Checking hashsum" 1>&2
if ! ($DOWNLOAD_COMMAND {{EXTERNAL_ADDRESS}}/bin/$NAME/$(uname)/$(uname -m)/sha256 $OUTPUT_ARG - | sha256sum -c - > /dev/null); then
echo "sha256 is invalid" 1>&2
exit 255
fi
fi
"#;
#[cfg(not(feature = "sha256"))]
static HASH_CALCULATION_SH: &str = "";
async fn reload_bins(bins: (&mut HashMap<String, Bin>, &mut Instant), args: &Args) { async fn reload_bins(bins: (&mut HashMap<String, Bin>, &mut Instant), args: &Args) {
if (Instant::now() - *bins.1).as_secs() > args.refresh { if (Instant::now() - *bins.1).as_secs() > args.refresh {
*bins.0 = get_bins(args).await; *bins.0 = get_bins(args).await;
@ -97,6 +108,7 @@ async fn get_script(bin: &str) -> ScriptResponse {
Some(bin) => { Some(bin) => {
let mut script = String::from(WEB_SH); let mut script = String::from(WEB_SH);
script = script script = script
.replace("{{HASH_CALCULATION}}", HASH_CALCULATION_SH)
.replace("{{NAME}}", &bin.name) .replace("{{NAME}}", &bin.name)
.replace("{{PLATFORM_LIST}}", &format_platform_list(bin)) .replace("{{PLATFORM_LIST}}", &format_platform_list(bin))
.replace("{{EXTERNAL_ADDRESS}}", &args.url); .replace("{{EXTERNAL_ADDRESS}}", &args.url);
@ -126,8 +138,13 @@ async fn get_binary(bin: &str, platform: &str, arch: &str) -> BinaryResponse {
} }
} }
#[cfg(feature = "sha256")]
#[get("/bin/<bin>/<platform>/<arch>/sha256")] #[get("/bin/<bin>/<platform>/<arch>/sha256")]
async fn get_binary_hash(bin: &str, platform: &str, arch: &str) -> ScriptResponse { async fn get_binary_hash(bin: &str, platform: &str, arch: &str) -> ScriptResponse {
use rocket::tokio::io::AsyncReadExt;
use sha2::digest::FixedOutput;
use sha2::Digest;
let args = Args::parse(); let args = Args::parse();
let file = NamedFile::open(format!( let file = NamedFile::open(format!(
"{}/{}/{}/{}/{}", "{}/{}/{}/{}/{}",
@ -155,6 +172,11 @@ async fn get_binary_hash(bin: &str, platform: &str, arch: &str) -> ScriptRespons
} }
} }
#[cfg(not(feature = "sha256"))]
#[get("/bin/<_bin>/<_platform>/<_arch>/sha256")]
async fn get_binary_hash(_bin: &str, _platform: &str, _arch: &str) -> ScriptResponse {
ScriptResponse::Status(Status::BadRequest)
}
#[launch] #[launch]
async fn rocket() -> _ { async fn rocket() -> _ {
let args = Args::parse(); let args = Args::parse();
@ -168,7 +190,7 @@ async fn rocket() -> _ {
} }
let figment = Figment::from(rocket::Config::default()) let figment = Figment::from(rocket::Config::default())
.merge(("ident", "binhost")) .merge(("ident", "BinHost"))
.merge(("port", args.port)) .merge(("port", args.port))
.merge(("address", args.address)); .merge(("address", args.address));
rocket::custom(figment).mount("/", routes![index, get_script, get_binary, get_binary_hash]) rocket::custom(figment).mount("/", routes![index, get_script, get_binary, get_binary_hash])

12
web.sh
View file

@ -38,17 +38,7 @@ $DOWNLOAD_COMMAND {{EXTERNAL_ADDRESS}}/bin/$NAME/$(uname)/$(uname -m) $OUTPUT_AR
chmod +x "$FILE" chmod +x "$FILE"
cd $DIR cd $DIR
{{HASH_CALCULATION}}
if ! which sha256sum > /dev/null; then
echo "No \`sha256sum\` command found, continuing without checking" 1>&2
else
echo ":: Checking hashsum" 1>&2
if ! ($DOWNLOAD_COMMAND {{EXTERNAL_ADDRESS}}/bin/$NAME/$(uname)/$(uname -m)/sha256 $OUTPUT_ARG - | sha256sum -c - > /dev/null); then
echo "sha256 is invalid" 1>&2
exit 255
fi
fi
$FILE < /dev/tty $FILE < /dev/tty
rm "$FILE" rm "$FILE"