tokio-gemini/examples/simple.rs
DarkCat09 1fc73d0cab
refactor/feat: rewrite certs::fingerprint with generic data type…
…and allowing different bin2txt (getting ready for DANE)
2024-08-08 20:57:29 +04:00

54 lines
1.3 KiB
Rust

use tokio_gemini::{
certs::{
fingerprint::{self, CertFingerprint},
SelfsignedCertVerifier,
},
Client, LibError,
};
// Much simpler than examples/main.rs
// Hardcoded URL, no cert check, always write to stdout
//
// cargo add tokio-gemini
// cargo add tokio -F macros,rt-multi-thread
//
const URL: &str = "gemini://geminiprotocol.net/docs/protocol-specification.gmi";
#[tokio::main]
async fn main() -> Result<(), LibError> {
let client = Client::builder()
.with_selfsigned_cert_verifier(CertVerifier)
.build();
match client.request(URL).await?.ensure_ok() {
Ok(mut resp) => {
println!("{}", resp.text().await?);
}
Err(resp) => {
println!("{} {}", resp.status().num(), resp.message());
}
}
Ok(())
}
struct CertVerifier;
impl SelfsignedCertVerifier for CertVerifier {
fn verify(
&self,
cert: &tokio_gemini::certs::CertificateDer,
host: &str,
_now: tokio_gemini::certs::UnixTime,
) -> Result<bool, tokio_rustls::rustls::Error> {
// For real verification example with known_hosts file
// see examples/main.rs
eprintln!(
"Host = {}\nFingerprint = {}",
host,
CertFingerprint::<fingerprint::Sha256>::new(cert).base64(),
);
Ok(true)
}
}