publicip: don't explode, just leave as None
parent
426916fadd
commit
46d9783109
|
@ -32,19 +32,22 @@ pub enum Preference {
|
||||||
Ipv6,
|
Ipv6,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_both() -> Result<(Option<Ipv4Addr>, Option<Ipv6Addr>), Error> {
|
pub fn get_both() -> (Option<Ipv4Addr>, Option<Ipv6Addr>) {
|
||||||
let ipv4 = Request::start(CLOUDFLARE_IPV4)?;
|
let ipv4 = Request::start(CLOUDFLARE_IPV4).ok();
|
||||||
let ipv6 = Request::start(CLOUDFLARE_IPV6)?;
|
let ipv6 = Request::start(CLOUDFLARE_IPV6).ok();
|
||||||
Ok((ipv4.read_response().ok(), ipv6.read_response().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> {
|
pub fn get_any(preference: Preference) -> Option<IpAddr> {
|
||||||
let (v4, v6) = get_both()?;
|
let (v4, v6) = get_both();
|
||||||
let (v4, v6) = (v4.map(IpAddr::from), v6.map(IpAddr::from));
|
let (v4, v6) = (v4.map(IpAddr::from), v6.map(IpAddr::from));
|
||||||
Ok(match preference {
|
match preference {
|
||||||
Preference::Ipv4 => v4.or(v6),
|
Preference::Ipv4 => v4.or(v6),
|
||||||
Preference::Ipv6 => v6.or(v4),
|
Preference::Ipv6 => v6.or(v4),
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Request<T> {
|
struct Request<T> {
|
||||||
|
@ -183,7 +186,7 @@ mod tests {
|
||||||
#[ignore]
|
#[ignore]
|
||||||
fn it_works() -> Result<(), Error> {
|
fn it_works() -> Result<(), Error> {
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
let (v4, v6) = get_both()?;
|
let (v4, v6) = get_both();
|
||||||
println!("Done in {}ms", now.elapsed().as_millis());
|
println!("Done in {}ms", now.elapsed().as_millis());
|
||||||
println!("v4: {:?}, v6: {:?}", v4, v6);
|
println!("v4: {:?}, v6: {:?}", v4, v6);
|
||||||
assert!(v4.is_some());
|
assert!(v4.is_some());
|
||||||
|
|
|
@ -144,7 +144,7 @@ pub fn init_wizard(conf: &ServerConfig, opts: InitializeOpts) -> Result<(), Erro
|
||||||
let endpoint: Endpoint = if let Some(endpoint) = opts.external_endpoint {
|
let endpoint: Endpoint = if let Some(endpoint) = opts.external_endpoint {
|
||||||
endpoint
|
endpoint
|
||||||
} else if opts.auto_external_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()
|
SocketAddr::new(ip, 51820).into()
|
||||||
} else {
|
} else {
|
||||||
prompts::ask_endpoint()?
|
prompts::ask_endpoint()?
|
||||||
|
|
|
@ -357,7 +357,7 @@ pub fn set_listen_port(
|
||||||
pub fn ask_endpoint() -> Result<Endpoint, Error> {
|
pub fn ask_endpoint() -> Result<Endpoint, Error> {
|
||||||
println!("getting external IP address.");
|
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);
|
let mut endpoint_builder = Input::with_theme(&*THEME);
|
||||||
if let Some(ip) = external_ip {
|
if let Some(ip) = external_ip {
|
||||||
|
|
Loading…
Reference in New Issue