mirror of
https://github.com/ivabus/binhost
synced 2024-11-25 09:15:07 +03:00
0.2.0: Set up hashing via feature
Signed-off-by: Ivan Bushchik <ivabus@ivabus.dev>
This commit is contained in:
parent
5672a605bc
commit
5518879b36
4 changed files with 35 additions and 17 deletions
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "binhost"
|
||||
version = "0.1.0"
|
||||
version = "0.2.0"
|
||||
edition = "2021"
|
||||
license = "MIT"
|
||||
repository = "https://github.com/ivabus/binhost"
|
||||
|
@ -11,4 +11,8 @@ clap = { version = "4.4.11", features = ["derive"] }
|
|||
rocket = "0.5.0"
|
||||
serde = { version = "1.0.193", features = ["derive"] }
|
||||
once_cell = "1.19.0"
|
||||
sha2 = "0.10.8"
|
||||
sha2 = { version = "0.10.8", optional = true }
|
||||
|
||||
[features]
|
||||
default = [ "sha256" ]
|
||||
sha256 = [ "dep:sha2" ]
|
||||
|
|
|
@ -67,6 +67,8 @@ GET /bin/<BIN>/<PLATFORM>/<ARCH> HTTP/1.1
|
|||
|
||||
### 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
|
||||
|
||||
```http request
|
||||
|
|
30
src/main.rs
30
src/main.rs
|
@ -12,14 +12,25 @@ extern crate rocket;
|
|||
|
||||
use rocket::http::Status;
|
||||
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 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) {
|
||||
if (Instant::now() - *bins.1).as_secs() > args.refresh {
|
||||
*bins.0 = get_bins(args).await;
|
||||
|
@ -97,6 +108,7 @@ async fn get_script(bin: &str) -> ScriptResponse {
|
|||
Some(bin) => {
|
||||
let mut script = String::from(WEB_SH);
|
||||
script = script
|
||||
.replace("{{HASH_CALCULATION}}", HASH_CALCULATION_SH)
|
||||
.replace("{{NAME}}", &bin.name)
|
||||
.replace("{{PLATFORM_LIST}}", &format_platform_list(bin))
|
||||
.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")]
|
||||
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 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]
|
||||
async fn rocket() -> _ {
|
||||
let args = Args::parse();
|
||||
|
@ -168,7 +190,7 @@ async fn rocket() -> _ {
|
|||
}
|
||||
|
||||
let figment = Figment::from(rocket::Config::default())
|
||||
.merge(("ident", "binhost"))
|
||||
.merge(("ident", "BinHost"))
|
||||
.merge(("port", args.port))
|
||||
.merge(("address", args.address));
|
||||
rocket::custom(figment).mount("/", routes![index, get_script, get_binary, get_binary_hash])
|
||||
|
|
12
web.sh
12
web.sh
|
@ -38,17 +38,7 @@ $DOWNLOAD_COMMAND {{EXTERNAL_ADDRESS}}/bin/$NAME/$(uname)/$(uname -m) $OUTPUT_AR
|
|||
chmod +x "$FILE"
|
||||
|
||||
cd $DIR
|
||||
|
||||
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
|
||||
|
||||
{{HASH_CALCULATION}}
|
||||
$FILE < /dev/tty
|
||||
|
||||
rm "$FILE"
|
||||
|
|
Loading…
Reference in a new issue