2024-08-20 09:52:24 +03:00
|
|
|
use tokio_gemini::{
|
|
|
|
certs::{file_sscv::FileBasedCertVerifier, insecure::AllowAllCertVerifier},
|
|
|
|
dns::DnsClient,
|
|
|
|
Client, LibError,
|
|
|
|
};
|
2024-08-01 11:05:39 +03:00
|
|
|
|
2024-08-06 11:01:41 +03:00
|
|
|
//
|
2024-08-20 09:52:24 +03:00
|
|
|
// cargo add tokio_gemini -F file-sscv,hickory
|
2024-08-06 11:01:41 +03:00
|
|
|
// cargo add tokio -F macros,rt-multi-thread,io-util,fs
|
|
|
|
//
|
|
|
|
|
2024-08-20 09:52:24 +03:00
|
|
|
const USAGE: &str = "-k\t\tinsecure mode (trust all certs)
|
|
|
|
-d <DNS server addr>\tuse custom DNS for resolving & DANE
|
|
|
|
-h\t\tshow help";
|
2024-08-01 11:05:39 +03:00
|
|
|
|
2024-08-20 09:52:24 +03:00
|
|
|
struct Config {
|
|
|
|
insecure: bool,
|
|
|
|
dns: Option<String>,
|
|
|
|
url: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<(), LibError> {
|
|
|
|
let config = parse_args();
|
|
|
|
let client = build_client(&config).await?;
|
2024-08-06 10:57:42 +03:00
|
|
|
|
2024-08-20 09:52:24 +03:00
|
|
|
let mut resp = client.request(&config.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(())
|
|
|
|
}
|
2024-08-20 09:52:24 +03:00
|
|
|
|
|
|
|
fn parse_args() -> Config {
|
|
|
|
let mut config = Config {
|
|
|
|
insecure: false,
|
|
|
|
dns: None,
|
|
|
|
url: "gemini://geminiprotocol.net/".to_owned(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut expected_dns = false;
|
|
|
|
|
|
|
|
for arg in std::env::args().skip(1) {
|
|
|
|
match arg.as_str() {
|
|
|
|
dns if expected_dns => {
|
|
|
|
config.dns = Some(dns.to_owned());
|
|
|
|
expected_dns = false;
|
|
|
|
}
|
|
|
|
"-k" => config.insecure = true,
|
|
|
|
"-d" => expected_dns = true,
|
|
|
|
"-h" => {
|
|
|
|
println!("{}", USAGE);
|
|
|
|
std::process::exit(0);
|
|
|
|
}
|
|
|
|
url => {
|
|
|
|
println!("URL: {}", url);
|
|
|
|
config.url = url.to_owned();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if expected_dns {
|
|
|
|
println!("{}", USAGE);
|
|
|
|
std::process::exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
config
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn build_client(config: &Config) -> Result<Client, LibError> {
|
|
|
|
let dns = if let Some(addr) = &config.dns {
|
|
|
|
Some(DnsClient::init(addr).await?)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let client = tokio_gemini::Client::builder();
|
|
|
|
|
|
|
|
let client = if config.insecure {
|
|
|
|
client.with_custom_verifier(AllowAllCertVerifier::yes_i_know_what_i_am_doing())
|
|
|
|
} else {
|
|
|
|
client.with_selfsigned_cert_verifier(
|
|
|
|
FileBasedCertVerifier::init("known_hosts", dns.clone()).await?,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
let client = if let Some(dns) = dns {
|
|
|
|
client.with_dns_client(dns)
|
|
|
|
} else {
|
|
|
|
client
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(client.build())
|
|
|
|
}
|