IotaFrontend

This commit is contained in:
Alex Emmet 2025-11-18 21:21:01 +01:00
commit f88888983e
9 changed files with 421 additions and 45 deletions

View file

@ -29,6 +29,8 @@ use crate::omikron::omikron_connection::OmikronConnection;
use crate::server::server::start;
use crate::users::user_manager;
use crate::util::config_util::CONFIG;
use crate::util::file_util::download_and_extract_zip;
use crate::util::file_util::has_dir;
pub static APP_STATE: LazyLock<Arc<Mutex<AppState>>> =
LazyLock::new(|| Arc::new(Mutex::new(AppState::new())));
@ -115,7 +117,13 @@ async fn main() {
log_message(format("community_start_error", &[&port.to_string()]));
}
}
if !has_dir("web") {
/*download_and_extract_zip(
"weblink",
"web",
)
.await;*/
}
loop {
let omikron: Arc<OmikronConnection> = Arc::new(OmikronConnection::new());
omikron.connect().await;

View file

@ -20,10 +20,8 @@ pub async fn handle(
.unwrap();
}
// Collect path parts into a vector to avoid iterator consumption issues
let path_parts: Vec<&str> = path.split("/").collect();
// path_parts[0] is empty (before first /), path_parts[1] should be "api", path_parts[2] is the endpoint
let (status, content, body_text) = if path_parts.len() >= 3 {
match path_parts[2] {
"app_state" => (StatusCode::OK, "application/json", {
@ -31,7 +29,6 @@ pub async fn handle(
json.to_json().to_string()
}),
"users" => (StatusCode::OK, "application/json", {
// Check for sub-endpoints like /api/users/add, /api/users/get, etc.
if path_parts.len() >= 4 {
match path_parts[3] {
"add" => "{}".to_string(),
@ -47,7 +44,6 @@ pub async fn handle(
_ => "{}".to_string(),
}
} else {
// Default: return all users
let users = user_manager::get_users();
let mut json = JsonValue::new_array();
for user in users {

View file

@ -1,12 +1,13 @@
use crate::gui::log_panel::log_message;
use crate::server::api;
use crate::server::socket::handle;
use crate::util::file_util::load_file_buf;
use crate::util::file_util::{load_file, load_file_buf};
use base64::Engine;
use base64::engine::general_purpose::STANDARD;
use futures::{StreamExt, TryFutureExt};
use http_body_util::Full;
use hyper::body::Bytes;
use hyper::header::CONTENT_LENGTH;
use hyper::{
Request as HttpRequest, Response as HttpResponse, StatusCode, body::Incoming,
server::conn::http1, upgrade,
@ -110,20 +111,44 @@ impl Service<HttpRequest<Incoming>> for HttpService {
if path.starts_with("/api") {
Ok(api::handle(&path, &is_local, headers).await)
} else {
let (status, content, body_text) = match path.as_str() {
"/" => (
StatusCode::OK,
"text/plain",
"Server: Try connecting to WebSocket at ws[s]://<host>:<port>/ws or check /status.",
),
"/status" => (StatusCode::OK, "text/plain", "Server Status: Online"),
"/index" => (
StatusCode::OK,
"text/html",
include_str!("../../static/web/index.html"),
),
_ => (StatusCode::NOT_FOUND, "text/plain", "404 Not Found"),
};
let mut path_parts: Vec<&str> = path.split("/").collect();
let name = path_parts.remove(path_parts.len() - 1);
let (status, content, body_text): (StatusCode, &str, String) =
if name.is_empty() {
let content = load_file("web", "index.html");
if content.is_empty() {
let content = load_file("web", "404.html");
if content.is_empty() {
(
StatusCode::NOT_FOUND,
"text/html",
include_str!("../../static/web/404.html").to_string(),
)
} else {
(StatusCode::OK, "text/html", content)
}
} else {
(StatusCode::OK, "text/html", content)
}
} else {
let content = load_file(&format!("web{}/", path_parts.join("/")), name);
if content.is_empty() {
let content = load_file("web", "404.html");
if content.is_empty() {
(
StatusCode::NOT_FOUND,
"text/html",
include_str!("../../static/web/404.html").to_string(),
)
} else {
(StatusCode::OK, "text/html", content)
}
} else {
(StatusCode::OK, "text/html", content)
}
};
let body = Full::new(Bytes::from(body_text.to_string()));
let response = HttpResponse::builder()
.header("Content-Type", content)
@ -145,19 +170,16 @@ impl Service<HttpRequest<Incoming>> for HttpService {
}
fn is_local_network(addr: IpAddr) -> bool {
// 1. Check for standard private ranges (RFC 1918) and loopback
if addr.is_loopback() {
return true;
}
// 2. Check for Link-Local Addresses (169.254.x.x)
if let IpAddr::V4(ipv4) = addr {
if ipv4.octets()[0] == 169 && ipv4.octets()[1] == 254 {
return true;
}
}
// 3. Check for IPv6 Unique Local Addresses (fc00::/7)
if let IpAddr::V6(ipv6) = addr {
if (ipv6.segments()[0] & 0xfe00) == 0xfc00 {
return true;
@ -167,9 +189,7 @@ fn is_local_network(addr: IpAddr) -> bool {
false
}
/// Runs the standard, unencrypted HTTP/WS server loop.
async fn run_http_server(port: u16) -> bool {
// Bind to the port
let listener = TcpListener::bind(format!("0.0.0.0:{}", port)).await;
if let Err(e) = listener {
log_message(format!("Failed to bind to port {}: {:?}", port, e));
@ -224,7 +244,6 @@ async fn run_http_server(port: u16) -> bool {
async fn run_tls_server(port: u16, tls_config: Arc<ServerConfig>) -> bool {
let acceptor = TlsAcceptor::from(tls_config);
// Bind to the port
let listener = TcpListener::bind(format!("0.0.0.0:{}", port)).await;
if let Err(e) = listener {
log_message(format!("Failed to bind to port {}: {:?}", port, e));

View file

@ -1,10 +1,13 @@
use std::ffi::OsStr;
use std::fs::{self, File};
use std::io::{self, BufReader, Read};
use std::io::{self, BufReader, Cursor, Read};
use std::path::{Path, PathBuf};
use sysinfo::System;
use uuid::Uuid;
use walkdir::WalkDir;
use zip::ZipArchive;
use crate::gui::log_panel::log_message;
pub fn delete_file(path: &str, name: &str) -> bool {
let dir = Path::new(&get_directory()).join(path);
@ -25,11 +28,11 @@ fn delete_dir_recursive(directory: &Path) -> bool {
return false;
}
if let Err(e) = fs::remove_dir_all(directory) {
println!(
log_message(format!(
"[IMPORTANT] Couldn't delete directory {}: {}",
directory.display(),
e
);
));
return false;
}
true
@ -48,7 +51,7 @@ pub fn load_file_buf(path: &str, name: &str) -> io::Result<BufReader<File>> {
// Ensure the directory exists, create if necessary
if !dir.exists() {
if let Err(e) = fs::create_dir_all(&dir) {
if let Err(_) = fs::create_dir_all(&dir) {
return Err(io::Error::new(
io::ErrorKind::NotFound,
"Directory creation failed",
@ -68,13 +71,37 @@ pub fn load_file_buf(path: &str, name: &str) -> io::Result<BufReader<File>> {
let file = File::open(&file_path)?;
Ok(BufReader::new(file))
}
pub fn has_file(path: &str, name: &str) -> bool {
let dir = Path::new(&get_directory()).join(path);
let file_path = dir.join(name);
if !dir.exists() {
return false;
}
if !file_path.exists() {
return false;
}
true
}
pub fn has_dir(path: &str) -> bool {
let dir = Path::new(&get_directory()).join(path);
if !dir.exists() {
return false;
}
true
}
pub fn load_file(path: &str, name: &str) -> String {
let dir = Path::new(&get_directory()).join(path);
let file_path = dir.join(name);
if !dir.exists() {
if let Err(e) = fs::create_dir_all(&dir) {
println!("[IMPORTANT] Couldn't create directories: {}", e);
log_message(format!("[IMPORTANT] Couldn't create directories: {}", e));
return String::new();
}
return String::new();
@ -82,7 +109,7 @@ pub fn load_file(path: &str, name: &str) -> String {
if !file_path.exists() {
if let Err(e) = File::create(&file_path) {
println!("[IMPORTANT] Couldn't create file: {}", e);
log_message(format!("[IMPORTANT] Couldn't create file: {}", e));
}
return String::new();
}
@ -100,17 +127,17 @@ pub fn save_file(path: &str, name: &str, value: &str) {
if !dir.exists() {
if let Err(e) = fs::create_dir_all(&dir) {
println!("[IMPORTANT] Couldn't create directories: {}", e);
log_message(format!("[IMPORTANT] Couldn't create directories: {}", e));
return;
}
}
if let Err(e) = fs::write(&file_path, value) {
println!(
log_message(format!(
"[IMPORTANT] Couldn't write file {}: {}",
file_path.display(),
e
);
));
}
}
@ -186,3 +213,122 @@ pub fn get_used_ram() -> String {
let total = sys.total_memory() * 1024;
format!("{}/{}", design_byte(used), design_byte(total))
}
// Helper to download the zip file content to a file on disk
async fn download_zip(url: &str, as_name: &Path) -> Result<(), Box<dyn std::error::Error>> {
let response = reqwest::get(url).await?;
// Check for successful response status
if !response.status().is_success() {
return Err(format!("Failed to download file: Status {}", response.status()).into());
}
let mut zip_file = File::create(as_name)?;
let body = response.bytes().await?;
io::copy(&mut &*body, &mut zip_file)?;
Ok(())
}
fn extract_zip_contents_to_folder(
zip_path: &Path,
target_dir: &Path,
) -> Result<(), Box<dyn std::error::Error>> {
let file = File::open(zip_path)?;
let mut archive = ZipArchive::new(file)?;
let staging_dir = target_dir.with_extension("staging");
let _ = fs::remove_dir_all(&staging_dir);
fs::create_dir_all(&staging_dir)?;
let mut first_item_name: Option<PathBuf> = None;
for i in 0..archive.len() {
let mut file = archive.by_index(i)?;
let entry_path = staging_dir.join(file.sanitized_name());
if i == 0 {
if file.name().ends_with('/') || file.sanitized_name().components().count() == 1 {
first_item_name = Some(file.sanitized_name());
}
}
if file.name().ends_with('/') {
fs::create_dir_all(&entry_path)?;
} else {
if let Some(parent) = entry_path.parent() {
fs::create_dir_all(parent)?;
}
let mut out_file = File::create(entry_path)?;
io::copy(&mut file, &mut out_file)?;
}
}
if let Some(root_path) = first_item_name {
let root_dir = staging_dir.join(&root_path);
if root_dir.is_dir() {
let root_contents_count = fs::read_dir(&staging_dir)?.count();
if root_contents_count == 1
|| (root_contents_count > 1 && fs::metadata(&root_dir).is_ok())
{
let _ = fs::remove_dir_all(target_dir);
fs::create_dir_all(target_dir)?;
for entry in fs::read_dir(root_dir)? {
let entry = entry?;
let src = entry.path();
let dest = target_dir.join(entry.file_name());
if let Err(_) = fs::rename(&src, &dest) {
if src.is_file() {
fs::copy(&src, &dest)?;
} else {
if entry.path().is_dir() {
fs::rename(&src, &dest)?;
}
}
}
}
let _ = fs::remove_dir_all(&staging_dir);
return Ok(());
}
}
}
log_message("Extracting directly (no single root folder detected).");
let _ = fs::remove_dir_all(target_dir);
fs::rename(&staging_dir, target_dir)?;
Ok(())
}
pub async fn download_and_extract_zip(url: &str, as_name: &str) {
let base_dir = PathBuf::from(get_directory());
let zip_filename = format!("{}.zip", Uuid::new_v4()); // Use a unique name for the downloaded ZIP file
let zip_path = base_dir.join(&zip_filename);
let target_dir = base_dir.join(as_name);
// Step 1: Download the ZIP file
if let Err(e) = download_zip(url, &zip_path).await {
log_message(format!("Error downloading file: {}", e));
return;
}
// Step 2: Extract and flatten the ZIP file contents into the target directory
if let Err(e) = extract_zip_contents_to_folder(&zip_path, &target_dir) {
log_message(format!("Error extracting ZIP file contents: {}", e));
}
// Step 3: Clean up the downloaded ZIP file
if let Err(e) = fs::remove_file(&zip_path) {
log_message(format!(
"Error cleaning up ZIP file {}: {}",
zip_path.display(),
e
));
}
}