Skip to content

Commit

Permalink
publicip: don't explode, just leave as None
Browse files Browse the repository at this point in the history
  • Loading branch information
mcginty committed May 9, 2021
1 parent 426916f commit 46d9783
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
21 changes: 12 additions & 9 deletions publicip/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,22 @@ pub enum Preference {
Ipv6,
}

pub fn get_both() -> Result<(Option<Ipv4Addr>, Option<Ipv6Addr>), Error> {
let ipv4 = Request::start(CLOUDFLARE_IPV4)?;
let ipv6 = Request::start(CLOUDFLARE_IPV6)?;
Ok((ipv4.read_response().ok(), ipv6.read_response().ok()))
pub fn get_both() -> (Option<Ipv4Addr>, Option<Ipv6Addr>) {
let ipv4 = Request::start(CLOUDFLARE_IPV4).ok();
let ipv6 = Request::start(CLOUDFLARE_IPV6).ok();
(
ipv4.and_then(|req| req.read_response().ok()),
ipv6.and_then(|req| req.read_response().ok()),
)
}

pub fn get_any(preference: Preference) -> Result<Option<IpAddr>, Error> {
let (v4, v6) = get_both()?;
pub fn get_any(preference: Preference) -> Option<IpAddr> {
let (v4, v6) = get_both();
let (v4, v6) = (v4.map(IpAddr::from), v6.map(IpAddr::from));
Ok(match preference {
match preference {
Preference::Ipv4 => v4.or(v6),
Preference::Ipv6 => v6.or(v4),
})
}
}

struct Request<T> {
Expand Down Expand Up @@ -183,7 +186,7 @@ mod tests {
#[ignore]
fn it_works() -> Result<(), Error> {
let now = Instant::now();
let (v4, v6) = get_both()?;
let (v4, v6) = get_both();
println!("Done in {}ms", now.elapsed().as_millis());
println!("v4: {:?}, v6: {:?}", v4, v6);
assert!(v4.is_some());
Expand Down
2 changes: 1 addition & 1 deletion server/src/initialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ pub fn init_wizard(conf: &ServerConfig, opts: InitializeOpts) -> Result<(), Erro
let endpoint: Endpoint = if let Some(endpoint) = opts.external_endpoint {
endpoint
} else if opts.auto_external_endpoint {
let ip = publicip::get_any(Preference::Ipv4)?.ok_or("couldn't get external IP")?;
let ip = publicip::get_any(Preference::Ipv4).ok_or("couldn't get external IP")?;
SocketAddr::new(ip, 51820).into()
} else {
prompts::ask_endpoint()?
Expand Down
2 changes: 1 addition & 1 deletion shared/src/prompts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ pub fn set_listen_port(
pub fn ask_endpoint() -> Result<Endpoint, Error> {
println!("getting external IP address.");

let external_ip = publicip::get_any(Preference::Ipv4)?;
let external_ip = publicip::get_any(Preference::Ipv4);

let mut endpoint_builder = Input::with_theme(&*THEME);
if let Some(ip) = external_ip {
Expand Down

0 comments on commit 46d9783

Please sign in to comment.