Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Delayed Invocation #27

Merged
merged 2 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "app"
version = "0.2.3"
version = "0.2.4"
description = "Reseda"
authors = ["bennjii"]
license = ""
Expand Down
40 changes: 26 additions & 14 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
windows_subsystem = "windows"
)]

use std::array::TryFromSliceError;
use std::path::PathBuf;
use std::process::{Command, Stdio};
use std::fs;
use std::io::{Write};
use std::thread;

use base64;
use rand_core::OsRng;
Expand Down Expand Up @@ -37,7 +37,7 @@ fn is_wireguard_up() -> String {
.expect("Failed to read stdout")
};

println!("Status Check:: Wireguard is {}", String::from_utf8(output.stdout.to_vec()).unwrap());
// println!("Status Check:: Wireguard is {}", String::from_utf8(output.stdout.to_vec()).unwrap());

String::from_utf8(output.stdout.to_vec()).unwrap()
}
Expand All @@ -49,17 +49,20 @@ fn start_wireguard_tunnel(path: String) -> String {

// Switch based on target operating sys.
let output = if cfg!(target_os = "windows") {
Command::new("net")
// thread::spawn(|| {
Command::new("net")
.arg("start")
.arg("WireGuardTunnel$wg0")
.output()
.expect("Failed to start wireguard!");

// })
} else {
Command::new("wg-quick")
.arg(format!("up {}/lib/wg0.conf", path))
.output()
.expect("Failed to start wireguard!");
// thread::spawn(move || {
Command::new("wg-quick")
.arg(format!("up {}/lib/wg0.conf", path))
.output()
.expect("Failed to start wireguard!");
// })
};

println!("Started Tunnel: {:?}", output);
Expand Down Expand Up @@ -223,10 +226,23 @@ fn remove_windows_service() -> Result<String, &'static str> {
Ok(output)
}

#[tauri::command]
fn verify_installation() -> bool {
let wireguard_installed = match Command::new("wg").output() {
Ok(_) => true,
Err(error) => {
println!("{:?}", error);
false
},
};

wireguard_installed
}

fn main() {
// Then Build TAURI.
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![generate_private_key, add_peer, remove_peer, start_wireguard_tunnel, stop_wireguard_tunnel, generate_public_key, is_wireguard_up, remove_windows_service, log_to_console])
.invoke_handler(tauri::generate_handler![verify_installation, generate_private_key, add_peer, remove_peer, start_wireguard_tunnel, stop_wireguard_tunnel, generate_public_key, is_wireguard_up, remove_windows_service, log_to_console])
.setup(| _app | {
let rpath = _app.path_resolver().resource_dir().expect("Unable to access resources directory.");
let apath = _app.path_resolver().app_dir().expect("Unable to access app directory...");
Expand All @@ -235,10 +251,7 @@ fn main() {
println!("Dir: {}", format!("{}/lib/wg0.conf", &apath.display()));

let exists_ = match wireguard_config_path_exists {
Ok(inner) => {
println!("File Is File? {:?}", inner);
true
},
Ok(_inner) => true,
Err(ref _e) => false
};

Expand All @@ -248,7 +261,6 @@ fn main() {
fs::create_dir_all(apath.clone().join("lib"))?;

let private_key = generate_private_key();
println!("{:?}", private_key);

write_text_file(
&apath,
Expand Down
41 changes: 34 additions & 7 deletions src/components/reseda.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ class WireGuard extends Component<{ file_path: string, user: any }> {
path: string,
fetching: boolean,
usage: Usage[],
registry: Server[]
registry: Server[],
installed: boolean
};
config: {
keys: {
Expand All @@ -77,11 +78,18 @@ class WireGuard extends Component<{ file_path: string, user: any }> {
super(props);
let { file_path, user } = props;

console.log("Constructing Wireguard Object")
console.log("Constructing Wireguard Object");



// Check if wireguard is installed,
// Check if there are any problems with setup, i.e. wireguard is on when meant to be off...
// Add loading UI so the user knows what is happening, i.e. disconnecting messages, reconnecting messages, ...

this.user = user;

this.state = {
installed: false,
connection: {
location: null,
connected: false,
Expand All @@ -108,6 +116,10 @@ class WireGuard extends Component<{ file_path: string, user: any }> {
})
};

invoke('verify_installation').then((e: boolean) => {
this.state.installed = e;
})

console.log("Loading configuration from file: ", file_path);

getConfigObjectFromFile({ filePath: file_path }).then((e) => {
Expand Down Expand Up @@ -223,12 +235,15 @@ class WireGuard extends Component<{ file_path: string, user: any }> {
case 1:
return (
<div className="flex flex-col items-center">
<h1 className="font-mono">{this?.state?.connection?.location?.country}</h1>
<h1 className="font-mono">{this?.state?.connection?.location?.country.replaceAll("_", " ")}</h1>
<p className="font-normal font-mono opacity-60 text-sm">
{
this?.state?.connection?.location?.id?.split("-").filter((e,i) => {
return i <= 1
}).join("-")
}).map(e => {
const k = e[0].toUpperCase();
return k + e.substring(1, e.length)
}).join(" ")
}
</p>
</div>
Expand Down Expand Up @@ -650,9 +665,21 @@ class WireGuard extends Component<{ file_path: string, user: any }> {
}

async up(cb: Function) {
await invoke('start_wireguard_tunnel', { path: this.config.wg.filePath }).then(e => {
cb();
})
let k = await invoke('is_wireguard_up').then(e => {
return `${e}`;
});

if(k.includes('RUNNING')) {
await this.down(async () => {
await invoke('start_wireguard_tunnel', { path: this.config.wg.filePath }).then(e => {
cb();
})
});
}else {
await invoke('start_wireguard_tunnel', { path: this.config.wg.filePath }).then(e => {
cb();
})
}
}

async down(cb: Function) {
Expand Down