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: detect and handle peer drop on active connections #355

Merged
merged 14 commits into from
Nov 15, 2024
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
4,389 changes: 1,917 additions & 2,472 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

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

73 changes: 71 additions & 2 deletions 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
Expand Up @@ -44,7 +44,7 @@ sqlx = { version = "0.8", features = [
] }
struct-patch = "0.4"
strum = { version = "0.26", features = ["derive"] }
tauri = { version = "1.8", features = [
tauri = { version = "1.8", features = [ "notification-all",
"dialog-all",
"clipboard-all",
"http-all",
Expand Down
5 changes: 3 additions & 2 deletions src-tauri/src/bin/defguard-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use defguard_client::{
database::{self, models::settings::Settings},
enterprise::periodic::config::poll_config,
events::SINGLE_INSTANCE,
periodic::version::poll_version,
periodic::{connection::verify_active_connections, version::poll_version},
service,
tray::{configure_tray_icon, handle_tray_event, reload_tray_menu},
utils::load_log_targets,
Expand All @@ -53,7 +53,7 @@ extern crate log;

// for tauri log plugin
const LOG_TARGETS: [LogTarget; 2] = [LogTarget::Stdout, LogTarget::LogDir];
const LOG_FILTER: [&str; 5] = ["tauri", "sqlx", "hyper", "h2", "tower"];
const LOG_FILTER: [&str; 6] = ["tauri", "sqlx", "hyper", "h2", "tower", "webview"];

lazy_static! {
static ref LOG_INCLUDES: Vec<String> = load_log_targets();
Expand Down Expand Up @@ -253,6 +253,7 @@ async fn main() {
debug!("Starting periodic tasks (config and version polling)...");
tauri::async_runtime::spawn(poll_version(app_handle.clone()));
tauri::async_runtime::spawn(poll_config(app_handle.clone()));
tauri::async_runtime::spawn(verify_active_connections(app_handle.clone()));
debug!("Periodic tasks have been started");

// load tray menu after database initialization to show all instance and locations
Expand Down
4 changes: 2 additions & 2 deletions src-tauri/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ pub async fn disconnect(
}
};
}

info!("Disconnected from {connection_type} {name}(ID: {location_id})");
Ok(())
} else {
Expand Down Expand Up @@ -629,7 +628,7 @@ impl DateTimeAggregation {
}
}

fn get_aggregation(from: NaiveDateTime) -> Result<DateTimeAggregation, Error> {
pub fn get_aggregation(from: NaiveDateTime) -> Result<DateTimeAggregation, Error> {
// Use hourly aggregation for longer periods
let aggregation = match Utc::now().naive_utc() - from {
duration if duration >= Duration::hours(8) => Ok(DateTimeAggregation::Hour),
Expand Down Expand Up @@ -658,6 +657,7 @@ pub async fn location_stats(
location_id,
&from,
&aggregation,
None,
)
.await?
.into_iter()
Expand Down
14 changes: 9 additions & 5 deletions src-tauri/src/database/models/location_stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ use crate::{
#[derive(Debug, Serialize, Deserialize)]
pub struct LocationStats<I = NoId> {
id: I,
location_id: Id,
pub(crate) location_id: Id,
upload: i64,
download: i64,
last_handshake: i64,
pub(crate) last_handshake: i64,
collected_at: NaiveDateTime,
listen_port: u32,
persistent_keepalive_interval: Option<u16>,
pub(crate) persistent_keepalive_interval: Option<u16>,
}

impl From<LocationStats<Id>> for CommonLocationStats<Id> {
Expand Down Expand Up @@ -138,11 +138,13 @@ impl LocationStats<Id> {
location_id: Id,
from: &NaiveDateTime,
aggregation: &DateTimeAggregation,
limit: Option<i32>,
) -> Result<Vec<Self>, Error>
where
E: SqliteExecutor<'e>,
{
let aggregation = aggregation.fstring();
let query_limit = limit.unwrap_or(-1);
let stats = query_as!(
LocationStats,
"WITH cte AS ( \
Expand All @@ -165,10 +167,12 @@ impl LocationStats<Id> {
persistent_keepalive_interval \"persistent_keepalive_interval?: u16\" \
FROM cte \
WHERE location_id = $2 AND collected_at >= $3 \
GROUP BY collected_at ORDER BY collected_at",
GROUP BY collected_at ORDER BY collected_at \
LIMIT $4",
aggregation,
location_id,
from
from,
query_limit
)
.fetch_all(executor)
.await?;
Expand Down
6 changes: 3 additions & 3 deletions src-tauri/src/database/models/tunnel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,13 @@ impl Tunnel<NoId> {
#[derive(Debug, Serialize, Deserialize)]
pub struct TunnelStats<I = NoId> {
id: I,
tunnel_id: Id,
pub tunnel_id: Id,
upload: i64,
download: i64,
last_handshake: i64,
pub last_handshake: i64,
collected_at: NaiveDateTime,
listen_port: u32,
persistent_keepalive_interval: Option<u16>,
pub persistent_keepalive_interval: Option<u16>,
}

impl TunnelStats {
Expand Down
1 change: 1 addition & 0 deletions src-tauri/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ pub static INSTANCE_UPDATE: &str = "instance-update";
pub static LOCATION_UPDATE: &str = "location-update";
pub static APP_VERSION_FETCH: &str = "app-version-fetch";
pub static CONFIG_CHANGED: &str = "config-changed";
pub static DEAD_CONNECTION_DROPPED: &str = "dead-connection-dropped";
Loading
Loading