refactor(core): separate portable core from native runtime (#2451)

Create easytier-core as the portable owner of configuration,
connectivity, tunnels, peer and routing state, gateways, management,
the data plane, and instance lifecycle. Keep operating-system
integration, native protocol engines, process startup, and presentation
in easytier behind explicit Host capability adapters.

Create easytier-proto to own schemas, generated RPC types, descriptors,
and feature-scoped protocol slices. Remove runtime protobuf reflection
from core while preserving unknown route-peer fields across forwarding.

Normalize instance construction through CoreInstance, CoreHostAdapters,
CoreProcessRuntime, and InstanceManager. Make the runtime config store
the only authoritative mutable configuration after startup.

Move the portable TCP/UDP data plane into core and extract a generic
OperationBroker for completion, cancellation, disposal, and capacity
accounting. Expose the session-based FFI v2 completion API and keep the
WASI guest ABI, wire schemas, and adapters with core.

Migrate CLI, GUI, web, FFI, Android JNI, OHOS, uptime, and mobile
consumers to the shared manager and core state. Add explicit user/web
config ownership and revision-aware web reconciliation.

Preserve configuration, wire, and management behavior while fixing
regressions discovered by the full platform and integration matrix:

- inherit advertised relay capabilities in foreign networks;
- refresh OSPF peer state immediately after runtime config changes;
- restore CLI GlobalCtx event output without forcing GUI logging;
- retain legacy encryption names and standalone RPC tunnel metadata;
- restore ICMP host composition and fragmented UDP handling;
- use portable 64-bit atomics on 32-bit MIPS targets; and
- retain discarded operations until late cancellation completes.

Validate the refactor across 45 GitHub checks, including Linux, macOS,
Windows, FreeBSD, web, GUI, Android, OHOS, feature profiles, and
three-node and subnet-proxy integration tests.

BREAKING CHANGE: internal Rust module paths are not preserved. Legacy
native data-plane APIs are replaced by the session-based FFI v2 API.
The dedicated Android data-plane wrapper is removed.
This commit is contained in:
KKRainbow
2026-07-26 15:41:55 +08:00
committed by GitHub
parent 346f32d3d0
commit 021f523431
523 changed files with 102785 additions and 67067 deletions
+1
View File
@@ -25,6 +25,7 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1"
easytier = { path = "../../easytier" }
easytier-core = { path = "../../easytier-core" }
tokio = { version = "1", features = ["full"] }
anyhow = "1.0"
chrono = { version = "0.4.37", features = ["serde"] }
+62 -57
View File
@@ -4,30 +4,35 @@
mod elevate;
use anyhow::Context;
#[cfg(target_os = "android")]
use easytier::instance::factory::subscribe_native_instance_event;
use easytier::proto::api::manage::{
CollectNetworkInfoResponse, ValidateConfigResponse, WebClientService,
WebClientServiceClientFactory,
};
use easytier::rpc_service::remote_client::{
GetNetworkMetasResponse, ListNetworkInstanceIdsJsonResp, ListNetworkProps, RemoteClientManager,
Storage,
};
use easytier::web_client::{self, WebClient};
use easytier::{
common::config::{NetworkConfig, NetworkConfigExt},
common::{
config::{
ConfigLoader, ConfigSource, FileLoggerConfig, LoggingConfigBuilder, TomlConfigLoader,
},
log,
},
instance_manager::NetworkInstanceManager,
launcher::NetworkConfig,
instance::factory::{NativeInstanceManager, native_instance_manager},
proto::rpc::standalone::{runtime_rpc_dialer, runtime_rpc_listener},
rpc_service::ApiRpcServer,
tunnel::TunnelListener,
tunnel::ring::RingTunnelListener,
tunnel::tcp::TcpTunnelListener,
utils::panic::setup_panic_handler,
};
use easytier_core::management::config_source_to_rpc;
use easytier_core::management::remote_client::{
GetNetworkMetasResponse, ListNetworkInstanceIdsJsonResp, ListNetworkProps, RemoteClientManager,
Storage,
};
use easytier_core::{
connectivity::protocol::raw::TunnelDialer as _, process_runtime::CoreProcessRuntime,
socket::SocketListener, tunnel::Tunnel,
};
use std::ops::Deref;
use std::sync::Arc;
use tokio::sync::{Mutex, RwLock, RwLockReadGuard};
@@ -38,7 +43,7 @@ use tauri::{AppHandle, Emitter, Manager as _};
#[cfg(not(target_os = "android"))]
use tauri::tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent};
static INSTANCE_MANAGER: once_cell::sync::Lazy<RwLock<Option<Arc<NetworkInstanceManager>>>> =
static INSTANCE_MANAGER: once_cell::sync::Lazy<RwLock<Option<Arc<NativeInstanceManager>>>> =
once_cell::sync::Lazy::new(|| RwLock::new(None));
static RPC_RING_UUID: once_cell::sync::Lazy<uuid::Uuid> =
@@ -47,7 +52,7 @@ static RPC_RING_UUID: once_cell::sync::Lazy<uuid::Uuid> =
static CLIENT_MANAGER: once_cell::sync::Lazy<RwLock<Option<manager::GUIClientManager>>> =
once_cell::sync::Lazy::new(|| RwLock::new(None));
type BoxedTunnelListener = Box<dyn TunnelListener>;
type BoxedTunnelListener = Box<dyn SocketListener<Accepted = Box<dyn Tunnel>>>;
#[derive(Clone, Copy, PartialEq, Eq)]
enum RpcServerKind {
@@ -165,7 +170,7 @@ async fn set_tun_fd(fd: i32) -> Result<(), String> {
.next()
{
instance_manager
.set_tun_fd(&uuid, fd)
.attach_tun_fd(uuid, fd)
.map_err(|e| e.to_string())?;
}
Ok(())
@@ -372,6 +377,21 @@ fn normalize_normal_mode_rpc_portal(portal: &str) -> Result<(url::Url, url::Url)
Ok((bind_url, connect_url))
}
async fn resolve_rpc_bind_url(url: &url::Url) -> Result<std::net::SocketAddr, String> {
if url.scheme() != "tcp" {
return Err(format!("RPC portal requires tcp URL: {url}"));
}
let host = url
.host_str()
.ok_or_else(|| format!("RPC portal has no host: {url}"))?;
let port = url.port().unwrap_or(11010);
tokio::net::lookup_host((host, port))
.await
.map_err(|error| format!("failed to resolve RPC portal {url}: {error}"))?
.next()
.ok_or_else(|| format!("RPC portal has no resolved address: {url}"))
}
#[tauri::command]
async fn init_rpc_connection(
_app: AppHandle,
@@ -390,11 +410,12 @@ async fn init_rpc_connection(
.map_err(|_| "Failed to acquire lock for rpc server")?;
let mut client_url = url.clone();
let mut local_process_runtime = None;
if is_normal_mode {
let instance_manager = if let Some(im) = instance_manager_guard.take() {
im
} else {
Arc::new(NetworkInstanceManager::new())
Arc::new(native_instance_manager())
};
let portal = url.and_then(|s| {
@@ -422,12 +443,14 @@ async fn init_rpc_connection(
*rpc_server_guard = None;
let tunnel: BoxedTunnelListener = match desired_kind {
RpcServerKind::Ring => Box::new(RingTunnelListener::new(
format!("ring://{}", RPC_RING_UUID.deref()).parse().unwrap(),
)),
RpcServerKind::Tcp => Box::new(TcpTunnelListener::new(
bind_url.clone().expect("tcp rpc must have bind url"),
)),
RpcServerKind::Ring => instance_manager
.process_runtime()
.bind_ring_tunnel(*RPC_RING_UUID.deref())
.map_err(|error| error.to_string())?,
RpcServerKind::Tcp => {
let bind_url = bind_url.as_ref().expect("tcp rpc must have bind url");
Box::new(runtime_rpc_listener(resolve_rpc_bind_url(bind_url).await?))
}
};
let rpc_server = ApiRpcServer::from_tunnel(tunnel, instance_manager.clone())
@@ -442,6 +465,7 @@ async fn init_rpc_connection(
});
}
local_process_runtime = Some(instance_manager.process_runtime());
*instance_manager_guard = Some(instance_manager);
client_url = connect_url.map(|u| u.to_string());
} else {
@@ -450,7 +474,7 @@ async fn init_rpc_connection(
let client_manager = tokio::time::timeout(
std::time::Duration::from_millis(1000),
manager::GUIClientManager::new(client_url),
manager::GUIClientManager::new(client_url, local_process_runtime),
)
.await
.map_err(|_| "connect remote rpc timed out".to_string())?
@@ -462,7 +486,8 @@ async fn init_rpc_connection(
drop(WEB_CLIENT.write().await.take());
if let Some(instance_manager) = instance_manager_guard.take() {
instance_manager
.retain_network_instance(vec![])
.retain_network_instances(&[])
.await
.map_err(|e| e.to_string())?;
drop(instance_manager);
}
@@ -600,19 +625,13 @@ mod manager {
use super::*;
use async_trait::async_trait;
use dashmap::{DashMap, DashSet};
use easytier::common::global_ctx::GlobalCtx;
use easytier::common::stun::MockStunInfoCollector;
use easytier::launcher::NetworkConfig;
use easytier::common::config::{NetworkConfig, NetworkConfigExt};
use easytier::proto::api::logger::{LoggerRpc, LoggerRpcClientFactory, SetLoggerConfigRequest};
use easytier::proto::api::manage::RunNetworkInstanceRequest;
use easytier::proto::common::NatType;
use easytier::proto::rpc_impl::bidirect::BidirectRpcManager;
use easytier::proto::rpc::bidirect::BidirectRpcManager;
use easytier::proto::rpc_types::controller::BaseController;
use easytier::rpc_service::logger::LoggerRpcService;
use easytier::rpc_service::remote_client::PersistentConfig;
use easytier::tunnel::TunnelConnector;
use easytier::tunnel::ring::RingTunnelConnector;
use easytier::web_client::WebClientHooks;
use easytier_core::management::remote_client::PersistentConfig;
pub(super) struct GuiHooks {
pub(super) app: AppHandle,
@@ -877,27 +896,16 @@ mod manager {
pub(super) rpc_manager: BidirectRpcManager,
}
impl GUIClientManager {
pub async fn new(rpc_url: Option<String>) -> Result<Self, anyhow::Error> {
let global_ctx = Arc::new(GlobalCtx::new(TomlConfigLoader::default()));
global_ctx.replace_stun_info_collector(Box::new(MockStunInfoCollector {
udp_nat_type: NatType::Unknown,
}));
let mut flags = global_ctx.get_flags();
flags.bind_device = false;
global_ctx.set_flags(flags);
pub async fn new(
rpc_url: Option<String>,
local_process_runtime: Option<Arc<CoreProcessRuntime>>,
) -> Result<Self, anyhow::Error> {
let tunnel = if let Some(url) = rpc_url {
let mut connector = easytier::connector::create_connector_by_url(
&url,
&global_ctx,
easytier::tunnel::IpVersion::Both,
)
.await?;
connector.connect().await?
runtime_rpc_dialer(url.parse()?).connect().await?
} else {
let mut connector = RingTunnelConnector::new(
format!("ring://{}", RPC_RING_UUID.deref()).parse().unwrap(),
);
connector.connect().await?
local_process_runtime
.context("local RPC requires a core process runtime")?
.connect_ring_tunnel(*RPC_RING_UUID.deref())?
};
let rpc_manager = BidirectRpcManager::new();
@@ -936,7 +944,7 @@ mod manager {
&self,
app: &AppHandle,
web_only: bool,
) -> Result<(), easytier::rpc_service::remote_client::RemoteClientError<anyhow::Error>>
) -> Result<(), easytier_core::management::remote_client::RemoteClientError<anyhow::Error>>
{
let inst_ids: Vec<uuid::Uuid> = if web_only {
self.get_enabled_instances_with_web_like_tun_ids().collect()
@@ -1011,11 +1019,8 @@ mod manager {
#[cfg(target_os = "android")]
if let Some(instance_manager) = super::INSTANCE_MANAGER.read().await.as_ref() {
let instance_uuid = *instance_id;
if let Some(instance_ref) = instance_manager
.iter()
.find(|item| *item.key() == instance_uuid)
{
if let Some(mut event_receiver) = instance_ref.value().subscribe_event() {
if let Some(instance) = instance_manager.instance(instance_uuid) {
if let Some(mut event_receiver) = subscribe_native_instance_event(&instance) {
let app_clone = app.clone();
let instance_id_clone = *instance_id;
tokio::spawn(async move {
@@ -1090,7 +1095,7 @@ mod manager {
.set_logger_config(
BaseController::default(),
SetLoggerConfigRequest {
level: LoggerRpcService::string_to_log_level(&level).into(),
level: easytier_core::management::parse_log_level(&level).into(),
},
)
.await?;
@@ -1139,7 +1144,7 @@ mod manager {
inst_id: None,
config: Some(config),
overwrite: false,
source: source.to_runtime_source().to_rpc(),
source: config_source_to_rpc(source.to_runtime_source()),
},
)
.await?;
+2
View File
@@ -52,6 +52,7 @@ declare global {
const mapWritableState: typeof import('pinia')['mapWritableState']
const markRaw: typeof import('vue')['markRaw']
const nextTick: typeof import('vue')['nextTick']
const normalizeConfigSource: typeof import('./composables/config_source')['normalizeConfigSource']
const onActivated: typeof import('vue')['onActivated']
const onBeforeMount: typeof import('vue')['onBeforeMount']
const onBeforeRouteLeave: typeof import('vue-router')['onBeforeRouteLeave']
@@ -177,6 +178,7 @@ declare module 'vue' {
readonly mapWritableState: UnwrapRef<typeof import('pinia')['mapWritableState']>
readonly markRaw: UnwrapRef<typeof import('vue')['markRaw']>
readonly nextTick: UnwrapRef<typeof import('vue')['nextTick']>
readonly normalizeConfigSource: UnwrapRef<typeof import('./composables/config_source')['normalizeConfigSource']>
readonly onActivated: UnwrapRef<typeof import('vue')['onActivated']>
readonly onBeforeMount: UnwrapRef<typeof import('vue')['onBeforeMount']>
readonly onBeforeRouteLeave: UnwrapRef<typeof import('vue-router')['onBeforeRouteLeave']>