164 lines
5.6 KiB
Rust
164 lines
5.6 KiB
Rust
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_ip_from_mac, get_network_address, is_reachable};
|
|
use crate::sudo::{is_run_as_root, run_as_root};
|
|
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;
|
|
mod sudo;
|
|
|
|
fn main() {
|
|
log("main", "========== PROGRAM START ==========", LogLevel::Info);
|
|
|
|
if !is_run_as_root() {
|
|
log("main", "Program is not run as root. Trying to run as root...", LogLevel::Warn);
|
|
run_as_root();
|
|
}
|
|
|
|
let network_interface : String;
|
|
|
|
let mut count : i32 = 0;
|
|
loop {
|
|
let interface_str : String = get_active_network_interface().unwrap().trim().to_string();
|
|
|
|
if !interface_str.is_empty() {
|
|
network_interface = interface_str;
|
|
break;
|
|
} else if count >= 10 {
|
|
log("main", "Couldn't find active network card, exiting.", LogLevel::Error);
|
|
exit(1);
|
|
}
|
|
|
|
log("main", "No active network card found, waiting 1 second.", LogLevel::Warn);
|
|
count = count+1;
|
|
sleep(Duration::from_secs(1));
|
|
}
|
|
log("main", &*format!("Active network interface found: {}", network_interface), LogLevel::Info);
|
|
|
|
let interface_address = get_interface_ip_address(network_interface.as_str()).unwrap().trim().to_string();
|
|
log("main", &*format!("Interface address: {}", interface_address), LogLevel::Info);
|
|
|
|
let network_address = get_network_address(interface_address.as_str()).unwrap().trim().to_string();
|
|
log("main", &*format!("Network address: {}", network_address), LogLevel::Info);
|
|
|
|
count = 0;
|
|
loop {
|
|
if is_reachable(network_address.as_str()) {
|
|
break;
|
|
} else if count >= 10 {
|
|
log("main", "Couldn't reach network address, exiting.", LogLevel::Error);
|
|
exit(1);
|
|
}
|
|
|
|
log("main", "Network address not reachable, waiting 1 second.", LogLevel::Warn);
|
|
count = count+1;
|
|
sleep(Duration::from_secs(1));
|
|
}
|
|
log("main", "Network address is reachable.", LogLevel::Info);
|
|
|
|
let handle_local: JoinHandle<()> = thread::spawn(move || mount_local(network_address));
|
|
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(network_address: String) {
|
|
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: Option<String>;
|
|
let mut count: i32 = 0;
|
|
|
|
loop {
|
|
device_address = get_ip_from_mac(mac_address, network_address.as_str());
|
|
|
|
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);
|
|
}
|
|
}
|