mirror of
https://github.com/EasyTier/EasyTier.git
synced 2026-05-06 17:59:11 +00:00
allow open rpc port in gui normal mode (#1795)
* allow open rpc port for gui normal mode * downgrade dev tool console
This commit is contained in:
@@ -54,7 +54,7 @@
|
||||
"unplugin-vue-router": "^0.10.8",
|
||||
"uuid": "^10.0.0",
|
||||
"vite": "^5.4.8",
|
||||
"vite-plugin-vue-devtools": "^8.0.5",
|
||||
"vite-plugin-vue-devtools": "^7.4.6",
|
||||
"vite-plugin-vue-layouts": "^0.11.0",
|
||||
"vue-i18n": "^10.0.0",
|
||||
"vue-tsc": "^2.1.10"
|
||||
|
||||
@@ -57,6 +57,8 @@ tauri-plugin-os = "2.3.0"
|
||||
uuid = "1.17.0"
|
||||
async-trait = "0.1.89"
|
||||
|
||||
url = { version = "2.5", features = ["serde"] }
|
||||
|
||||
[target.'cfg(target_os = "windows")'.dependencies]
|
||||
windows = { version = "0.52", features = ["Win32_Foundation", "Win32_UI_Shell", "Win32_UI_WindowsAndMessaging"] }
|
||||
winapi = { version = "0.3.9", features = ["securitybaseapi", "processthreadsapi"] }
|
||||
|
||||
@@ -19,11 +19,13 @@ use easytier::{
|
||||
launcher::NetworkConfig,
|
||||
rpc_service::ApiRpcServer,
|
||||
tunnel::ring::RingTunnelListener,
|
||||
tunnel::tcp::TcpTunnelListener,
|
||||
tunnel::TunnelListener,
|
||||
utils::{self},
|
||||
};
|
||||
use std::ops::Deref;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::{RwLock, RwLockReadGuard};
|
||||
use tokio::sync::{Mutex, RwLock, RwLockReadGuard};
|
||||
use uuid::Uuid;
|
||||
|
||||
use tauri::{AppHandle, Emitter, Manager as _};
|
||||
@@ -40,8 +42,21 @@ 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));
|
||||
|
||||
static RING_RPC_SERVER: once_cell::sync::Lazy<RwLock<Option<ApiRpcServer<RingTunnelListener>>>> =
|
||||
once_cell::sync::Lazy::new(|| RwLock::new(None));
|
||||
type BoxedTunnelListener = Box<dyn TunnelListener>;
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
enum RpcServerKind {
|
||||
Ring,
|
||||
Tcp,
|
||||
}
|
||||
|
||||
struct RpcServer {
|
||||
kind: RpcServerKind,
|
||||
_server: ApiRpcServer<BoxedTunnelListener>,
|
||||
bind_url: Option<url::Url>,
|
||||
}
|
||||
static RPC_SERVER: once_cell::sync::Lazy<Mutex<Option<RpcServer>>> =
|
||||
once_cell::sync::Lazy::new(|| Mutex::new(None));
|
||||
|
||||
static WEB_CLIENT: once_cell::sync::Lazy<RwLock<Option<WebClient>>> =
|
||||
once_cell::sync::Lazy::new(|| RwLock::new(None));
|
||||
@@ -322,8 +337,25 @@ fn get_service_status() -> Result<&'static str, String> {
|
||||
}
|
||||
}
|
||||
|
||||
fn normalize_normal_mode_rpc_portal(portal: &str) -> Result<(url::Url, url::Url), String> {
|
||||
let portal_url: url::Url = portal
|
||||
.parse()
|
||||
.map_err(|e| format!("invalid rpc portal: {:#}", e))?;
|
||||
let bind_url = portal_url.clone();
|
||||
let mut connect_url = portal_url.clone();
|
||||
// if bind addr is 0.0.0.0, should convert to 127.0.0.1
|
||||
if connect_url.host_str() == Some("0.0.0.0") {
|
||||
connect_url.set_host(Some("127.0.0.1")).unwrap();
|
||||
}
|
||||
Ok((bind_url, connect_url))
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn init_rpc_connection(_app: AppHandle, url: Option<String>) -> Result<(), String> {
|
||||
async fn init_rpc_connection(
|
||||
_app: AppHandle,
|
||||
is_normal_mode: bool,
|
||||
url: Option<String>,
|
||||
) -> Result<(), String> {
|
||||
let mut client_manager_guard =
|
||||
tokio::time::timeout(std::time::Duration::from_secs(5), CLIENT_MANAGER.write())
|
||||
.await
|
||||
@@ -331,41 +363,72 @@ async fn init_rpc_connection(_app: AppHandle, url: Option<String>) -> Result<(),
|
||||
let mut instance_manager_guard = INSTANCE_MANAGER
|
||||
.try_write()
|
||||
.map_err(|_| "Failed to acquire write lock for instance manager")?;
|
||||
let mut ring_rpc_server_guard = RING_RPC_SERVER
|
||||
.try_write()
|
||||
.map_err(|_| "Failed to acquire write lock for ring rpc server")?;
|
||||
let mut rpc_server_guard = RPC_SERVER
|
||||
.try_lock()
|
||||
.map_err(|_| "Failed to acquire lock for rpc server")?;
|
||||
|
||||
let normal_mode = url.is_none();
|
||||
if normal_mode {
|
||||
let mut client_url = url.clone();
|
||||
if is_normal_mode {
|
||||
let instance_manager = if let Some(im) = instance_manager_guard.take() {
|
||||
im
|
||||
} else {
|
||||
Arc::new(NetworkInstanceManager::new())
|
||||
};
|
||||
let rpc_server = if let Some(rpc_server) = ring_rpc_server_guard.take() {
|
||||
rpc_server
|
||||
|
||||
let portal = url.and_then(|s| {
|
||||
let trimmed = s.trim().to_string();
|
||||
if trimmed.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(trimmed)
|
||||
}
|
||||
});
|
||||
|
||||
let (desired_kind, bind_url, connect_url) = if let Some(portal) = portal {
|
||||
let (bind_url, connect_url) = normalize_normal_mode_rpc_portal(&portal)?;
|
||||
(RpcServerKind::Tcp, Some(bind_url), Some(connect_url))
|
||||
} else {
|
||||
ApiRpcServer::from_tunnel(
|
||||
RingTunnelListener::new(
|
||||
format!("ring://{}", RPC_RING_UUID.deref()).parse().unwrap(),
|
||||
),
|
||||
instance_manager.clone(),
|
||||
)
|
||||
.with_rx_timeout(None)
|
||||
.serve()
|
||||
.await
|
||||
.map_err(|e| e.to_string())?
|
||||
(RpcServerKind::Ring, None, None)
|
||||
};
|
||||
|
||||
let need_restart = rpc_server_guard
|
||||
.as_ref()
|
||||
.map(|x| x.kind != desired_kind || x.bind_url != bind_url)
|
||||
.unwrap_or(true);
|
||||
|
||||
if need_restart {
|
||||
*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"),
|
||||
)),
|
||||
};
|
||||
|
||||
let rpc_server = ApiRpcServer::from_tunnel(tunnel, instance_manager.clone())
|
||||
.with_rx_timeout(None)
|
||||
.serve()
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
*rpc_server_guard = Some(RpcServer {
|
||||
kind: desired_kind,
|
||||
_server: rpc_server,
|
||||
bind_url,
|
||||
});
|
||||
}
|
||||
|
||||
*instance_manager_guard = Some(instance_manager);
|
||||
*ring_rpc_server_guard = Some(rpc_server);
|
||||
client_url = connect_url.map(|u| u.to_string());
|
||||
} else {
|
||||
*ring_rpc_server_guard = None;
|
||||
*rpc_server_guard = None;
|
||||
}
|
||||
|
||||
let client_manager = tokio::time::timeout(
|
||||
std::time::Duration::from_millis(1000),
|
||||
manager::GUIClientManager::new(url),
|
||||
manager::GUIClientManager::new(client_url),
|
||||
)
|
||||
.await
|
||||
.map_err(|_| "connect remote rpc timed out".to_string())?
|
||||
@@ -373,7 +436,7 @@ async fn init_rpc_connection(_app: AppHandle, url: Option<String>) -> Result<(),
|
||||
.map_err(|e| format!("{:#}", e))?;
|
||||
*client_manager_guard = Some(client_manager);
|
||||
|
||||
if !normal_mode {
|
||||
if !is_normal_mode {
|
||||
drop(WEB_CLIENT.write().await.take());
|
||||
if let Some(instance_manager) = instance_manager_guard.take() {
|
||||
instance_manager
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, watch, onMounted, ref } from 'vue';
|
||||
import type { Mode, ServiceMode, RemoteMode } from '~/composables/mode';
|
||||
import type { Mode, ServiceMode, RemoteMode, NormalMode } from '~/composables/mode';
|
||||
import { appConfigDir, appLogDir } from '@tauri-apps/api/path';
|
||||
import { join } from '@tauri-apps/api/path';
|
||||
import { getServiceStatus, type ServiceStatus } from '~/composables/backend';
|
||||
@@ -15,6 +15,14 @@ const defaultLogDir = ref('')
|
||||
const serviceStatus = ref<ServiceStatus>('NotInstalled')
|
||||
const isServiceStatusLoaded = ref(false)
|
||||
|
||||
function normalizeRpcListenPort(port: unknown): number {
|
||||
const defaultPort = 15999
|
||||
const numericPort = typeof port === 'number' ? port : Number.parseInt(String(port ?? ''), 10)
|
||||
if (Number.isNaN(numericPort))
|
||||
return defaultPort
|
||||
return Math.min(65535, Math.max(1, Math.floor(numericPort)))
|
||||
}
|
||||
|
||||
onMounted(async () => {
|
||||
defaultConfigDir.value = await join(await appConfigDir(), 'config.d')
|
||||
defaultLogDir.value = await appLogDir()
|
||||
@@ -26,6 +34,43 @@ const modeOptions = computed(() => [
|
||||
{ label: t('mode.remote'), value: 'remote' },
|
||||
]);
|
||||
|
||||
const normalMode = computed({
|
||||
get: () => model.value.mode === 'normal' ? model.value as NormalMode : undefined,
|
||||
set: (value) => {
|
||||
if (value) {
|
||||
model.value = value
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const rpcListenOptions = computed(() => [
|
||||
{ label: t('web.common.disable'), value: false },
|
||||
{ label: t('web.common.enable'), value: true },
|
||||
])
|
||||
|
||||
const rpcListenEnabled = computed<boolean>({
|
||||
get: () => !!normalMode.value?.enable_rpc_port_listen,
|
||||
set: (value) => {
|
||||
if (!normalMode.value)
|
||||
return
|
||||
normalMode.value.enable_rpc_port_listen = value
|
||||
},
|
||||
})
|
||||
|
||||
const rpcListenPort = computed<string>({
|
||||
get: () => String(normalMode.value?.rpc_listen_port ?? 15999),
|
||||
set: (value) => {
|
||||
if (!normalMode.value)
|
||||
return
|
||||
const trimmed = value.trim()
|
||||
if (trimmed === '')
|
||||
return
|
||||
if (!/^\d+$/.test(trimmed))
|
||||
return
|
||||
normalMode.value.rpc_listen_port = Number.parseInt(trimmed, 10)
|
||||
},
|
||||
})
|
||||
|
||||
const serviceMode = computed({
|
||||
get: () => model.value.mode === 'service' ? model.value as ServiceMode : undefined,
|
||||
set: (value) => {
|
||||
@@ -57,6 +102,24 @@ const statusColorClass = computed(() => {
|
||||
}
|
||||
})
|
||||
|
||||
watch(() => [normalMode.value?.enable_rpc_port_listen, normalMode.value?.rpc_listen_port], ([enabled, port]) => {
|
||||
if (!normalMode.value)
|
||||
return
|
||||
|
||||
if (!enabled) {
|
||||
normalMode.value.rpc_portal = undefined
|
||||
return
|
||||
}
|
||||
|
||||
const normalizedPort = normalizeRpcListenPort(port)
|
||||
if (normalMode.value.rpc_listen_port !== normalizedPort)
|
||||
normalMode.value.rpc_listen_port = normalizedPort
|
||||
|
||||
const desiredPortal = `tcp://0.0.0.0:${normalizedPort}`
|
||||
if (normalMode.value.rpc_portal !== desiredPortal)
|
||||
normalMode.value.rpc_portal = desiredPortal
|
||||
}, { immediate: true })
|
||||
|
||||
watch(() => model.value.mode, async (newMode, oldMode) => {
|
||||
if (newMode === oldMode)
|
||||
return
|
||||
@@ -69,8 +132,12 @@ watch(() => model.value.mode, async (newMode, oldMode) => {
|
||||
const oldModelValue = { ...model.value }
|
||||
|
||||
if (newMode === 'normal') {
|
||||
const portal = normalMode.value?.rpc_portal?.trim()
|
||||
model.value = {
|
||||
...oldModelValue,
|
||||
rpc_portal: portal || undefined,
|
||||
enable_rpc_port_listen: normalMode.value?.enable_rpc_port_listen,
|
||||
rpc_listen_port: normalMode.value?.rpc_listen_port,
|
||||
mode: 'normal',
|
||||
}
|
||||
}
|
||||
@@ -113,6 +180,20 @@ watch(() => model.value.mode, async (newMode, oldMode) => {
|
||||
{{ t('mode.remote_description') }}
|
||||
</div>
|
||||
|
||||
<div v-if="normalMode" class="flex flex-col gap-2">
|
||||
<div class="flex items-center gap-2">
|
||||
<label for="rpc-listen-toggle">{{ t('mode.enable_rpc_tcp_listen') }}</label>
|
||||
<SelectButton id="rpc-listen-toggle" v-model="rpcListenEnabled" :options="rpcListenOptions" option-label="label"
|
||||
option-value="value" />
|
||||
</div>
|
||||
<div v-if="rpcListenEnabled" class="flex flex-col gap-2">
|
||||
<div class="flex items-center gap-2">
|
||||
<label for="rpc-listen-port">{{ t('mode.rpc_listen_port') }}</label>
|
||||
<InputText id="rpc-listen-port" v-model="rpcListenPort" class="flex-1" inputmode="numeric" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div v-if="serviceMode" class="flex flex-col gap-2">
|
||||
<div class="flex items-center gap-2">
|
||||
<label for="config-dir">{{ t('mode.config_dir') }}</label>
|
||||
|
||||
@@ -89,8 +89,8 @@ export async function getServiceStatus() {
|
||||
return await invoke<ServiceStatus>('get_service_status')
|
||||
}
|
||||
|
||||
export async function initRpcConnection(url?: string) {
|
||||
return await invoke('init_rpc_connection', { url })
|
||||
export async function initRpcConnection(isNormalMode: boolean, url?: string) {
|
||||
return await invoke('init_rpc_connection', { isNormalMode, url })
|
||||
}
|
||||
|
||||
export async function isClientRunning() {
|
||||
|
||||
@@ -4,8 +4,12 @@ export interface WebClientConfig {
|
||||
config_server_url?: string
|
||||
}
|
||||
|
||||
interface NormalMode extends WebClientConfig {
|
||||
export interface NormalMode extends WebClientConfig {
|
||||
mode: 'normal'
|
||||
// if not provided will use ring tunnel rpc server
|
||||
rpc_portal?: string
|
||||
enable_rpc_port_listen?: boolean
|
||||
rpc_listen_port?: number
|
||||
}
|
||||
|
||||
export interface ServiceMode extends WebClientConfig {
|
||||
|
||||
@@ -156,13 +156,23 @@ async function initWithMode(mode: Mode) {
|
||||
url = "tcp://" + mode.rpc_portal.replace("0.0.0.0", "127.0.0.1")
|
||||
retrys = 5
|
||||
break;
|
||||
case 'normal':
|
||||
url = mode.rpc_portal;
|
||||
break;
|
||||
}
|
||||
for (let i = 0; i < retrys; i++) {
|
||||
try {
|
||||
await connectRpcClient(url)
|
||||
await connectRpcClient(mode.mode === 'normal', url)
|
||||
break;
|
||||
} catch (e) {
|
||||
if (i === retrys - 1) {
|
||||
const errMsg = e instanceof Error ? e.message : String(e)
|
||||
toast.add({
|
||||
severity: 'error',
|
||||
summary: t('error'),
|
||||
detail: t('mode.rpc_connection_failed', { error: errMsg }),
|
||||
life: 1000,
|
||||
})
|
||||
throw e;
|
||||
}
|
||||
console.error("Error connecting rpc client, retrying...", e)
|
||||
@@ -332,9 +342,9 @@ const setting_menu_items: Ref<MenuItem[]> = ref([
|
||||
},
|
||||
])
|
||||
|
||||
async function connectRpcClient(url?: string) {
|
||||
await initRpcConnection(url)
|
||||
console.log("easytier rpc connection established")
|
||||
async function connectRpcClient(isNormalMode: boolean, url?: string) {
|
||||
await initRpcConnection(isNormalMode, url)
|
||||
console.log("easytier rpc connection established, isNormalMode: ", isNormalMode)
|
||||
}
|
||||
|
||||
onMounted(async () => {
|
||||
|
||||
Reference in New Issue
Block a user