From d7cf24c63ce0390c99f4d0f99d4f4cdcadc546d9 Mon Sep 17 00:00:00 2001 From: Jake McGinty Date: Fri, 5 Nov 2021 12:36:35 +0900 Subject: [PATCH] server: validate hostname in 'new' fixes #164 --- server/src/initialize.rs | 6 +++--- shared/src/types.rs | 19 ++++++++----------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/server/src/initialize.rs b/server/src/initialize.rs index fd36ba3..236a2c3 100644 --- a/server/src/initialize.rs +++ b/server/src/initialize.rs @@ -5,7 +5,7 @@ use dialoguer::{theme::ColorfulTheme, Input}; use indoc::printdoc; use publicip::Preference; 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; fn create_database>( @@ -26,7 +26,7 @@ fn create_database>( pub struct InitializeOpts { /// The network name (ex: evilcorp) #[structopt(long)] - pub network_name: Option, + pub network_name: Option, /// The network CIDR (ex: 10.42.0.0/16) #[structopt(long)] @@ -122,7 +122,7 @@ pub fn init_wizard(conf: &ServerConfig, opts: InitializeOpts) -> Result<(), Erro \n" ); - let name: InterfaceName = if let Some(name) = opts.network_name { + let name: Interface = if let Some(name) = opts.network_name { name } else { Input::with_theme(&theme) diff --git a/shared/src/types.rs b/shared/src/types.rs index 85b7503..db33c77 100644 --- a/shared/src/types.rs +++ b/shared/src/types.rs @@ -3,16 +3,7 @@ use ipnetwork::IpNetwork; use lazy_static::lazy_static; use regex::Regex; use serde::{Deserialize, Serialize}; -use std::{ - fmt::{self, Display, Formatter}, - io, - net::{IpAddr, SocketAddr, ToSocketAddrs}, - ops::{Deref, DerefMut}, - path::Path, - str::FromStr, - time::{Duration, SystemTime}, - vec, -}; +use std::{fmt::{self, Display, Formatter}, io, net::{IpAddr, SocketAddr, ToSocketAddrs}, ops::{Deref, DerefMut}, path::Path, str::FromStr, time::{Duration, SystemTime}, vec}; use structopt::StructOpt; use url::Host; use wireguard_control::{ @@ -22,7 +13,7 @@ use wireguard_control::{ use crate::wg::PeerInfoExt; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] pub struct Interface { 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)] /// An external endpoint that supports both IP and domain name hosts. pub struct Endpoint {