Skip to content

Commit

Permalink
rename nvs structs
Browse files Browse the repository at this point in the history
  • Loading branch information
indexds committed Dec 27, 2024
1 parent df6c41b commit 2af5a52
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 48 deletions.
12 changes: 6 additions & 6 deletions src/http/index.rs
Original file line number Diff line number Diff line change
@@ -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<String> {
pub fn index_html(wg_config: &WgConfig) -> anyhow::Result<String> {
let favicon = BASE64_STANDARD.encode(FAVICON_DATA);

Ok(format!(
Expand Down Expand Up @@ -72,9 +72,9 @@ pub fn index_html(nvs: &NvsWireguard) -> anyhow::Result<String> {
<script src="index.js"></script>
</html>
"###,
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(),
))
}
4 changes: 2 additions & 2 deletions src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)?;

Expand Down
8 changes: 4 additions & 4 deletions src/http/wg_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions src/http/wifi_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions src/network/wifi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
});

Expand Down
42 changes: 20 additions & 22 deletions src/utils/nvs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,

Expand All @@ -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 = "";
Expand All @@ -46,13 +46,13 @@ impl NvsWireguard {
}

/// Call to set the Wireguard configuration in nvs.
pub fn set_fields(nvs: Arc<Mutex<EspNvs<NvsDefault>>>, keys: NvsWireguard) -> anyhow::Result<()> {
pub fn set_fields(nvs: Arc<Mutex<EspNvs<NvsDefault>>>, 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(())
}
Expand All @@ -64,30 +64,28 @@ 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()),
),
})
}
}

#[derive(Deserialize)]
pub struct NvsWifi {
pub struct WifiConfig {
#[serde(rename = "ssid")]
pub sta_ssid: HeaplessString<32>,

Expand All @@ -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 = "";
Expand All @@ -121,12 +119,12 @@ impl NvsWifi {
}

/// Call to set the wifi configuration in nvs.
pub fn set_fields(nvs: Arc<Mutex<EspNvs<NvsDefault>>>, keys: NvsWifi) -> anyhow::Result<()> {
pub fn set_fields(nvs: Arc<Mutex<EspNvs<NvsDefault>>>, 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(())
}
Expand All @@ -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()),
),
})
Expand Down
12 changes: 6 additions & 6 deletions src/wireguard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -54,19 +54,19 @@ pub fn sync_sntp(wifi: Arc<Mutex<EspWifi<'static>>>) -> anyhow::Result<()> {
}

pub fn start_wg_tunnel(nvs: Arc<Mutex<EspNvs<NvsDefault>>>) -> 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 _;

Expand Down

0 comments on commit 2af5a52

Please sign in to comment.