Skip to content

Commit 76204b8

Browse files
authored
feat(cli): improve local IP detection (#5817)
1 parent a9b4cf2 commit 76204b8

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"cli.rs": patch
3+
"cli.js": patch
4+
---
5+
6+
Improve local IP address detection with user selection.

tooling/cli/src/dev.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ use crate::{
1111
interface::{AppInterface, DevProcess, ExitReason, Interface},
1212
CommandExt, Result,
1313
};
14-
use clap::{ArgAction, Parser};
1514

1615
use anyhow::{bail, Context};
16+
use clap::{ArgAction, Parser};
1717
use log::{error, info, warn};
1818
use once_cell::sync::OnceCell;
1919
use shared_child::SharedChild;
@@ -88,7 +88,34 @@ fn command_internal(mut options: Options) -> Result<()> {
8888

8989
fn local_ip_address() -> &'static IpAddr {
9090
static LOCAL_IP: OnceCell<IpAddr> = OnceCell::new();
91-
LOCAL_IP.get_or_init(|| local_ip_address::local_ip().expect("failed to resolve local IP address"))
91+
LOCAL_IP.get_or_init(|| {
92+
let addresses: Vec<IpAddr> = local_ip_address::list_afinet_netifas()
93+
.expect("failed to list networks")
94+
.into_iter()
95+
.map(|(_, ipaddr)| ipaddr)
96+
.filter(|ipaddr| match ipaddr {
97+
IpAddr::V4(i) => i != &Ipv4Addr::LOCALHOST,
98+
_ => false,
99+
})
100+
.collect();
101+
match addresses.len() {
102+
0 => panic!("No external IP detected."),
103+
1 => {
104+
let ipaddr = addresses.first().unwrap();
105+
log::info!("Detected external IP {ipaddr}.");
106+
*ipaddr
107+
}
108+
_ => {
109+
let selected = dialoguer::Select::with_theme(&dialoguer::theme::ColorfulTheme::default())
110+
.with_prompt("What external IP should we use for your development server?")
111+
.items(&addresses)
112+
.default(0)
113+
.interact()
114+
.expect("failed to select external IP");
115+
*addresses.get(selected).unwrap()
116+
}
117+
}
118+
})
92119
}
93120

94121
pub fn setup(options: &mut Options, mobile: bool) -> Result<AppInterface> {

0 commit comments

Comments
 (0)