mirror of
https://mirror.suhoan.cn/https://github.com/EasyTier/EasyTier.git
synced 2025-12-15 14:17:24 +08:00
refactor(rpc): Centralize RPC service and unify API (#1427)
This change introduces a major refactoring of the RPC service layer to improve modularity, unify the API, and simplify the overall architecture. Key changes: - Replaced per-network-instance RPC services with a single global RPC server, reducing resource usage and simplifying management. - All clients (CLI, Web UI, etc.) now interact with EasyTier core through a unified RPC entrypoint, enabling consistent authentication and control. - RPC implementation logic has been moved to `easytier/src/rpc_service/` and organized by functionality (e.g., `instance_manage.rs`, `peer_manage.rs`, `config.rs`) for better maintainability. - Standardized Protobuf API definitions under `easytier/src/proto/` with an `api_` prefix (e.g., `cli.proto` → `api_instance.proto`) to provide a consistent interface. - CLI commands now require explicit `--instance-id` or `--instance-name` when multiple network instances are running; the parameter is optional when only one instance exists. BREAKING CHANGE: RPC portal configuration (`rpc_portal` and `rpc_portal_whitelist`) has been removed from per-instance configs and the Web UI. The RPC listen address must now be specified globally via the `--rpc-portal` command-line flag or the `ET_RPC_PORTAL` environment variable, as there is only one RPC service for the entire application.
This commit is contained in:
@@ -35,7 +35,7 @@ use crate::{
|
||||
use_global_var,
|
||||
};
|
||||
|
||||
use crate::proto::cli::PeerConnInfo;
|
||||
use crate::proto::api::instance::PeerConnInfo;
|
||||
use anyhow::Context;
|
||||
use rand::Rng;
|
||||
use tokio::{net::UdpSocket, task::JoinSet, time::timeout};
|
||||
|
||||
@@ -18,12 +18,14 @@ use crate::{
|
||||
common::{dns::socket_addrs, join_joinset_background, PeerId},
|
||||
peers::peer_conn::PeerConnId,
|
||||
proto::{
|
||||
cli::{
|
||||
ConnectorManageAction, ListConnectorResponse, ManageConnectorResponse, PeerConnInfo,
|
||||
api::instance::{
|
||||
Connector, ConnectorManageRpc, ConnectorStatus, ListConnectorRequest,
|
||||
ListConnectorResponse, PeerConnInfo,
|
||||
},
|
||||
rpc_types::{self, controller::BaseController},
|
||||
},
|
||||
tunnel::{IpVersion, TunnelConnector},
|
||||
utils::weak_upgrade,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
@@ -33,10 +35,6 @@ use crate::{
|
||||
netns::NetNS,
|
||||
},
|
||||
peers::peer_manager::PeerManager,
|
||||
proto::cli::{
|
||||
Connector, ConnectorManageRpc, ConnectorStatus, ListConnectorRequest,
|
||||
ManageConnectorRequest,
|
||||
},
|
||||
use_global_var,
|
||||
};
|
||||
|
||||
@@ -126,6 +124,14 @@ impl ManualConnectorManager {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn clear_connectors(&self) {
|
||||
self.list_connectors().await.iter().for_each(|x| {
|
||||
if let Some(url) = &x.url {
|
||||
self.data.removed_conn_urls.insert(url.to_string());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub async fn list_connectors(&self) -> Vec<Connector> {
|
||||
let conn_urls: BTreeSet<String> = self
|
||||
.data
|
||||
@@ -421,7 +427,7 @@ impl ManualConnectorManager {
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ConnectorManagerRpcService(pub Arc<ManualConnectorManager>);
|
||||
pub struct ConnectorManagerRpcService(pub Weak<ManualConnectorManager>);
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl ConnectorManageRpc for ConnectorManagerRpcService {
|
||||
@@ -433,31 +439,10 @@ impl ConnectorManageRpc for ConnectorManagerRpcService {
|
||||
_request: ListConnectorRequest,
|
||||
) -> Result<ListConnectorResponse, rpc_types::error::Error> {
|
||||
let mut ret = ListConnectorResponse::default();
|
||||
let connectors = self.0.list_connectors().await;
|
||||
let connectors = weak_upgrade(&self.0)?.list_connectors().await;
|
||||
ret.connectors = connectors;
|
||||
Ok(ret)
|
||||
}
|
||||
|
||||
async fn manage_connector(
|
||||
&self,
|
||||
_: BaseController,
|
||||
req: ManageConnectorRequest,
|
||||
) -> Result<ManageConnectorResponse, rpc_types::error::Error> {
|
||||
let url: url::Url = req.url.ok_or(anyhow::anyhow!("url is empty"))?.into();
|
||||
if req.action == ConnectorManageAction::Remove as i32 {
|
||||
self.0
|
||||
.remove_connector(url.clone())
|
||||
.await
|
||||
.with_context(|| format!("remove connector failed: {:?}", url))?;
|
||||
return Ok(ManageConnectorResponse::default());
|
||||
} else {
|
||||
self.0
|
||||
.add_connector_by_url(url.as_str())
|
||||
.await
|
||||
.with_context(|| format!("add connector failed: {:?}", url))?;
|
||||
}
|
||||
Ok(ManageConnectorResponse::default())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in New Issue
Block a user