publicip: don't explode, just leave as None
parent
426916fadd
commit
46d9783109
|
@ -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> {
|
||||
|
@ -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());
|
||||
|
|
|
@ -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()?
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue