server: validate hostname in 'new'

fixes #164
pull/172/head
Jake McGinty 2021-11-05 12:36:35 +09:00
parent 17dd26921f
commit d7cf24c63c
2 changed files with 11 additions and 14 deletions

View File

@ -5,7 +5,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::{prompts, CidrContents, Endpoint, PeerContents, PERSISTENT_KEEPALIVE_INTERVAL_SECS}; use shared::{CidrContents, Endpoint, PERSISTENT_KEEPALIVE_INTERVAL_SECS, PeerContents, prompts};
use wireguard_control::KeyPair; use wireguard_control::KeyPair;
fn create_database<P: AsRef<Path>>( fn create_database<P: AsRef<Path>>(
@ -26,7 +26,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<InterfaceName>, pub network_name: Option<Interface>,
/// The network CIDR (ex: 10.42.0.0/16) /// The network CIDR (ex: 10.42.0.0/16)
#[structopt(long)] #[structopt(long)]
@ -122,7 +122,7 @@ pub fn init_wizard(conf: &ServerConfig, opts: InitializeOpts) -> Result<(), Erro
\n" \n"
); );
let name: InterfaceName = if let Some(name) = opts.network_name { let name: Interface = if let Some(name) = opts.network_name {
name name
} else { } else {
Input::with_theme(&theme) Input::with_theme(&theme)

View File

@ -3,16 +3,7 @@ use ipnetwork::IpNetwork;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use regex::Regex; use regex::Regex;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{ use std::{fmt::{self, Display, Formatter}, io, net::{IpAddr, SocketAddr, ToSocketAddrs}, ops::{Deref, DerefMut}, path::Path, str::FromStr, time::{Duration, SystemTime}, vec};
fmt::{self, Display, Formatter},
io,
net::{IpAddr, SocketAddr, ToSocketAddrs},
ops::{Deref, DerefMut},
path::Path,
str::FromStr,
time::{Duration, SystemTime},
vec,
};
use structopt::StructOpt; use structopt::StructOpt;
use url::Host; use url::Host;
use wireguard_control::{ use wireguard_control::{
@ -22,7 +13,7 @@ use wireguard_control::{
use crate::wg::PeerInfoExt; use crate::wg::PeerInfoExt;
#[derive(Debug, Clone)] #[derive(Debug, Clone, PartialEq)]
pub struct Interface { pub struct Interface {
name: InterfaceName, name: InterfaceName,
} }
@ -49,6 +40,12 @@ impl Deref for Interface {
} }
} }
impl Display for Interface {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str(&self.name.to_string())
}
}
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
/// An external endpoint that supports both IP and domain name hosts. /// An external endpoint that supports both IP and domain name hosts.
pub struct Endpoint { pub struct Endpoint {