diff --git a/src/main.rs b/src/main.rs index f9c05f0..59b98c6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,20 @@ -use std::process::exit; -use std::thread::sleep; -use std::time::Duration; -use crate::log::LogLevel; +use crate::config::get_config; +use crate::filesystem::mount::{mount, unmount}; +use crate::filesystem::mounted::{is_mounted, is_mounted_as}; use crate::log::log; +use crate::log::LogLevel; use crate::network::network_interface::{get_active_network_interface, get_interface_ip_address}; -use crate::network::utils::{get_network_address, is_reachable}; +use crate::network::utils::{get_ip_from_mac, get_network_address, is_reachable}; +use std::process::exit; +use std::thread; +use std::thread::{sleep, JoinHandle}; +use std::time::Duration; mod log; mod network; mod config; mod program; +mod filesystem; fn main() { log("main", "========== PROGRAM START ==========", LogLevel::Info); @@ -55,5 +60,97 @@ fn main() { } log("main", "Network address is reachable.", LogLevel::Info); + let handle_local: JoinHandle<()> = thread::spawn(mount_local); + let handle_remote: JoinHandle<()> = thread::spawn(mount_remote); + + handle_local.join().unwrap(); + handle_remote.join().unwrap(); + log("main", "========== PROGRAM END ==========", LogLevel::Info); } + +fn mount_local() { + log("main", "Trying to mount filesystem locally...", LogLevel::Info); + + let mount_point: &str = get_config().general.mount_point.as_str(); + + let mac_address: &str = get_config().local.device_mac.as_str(); + let mount_type: &str = &get_config().local.mount_type.as_str(); + + let mut device_address = None; + let mut count: i32 = 0; + + loop { + device_address = get_ip_from_mac(mac_address); + + if device_address.is_some() || count >= 10 { + break; + } + + log("main", "Couldn't find MAC adress in local network, waiting 1 second.", LogLevel::Warn); + count = count + 1; + sleep(Duration::from_secs(1)); + } + + if device_address.is_none() { + log("main", "Couldn't find device address for MAC address.", LogLevel::Warn); + + if is_mounted_as(mount_point, mount_type) { + log("main", "Filesystem is mounted locally. Unmounting...", LogLevel::Info); + unmount(mount_point); + } + + mount_remote(); + } else { + let dev_ip: String = device_address.unwrap(); + log("main", "Found MAC address in local network.", LogLevel::Info); + + if !is_mounted_as(mount_point, mount_type) { + if is_mounted_as(mount_point, get_config().remote.mount_type.as_str()) { + log("main", "Filesystem is mounted remotely. Unmounting...", LogLevel::Info); + unmount(mount_point); + } + + log("main", "Mounting local filesystem...", LogLevel::Info); + mount(mount_point, &*format!("{}:{}", dev_ip, get_config().local.mount_path), mount_type); + } + } +} + +fn mount_remote() { + let mount_point: &str = get_config().general.mount_point.as_str(); + let mount_type: &str = &get_config().remote.mount_type.as_str(); + + let mut could_reach: bool; + let mut count: i32 = 0; + + loop { + could_reach = is_reachable("https://ping.creative-dragonslayer.de"); + + if could_reach || count >= 10 { + break; + } + + log("main", "Couldn't reach remote server, waiting 1 second.", LogLevel::Warn); + count = count + 1; + sleep(Duration::from_secs(1)); + } + + if could_reach { + log("main", "Remote server reachable.", LogLevel::Info); + if is_mounted_as(mount_point, get_config().local.mount_type.as_str()) || + is_mounted_as(mount_point, mount_type) { + log("main", "Filesystem is already mounted. Doing nothing.", LogLevel::Info); + } else { + if (is_mounted(mount_point)) { + log("main", "Filesystem is already mounted. Unmounting...", LogLevel::Info); + unmount(mount_point); + } + + log("main", "Mounting remote filesystem...", LogLevel::Info); + mount(mount_point, &*format!("{}:{}@{}", get_config().remote.username, get_config().remote.password, get_config().remote.mount_path), mount_type); + } + } else { + log("main", "Remote server not reachable.", LogLevel::Warn); + } +}