1
0
mirror of https://github.com/tonarino/innernet.git synced 2024-11-24 08:42:33 +02:00

shared(prompts): add consent prompt for public IP query

Closes #73
This commit is contained in:
Jake McGinty 2021-05-10 04:39:46 +09:00
parent 8a021a3674
commit 3cb766f795
2 changed files with 12 additions and 12 deletions

View File

@ -4,9 +4,7 @@ use dialoguer::{theme::ColorfulTheme, Input};
use indoc::printdoc; use indoc::printdoc;
use publicip::Preference; use publicip::Preference;
use rusqlite::{params, Connection}; use rusqlite::{params, Connection};
use shared::{ use shared::{prompts, CidrContents, Endpoint, PeerContents, PERSISTENT_KEEPALIVE_INTERVAL_SECS};
prompts, CidrContents, Endpoint, Hostname, PeerContents, PERSISTENT_KEEPALIVE_INTERVAL_SECS,
};
use wgctrl::KeyPair; use wgctrl::KeyPair;
fn create_database<P: AsRef<Path>>( fn create_database<P: AsRef<Path>>(
@ -26,7 +24,7 @@ fn create_database<P: AsRef<Path>>(
pub struct InitializeOpts { pub struct InitializeOpts {
/// The network name (ex: evilcorp) /// The network name (ex: evilcorp)
#[structopt(long)] #[structopt(long)]
pub network_name: Option<Hostname>, pub network_name: Option<InterfaceName>,
/// The network CIDR (ex: 10.42.0.0/16) /// The network CIDR (ex: 10.42.0.0/16)
#[structopt(long)] #[structopt(long)]
@ -121,7 +119,7 @@ pub fn init_wizard(conf: &ServerConfig, opts: InitializeOpts) -> Result<(), Erro
\n" \n"
); );
let name: Hostname = if let Some(name) = opts.network_name { let name: InterfaceName = if let Some(name) = opts.network_name {
name name
} else { } else {
Input::with_theme(&theme) Input::with_theme(&theme)
@ -138,9 +136,6 @@ pub fn init_wizard(conf: &ServerConfig, opts: InitializeOpts) -> Result<(), Erro
.interact()? .interact()?
}; };
// This probably won't error because of the `hostname_validator` regex.
let name = name.parse()?;
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 {

View File

@ -357,13 +357,18 @@ 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 = if Confirm::with_theme(&*THEME)
.with_prompt("Auto-fill public IP address (using a DNS query to 1.1.1.1)?")
.interact()?
{
publicip::get_any(Preference::Ipv4)
} else {
None
};
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 {
endpoint_builder.default(SocketAddr::new(ip, 51820).into()); endpoint_builder.with_initial_text(SocketAddr::new(ip, 51820).to_string());
} else {
println!("failed to get external IP.");
} }
endpoint_builder endpoint_builder
.with_prompt("External endpoint") .with_prompt("External endpoint")