2024-08-21 16:01:21 +04:00
|
|
|
use async_trait::async_trait;
|
2024-08-20 10:52:24 +04:00
|
|
|
use tokio_gemini::{
|
2024-08-21 15:46:52 +04:00
|
|
|
certs::{
|
|
|
|
dane, file_sscv::KnownHostsFile, fingerprint::CertFingerprint, SelfsignedCertVerifier,
|
|
|
|
},
|
2024-08-20 10:52:24 +04:00
|
|
|
dns::DnsClient,
|
|
|
|
Client, LibError,
|
|
|
|
};
|
2024-08-01 12:05:39 +04:00
|
|
|
|
2024-08-06 12:01:41 +04:00
|
|
|
//
|
2024-08-21 15:46:52 +04:00
|
|
|
// cargo add tokio_gemini -F file-sscv,dane
|
2024-08-06 12:01:41 +04:00
|
|
|
// cargo add tokio -F macros,rt-multi-thread,io-util,fs
|
|
|
|
//
|
|
|
|
|
2024-08-20 10:52:24 +04:00
|
|
|
const USAGE: &str = "-k\t\tinsecure mode (trust all certs)
|
2024-08-28 14:41:43 +04:00
|
|
|
-d <DNS server>\tuse custom DNS for resolving & DANE
|
2024-08-20 10:52:24 +04:00
|
|
|
-h\t\tshow help";
|
2024-08-01 12:05:39 +04:00
|
|
|
|
2024-08-20 10:52:24 +04:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<(), LibError> {
|
|
|
|
let config = parse_args();
|
|
|
|
let client = build_client(&config).await?;
|
2024-08-06 11:57:42 +04:00
|
|
|
|
2024-08-20 10:52:24 +04:00
|
|
|
let mut resp = client.request(&config.url).await?;
|
2024-08-06 11:57:42 +04:00
|
|
|
|
2024-08-01 12:05:39 +04:00
|
|
|
{
|
|
|
|
let status_code = resp.status().status_code();
|
|
|
|
let status_num: u8 = status_code.into();
|
|
|
|
eprintln!("{} {:?}", status_num, status_code);
|
|
|
|
}
|
2024-08-06 11:57:42 +04:00
|
|
|
|
2024-08-01 12:05:39 +04:00
|
|
|
if resp.status().reply_type() == tokio_gemini::ReplyType::Success {
|
|
|
|
let mime = resp.mime()?;
|
|
|
|
eprintln!("Mime: {}", mime);
|
2024-08-06 11:57:42 +04:00
|
|
|
|
2024-08-01 12:05:39 +04:00
|
|
|
if mime.type_() == mime::TEXT {
|
2024-08-06 11:57:42 +04:00
|
|
|
println!("{}", resp.text().await?);
|
2024-08-01 12:05:39 +04:00
|
|
|
} else {
|
|
|
|
eprintln!("Downloading into content.bin");
|
|
|
|
let mut f = tokio::fs::File::create("content.bin").await?;
|
2024-08-06 11:57:42 +04:00
|
|
|
tokio::io::copy(&mut resp.stream(), &mut f).await?;
|
2024-08-01 12:05:39 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
eprintln!("Message: {}", resp.message());
|
|
|
|
}
|
2024-08-06 11:57:42 +04:00
|
|
|
|
2024-08-01 12:05:39 +04:00
|
|
|
Ok(())
|
|
|
|
}
|
2024-08-20 10:52:24 +04: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" => {
|
2024-08-21 15:46:52 +04:00
|
|
|
eprintln!("{}", USAGE);
|
2024-08-20 10:52:24 +04:00
|
|
|
std::process::exit(0);
|
|
|
|
}
|
|
|
|
url => {
|
2024-08-21 15:46:52 +04:00
|
|
|
eprintln!("URL: {}", url);
|
2024-08-20 10:52:24 +04:00
|
|
|
config.url = url.to_owned();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if expected_dns {
|
2024-08-21 15:46:52 +04:00
|
|
|
eprintln!("{}", USAGE);
|
2024-08-20 10:52:24 +04:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2024-08-21 15:46:52 +04:00
|
|
|
let known_hosts = KnownHostsFile::parse_file("known_hosts").await?;
|
|
|
|
let verifier = CertVerifier {
|
|
|
|
known_hosts,
|
|
|
|
dns: dns.clone(),
|
|
|
|
};
|
|
|
|
|
2024-08-20 10:52:24 +04:00
|
|
|
let client = tokio_gemini::Client::builder();
|
|
|
|
|
|
|
|
let client = if config.insecure {
|
2024-08-21 15:46:52 +04:00
|
|
|
client.dangerous_with_no_verifier()
|
2024-08-20 10:52:24 +04:00
|
|
|
} else {
|
2024-08-21 15:46:52 +04:00
|
|
|
client.with_selfsigned_cert_verifier(verifier)
|
2024-08-20 10:52:24 +04:00
|
|
|
};
|
|
|
|
|
2024-08-21 15:46:52 +04:00
|
|
|
let client = client.maybe_with_dns_client(dns);
|
2024-08-20 10:52:24 +04:00
|
|
|
|
|
|
|
Ok(client.build())
|
|
|
|
}
|
2024-08-21 15:46:52 +04:00
|
|
|
|
|
|
|
struct Config {
|
|
|
|
insecure: bool,
|
|
|
|
dns: Option<String>,
|
|
|
|
url: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CertVerifier {
|
|
|
|
known_hosts: KnownHostsFile,
|
|
|
|
dns: Option<DnsClient>,
|
|
|
|
}
|
|
|
|
|
2024-08-21 16:01:21 +04:00
|
|
|
#[async_trait]
|
2024-08-21 15:46:52 +04:00
|
|
|
impl SelfsignedCertVerifier for CertVerifier {
|
2024-08-21 16:01:21 +04:00
|
|
|
async fn verify(
|
2024-08-21 15:46:52 +04:00
|
|
|
&self,
|
2024-08-21 16:01:21 +04:00
|
|
|
cert: &tokio_gemini::certs::CertificateDer<'_>,
|
2024-08-21 15:46:52 +04:00
|
|
|
host: &str,
|
|
|
|
port: u16,
|
|
|
|
) -> Result<bool, tokio_gemini::LibError> {
|
|
|
|
if let Some(known) = self.known_hosts.get_known_cert(host) {
|
|
|
|
// if found in known_hosts, just compare certs
|
|
|
|
Ok(known.fingerprint.hash_and_compare(cert))
|
|
|
|
} else {
|
|
|
|
// otherwise, generate a hash and add to known_hosts
|
|
|
|
let hash = if let Some(dns) = &self.dns {
|
|
|
|
// if DNS client is configured, try verifying the cert
|
|
|
|
// via DANE instead of blindly trusting
|
|
|
|
match dane::dane(&dns, cert, host, port).await {
|
|
|
|
Ok(hash) => hash, // use the fingerprint matched with TLSA record
|
|
|
|
Err(LibError::HostLookupError) => {
|
|
|
|
// no TLSA record found -- server admin haven't set it up
|
|
|
|
eprintln!(
|
|
|
|
"TLSA not configured for tcp:{}:{}, trusting on first use",
|
|
|
|
host, port,
|
|
|
|
);
|
|
|
|
// just generate a hash for this cert
|
|
|
|
CertFingerprint::new_sha256(cert)
|
|
|
|
}
|
|
|
|
Err(e) => {
|
2024-08-28 17:02:56 +04:00
|
|
|
// cert not matched, DNS server rejected request, etc.
|
2024-08-21 15:46:52 +04:00
|
|
|
eprintln!("DANE verification failed: {:?}", e);
|
|
|
|
return Err(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
eprintln!("DANE disabled");
|
|
|
|
// just generate a hash for this cert
|
|
|
|
CertFingerprint::new_sha256(cert)
|
|
|
|
};
|
|
|
|
|
|
|
|
let fingerprint = hash.base64();
|
|
|
|
let fptype = hash.fingerprint_type_str();
|
|
|
|
eprintln!(
|
|
|
|
"Warning: adding trusted cert for {} with FP {}",
|
|
|
|
host, &fingerprint,
|
|
|
|
);
|
|
|
|
|
|
|
|
// adding the cert hash to trusted
|
|
|
|
// can be done simplier:
|
|
|
|
// self.known_hosts.add_trusted_cert(host, hash).await.unwrap...
|
|
|
|
self.known_hosts.add_cert_to_hashmap(host, hash);
|
|
|
|
self.known_hosts
|
|
|
|
.add_cert_to_file(host, &fingerprint, fptype)
|
|
|
|
.await
|
|
|
|
.unwrap_or_else(|e| {
|
|
|
|
eprintln!("Cert saved in-memory, unable to write to file: {:?}", e);
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|