2024-08-06 20:03:29 +03:00
|
|
|
use tokio_gemini::certs::{file_sscv::FileBasedCertVerifier, insecure::AllowAllCertVerifier};
|
2024-08-01 11:05:39 +03:00
|
|
|
|
2024-08-06 11:01:41 +03:00
|
|
|
//
|
2024-08-06 20:03:29 +03:00
|
|
|
// cargo add tokio_gemini -F file-sscv
|
2024-08-06 11:01:41 +03:00
|
|
|
// cargo add tokio -F macros,rt-multi-thread,io-util,fs
|
|
|
|
//
|
|
|
|
|
2024-08-01 11:05:39 +03:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<(), tokio_gemini::LibError> {
|
|
|
|
let mut args = std::env::args();
|
|
|
|
let mut insecure = false;
|
|
|
|
let mut url = "gemini://geminiprotocol.net/".to_owned();
|
2024-08-05 15:09:59 +03:00
|
|
|
_ = args.next(); // skip exe path
|
|
|
|
if let Some(arg) = args.next() {
|
2024-08-01 11:05:39 +03:00
|
|
|
if arg == "-k" {
|
|
|
|
insecure = true;
|
2024-08-05 15:09:59 +03:00
|
|
|
if let Some(arg) = args.next() {
|
2024-08-01 11:05:39 +03:00
|
|
|
url = arg;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
url = arg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let client = if insecure {
|
2024-08-06 10:57:42 +03:00
|
|
|
tokio_gemini::Client::builder()
|
|
|
|
.with_custom_verifier(AllowAllCertVerifier::yes_i_know_what_i_am_doing())
|
|
|
|
.build()
|
2024-08-01 11:05:39 +03:00
|
|
|
} else {
|
2024-08-06 10:57:42 +03:00
|
|
|
tokio_gemini::Client::builder()
|
2024-08-06 20:03:29 +03:00
|
|
|
.with_selfsigned_cert_verifier(FileBasedCertVerifier::init("known_hosts").await?)
|
2024-08-06 10:57:42 +03:00
|
|
|
.build()
|
2024-08-01 11:05:39 +03:00
|
|
|
};
|
2024-08-06 10:57:42 +03:00
|
|
|
|
2024-08-01 11:05:39 +03:00
|
|
|
let mut resp = client.request(&url).await?;
|
2024-08-06 10:57:42 +03:00
|
|
|
|
2024-08-01 11:05:39 +03:00
|
|
|
{
|
|
|
|
let status_code = resp.status().status_code();
|
|
|
|
let status_num: u8 = status_code.into();
|
|
|
|
eprintln!("{} {:?}", status_num, status_code);
|
|
|
|
}
|
2024-08-06 10:57:42 +03:00
|
|
|
|
2024-08-01 11:05:39 +03:00
|
|
|
if resp.status().reply_type() == tokio_gemini::ReplyType::Success {
|
|
|
|
let mime = resp.mime()?;
|
|
|
|
eprintln!("Mime: {}", mime);
|
2024-08-06 10:57:42 +03:00
|
|
|
|
2024-08-01 11:05:39 +03:00
|
|
|
if mime.type_() == mime::TEXT {
|
2024-08-06 10:57:42 +03:00
|
|
|
println!("{}", resp.text().await?);
|
2024-08-01 11:05:39 +03:00
|
|
|
} else {
|
|
|
|
eprintln!("Downloading into content.bin");
|
|
|
|
let mut f = tokio::fs::File::create("content.bin").await?;
|
2024-08-06 10:57:42 +03:00
|
|
|
tokio::io::copy(&mut resp.stream(), &mut f).await?;
|
2024-08-01 11:05:39 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
eprintln!("Message: {}", resp.message());
|
|
|
|
}
|
2024-08-06 10:57:42 +03:00
|
|
|
|
2024-08-01 11:05:39 +03:00
|
|
|
Ok(())
|
|
|
|
}
|