Add -k option to cureq example (#342)

By analogy with curl, this turns off certificate verification. Requires
enabling the "dangerous_configuration" feature in the rustls dependency.
This commit is contained in:
Jacob Hoffman-Andrews
2021-03-23 17:00:32 -07:00
committed by GitHub
parent a34d450657
commit 9ec4e7192a
2 changed files with 32 additions and 5 deletions

View File

@@ -30,7 +30,7 @@ cookie = { version = "0.15", features = ["percent-encode"], optional = true}
once_cell = "1" once_cell = "1"
url = "2" url = "2"
socks = { version = "0.3.2", optional = true } socks = { version = "0.3.2", optional = true }
rustls = { version = "0.19", optional = true, features = [] } rustls = { version = "0.19", optional = true }
webpki = { version = "0.21", optional = true } webpki = { version = "0.21", optional = true }
webpki-roots = { version = "0.21", optional = true } webpki-roots = { version = "0.21", optional = true }
rustls-native-certs = { version = "0.5", optional = true } rustls-native-certs = { version = "0.5", optional = true }
@@ -43,6 +43,7 @@ log = "0.4.11"
[dev-dependencies] [dev-dependencies]
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }
env_logger = "0.8.1" env_logger = "0.8.1"
rustls = { version = "0.19", features = ["dangerous_configuration"] }
[[example]] [[example]]
name = "smoke-test" name = "smoke-test"

View File

@@ -1,10 +1,14 @@
use std::env;
use std::error; use std::error;
use std::fmt; use std::fmt;
use std::io; use std::io;
use std::time::Duration; use std::time::Duration;
use std::{env, sync::Arc};
use rustls::{
Certificate, ClientConfig, RootCertStore, ServerCertVerified, ServerCertVerifier, TLSError,
};
use ureq; use ureq;
use webpki::DNSNameRef;
#[derive(Debug)] #[derive(Debug)]
struct StringError(String); struct StringError(String);
@@ -79,6 +83,20 @@ fn get(agent: &ureq::Agent, url: &str, print_headers: bool) -> Result<(), Error>
Ok(()) Ok(())
} }
struct AcceptAll {}
impl ServerCertVerifier for AcceptAll {
fn verify_server_cert(
&self,
_roots: &RootCertStore,
_presented_certs: &[Certificate],
_dns_name: DNSNameRef<'_>,
_ocsp_response: &[u8],
) -> Result<ServerCertVerified, TLSError> {
Ok(ServerCertVerified::assertion())
}
}
fn main() { fn main() {
match main2() { match main2() {
Ok(()) => {} Ok(()) => {}
@@ -103,10 +121,9 @@ Fetch url and copy it to stdout.
} }
args.remove(0); args.remove(0);
env_logger::init(); env_logger::init();
let agent = ureq::builder() let mut builder = ureq::builder()
.timeout_connect(Duration::from_secs(30)) .timeout_connect(Duration::from_secs(30))
.timeout(Duration::from_secs(300)) .timeout(Duration::from_secs(300));
.build();
let flags: Vec<&String> = args.iter().filter(|s| s.starts_with("-")).collect(); let flags: Vec<&String> = args.iter().filter(|s| s.starts_with("-")).collect();
let nonflags: Vec<&String> = args.iter().filter(|s| !s.starts_with("-")).collect(); let nonflags: Vec<&String> = args.iter().filter(|s| !s.starts_with("-")).collect();
@@ -114,10 +131,19 @@ Fetch url and copy it to stdout.
for flag in flags { for flag in flags {
match flag.as_ref() { match flag.as_ref() {
"-i" => print_headers = true, "-i" => print_headers = true,
"-k" => {
let mut client_config = ClientConfig::new();
client_config
.dangerous()
.set_certificate_verifier(Arc::new(AcceptAll {}));
builder = builder.tls_config(Arc::new(client_config));
}
f => Err(StringError(format!("unrecognized flag '{}'", f)))?, f => Err(StringError(format!("unrecognized flag '{}'", f)))?,
} }
} }
let agent = builder.build();
for url in nonflags { for url in nonflags {
get(&agent, &url, print_headers)?; get(&agent, &url, print_headers)?;
} }