feat: add example and dev deps

Note: current version of main.rs example just disables TLS cert verification if `-k` arg provided.
Instead, it must use custom storage for self-signed certs.

tokio with macros & fs features in dev deps is needed for main.rs example.
This commit is contained in:
DarkCat09 2024-08-01 12:05:39 +04:00
parent 16fc740397
commit 95362fd9e8
Signed by: DarkCat09
GPG key ID: 0A26CD5B3345D6E3
3 changed files with 130 additions and 0 deletions

12
Cargo.lock generated
View file

@ -340,6 +340,7 @@ dependencies = [
"mio", "mio",
"pin-project-lite", "pin-project-lite",
"socket2", "socket2",
"tokio-macros",
"windows-sys", "windows-sys",
] ]
@ -355,6 +356,17 @@ dependencies = [
"webpki-roots", "webpki-roots",
] ]
[[package]]
name = "tokio-macros"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "tokio-rustls" name = "tokio-rustls"
version = "0.26.0" version = "0.26.0"

View file

@ -10,3 +10,10 @@ tokio = { version = "1.39.2", features = ["io-util", "net"] }
tokio-rustls = { version = "0.26.0", default-features = false, features = ["ring"] } tokio-rustls = { version = "0.26.0", default-features = false, features = ["ring"] }
url = "2.5.2" url = "2.5.2"
webpki-roots = "0.26.3" webpki-roots = "0.26.3"
[[example]]
name = "main"
path = "examples/main.rs"
[dev-dependencies]
tokio = { version = "1.39.2", features = ["macros", "rt-multi-thread", "io-util", "fs"] }

111
examples/main.rs Normal file
View file

@ -0,0 +1,111 @@
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio_rustls::rustls::{
client::danger::{HandshakeSignatureValid, ServerCertVerified, ServerCertVerifier},
ClientConfig, SignatureScheme,
};
#[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();
if let Some(arg) = args.nth(1) {
if arg == "-k" {
insecure = true;
if let Some(arg) = args.nth(2) {
url = arg;
}
} else {
url = arg;
}
}
let client = if insecure {
tokio_gemini::Client::from(get_insecure_config())
} else {
tokio_gemini::Client::default()
};
let mut resp = client.request(&url).await?;
{
let status_code = resp.status().status_code();
let status_num: u8 = status_code.into();
eprintln!("{} {:?}", status_num, status_code);
}
if resp.status().reply_type() == tokio_gemini::ReplyType::Success {
let mime = resp.mime()?;
eprintln!("Mime: {}", mime);
let mut buf = [0u8, 128];
let body = resp.body();
if mime.type_() == mime::TEXT {
loop {
let n = body.read(&mut buf).await?;
if n == 0 {
break;
}
print!("{}", std::str::from_utf8(&buf[..n])?);
}
} else {
eprintln!("Downloading into content.bin");
let mut f = tokio::fs::File::create("content.bin").await?;
loop {
let n = body.read(&mut buf).await?;
if n == 0 {
break;
}
f.write_all(&buf[..n]).await?;
}
}
} else {
eprintln!("Message: {}", resp.message());
}
Ok(())
}
fn get_insecure_config() -> ClientConfig {
ClientConfig::builder()
.dangerous()
.with_custom_certificate_verifier(std::sync::Arc::new(NoCertVerification {}))
.with_no_client_auth()
}
#[derive(Debug)]
struct NoCertVerification;
impl ServerCertVerifier for NoCertVerification {
fn verify_server_cert(
&self,
end_entity: &tokio_rustls::rustls::pki_types::CertificateDer<'_>,
intermediates: &[tokio_rustls::rustls::pki_types::CertificateDer<'_>],
server_name: &tokio_rustls::rustls::pki_types::ServerName<'_>,
ocsp_response: &[u8],
now: tokio_rustls::rustls::pki_types::UnixTime,
) -> Result<ServerCertVerified, tokio_rustls::rustls::Error> {
Ok(ServerCertVerified::assertion())
}
fn verify_tls12_signature(
&self,
message: &[u8],
cert: &tokio_rustls::rustls::pki_types::CertificateDer<'_>,
dss: &tokio_rustls::rustls::DigitallySignedStruct,
) -> Result<HandshakeSignatureValid, tokio_rustls::rustls::Error> {
Ok(HandshakeSignatureValid::assertion())
}
fn verify_tls13_signature(
&self,
message: &[u8],
cert: &tokio_rustls::rustls::pki_types::CertificateDer<'_>,
dss: &tokio_rustls::rustls::DigitallySignedStruct,
) -> Result<HandshakeSignatureValid, tokio_rustls::rustls::Error> {
Ok(HandshakeSignatureValid::assertion())
}
fn supported_verify_schemes(&self) -> Vec<tokio_rustls::rustls::SignatureScheme> {
vec![
SignatureScheme::ECDSA_NISTP256_SHA256,
SignatureScheme::ECDSA_NISTP384_SHA384,
SignatureScheme::ECDSA_NISTP521_SHA512,
]
}
}