diff --git a/src-tauri/.sqlx/query-380c1a0fd567e9f39205f68b69992ef6f85ba760f5a266b320f299c38e5953ef.json b/src-tauri/.sqlx/query-380c1a0fd567e9f39205f68b69992ef6f85ba760f5a266b320f299c38e5953ef.json deleted file mode 100644 index 7136408f..00000000 --- a/src-tauri/.sqlx/query-380c1a0fd567e9f39205f68b69992ef6f85ba760f5a266b320f299c38e5953ef.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "db_name": "SQLite", - "query": "UPDATE instance SET name = $1, uuid = $2, url = $3, proxy_url = $4, username = $5, disable_all_traffic = $6 WHERE id = $7;", - "describe": { - "columns": [], - "parameters": { - "Right": 7 - }, - "nullable": [] - }, - "hash": "380c1a0fd567e9f39205f68b69992ef6f85ba760f5a266b320f299c38e5953ef" -} diff --git a/src-tauri/.sqlx/query-0b25022877adfb942f54ea5fdf05a7fc5c5885c9c6cbf9b3da24ee4f161792db.json b/src-tauri/.sqlx/query-76998bbd9e0096deb957c8e60df4b7f10eb86c8341e3cc0bc081e5c4dcbcee44.json similarity index 79% rename from src-tauri/.sqlx/query-0b25022877adfb942f54ea5fdf05a7fc5c5885c9c6cbf9b3da24ee4f161792db.json rename to src-tauri/.sqlx/query-76998bbd9e0096deb957c8e60df4b7f10eb86c8341e3cc0bc081e5c4dcbcee44.json index 3214a02e..5ed80c68 100644 --- a/src-tauri/.sqlx/query-0b25022877adfb942f54ea5fdf05a7fc5c5885c9c6cbf9b3da24ee4f161792db.json +++ b/src-tauri/.sqlx/query-76998bbd9e0096deb957c8e60df4b7f10eb86c8341e3cc0bc081e5c4dcbcee44.json @@ -1,6 +1,6 @@ { "db_name": "SQLite", - "query": "SELECT id \"id: _\", name, uuid, url, proxy_url, username, token \"token?\", disable_all_traffic FROM instance;", + "query": "SELECT id \"id: _\", name, uuid, url, proxy_url, username, token \"token?\", disable_all_traffic, enterprise_enabled FROM instance;", "describe": { "columns": [ { @@ -42,6 +42,11 @@ "name": "disable_all_traffic", "ordinal": 7, "type_info": "Bool" + }, + { + "name": "enterprise_enabled", + "ordinal": 8, + "type_info": "Bool" } ], "parameters": { @@ -55,8 +60,9 @@ false, false, true, + false, false ] }, - "hash": "0b25022877adfb942f54ea5fdf05a7fc5c5885c9c6cbf9b3da24ee4f161792db" + "hash": "76998bbd9e0096deb957c8e60df4b7f10eb86c8341e3cc0bc081e5c4dcbcee44" } diff --git a/src-tauri/.sqlx/query-8509a9a4d6ccbf8f1a26e77df15fbec973479fef2cc42b5cd203ddf78f409e31.json b/src-tauri/.sqlx/query-8509a9a4d6ccbf8f1a26e77df15fbec973479fef2cc42b5cd203ddf78f409e31.json new file mode 100644 index 00000000..20adea9d --- /dev/null +++ b/src-tauri/.sqlx/query-8509a9a4d6ccbf8f1a26e77df15fbec973479fef2cc42b5cd203ddf78f409e31.json @@ -0,0 +1,12 @@ +{ + "db_name": "SQLite", + "query": "UPDATE instance SET name = $1, uuid = $2, url = $3, proxy_url = $4, username = $5, disable_all_traffic = $6, enterprise_enabled = $7 WHERE id = $8;", + "describe": { + "columns": [], + "parameters": { + "Right": 8 + }, + "nullable": [] + }, + "hash": "8509a9a4d6ccbf8f1a26e77df15fbec973479fef2cc42b5cd203ddf78f409e31" +} diff --git a/src-tauri/.sqlx/query-6359ce19d5563e473293cb8b2f142a6e6bc70a04747bd6059ee9e8599e3d7c28.json b/src-tauri/.sqlx/query-95b89ca80a622d2c625c8d5523a8a59d947ae38b9ddf85a12e6c7bc9a6767f08.json similarity index 58% rename from src-tauri/.sqlx/query-6359ce19d5563e473293cb8b2f142a6e6bc70a04747bd6059ee9e8599e3d7c28.json rename to src-tauri/.sqlx/query-95b89ca80a622d2c625c8d5523a8a59d947ae38b9ddf85a12e6c7bc9a6767f08.json index a287973a..4b7ed9e3 100644 --- a/src-tauri/.sqlx/query-6359ce19d5563e473293cb8b2f142a6e6bc70a04747bd6059ee9e8599e3d7c28.json +++ b/src-tauri/.sqlx/query-95b89ca80a622d2c625c8d5523a8a59d947ae38b9ddf85a12e6c7bc9a6767f08.json @@ -1,6 +1,6 @@ { "db_name": "SQLite", - "query": "INSERT INTO instance (name, uuid, url, proxy_url, username, token, disable_all_traffic) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING id;", + "query": "INSERT INTO instance (name, uuid, url, proxy_url, username, token, disable_all_traffic, enterprise_enabled) VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING id;", "describe": { "columns": [ { @@ -10,11 +10,11 @@ } ], "parameters": { - "Right": 7 + "Right": 8 }, "nullable": [ false ] }, - "hash": "6359ce19d5563e473293cb8b2f142a6e6bc70a04747bd6059ee9e8599e3d7c28" + "hash": "95b89ca80a622d2c625c8d5523a8a59d947ae38b9ddf85a12e6c7bc9a6767f08" } diff --git a/src-tauri/.sqlx/query-ba54bef9b71c2add858203b7be2c0e87d3a54d536ef96a923a6b949e16b9746e.json b/src-tauri/.sqlx/query-ba54bef9b71c2add858203b7be2c0e87d3a54d536ef96a923a6b949e16b9746e.json new file mode 100644 index 00000000..a8ba1030 --- /dev/null +++ b/src-tauri/.sqlx/query-ba54bef9b71c2add858203b7be2c0e87d3a54d536ef96a923a6b949e16b9746e.json @@ -0,0 +1,12 @@ +{ + "db_name": "SQLite", + "query": "UPDATE location SET route_all_traffic = 0 WHERE instance_id = $1;", + "describe": { + "columns": [], + "parameters": { + "Right": 1 + }, + "nullable": [] + }, + "hash": "ba54bef9b71c2add858203b7be2c0e87d3a54d536ef96a923a6b949e16b9746e" +} diff --git a/src-tauri/.sqlx/query-8ef98f97d4a36d666b5864a849c9e872a4ff4c82efc80537f357bdaf5523c15a.json b/src-tauri/.sqlx/query-be25a82798d12394363f3379f2b966d29003dd126f4ac0f8c5236d00891b31c5.json similarity index 78% rename from src-tauri/.sqlx/query-8ef98f97d4a36d666b5864a849c9e872a4ff4c82efc80537f357bdaf5523c15a.json rename to src-tauri/.sqlx/query-be25a82798d12394363f3379f2b966d29003dd126f4ac0f8c5236d00891b31c5.json index f78dcd1b..defb9b73 100644 --- a/src-tauri/.sqlx/query-8ef98f97d4a36d666b5864a849c9e872a4ff4c82efc80537f357bdaf5523c15a.json +++ b/src-tauri/.sqlx/query-be25a82798d12394363f3379f2b966d29003dd126f4ac0f8c5236d00891b31c5.json @@ -1,6 +1,6 @@ { "db_name": "SQLite", - "query": "SELECT id \"id: _\", name, uuid, url, proxy_url, username, token \"token?\", disable_all_traffic FROM instance WHERE id = $1;", + "query": "SELECT id \"id: _\", name, uuid, url, proxy_url, username, token \"token?\", disable_all_traffic, enterprise_enabled FROM instance WHERE uuid = $1;", "describe": { "columns": [ { @@ -42,6 +42,11 @@ "name": "disable_all_traffic", "ordinal": 7, "type_info": "Bool" + }, + { + "name": "enterprise_enabled", + "ordinal": 8, + "type_info": "Bool" } ], "parameters": { @@ -55,8 +60,9 @@ false, false, true, + false, false ] }, - "hash": "8ef98f97d4a36d666b5864a849c9e872a4ff4c82efc80537f357bdaf5523c15a" + "hash": "be25a82798d12394363f3379f2b966d29003dd126f4ac0f8c5236d00891b31c5" } diff --git a/src-tauri/.sqlx/query-9ba230bd827f311aa161a2271402222545b9b45f695b37b10d17b96f505fae6f.json b/src-tauri/.sqlx/query-c688e91a89197793b2a63dcefc41c652fc89286481300450763a7a9b66d146f9.json similarity index 78% rename from src-tauri/.sqlx/query-9ba230bd827f311aa161a2271402222545b9b45f695b37b10d17b96f505fae6f.json rename to src-tauri/.sqlx/query-c688e91a89197793b2a63dcefc41c652fc89286481300450763a7a9b66d146f9.json index 9bb2b9cf..b09e9828 100644 --- a/src-tauri/.sqlx/query-9ba230bd827f311aa161a2271402222545b9b45f695b37b10d17b96f505fae6f.json +++ b/src-tauri/.sqlx/query-c688e91a89197793b2a63dcefc41c652fc89286481300450763a7a9b66d146f9.json @@ -1,6 +1,6 @@ { "db_name": "SQLite", - "query": "SELECT id \"id: _\", name, uuid, url, proxy_url, username, token \"token?\", disable_all_traffic FROM instance WHERE uuid = $1;", + "query": "SELECT id \"id: _\", name, uuid, url, proxy_url, username, token \"token?\", disable_all_traffic, enterprise_enabled FROM instance WHERE id = $1;", "describe": { "columns": [ { @@ -42,6 +42,11 @@ "name": "disable_all_traffic", "ordinal": 7, "type_info": "Bool" + }, + { + "name": "enterprise_enabled", + "ordinal": 8, + "type_info": "Bool" } ], "parameters": { @@ -55,8 +60,9 @@ false, false, true, + false, false ] }, - "hash": "9ba230bd827f311aa161a2271402222545b9b45f695b37b10d17b96f505fae6f" + "hash": "c688e91a89197793b2a63dcefc41c652fc89286481300450763a7a9b66d146f9" } diff --git a/src-tauri/migrations/20240912084922_add_enterprise_enabled_flag.sql b/src-tauri/migrations/20240912084922_add_enterprise_enabled_flag.sql new file mode 100644 index 00000000..71f4566f --- /dev/null +++ b/src-tauri/migrations/20240912084922_add_enterprise_enabled_flag.sql @@ -0,0 +1 @@ +ALTER TABLE instance ADD COLUMN enterprise_enabled BOOLEAN NOT NULL DEFAULT FALSE; diff --git a/src-tauri/proto b/src-tauri/proto index de58067a..8309982b 160000 --- a/src-tauri/proto +++ b/src-tauri/proto @@ -1 +1 @@ -Subproject commit de58067ab652f6e5ddf84c7bbc6e4d2363914738 +Subproject commit 8309982b94e82a7cbe39dd529967f43e49b3ef1d diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index 87f1d156..5ef84a1d 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -1,4 +1,8 @@ -use std::{collections::HashMap, env, str::FromStr}; +use std::{ + collections::{HashMap, HashSet}, + env, + str::FromStr, +}; use chrono::{DateTime, Duration, NaiveDateTime, Utc}; use serde::{Deserialize, Serialize}; @@ -262,6 +266,7 @@ pub async fn all_instances(app_state: State<'_, AppState>) -> Result, + instance: &Instance, + device_config: &DeviceConfigResponse, +) -> Result { + let db_locations: Vec> = + Location::find_by_instance_id(transaction.as_mut(), instance.id) + .await? + .into_iter() + .map(Location::::from) + .collect(); + let db_locations: HashSet> = HashSet::from_iter(db_locations); + let core_locations: Vec> = device_config + .configs + .iter() + .map(|config| device_config_to_location(config.clone(), instance.id)) + .map(Location::::from) + .collect(); + let core_locations: HashSet> = HashSet::from_iter(core_locations); + + Ok(db_locations != core_locations) +} + pub async fn do_update_instance( transaction: &mut Transaction<'_, Sqlite>, instance: &mut Instance, @@ -383,6 +412,7 @@ pub async fn do_update_instance( ) -> Result<(), Error> { // update instance debug!("Updating instance {}({}).", instance.name, instance.id); + let locations_changed = locations_changed(transaction, instance, &response).await?; let instance_info = response .instance .expect("Missing instance info in device config response"); @@ -390,77 +420,108 @@ pub async fn do_update_instance( instance.url = instance_info.url; instance.proxy_url = instance_info.proxy_url; instance.username = instance_info.username; + // Make sure to update the locations too if we are disabling all traffic + if instance.disable_all_traffic != instance_info.disable_all_traffic + && instance_info.disable_all_traffic + { + debug!( + "Disabling all traffic for all locations of instance {}({}).", + instance.name, instance.id + ); + Location::disable_all_traffic_for_all(transaction.as_mut(), instance.id).await?; + debug!( + "Disabled all traffic for all locations of instance {}({}).", + instance.name, instance.id + ); + } + instance.disable_all_traffic = instance_info.disable_all_traffic; + instance.enterprise_enabled = instance_info.enterprise_enabled; instance.save(transaction.as_mut()).await?; - - // process locations received in response debug!( - "Updating locations for instance {}({}).", + "Instance {}({}) main config applied from core's response.", instance.name, instance.id ); - // fetch existing locations for given instance - let mut current_locations = - Location::find_by_instance_id(transaction.as_mut(), instance.id).await?; - for location in response.configs { - // parse device config - let new_location = device_config_to_location(location, instance.id); - // check if location is already present in current locations - if let Some(position) = current_locations - .iter() - .position(|loc| loc.network_id == new_location.network_id) - { - // remove from list of existing locations - let mut current_location = current_locations.remove(position); - debug!( - "Updating existing location {}({}) for instance {}({}).", - current_location.name, current_location.id, instance.name, instance.id, - ); - // update existing location - current_location.name = new_location.name; - current_location.address = new_location.address; - current_location.pubkey = new_location.pubkey; - current_location.endpoint = new_location.endpoint; - current_location.allowed_ips = new_location.allowed_ips; - current_location.mfa_enabled = new_location.mfa_enabled; - current_location.keepalive_interval = new_location.keepalive_interval; - current_location.dns = new_location.dns; - current_location.save(transaction.as_mut()).await?; - info!( - "Location {}({}) updated for instance {}({}).", - current_location.name, current_location.id, instance.name, instance.id, - ); - } else { - // create new location - debug!( - "Creating new location for instance {}({}).", - instance.name, instance.id - ); - let new_location = new_location.save(transaction.as_mut()).await?; - info!( - "New location {}({}) created for instance {}({})", - new_location.name, new_location.id, instance.name, instance.id - ); + // check if locations have changed + if locations_changed { + // process locations received in response + debug!( + "Updating locations for instance {}({}).", + instance.name, instance.id + ); + // fetch existing locations for given instance + let mut current_locations = + Location::find_by_instance_id(transaction.as_mut(), instance.id).await?; + for location in response.configs { + // parse device config + let new_location = device_config_to_location(location, instance.id); + + // check if location is already present in current locations + if let Some(position) = current_locations + .iter() + .position(|loc| loc.network_id == new_location.network_id) + { + // remove from list of existing locations + let mut current_location = current_locations.remove(position); + debug!( + "Updating existing location {}({}) for instance {}({}).", + current_location.name, current_location.id, instance.name, instance.id, + ); + // update existing location + current_location.name = new_location.name; + current_location.address = new_location.address; + current_location.pubkey = new_location.pubkey; + current_location.endpoint = new_location.endpoint; + current_location.allowed_ips = new_location.allowed_ips; + current_location.mfa_enabled = new_location.mfa_enabled; + current_location.keepalive_interval = new_location.keepalive_interval; + current_location.dns = new_location.dns; + current_location.save(transaction.as_mut()).await?; + info!( + "Location {}({}) updated for instance {}({}).", + current_location.name, current_location.id, instance.name, instance.id, + ); + } else { + // create new location + debug!( + "Creating new location for instance {}({}).", + instance.name, instance.id + ); + let new_location = new_location.save(transaction.as_mut()).await?; + info!( + "New location {}({}) created for instance {}({})", + new_location.name, new_location.id, instance.name, instance.id + ); + } } - } - info!( - "Locations updated for instance {}({}).", - instance.name, instance.id - ); + info!( + "Locations updated for instance {}({}).", + instance.name, instance.id + ); - // remove locations which were present in current locations - // but no longer found in core response - debug!( - "Removing locations for instance {}({}).", - instance.name, instance.id - ); - for removed_location in current_locations { - removed_location.delete(transaction.as_mut()).await?; + // remove locations which were present in current locations + // but no longer found in core response + debug!( + "Removing locations for instance {}({}).", + instance.name, instance.id + ); + for removed_location in current_locations { + removed_location.delete(transaction.as_mut()).await?; + } + info!( + "Locations removed for instance {}({}).", + instance.name, instance.id + ); + } else { + info!( + "Locations for instance {}({}) didn't change. Not updating them.", + instance.name, instance.id + ); } info!( - "Locations removed for instance {}({}).", + "Instance {}({}) update is done.", instance.name, instance.id ); - info!("Instance {}({}) updated", instance.name, instance.id); Ok(()) } diff --git a/src-tauri/src/database/models/instance.rs b/src-tauri/src/database/models/instance.rs index bda16d96..3bfbf3c7 100644 --- a/src-tauri/src/database/models/instance.rs +++ b/src-tauri/src/database/models/instance.rs @@ -14,6 +14,7 @@ pub struct Instance { pub username: String, pub token: Option, pub disable_all_traffic: bool, + pub enterprise_enabled: bool, } impl From for Instance { @@ -26,7 +27,8 @@ impl From for Instance { proxy_url: instance_info.proxy_url, username: instance_info.username, token: None, - disable_all_traffic: false, + disable_all_traffic: instance_info.disable_all_traffic, + enterprise_enabled: instance_info.enterprise_enabled, } } } @@ -40,13 +42,14 @@ impl Instance { let proxy_url = self.proxy_url.to_string(); // Update the existing record when there is an ID query!( - "UPDATE instance SET name = $1, uuid = $2, url = $3, proxy_url = $4, username = $5, disable_all_traffic = $6 WHERE id = $7;", + "UPDATE instance SET name = $1, uuid = $2, url = $3, proxy_url = $4, username = $5, disable_all_traffic = $6, enterprise_enabled = $7 WHERE id = $8;", self.name, self.uuid, url, proxy_url, self.username, self.disable_all_traffic, + self.enterprise_enabled, self.id ) .execute(executor) @@ -60,7 +63,7 @@ impl Instance { { let instances = query_as!( Self, - "SELECT id \"id: _\", name, uuid, url, proxy_url, username, token \"token?\", disable_all_traffic FROM instance;" + "SELECT id \"id: _\", name, uuid, url, proxy_url, username, token \"token?\", disable_all_traffic, enterprise_enabled FROM instance;" ) .fetch_all(executor) .await?; @@ -73,7 +76,7 @@ impl Instance { { let instance = query_as!( Self, - "SELECT id \"id: _\", name, uuid, url, proxy_url, username, token \"token?\", disable_all_traffic FROM instance WHERE id = $1;", + "SELECT id \"id: _\", name, uuid, url, proxy_url, username, token \"token?\", disable_all_traffic, enterprise_enabled FROM instance WHERE id = $1;", id ) .fetch_optional(executor) @@ -87,7 +90,7 @@ impl Instance { { let instance = query_as!( Self, - "SELECT id \"id: _\", name, uuid, url, proxy_url, username, token \"token?\", disable_all_traffic FROM instance WHERE uuid = $1;", + "SELECT id \"id: _\", name, uuid, url, proxy_url, username, token \"token?\", disable_all_traffic, enterprise_enabled FROM instance WHERE uuid = $1;", uuid ) .fetch_optional(executor) @@ -113,6 +116,37 @@ impl Instance { Instance::delete_by_id(executor, self.id).await?; Ok(()) } + + pub async fn disable_enterprise_features<'e, E>(&mut self, executor: E) -> Result<(), Error> + where + E: SqliteExecutor<'e>, + { + debug!( + "Disabling enterprise features for instance {}({})", + self.name, self.id + ); + self.enterprise_enabled = false; + self.disable_all_traffic = false; + self.save(executor).await?; + debug!( + "Disabled enterprise features for instance {}({})", + self.name, self.id + ); + Ok(()) + } +} + +// This compares proto::InstanceInfo, not to be confused with regular InstanceInfo defined below +impl PartialEq for Instance { + fn eq(&self, other: &proto::InstanceInfo) -> bool { + self.name == other.name + && self.uuid == other.id + && self.url == other.url + && self.proxy_url == other.proxy_url + && self.username == other.username + && self.disable_all_traffic == other.disable_all_traffic + && self.enterprise_enabled == other.enterprise_enabled + } } impl Instance { @@ -133,6 +167,7 @@ impl Instance { username, token: None, disable_all_traffic: false, + enterprise_enabled: false, } } @@ -143,14 +178,15 @@ impl Instance { let url = self.url.clone(); let proxy_url = self.proxy_url.clone(); let result = query!( - "INSERT INTO instance (name, uuid, url, proxy_url, username, token, disable_all_traffic) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING id;", + "INSERT INTO instance (name, uuid, url, proxy_url, username, token, disable_all_traffic, enterprise_enabled) VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING id;", self.name, self.uuid, url, proxy_url, self.username, self.token, - self.disable_all_traffic + self.disable_all_traffic, + self.enterprise_enabled ) .fetch_one(executor) .await?; @@ -163,6 +199,7 @@ impl Instance { username: self.username, token: self.token, disable_all_traffic: self.disable_all_traffic, + enterprise_enabled: self.enterprise_enabled, }) } } @@ -177,4 +214,5 @@ pub struct InstanceInfo { pub active: bool, pub pubkey: String, pub disable_all_traffic: bool, + pub enterprise_enabled: bool, } diff --git a/src-tauri/src/database/models/location.rs b/src-tauri/src/database/models/location.rs index a78ff09b..2323fa2c 100644 --- a/src-tauri/src/database/models/location.rs +++ b/src-tauri/src/database/models/location.rs @@ -134,6 +134,23 @@ impl Location { .await?; Ok(()) } + + /// Disables all traffic for locations related to the given instance + pub async fn disable_all_traffic_for_all<'e, E>( + executor: E, + instance_id: i64, + ) -> Result<(), Error> + where + E: sqlx::Executor<'e, Database = sqlx::Sqlite>, + { + query!( + "UPDATE location SET route_all_traffic = 0 WHERE instance_id = $1;", + instance_id + ) + .execute(executor) + .await?; + Ok(()) + } } impl Location { diff --git a/src-tauri/src/periodic/config.rs b/src-tauri/src/periodic/config.rs index 6c73d405..fb427068 100644 --- a/src-tauri/src/periodic/config.rs +++ b/src-tauri/src/periodic/config.rs @@ -1,16 +1,14 @@ -use std::{collections::HashSet, str::FromStr, time::Duration}; +use std::{str::FromStr, time::Duration}; +use reqwest::StatusCode; use sqlx::{Sqlite, Transaction}; use tauri::{AppHandle, Manager, State, Url}; use tokio::time::sleep; use crate::{ appstate::AppState, - commands::{device_config_to_location, do_update_instance}, - database::{ - models::{Id, NoId}, - Instance, Location, - }, + commands::{do_update_instance, locations_changed}, + database::{models::Id, Instance}, error::Error, events::{CONFIG_CHANGED, INSTANCE_UPDATE}, proto::{DeviceConfigResponse, InstanceInfoRequest, InstanceInfoResponse}, @@ -103,6 +101,30 @@ pub async fn poll_instance( })?; debug!("InstanceInfoResponse: {response:?}"); + // Return early if the enterprise features are disabled in the core + if response.status() == StatusCode::PAYMENT_REQUIRED { + debug!( + "Instance {}({}) has enterprise features disabled, checking if this state is reflected on our end...", + instance.name, instance.id + ); + if instance.enterprise_enabled { + info!( + "Instance {}({}) has enterprise features disabled, but we have them enabled, disabling...", + instance.name, instance.id + ); + instance + .disable_enterprise_features(transaction.as_mut()) + .await?; + handle.emit_all(INSTANCE_UPDATE, ())?; + } else { + debug!( + "Instance {}({}) has enterprise features disabled, and we have them disabled as well, no action needed", + instance.name, instance.id + ); + } + return Ok(()); + } + // Parse the response let response: InstanceInfoResponse = response.json().await.map_err(|err| { Error::InternalError(format!( @@ -114,22 +136,6 @@ pub async fn poll_instance( .device_config .as_ref() .ok_or_else(|| Error::InternalError("Device config not present in response".to_string()))?; - let instance_config = response.instance_config.as_ref().ok_or_else(|| { - Error::InternalError("Instance config not present in response".to_string()) - })?; - - if instance.disable_all_traffic != instance_config.disable_all_traffic { - debug!( - "Instance {}({}) disable_all_traffic changed from {} to {}", - instance.name, - instance.id, - instance.disable_all_traffic, - instance_config.disable_all_traffic - ); - instance.disable_all_traffic = instance_config.disable_all_traffic; - instance.save(&mut **transaction).await?; - handle.emit_all(INSTANCE_UPDATE, ())?; - } // Early return if config didn't change if !config_changed(transaction, instance, device_config).await? { @@ -174,28 +180,25 @@ pub async fn poll_instance( Ok(()) } -/// Returns true if configuration in instance_info differs from current configuration async fn config_changed( transaction: &mut Transaction<'_, Sqlite>, instance: &Instance, device_config: &DeviceConfigResponse, ) -> Result { - let db_locations: Vec> = - Location::find_by_instance_id(transaction.as_mut(), instance.id) - .await? - .into_iter() - .map(Location::::from) - .collect(); - let db_locations: HashSet> = HashSet::from_iter(db_locations); - let core_locations: Vec> = device_config - .configs - .iter() - .map(|config| device_config_to_location(config.clone(), instance.id)) - .map(Location::::from) - .collect(); - let core_locations: HashSet> = HashSet::from_iter(core_locations); - - Ok(db_locations != core_locations) + debug!( + "Checking if config changed for instance {}({})", + instance.name, instance.id + ); + let locations_changed = locations_changed(transaction, instance, device_config).await?; + let info_changed = match &device_config.instance { + Some(info) => instance != info, + None => false, + }; + debug!( + "Did the locations change: {}. Did the instance information change: {}", + locations_changed, info_changed + ); + Ok(locations_changed || info_changed) } /// Retrieves pubkey & token to build InstanceInfoRequest diff --git a/src/pages/client/pages/ClientInstancePage/components/LocationsList/components/LocationCardRoute/LocationCardRoute.tsx b/src/pages/client/pages/ClientInstancePage/components/LocationsList/components/LocationCardRoute/LocationCardRoute.tsx index c0742c2d..05ca97f6 100644 --- a/src/pages/client/pages/ClientInstancePage/components/LocationsList/components/LocationCardRoute/LocationCardRoute.tsx +++ b/src/pages/client/pages/ClientInstancePage/components/LocationsList/components/LocationCardRoute/LocationCardRoute.tsx @@ -51,7 +51,11 @@ export const LocationCardRoute = ({ location, selectedDefguardInstance }: Props) { if (!location?.active) {