Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:DefGuard/client into stats_command
Browse files Browse the repository at this point in the history
  • Loading branch information
dzania committed Sep 21, 2023
2 parents b673283 + 91c3b68 commit 0525195
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ serde = { version = "1.0", features = ["derive"] }
sqlx = { version = "0.7.1", features = ["chrono", "sqlite", "runtime-tokio", "uuid", "macros"] }
dirs = "5.0.1"
thiserror = "1.0.48"
chrono = "0.4.30"
chrono = { version = "0.4", features = ["serde"] }
wireguard_rs = { path = "wireguard-rs"}
base64 = "0.21.4"
x25519-dalek = { version = "2", features = [
Expand Down
18 changes: 18 additions & 0 deletions src-tauri/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ use local_ip_address::local_ip;
use serde::{Deserialize, Serialize};
use tauri::{Manager, State};
use tokio;
use tokio::time::{sleep, Duration};
use wireguard_rs::{netlink::delete_interface, wgapi::WGApi};

#[derive(Clone, serde::Serialize)]
struct Payload {
message: String,
}

// Create new wireguard interface
#[tauri::command(async)]
pub async fn connect(location_id: i64, handle: tauri::AppHandle) -> Result<(), String> {
Expand All @@ -21,12 +27,22 @@ pub async fn connect(location_id: i64, handle: tauri::AppHandle) -> Result<(), S
.await
.map_err(|err| err.to_string())?
{
println!("Location: {:#?}", location);
setup_interface(&location, &state.get_pool())
.await
.map_err(|err| err.to_string())?;
let address = local_ip().map_err(|err| err.to_string())?;
let connection = Connection::new(location_id, address.to_string());
state.active_connections.lock().unwrap().push(connection);
println!("{:#?}", state.active_connections.lock().unwrap());
handle
.emit_all(
"connection-changed",
Payload {
message: "Created new connection".into(),
},
)
.unwrap();
// Spawn stats threads
let api = WGApi::new(location.name, false);
tokio::spawn(async move {
Expand All @@ -41,11 +57,13 @@ pub async fn connect(location_id: i64, handle: tauri::AppHandle) -> Result<(), S
.await
.map_err(|err| err.to_string())
.unwrap();
println!("{:#?}", location_stats);
let _ = location_stats.save(&state.get_pool()).await;
}
}
Err(e) => println!("error: {}", e),
}
sleep(Duration::from_secs(20)).await;
}
});
}
Expand Down
3 changes: 2 additions & 1 deletion src-tauri/src/database/models/connection.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use chrono::{NaiveDateTime, Utc};
use serde::Serialize;
use sqlx::{query, FromRow};

use crate::{database::DbPool, error::Error};

#[derive(FromRow)]
#[derive(FromRow, Debug, Serialize)]
pub struct Connection {
pub id: Option<i64>,
pub location_id: i64,
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/database/models/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct Location {
pub allowed_ips: String,
}

#[derive(FromRow, Serialize)]
#[derive(FromRow, Debug, Serialize, Deserialize)]
pub struct LocationStats {
id: Option<i64>,
location_id: i64,
Expand Down
5 changes: 5 additions & 0 deletions src-tauri/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub async fn setup_interface(location: &Location, pool: &DbPool) -> Result<(), E
create_interface(&location.name)?;
address_interface(&location.name, &IpAddrMask::from_str(&location.address)?)?;
let api = WGApi::new(location.name.clone(), false);

let mut host = api.read_host()?;
if let Some(keys) = WireguardKeys::find_by_instance_id(pool, location.instance_id).await? {
// TODO: handle unwrap
Expand All @@ -32,6 +33,7 @@ pub async fn setup_interface(location: &Location, pool: &DbPool) -> Result<(), E
.split(',')
.map(str::to_string)
.collect();

for allowed_ip in allowed_ips {
let addr = IpAddrMask::from_str(&allowed_ip)?;
peer.allowed_ips.push(addr);
Expand All @@ -41,6 +43,9 @@ pub async fn setup_interface(location: &Location, pool: &DbPool) -> Result<(), E
.args(["-4", "route", "add", &allowed_ip, "dev", &location.name])
.output()?;
}

api.write_host(&host)?;
api.write_peer(&peer)?;
};

Ok(())
Expand Down

0 comments on commit 0525195

Please sign in to comment.