mirror of
https://github.com/EasyTier/EasyTier.git
synced 2026-05-15 10:25:40 +00:00
refactor(web): Refactor web logic to extract reusable remote client management module (#1465)
This commit is contained in:
@@ -7,13 +7,19 @@ use std::sync::{
|
||||
};
|
||||
|
||||
use dashmap::DashMap;
|
||||
use easytier::{proto::web::HeartbeatRequest, tunnel::TunnelListener};
|
||||
use easytier::{
|
||||
proto::{
|
||||
api::manage::WebClientService, rpc_types::controller::BaseController, web::HeartbeatRequest,
|
||||
},
|
||||
rpc_service::remote_client::{self, RemoteClientManager},
|
||||
tunnel::TunnelListener,
|
||||
};
|
||||
use maxminddb::geoip2;
|
||||
use session::{Location, Session};
|
||||
use storage::{Storage, StorageToken};
|
||||
use tokio::task::JoinSet;
|
||||
|
||||
use crate::db::{Db, UserIdInDb};
|
||||
use crate::db::{entity::user_running_network_configs, Db, UserIdInDb};
|
||||
|
||||
#[derive(rust_embed::Embed)]
|
||||
#[folder = "resources/"]
|
||||
@@ -152,7 +158,7 @@ impl ClientManager {
|
||||
s.data().read().await.location().cloned()
|
||||
}
|
||||
|
||||
pub fn db(&self) -> &Db {
|
||||
fn db(&self) -> &Db {
|
||||
self.storage.db()
|
||||
}
|
||||
|
||||
@@ -245,6 +251,32 @@ impl ClientManager {
|
||||
}
|
||||
}
|
||||
|
||||
impl
|
||||
RemoteClientManager<
|
||||
(UserIdInDb, uuid::Uuid),
|
||||
user_running_network_configs::Model,
|
||||
sea_orm::DbErr,
|
||||
> for ClientManager
|
||||
{
|
||||
fn get_rpc_client(
|
||||
&self,
|
||||
(user_id, machine_id): (UserIdInDb, uuid::Uuid),
|
||||
) -> Option<Box<dyn WebClientService<Controller = BaseController> + Send>> {
|
||||
let s = self.get_session_by_machine_id(user_id, &machine_id)?;
|
||||
Some(s.scoped_rpc_client())
|
||||
}
|
||||
|
||||
fn get_storage(
|
||||
&self,
|
||||
) -> &impl remote_client::Storage<
|
||||
(UserIdInDb, uuid::Uuid),
|
||||
user_running_network_configs::Model,
|
||||
sea_orm::DbErr,
|
||||
> {
|
||||
self.storage.db()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::{sync::Arc, time::Duration};
|
||||
|
||||
@@ -12,12 +12,11 @@ use easytier::{
|
||||
rpc_types::{self, controller::BaseController},
|
||||
web::{HeartbeatRequest, HeartbeatResponse, WebServerService, WebServerServiceServer},
|
||||
},
|
||||
rpc_service::remote_client::{ListNetworkProps, Storage as _},
|
||||
tunnel::Tunnel,
|
||||
};
|
||||
use tokio::sync::{broadcast, RwLock};
|
||||
|
||||
use crate::db::ListNetworkProps;
|
||||
|
||||
use super::storage::{Storage, StorageToken, WeakRefStorage};
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
@@ -224,10 +223,10 @@ impl Session {
|
||||
}
|
||||
|
||||
let req = req.unwrap();
|
||||
if req.machine_id.is_none() {
|
||||
let Some(machine_id) = req.machine_id else {
|
||||
tracing::warn!(?req, "Machine id is not set, ignore");
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
let running_inst_ids = req
|
||||
.running_network_instances
|
||||
@@ -257,11 +256,7 @@ impl Session {
|
||||
|
||||
let local_configs = match storage
|
||||
.db
|
||||
.list_network_configs(
|
||||
user_id,
|
||||
Some(req.machine_id.unwrap().into()),
|
||||
ListNetworkProps::EnabledOnly,
|
||||
)
|
||||
.list_network_configs((user_id, machine_id.into()), ListNetworkProps::EnabledOnly)
|
||||
.await
|
||||
{
|
||||
Ok(configs) => configs,
|
||||
|
||||
Reference in New Issue
Block a user