Skip to content

Commit 2f9ac4b

Browse files
committed
review changes
1 parent 1160805 commit 2f9ac4b

7 files changed

Lines changed: 134 additions & 186 deletions

File tree

src-tauri/src/active_connections.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub(crate) async fn find_connection(
8282
pub(crate) async fn active_connections(
8383
instance: &Instance<Id>,
8484
) -> Result<Vec<ActiveConnection>, Error> {
85-
let locations: HashSet<Id> = Location::find_by_instance_id(&*DB_POOL, instance.id)
85+
let locations: HashSet<Id> = Location::find_by_instance_id(&*DB_POOL, instance.id, false)
8686
.await?
8787
.iter()
8888
.map(|location| location.id)

src-tauri/src/commands.rs

Lines changed: 19 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ use crate::{
3939
proto::DeviceConfigResponse,
4040
service::{
4141
proto::{
42-
DeleteServiceLocationsRequest, RemoveInterfaceRequest, ResetServiceLocationRequest,
43-
SaveServiceLocationsRequest, ServiceLocation,
42+
DeleteServiceLocationsRequest, RemoveInterfaceRequest, SaveServiceLocationsRequest,
43+
ServiceLocation,
4444
},
4545
utils::DAEMON_CLIENT,
4646
},
@@ -290,7 +290,7 @@ pub async fn save_device_config(
290290
transaction.commit().await?;
291291
info!("New instance {instance} created.");
292292
trace!("Created following instance: {instance:#?}");
293-
let locations = Location::find_by_instance_id(&*DB_POOL, instance.id).await?;
293+
let locations = Location::find_by_instance_id(&*DB_POOL, instance.id, true).await?;
294294
trace!("Created following locations: {locations:#?}");
295295

296296
let mut service_locations = Vec::<ServiceLocation>::new();
@@ -332,36 +332,6 @@ pub async fn save_device_config(
332332
"Saved service locations to the daemon for instance {}({}).",
333333
instance.name, instance.id,
334334
);
335-
336-
let locations_pubkeys = service_locations
337-
.iter()
338-
.map(|loc| loc.pubkey.clone())
339-
.collect::<HashSet<String>>();
340-
341-
for location_pubkey in locations_pubkeys {
342-
let restart_request = ResetServiceLocationRequest {
343-
instance_id: instance.uuid.clone(),
344-
pubkey: location_pubkey.clone(),
345-
};
346-
debug!(
347-
"Restarting service location with pubkey {} on instance {}.",
348-
restart_request.pubkey, restart_request.instance_id,
349-
);
350-
DAEMON_CLIENT.clone()
351-
.reset_service_location(restart_request)
352-
.await
353-
.map_err(|err| {
354-
error!(
355-
"Error while restarting service location with pubkey {} on instance {}: {err}",
356-
location_pubkey, instance.uuid,
357-
);
358-
Error::InternalError(err.to_string())
359-
})?;
360-
debug!(
361-
"Restarted service location with pubkey {} on instance {}.",
362-
location_pubkey, instance.uuid
363-
);
364-
}
365335
}
366336

367337
handle.emit(EventKey::InstanceUpdate.into(), ())?;
@@ -386,7 +356,7 @@ pub async fn all_instances() -> Result<Vec<InstanceInfo<Id>>, Error> {
386356
let mut instance_info = Vec::new();
387357
let connection_ids = get_connection_id_by_type(ConnectionType::Location).await;
388358
for instance in instances {
389-
let locations = Location::find_by_instance_id(&*DB_POOL, instance.id).await?;
359+
let locations = Location::find_by_instance_id(&*DB_POOL, instance.id, false).await?;
390360
let location_ids: Vec<i64> = locations.iter().map(|location| location.id).collect();
391361
let connected = connection_ids
392362
.iter()
@@ -460,24 +430,14 @@ pub async fn all_locations(instance_id: Id) -> Result<Vec<LocationInfo>, Error>
460430
"Getting information about all locations for instance {}.",
461431
instance.name
462432
);
463-
let locations = Location::find_by_instance_id(&*DB_POOL, instance_id).await?;
433+
let locations = Location::find_by_instance_id(&*DB_POOL, instance_id, false).await?;
464434
trace!(
465435
"Found {} locations for instance {instance} to return information about.",
466436
locations.len()
467437
);
468438
let active_locations_ids = get_connection_id_by_type(ConnectionType::Location).await;
469439
let mut location_info = Vec::new();
470440
for location in locations {
471-
// Skip service locations, those shouldn't be shown in the UI.
472-
if location.is_service_location() {
473-
debug!(
474-
"Skipping service location {}({}) for instance {}({}) when returning \
475-
locations to the frontend.",
476-
location.name, location.id, instance.name, instance.id,
477-
);
478-
continue;
479-
}
480-
481441
let info = LocationInfo {
482442
id: location.id,
483443
instance_id: location.instance_id,
@@ -560,7 +520,7 @@ pub(crate) async fn locations_changed(
560520
device_config: &DeviceConfigResponse,
561521
) -> Result<bool, Error> {
562522
let db_locations: HashSet<Location<NoId>> =
563-
Location::find_by_instance_id(transaction.as_mut(), instance.id)
523+
Location::find_by_instance_id(transaction.as_mut(), instance.id, true)
564524
.await?
565525
.into_iter()
566526
.map(|location| {
@@ -633,14 +593,13 @@ pub(crate) async fn do_update_instance(
633593
);
634594
// fetch existing locations for given instance
635595
let mut current_locations =
636-
Location::find_by_instance_id(transaction.as_mut(), instance.id).await?;
596+
Location::find_by_instance_id(transaction.as_mut(), instance.id, true).await?;
637597
for dev_config in response.configs {
638598
// parse device config
639599
let new_location = dev_config.into_location(instance.id);
640-
let saved_location: Location<Id>;
641600

642601
// check if location is already present in current locations
643-
if let Some(position) = current_locations
602+
let saved_location = if let Some(position) = current_locations
644603
.iter()
645604
.position(|loc| loc.network_id == new_location.network_id)
646605
{
@@ -662,14 +621,14 @@ pub(crate) async fn do_update_instance(
662621
current_location.service_location_mode = new_location.service_location_mode;
663622
current_location.save(transaction.as_mut()).await?;
664623
info!("Location {current_location} configuration updated for instance {instance}");
665-
saved_location = current_location;
624+
current_location
666625
} else {
667626
// create new location
668627
debug!("Creating new location {new_location} for instance instance {instance}");
669628
let new_location = new_location.save(transaction.as_mut()).await?;
670629
info!("New location {new_location} created for instance {instance}");
671-
saved_location = new_location;
672-
}
630+
new_location
631+
};
673632

674633
if saved_location.is_service_location() {
675634
debug!(
@@ -699,7 +658,12 @@ pub(crate) async fn do_update_instance(
699658
.ok_or(Error::NotFound)?
700659
.prvkey;
701660

702-
if !service_locations.is_empty() {
661+
if service_locations.is_empty() {
662+
debug!(
663+
"No service locations to process for instance {}({})",
664+
instance.name, instance.id
665+
);
666+
} else {
703667
debug!(
704668
"Processing {} service location(s) for instance {}({})",
705669
service_locations.len(),
@@ -739,49 +703,10 @@ pub(crate) async fn do_update_instance(
739703
instance.id
740704
);
741705

742-
let service_locations_pubkeys = service_locations
743-
.iter()
744-
.map(|loc| loc.pubkey.clone())
745-
.collect::<HashSet<String>>();
746-
747-
let instance_id = instance.uuid.clone();
748-
749-
for pubkey in service_locations_pubkeys {
750-
debug!(
751-
"Sending state reset request for service location with pubkey {} on instance {}",
752-
pubkey, instance_id
753-
);
754-
755-
DAEMON_CLIENT
756-
.clone()
757-
.reset_service_location(ResetServiceLocationRequest {
758-
instance_id: instance_id.clone(),
759-
pubkey: pubkey.clone(),
760-
})
761-
.await
762-
.map_err(|err| {
763-
error!(
764-
"Error while restarting service location with pubkey {} on instance {}: {err}",
765-
pubkey, instance_id,
766-
);
767-
Error::InternalError(err.to_string())
768-
})?;
769-
770-
info!(
771-
"Successfully reset the state of service location with pubkey {} on instance {}",
772-
pubkey, instance_id
773-
);
774-
}
775-
776706
debug!(
777707
"Completed processing all service locations for instance {}({})",
778708
instance.name, instance.id
779709
);
780-
} else {
781-
debug!(
782-
"No service locations to process for instance {}({})",
783-
instance.name, instance.id
784-
);
785710
}
786711

787712
Ok(())
@@ -1007,7 +932,8 @@ pub async fn delete_instance(instance_id: Id, handle: AppHandle) -> Result<(), E
1007932
};
1008933
debug!("The instance that is being deleted has been identified as {instance}");
1009934

1010-
let instance_locations = Location::find_by_instance_id(&mut *transaction, instance_id).await?;
935+
let instance_locations =
936+
Location::find_by_instance_id(&mut *transaction, instance_id, false).await?;
1011937
if !instance_locations.is_empty() {
1012938
debug!(
1013939
"Found locations associated with the instance {instance}, closing their connections."

src-tauri/src/database/models/location.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,24 @@ impl fmt::Display for Location<NoId> {
8484
}
8585

8686
impl Location<Id> {
87+
/// Ignores service locations
8788
#[cfg(windows)]
88-
pub(crate) async fn all<'e, E>(executor: E) -> Result<Vec<Self>, SqlxError>
89+
pub(crate) async fn all<'e, E>(
90+
executor: E,
91+
include_service_locations: bool,
92+
) -> Result<Vec<Self>, SqlxError>
8993
where
9094
E: SqliteExecutor<'e>,
9195
{
96+
let max_mode = if include_service_locations { 2 } else { 0 }; // 0 to exclude service locations, 2 to include them
9297
query_as!(
9398
Self,
94-
"SELECT id, instance_id, name, address, pubkey, endpoint, allowed_ips, dns, network_id,\
95-
route_all_traffic, keepalive_interval, \
96-
location_mfa_mode \"location_mfa_mode: LocationMfaMode\", service_location_mode \"service_location_mode: ServiceLocationMode\" \
97-
FROM location ORDER BY name ASC;"
99+
"SELECT id, instance_id, name, address, pubkey, endpoint, allowed_ips, dns, network_id,\
100+
route_all_traffic, keepalive_interval, \
101+
location_mfa_mode \"location_mfa_mode: LocationMfaMode\", service_location_mode \"service_location_mode: ServiceLocationMode\" \
102+
FROM location WHERE service_location_mode <= $1 \
103+
ORDER BY name ASC;",
104+
max_mode
98105
)
99106
.fetch_all(executor)
100107
.await
@@ -151,16 +158,20 @@ impl Location<Id> {
151158
pub(crate) async fn find_by_instance_id<'e, E>(
152159
executor: E,
153160
instance_id: Id,
161+
include_service_locations: bool,
154162
) -> Result<Vec<Self>, SqlxError>
155163
where
156164
E: SqliteExecutor<'e>,
157165
{
166+
let max_mode = if include_service_locations { 2 } else { 0 }; // 0 to exclude service locations, 2 to include them
158167
query_as!(
159168
Self,
160169
"SELECT id \"id: _\", instance_id, name, address, pubkey, endpoint, allowed_ips, dns, \
161170
network_id, route_all_traffic, keepalive_interval, location_mfa_mode \"location_mfa_mode: LocationMfaMode\", service_location_mode \"service_location_mode: ServiceLocationMode\" \
162-
FROM location WHERE instance_id = $1 ORDER BY name ASC",
163-
instance_id
171+
FROM location WHERE instance_id = $1 AND service_location_mode <= $2 \
172+
ORDER BY name ASC",
173+
instance_id,
174+
max_mode
164175
)
165176
.fetch_all(executor)
166177
.await

src-tauri/src/enterprise/service_locations/windows.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -700,12 +700,11 @@ impl ServiceLocationManager {
700700
location.name
701701
);
702702
continue;
703-
} else {
704-
debug!(
703+
}
704+
debug!(
705705
"Proceeding to connect pre-logon service location '{}' because no user is logged in",
706706
location.name
707707
);
708-
}
709708
}
710709

711710
if self.is_service_location_connected(&instance_data.instance_id, &location.pubkey)
@@ -748,6 +747,7 @@ impl ServiceLocationManager {
748747
}
749748

750749
pub fn save_service_locations(
750+
&self,
751751
service_locations: &[ServiceLocation],
752752
instance_id: &str,
753753
private_key: &str,

0 commit comments

Comments
 (0)