diff --git a/src/http/index.rs b/src/http/index.rs index aa3bcd8..d89e196 100644 --- a/src/http/index.rs +++ b/src/http/index.rs @@ -1,11 +1,11 @@ use base64::prelude::BASE64_STANDARD; use base64::Engine; -use crate::utils::nvs::NvsWireguard; +use crate::utils::nvs::WgConfig; const FAVICON_DATA: &[u8] = include_bytes!("./static/assets/favicon.ico"); -pub fn index_html(nvs: &NvsWireguard) -> anyhow::Result { +pub fn index_html(wg_config: &WgConfig) -> anyhow::Result { let favicon = BASE64_STANDARD.encode(FAVICON_DATA); Ok(format!( @@ -72,9 +72,9 @@ pub fn index_html(nvs: &NvsWireguard) -> anyhow::Result { "###, - nvs.address.clean_string().as_str(), - nvs.port.clean_string().as_str(), - nvs.client_private_key.clean_string().as_str(), - nvs.server_public_key.clean_string().as_str(), + wg_config.address.clean_string().as_str(), + wg_config.port.clean_string().as_str(), + wg_config.client_private_key.clean_string().as_str(), + wg_config.server_public_key.clean_string().as_str(), )) } diff --git a/src/http/mod.rs b/src/http/mod.rs index cfd06f4..b945f6f 100644 --- a/src/http/mod.rs +++ b/src/http/mod.rs @@ -6,7 +6,7 @@ use esp_idf_svc::mdns::EspMdns; use esp_idf_svc::nvs::{EspNvs, NvsDefault}; use esp_idf_svc::wifi::EspWifi; -use crate::utils::nvs::NvsWireguard; +use crate::utils::nvs::WgConfig; mod assets_routes; mod index; @@ -30,7 +30,7 @@ pub fn start_http_server( http_server.fn_handler("/", Method::Get, { let nvs = Arc::clone(&nvs); move |mut request| { - let wg_conf = NvsWireguard::new(Arc::clone(&nvs))?; + let wg_conf = WgConfig::new(Arc::clone(&nvs))?; let html = index::index_html(&wg_conf)?; diff --git a/src/http/wg_routes.rs b/src/http/wg_routes.rs index b54d890..c633fb5 100644 --- a/src/http/wg_routes.rs +++ b/src/http/wg_routes.rs @@ -6,7 +6,7 @@ use esp_idf_svc::http::server::{EspHttpServer, Method}; use esp_idf_svc::nvs::{EspNvs, NvsDefault}; use esp_idf_svc::wifi::EspWifi; -use crate::utils::nvs::NvsWireguard; +use crate::utils::nvs::WgConfig; use crate::wireguard; use crate::wireguard::ctx::WG_CTX; @@ -45,9 +45,9 @@ pub fn set_routes( } } - let wg_conf: NvsWireguard = serde_urlencoded::from_str(String::from_utf8(body)?.as_str())?; + let wg_conf: WgConfig = serde_urlencoded::from_str(String::from_utf8(body)?.as_str())?; - NvsWireguard::set_fields(Arc::clone(&nvs), wg_conf)?; + WgConfig::set_fields(Arc::clone(&nvs), wg_conf)?; // Yeah.. let wifi = Arc::clone(&wifi); @@ -94,7 +94,7 @@ pub fn set_routes( let ctx = WG_CTX.lock().unwrap(); let is_connected = (*ctx).is_some(); - let nvs = NvsWireguard::new(Arc::clone(&nvs))?; + let nvs = WgConfig::new(Arc::clone(&nvs))?; let svg_status = if is_connected { "connected" } else { "disconnected" }; let status = if is_connected { diff --git a/src/http/wifi_routes.rs b/src/http/wifi_routes.rs index 14a362f..a60f2be 100644 --- a/src/http/wifi_routes.rs +++ b/src/http/wifi_routes.rs @@ -7,7 +7,7 @@ use esp_idf_svc::nvs::{EspNvs, NvsDefault}; use esp_idf_svc::wifi::{AuthMethod, EspWifi}; use crate::network::wifi; -use crate::utils::nvs::NvsWifi; +use crate::utils::nvs::WifiConfig; pub fn set_routes( http_server: &mut EspHttpServer<'static>, @@ -45,9 +45,9 @@ pub fn set_routes( } } - let wifi_conf: NvsWifi = serde_urlencoded::from_str(String::from_utf8(body)?.as_str())?; + let wifi_conf: WifiConfig = serde_urlencoded::from_str(String::from_utf8(body)?.as_str())?; - NvsWifi::set_fields(Arc::clone(&nvs), wifi_conf)?; + WifiConfig::set_fields(Arc::clone(&nvs), wifi_conf)?; let nvs_thread = Arc::clone(&nvs); let wifi = Arc::clone(&wifi); diff --git a/src/network/wifi.rs b/src/network/wifi.rs index bdb567d..d5d233d 100644 --- a/src/network/wifi.rs +++ b/src/network/wifi.rs @@ -6,7 +6,7 @@ use esp_idf_svc::netif::{EspNetif, NetifConfiguration, NetifStack}; use esp_idf_svc::nvs::{EspDefaultNvsPartition, EspNvs, NvsDefault}; use esp_idf_svc::wifi::{ClientConfiguration, Configuration, EspWifi, WifiDriver}; -use crate::utils::nvs::NvsWifi; +use crate::utils::nvs::WifiConfig; pub fn init_netif( modem: Modem, @@ -38,12 +38,12 @@ pub fn set_configuration( let mut wifi = wifi.lock().unwrap(); - let nvs = NvsWifi::new(Arc::clone(&nvs))?; + let config = WifiConfig::new(Arc::clone(&nvs))?; let wifi_config = Configuration::Client(ClientConfiguration { - ssid: nvs.sta_ssid.0, - password: nvs.sta_passwd.0, - auth_method: nvs.sta_auth.as_str().try_into()?, + ssid: config.sta_ssid.0, + password: config.sta_passwd.0, + auth_method: config.sta_auth.as_str().try_into()?, ..Default::default() }); diff --git a/src/utils/nvs.rs b/src/utils/nvs.rs index 029ec38..806a924 100644 --- a/src/utils/nvs.rs +++ b/src/utils/nvs.rs @@ -7,7 +7,7 @@ use serde::Deserialize; use crate::utils::heapless::HeaplessString; #[derive(Deserialize)] -pub struct NvsWireguard { +pub struct WgConfig { #[serde(rename = "address")] pub address: HeaplessString<32>, @@ -21,7 +21,7 @@ pub struct NvsWireguard { pub server_public_key: HeaplessString<64>, } -impl NvsWireguard { +impl WgConfig { const ADDR: &'static str = "ADDR"; const CLIENT_PRIV: &'static str = "PRIVKEY"; const DEFAULT_ADDR: &str = ""; @@ -46,13 +46,13 @@ impl NvsWireguard { } /// Call to set the Wireguard configuration in nvs. - pub fn set_fields(nvs: Arc>>, keys: NvsWireguard) -> anyhow::Result<()> { + pub fn set_fields(nvs: Arc>>, config: WgConfig) -> anyhow::Result<()> { let mut nvs = nvs.lock().unwrap(); - nvs.set_str(Self::ADDR, keys.address.clean_string().as_str())?; - nvs.set_str(Self::PORT, keys.port.clean_string().as_str())?; - nvs.set_str(Self::CLIENT_PRIV, keys.client_private_key.clean_string().as_str())?; - nvs.set_str(Self::SERVER_PUB, keys.server_public_key.clean_string().as_str())?; + nvs.set_str(Self::ADDR, config.address.clean_string().as_str())?; + nvs.set_str(Self::PORT, config.port.clean_string().as_str())?; + nvs.set_str(Self::CLIENT_PRIV, config.client_private_key.clean_string().as_str())?; + nvs.set_str(Self::SERVER_PUB, config.server_public_key.clean_string().as_str())?; Ok(()) } @@ -64,22 +64,20 @@ impl NvsWireguard { Ok(Self { address: HeaplessString( - NvsWireguard::get_field::<32>(&nvs, Self::ADDR) - .unwrap_or_else(|_| Self::DEFAULT_ADDR.try_into().unwrap()), + WgConfig::get_field::<32>(&nvs, Self::ADDR).unwrap_or_else(|_| Self::DEFAULT_ADDR.try_into().unwrap()), ), port: HeaplessString( - NvsWireguard::get_field::<16>(&nvs, Self::PORT) - .unwrap_or_else(|_| Self::DEFAULT_PORT.try_into().unwrap()), + WgConfig::get_field::<16>(&nvs, Self::PORT).unwrap_or_else(|_| Self::DEFAULT_PORT.try_into().unwrap()), ), client_private_key: HeaplessString( - NvsWireguard::get_field::<64>(&nvs, Self::CLIENT_PRIV) + WgConfig::get_field::<64>(&nvs, Self::CLIENT_PRIV) .unwrap_or_else(|_| Self::DEFAULT_CLIENT_PRIV.try_into().unwrap()), ), server_public_key: HeaplessString( - NvsWireguard::get_field::<64>(&nvs, Self::SERVER_PUB) + WgConfig::get_field::<64>(&nvs, Self::SERVER_PUB) .unwrap_or_else(|_| Self::DEFAULT_SERVER_PUB.try_into().unwrap()), ), }) @@ -87,7 +85,7 @@ impl NvsWireguard { } #[derive(Deserialize)] -pub struct NvsWifi { +pub struct WifiConfig { #[serde(rename = "ssid")] pub sta_ssid: HeaplessString<32>, @@ -98,7 +96,7 @@ pub struct NvsWifi { pub sta_auth: HeaplessString<32>, } -impl NvsWifi { +impl WifiConfig { const DEFAULT_STA_AUTH: &str = "wpa2personal"; const DEFAULT_STA_PASSWD: &str = ""; const DEFAULT_STA_SSID: &str = ""; @@ -121,12 +119,12 @@ impl NvsWifi { } /// Call to set the wifi configuration in nvs. - pub fn set_fields(nvs: Arc>>, keys: NvsWifi) -> anyhow::Result<()> { + pub fn set_fields(nvs: Arc>>, config: WifiConfig) -> anyhow::Result<()> { let mut nvs = nvs.lock().unwrap(); - nvs.set_str(Self::STA_SSID, keys.sta_ssid.clean_string().as_str())?; - nvs.set_str(Self::STA_PASSWD, keys.sta_passwd.clean_string().as_str())?; - nvs.set_str(Self::STA_AUTH, keys.sta_auth.clean_string().as_str())?; + nvs.set_str(Self::STA_SSID, config.sta_ssid.clean_string().as_str())?; + nvs.set_str(Self::STA_PASSWD, config.sta_passwd.clean_string().as_str())?; + nvs.set_str(Self::STA_AUTH, config.sta_auth.clean_string().as_str())?; Ok(()) } @@ -139,17 +137,17 @@ impl NvsWifi { // These cannot fail, so we don't care about the unwraps Ok(Self { sta_ssid: HeaplessString( - NvsWifi::get_field::<32>(&nvs, Self::STA_SSID) + WifiConfig::get_field::<32>(&nvs, Self::STA_SSID) .unwrap_or_else(|_| Self::DEFAULT_STA_SSID.try_into().unwrap()), ), sta_passwd: HeaplessString( - NvsWifi::get_field::<64>(&nvs, Self::STA_PASSWD) + WifiConfig::get_field::<64>(&nvs, Self::STA_PASSWD) .unwrap_or_else(|_| Self::DEFAULT_STA_PASSWD.try_into().unwrap()), ), sta_auth: HeaplessString( - NvsWifi::get_field::<32>(&nvs, Self::STA_AUTH) + WifiConfig::get_field::<32>(&nvs, Self::STA_AUTH) .unwrap_or_else(|_| Self::DEFAULT_STA_AUTH.try_into().unwrap()), ), }) diff --git a/src/wireguard/mod.rs b/src/wireguard/mod.rs index 83ef984..8c45238 100644 --- a/src/wireguard/mod.rs +++ b/src/wireguard/mod.rs @@ -9,7 +9,7 @@ use esp_idf_svc::sys::esp; pub use esp_idf_svc::sys::wg::wireguard_ctx_t; use esp_idf_svc::wifi::EspWifi; -use crate::utils::nvs::NvsWireguard; +use crate::utils::nvs::WgConfig; pub mod ctx; @@ -54,19 +54,19 @@ pub fn sync_sntp(wifi: Arc>>) -> anyhow::Result<()> { } pub fn start_wg_tunnel(nvs: Arc>>) -> anyhow::Result<()> { - let nvs = NvsWireguard::new(nvs)?; + let wg_conf = WgConfig::new(nvs)?; unsafe { let config = &mut wireguard_config_t { - private_key: CString::new(nvs.client_private_key.clean_string().as_str())?.into_raw(), + private_key: CString::new(wg_conf.client_private_key.clean_string().as_str())?.into_raw(), listen_port: 51820, fw_mark: 0, - public_key: CString::new(nvs.server_public_key.clean_string().as_str())?.into_raw(), + public_key: CString::new(wg_conf.server_public_key.clean_string().as_str())?.into_raw(), preshared_key: core::ptr::null_mut(), allowed_ip: CString::new("0.0.0.0")?.into_raw(), allowed_ip_mask: CString::new("0.0.0.0")?.into_raw(), - endpoint: CString::new(nvs.address.clean_string().as_str())?.into_raw(), - port: nvs.port.clean_string().as_str().parse()?, + endpoint: CString::new(wg_conf.address.clean_string().as_str())?.into_raw(), + port: wg_conf.port.clean_string().as_str().parse()?, persistent_keepalive: 20, } as *mut _;