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

Connection status #9

Merged
merged 6 commits into from
Sep 21, 2023
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

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

38 changes: 38 additions & 0 deletions src-tauri/Cargo.lock

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

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ prost = "0.11"

tauri = { version = "1.4.1", features = [ "window-all", "system-tray"] }
tauri-plugin-single-instance = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
local-ip-address = "0.5.5"

[features]
# this feature is used for production builds or when `devPath` points to the filesystem and the built-in dev server is disabled.
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/appstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::database::{Connection, DbPool};
#[derive(Default)]
pub struct AppState {
pub db: Mutex<Option<DbPool>>,
pub active_connections: Vec<Connection>,
pub active_connections: Mutex<Vec<Connection>>,
}
impl AppState {
pub fn get_pool(&self) -> DbPool {
Expand Down
50 changes: 46 additions & 4 deletions src-tauri/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::{
database::{models::instance::InstanceInfo, Instance, Location, WireguardKeys},
database::{models::instance::InstanceInfo, Connection, Instance, Location, WireguardKeys},
error::Error,
utils::setup_interface,
AppState,
};
use local_ip_address::local_ip;
use serde::{Deserialize, Serialize};
use tauri::State;
use wireguard_rs::netlink::delete_interface;
Expand All @@ -13,6 +14,13 @@ use wireguard_rs::netlink::delete_interface;
pub async fn connect(location_id: i64, app_state: State<'_, AppState>) -> Result<(), Error> {
if let Some(location) = Location::find_by_id(&app_state.get_pool(), location_id).await? {
setup_interface(location, &app_state.get_pool()).await?;
let address = local_ip()?;
let connection = Connection::new(location_id, address.to_string());
app_state
.active_connections
.lock()
.unwrap()
.push(connection);
}
Ok(())
}
Expand Down Expand Up @@ -117,6 +125,8 @@ pub async fn all_instances(app_state: State<'_, AppState>) -> Result<Vec<Instanc
.map_err(|err| err.to_string())?;
let connection_ids: Vec<i64> = app_state
.active_connections
.lock()
.unwrap()
.iter()
.map(|connection| connection.location_id)
.collect();
Expand All @@ -138,12 +148,44 @@ pub async fn all_instances(app_state: State<'_, AppState>) -> Result<Vec<Instanc
Ok(instance_info)
}

#[derive(Serialize)]
pub struct LocationInfo {
pub id: i64,
pub instance_id: i64,
// Native id of network from defguard
pub name: String,
pub address: String,
pub endpoint: String,
pub active: bool,
}

#[tauri::command(async)]
pub async fn all_locations(
instance_id: i64,
app_state: State<'_, AppState>,
) -> Result<Vec<Location>, String> {
Location::find_by_instance_id(&app_state.get_pool(), instance_id)
) -> Result<Vec<LocationInfo>, String> {
let locations = Location::find_by_instance_id(&app_state.get_pool(), instance_id)
.await
.map_err(|err| err.to_string())
.map_err(|err| err.to_string())?;
let active_locations_ids: Vec<i64> = app_state
.active_connections
.lock()
.unwrap()
.iter()
.map(|con| con.location_id)
.collect();
let mut location_info = vec![];
for location in locations {
let info = LocationInfo {
id: location.id.unwrap(),
instance_id: location.instance_id,
name: location.name,
address: location.address,
endpoint: location.endpoint,
active: active_locations_ids.contains(&location.id.unwrap()),
};
location_info.push(info);
}

Ok(location_info)
}
5 changes: 3 additions & 2 deletions src-tauri/src/database/models/connection.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use chrono::NaiveDateTime;
use chrono::{NaiveDateTime, Utc};
use sqlx::{query, FromRow};

use crate::{database::DbPool, error::Error};
Expand All @@ -13,7 +13,8 @@ pub struct Connection {
}

impl Connection {
pub fn new(location_id: i64, connected_from: String, start: NaiveDateTime) -> Self {
pub fn new(location_id: i64, connected_from: String) -> Self {
let start = Utc::now().naive_utc(); // Get the current time as NaiveDateTime in UTC
Connection {
id: None,
location_id,
Expand Down
3 changes: 3 additions & 0 deletions src-tauri/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::net::AddrParseError;

use base64;
use local_ip_address::Error as LocalIpError;
use sqlx;
use thiserror::Error;
use wireguard_rs::{error::WireguardError, IpAddrParseError};
Expand All @@ -23,4 +24,6 @@ pub enum Error {
IpAddrMask(#[from] IpAddrParseError),
#[error("IP address/mask error")]
AddrParse(#[from] AddrParseError),
#[error("Local Ip Error")]
LocalIpError(#[from] LocalIpError),
}
Loading