From 46d97831094d04fe3ad802a4bf2ac645e09d568c Mon Sep 17 00:00:00 2001 From: Jake McGinty Date: Mon, 10 May 2021 04:17:02 +0900 Subject: [PATCH] publicip: don't explode, just leave as None --- publicip/src/lib.rs | 21 ++++++++++++--------- server/src/initialize.rs | 2 +- shared/src/prompts.rs | 2 +- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/publicip/src/lib.rs b/publicip/src/lib.rs index a2179a5..f5e4425 100644 --- a/publicip/src/lib.rs +++ b/publicip/src/lib.rs @@ -32,19 +32,22 @@ pub enum Preference { Ipv6, } -pub fn get_both() -> Result<(Option, Option), 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, Option) { + 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, Error> { - let (v4, v6) = get_both()?; +pub fn get_any(preference: Preference) -> Option { + 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 { @@ -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()); diff --git a/server/src/initialize.rs b/server/src/initialize.rs index 7b9b7f5..9b895cc 100644 --- a/server/src/initialize.rs +++ b/server/src/initialize.rs @@ -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()? diff --git a/shared/src/prompts.rs b/shared/src/prompts.rs index 8d10117..bb9bf9c 100644 --- a/shared/src/prompts.rs +++ b/shared/src/prompts.rs @@ -357,7 +357,7 @@ pub fn set_listen_port( pub fn ask_endpoint() -> Result { 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 {