From 4f52475f2ca1c7012de6aad9773315df2d9d4038 Mon Sep 17 00:00:00 2001 From: DarkCat09 Date: Tue, 6 Aug 2024 12:32:42 +0400 Subject: [PATCH] add simple.rs example --- Cargo.toml | 4 ++++ examples/simple.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 examples/simple.rs diff --git a/Cargo.toml b/Cargo.toml index 12f6b06..96f61f9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,10 @@ tokio-rustls = { version = "0.26.0", default-features = false, features = ["ring url = "2.5.2" webpki-roots = "0.26.3" +[[example]] +name = "simple" +path = "examples/simple.rs" + [[example]] name = "main" path = "examples/main.rs" diff --git a/examples/simple.rs b/examples/simple.rs new file mode 100644 index 0000000..f4105be --- /dev/null +++ b/examples/simple.rs @@ -0,0 +1,54 @@ +use tokio_gemini::{ + certs::{ + fingerprint::{generate_fingerprint, Algorithm}, + verifier::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::verifier::CertificateDer, + host: &str, + _now: tokio_gemini::certs::verifier::UnixTime, + ) -> Result { + // For real verification example with known_hosts file + // see examples/main.rs + eprintln!( + "Host = {}\nFingerprint = {}", + host, + generate_fingerprint(cert, Algorithm::Sha512), + ); + Ok(true) + } +}