-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add CLI tool to derive caBLE tunnel server domain #305
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
//! Derives caBLE tunnel server hostnames. | ||
//! | ||
//! ```sh | ||
//! cargo run --example cable_domain --features cable -- --help | ||
//! ``` | ||
use clap::{ArgGroup, CommandFactory, Parser, ValueHint}; | ||
use std::net::ToSocketAddrs; | ||
use webauthn_authenticator_rs::cable::get_domain; | ||
|
||
#[derive(Debug, clap::Parser)] | ||
#[clap( | ||
about = "Derives caBLE tunnel server hostnames", | ||
after_help = "\ | ||
When deriving exactly one tunnel server hostname, only the hostname will be \ | ||
printed, and an unknown ID will result in an error. | ||
|
||
When deriving multiple (or all) tunnel server hostnames, or when using `--csv` \ | ||
or `--resolve`, the output will switch to CSV format. | ||
|
||
When outputting CSV, entries for unknown tunnel IDs will be an empty string, \ | ||
rather than an error." | ||
)] | ||
#[clap(group( | ||
ArgGroup::new("ids") | ||
.required(true) | ||
.args(&["tunnel-server-ids", "all"]) | ||
))] | ||
pub struct CliParser { | ||
/// One or more tunnel server IDs. | ||
#[clap(value_name = "ID", value_hint = ValueHint::Other)] | ||
pub tunnel_server_ids: Vec<u16>, | ||
|
||
/// Derives all 65,536 possible tunnel server hostnames. | ||
#[clap(short, long)] | ||
pub all: bool, | ||
|
||
/// Enable CSV output mode. | ||
/// | ||
/// This is automatically enabled when requesting multiple (or all) tunnel | ||
/// server IDs, or when using `--resolve`. | ||
#[clap(short, long)] | ||
pub csv: bool, | ||
|
||
/// Resolve tunnel server hostnames to IP addresses. | ||
/// | ||
/// This generally results in network traffic, so will be slow. | ||
#[clap(short, long)] | ||
pub resolve: bool, | ||
} | ||
|
||
/// Resolves a hostname to (an) IP address(es) using the system resolver, | ||
/// and return it as a comma-separated list. | ||
/// | ||
/// Returns [None] on resolution failure or no results. | ||
fn resolver(hostname: &str) -> Option<String> { | ||
(hostname, 443).to_socket_addrs().ok().and_then(|addrs| { | ||
let mut o: String = addrs | ||
.map(|addr| format!("{},", addr.ip().to_string())) | ||
.collect(); | ||
o.pop(); | ||
|
||
if o.is_empty() { | ||
return None; | ||
} | ||
|
||
Some(o) | ||
}) | ||
} | ||
|
||
/// Prints caBLE tunnel server hostnames in CSV format. | ||
fn print_hostnames(i: impl Iterator<Item = u16>, resolve: bool) { | ||
for domain_id in i { | ||
let domain = get_domain(domain_id); | ||
let addrs = if resolve { | ||
domain.as_deref().and_then(resolver) | ||
} else { | ||
None | ||
} | ||
.unwrap_or_default(); | ||
let domain = domain.unwrap_or_default(); | ||
|
||
if resolve { | ||
println!("{domain_id},{domain:?},{addrs:?}",); | ||
} else { | ||
println!("{domain_id},{domain:?}",); | ||
} | ||
} | ||
} | ||
|
||
fn main() { | ||
let opt = CliParser::parse(); | ||
tracing_subscriber::fmt::init(); | ||
|
||
if opt.tunnel_server_ids.len() == 1 && !(opt.resolve || opt.csv) { | ||
let domain_id = opt.tunnel_server_ids[0]; | ||
match get_domain(domain_id) { | ||
Some(d) => println!("{d}"), | ||
None => CliParser::command() | ||
.error( | ||
clap::ErrorKind::ValueValidation, | ||
format!("unknown tunnel server ID: {domain_id}"), | ||
) | ||
.exit(), | ||
} | ||
} else { | ||
if opt.resolve { | ||
println!("tunnel_server_id,hostname,addrs"); | ||
} else { | ||
println!("tunnel_server_id,hostname"); | ||
} | ||
|
||
if opt.all { | ||
print_hostnames(u16::MIN..=u16::MAX, opt.resolve); | ||
} else { | ||
print_hostnames(opt.tunnel_server_ids.into_iter(), opt.resolve); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to talk in future about how to move these out of being examples, since there are multiple tools here that actually are good cli tools, and not examples.