Refactor network client code so that it can be shared.
This commit is contained in:
parent
975ce49581
commit
d4632beb77
@ -364,11 +364,24 @@ impl fmt::Display for Query {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn dispatch_keyserver(mut config: Config, c: cli::keyserver::Command)
|
impl Query {
|
||||||
-> Result<()>
|
/// Parses command line arguments to queries.
|
||||||
{
|
fn parse(args: &[String]) -> Result<Vec<Query>> {
|
||||||
// Get the filename for the CA's key and the default User ID.
|
args.iter().map(
|
||||||
let ca = |uri: &str| -> Option<(String, String)> {
|
|q| if let Ok(h) = q.parse::<KeyHandle>() {
|
||||||
|
Ok(Query::Handle(h))
|
||||||
|
} else if let Ok(Some(addr)) = UserID::from(q.as_str()).email2() {
|
||||||
|
Ok(Query::Address(addr.to_string()))
|
||||||
|
} else {
|
||||||
|
Err(anyhow::anyhow!(
|
||||||
|
"Query must be a fingerprint, a keyid, \
|
||||||
|
or an email address: {:?}", q))
|
||||||
|
}).collect::<Result<Vec<Query>>>()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gets the filename for the CA's key and the default User ID.
|
||||||
|
fn keyserver_ca(uri: &str) -> Option<(String, String, usize)> {
|
||||||
if let Some(server) = uri.strip_prefix("hkps://") {
|
if let Some(server) = uri.strip_prefix("hkps://") {
|
||||||
// We only certify the certificate if the transport was
|
// We only certify the certificate if the transport was
|
||||||
// encrypted and authenticated.
|
// encrypted and authenticated.
|
||||||
@ -406,13 +419,18 @@ pub fn dispatch_keyserver(mut config: Config, c: cli::keyserver::Command)
|
|||||||
}
|
}
|
||||||
|
|
||||||
Some((format!("_keyserver_{}.pgp", server),
|
Some((format!("_keyserver_{}.pgp", server),
|
||||||
format!("Downloaded from the keyserver {}", server)))
|
format!("Downloaded from the keyserver {}", server),
|
||||||
|
KEYSERVER_CA_TRUST_AMOUNT))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
let ca_trust_amount = 1;
|
|
||||||
|
|
||||||
|
const KEYSERVER_CA_TRUST_AMOUNT: usize = 1;
|
||||||
|
|
||||||
|
pub fn dispatch_keyserver(mut config: Config, c: cli::keyserver::Command)
|
||||||
|
-> Result<()>
|
||||||
|
{
|
||||||
let servers = c.servers.iter().map(
|
let servers = c.servers.iter().map(
|
||||||
|uri| KeyServer::new(uri)
|
|uri| KeyServer::new(uri)
|
||||||
.with_context(|| format!("Malformed keyserver URI: {}", uri))
|
.with_context(|| format!("Malformed keyserver URI: {}", uri))
|
||||||
@ -424,16 +442,7 @@ pub fn dispatch_keyserver(mut config: Config, c: cli::keyserver::Command)
|
|||||||
use crate::cli::keyserver::Subcommands::*;
|
use crate::cli::keyserver::Subcommands::*;
|
||||||
match c.subcommand {
|
match c.subcommand {
|
||||||
Get(c) => rt.block_on(async {
|
Get(c) => rt.block_on(async {
|
||||||
let queries = c.query.iter().map(
|
let queries = Query::parse(&c.query)?;
|
||||||
|q| if let Ok(h) = q.parse::<KeyHandle>() {
|
|
||||||
Ok(Query::Handle(h))
|
|
||||||
} else if let Ok(Some(addr)) = UserID::from(q.as_str()).email2() {
|
|
||||||
Ok(Query::Address(addr.to_string()))
|
|
||||||
} else {
|
|
||||||
Err(anyhow::anyhow!(
|
|
||||||
"Query must be a fingerprint, a keyid, \
|
|
||||||
or an email address: {:?}", q))
|
|
||||||
}).collect::<Result<Vec<Query>>>()?;
|
|
||||||
|
|
||||||
let mut requests = tokio::task::JoinSet::new();
|
let mut requests = tokio::task::JoinSet::new();
|
||||||
queries.into_iter().for_each(|query| {
|
queries.into_iter().for_each(|query| {
|
||||||
@ -460,8 +469,8 @@ pub fn dispatch_keyserver(mut config: Config, c: cli::keyserver::Command)
|
|||||||
} else {
|
} else {
|
||||||
// We certify here because we know the
|
// We certify here because we know the
|
||||||
// keyserver URL here.
|
// keyserver URL here.
|
||||||
if let Some((ca_filename, ca_userid)) =
|
if let Some((ca_filename, ca_userid, ca_trust_amount)) =
|
||||||
ca(url.as_str())
|
keyserver_ca(url.as_str())
|
||||||
{
|
{
|
||||||
certs.append(&mut certify_downloads(
|
certs.append(&mut certify_downloads(
|
||||||
&mut config, &ca_filename, &ca_userid,
|
&mut config, &ca_filename, &ca_userid,
|
||||||
@ -524,11 +533,11 @@ pub fn dispatch_keyserver(mut config: Config, c: cli::keyserver::Command)
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn dispatch_wkd(mut config: Config, c: cli::wkd::Command) -> Result<()> {
|
const WKD_CA_FILENAME: &'static str = "_wkd.pgp";
|
||||||
let ca_filename = "_wkd.pgp";
|
const WKD_CA_USERID: &'static str = "Downloaded from a WKD";
|
||||||
let ca_userid = "Downloaded from a WKD";
|
const WKD_CA_TRUST_AMOUNT: usize = 1;
|
||||||
let ca_trust_amount = 1;
|
|
||||||
|
|
||||||
|
pub fn dispatch_wkd(mut config: Config, c: cli::wkd::Command) -> Result<()> {
|
||||||
let rt = tokio::runtime::Runtime::new()?;
|
let rt = tokio::runtime::Runtime::new()?;
|
||||||
|
|
||||||
use crate::cli::wkd::Subcommands::*;
|
use crate::cli::wkd::Subcommands::*;
|
||||||
@ -577,7 +586,8 @@ pub fn dispatch_wkd(mut config: Config, c: cli::wkd::Command) -> Result<()> {
|
|||||||
// query here.
|
// query here.
|
||||||
let mut cert = certify_downloads(
|
let mut cert = certify_downloads(
|
||||||
&mut config,
|
&mut config,
|
||||||
&ca_filename, &ca_userid, ca_trust_amount,
|
WKD_CA_FILENAME, WKD_CA_USERID,
|
||||||
|
WKD_CA_TRUST_AMOUNT,
|
||||||
vec![cert], Some(&address));
|
vec![cert], Some(&address));
|
||||||
|
|
||||||
certs.append(&mut cert);
|
certs.append(&mut cert);
|
||||||
@ -634,11 +644,11 @@ pub fn dispatch_wkd(mut config: Config, c: cli::wkd::Command) -> Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn dispatch_dane(mut config: Config, c: cli::dane::Command) -> Result<()> {
|
const DANE_CA_FILENAME: &'static str = "_dane.pgp";
|
||||||
let ca_filename = "_dane.pgp";
|
const DANE_CA_USERID: &'static str = "Downloaded from DANE";
|
||||||
let ca_userid = "Downloaded from DANE";
|
const DANE_CA_TRUST_AMOUNT: usize = 1;
|
||||||
let ca_trust_amount = 1;
|
|
||||||
|
|
||||||
|
pub fn dispatch_dane(mut config: Config, c: cli::dane::Command) -> Result<()> {
|
||||||
let rt = tokio::runtime::Runtime::new()?;
|
let rt = tokio::runtime::Runtime::new()?;
|
||||||
|
|
||||||
use crate::cli::dane::Subcommands::*;
|
use crate::cli::dane::Subcommands::*;
|
||||||
@ -691,7 +701,8 @@ pub fn dispatch_dane(mut config: Config, c: cli::dane::Command) -> Result<()> {
|
|||||||
// query here.
|
// query here.
|
||||||
let mut cert = certify_downloads(
|
let mut cert = certify_downloads(
|
||||||
&mut config,
|
&mut config,
|
||||||
&ca_filename, &ca_userid, ca_trust_amount,
|
DANE_CA_FILENAME, DANE_CA_USERID,
|
||||||
|
DANE_CA_TRUST_AMOUNT,
|
||||||
vec![cert], Some(&address));
|
vec![cert], Some(&address));
|
||||||
|
|
||||||
certs.append(&mut cert);
|
certs.append(&mut cert);
|
||||||
|
Loading…
Reference in New Issue
Block a user