From a04fae725dc60bf73617e5d8d703dff3e2fcbe8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=91=E5=85=88=E6=A3=AE?= Date: Thu, 17 Oct 2024 19:30:05 +0800 Subject: [PATCH] =?UTF-8?q?#improvement=20=E8=81=94=E6=9C=BA=E6=88=90?= =?UTF-8?q?=E5=8A=9F=E5=90=8E=E5=8F=B3=E4=B8=8B=E8=A7=92=E8=A7=92=E6=A0=87?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BB=BF=E7=82=B9=EF=BC=8C=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E4=BA=8C=E7=BA=A7=E7=AA=97=E5=8F=A3=E5=88=9B=E5=BB=BA=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E4=BF=AE=E5=A4=8D=E6=8E=A7=E5=88=B6=E5=8F=B0=E6=8A=A5?= =?UTF-8?q?=E9=94=99=EF=BC=8C=E5=A2=9E=E5=8A=A0=E5=BC=80=E6=9C=BA=E8=87=AA?= =?UTF-8?q?=E5=90=AF=E5=8A=9F=E8=83=BD=EF=BC=8C=E7=A6=81=E7=94=A8p2p?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E6=94=B9=E4=B8=BA=E4=B8=8D=E7=A6=81=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composables/tray.ts | 143 +++++++++-------- nuxt.config.ts | 2 +- package.json | 9 +- pages/index.vue | 230 ++++++++++++++++++---------- pages/member.vue | 2 +- pnpm-lock.yaml | 11 ++ src-tauri/Cargo.lock | 68 +++++++- src-tauri/Cargo.toml | 3 +- src-tauri/capabilities/default.json | 6 + src-tauri/capabilities/desktop.json | 11 ++ src-tauri/src/lib.rs | 10 +- src-tauri/tauri.conf.json | 12 +- stores/index.ts | 4 +- 13 files changed, 339 insertions(+), 172 deletions(-) create mode 100644 src-tauri/capabilities/desktop.json diff --git a/composables/tray.ts b/composables/tray.ts index 6e12c1a..312f56b 100644 --- a/composables/tray.ts +++ b/composables/tray.ts @@ -1,89 +1,86 @@ -import { Menu, MenuItem, PredefinedMenuItem } from '@tauri-apps/api/menu' -import { TrayIcon } from '@tauri-apps/api/tray' -import { getCurrentWindow } from '@tauri-apps/api/window' -import pkg from '@/package.json' +import { Menu, MenuItem, PredefinedMenuItem } from "@tauri-apps/api/menu"; +import { TrayIcon } from "@tauri-apps/api/tray"; +import { getCurrentWindow } from "@tauri-apps/api/window"; +import pkg from "@/package.json"; -const DEFAULT_TRAY_NAME = 'main' +const DEFAULT_TRAY_NAME = "main"; async function toggleVisibility() { - if (await getCurrentWindow().isVisible()) { - await getCurrentWindow().hide() - } - else { - await getCurrentWindow().show() - await getCurrentWindow().setFocus() - } + if (await getCurrentWindow().isVisible()) { + await getCurrentWindow().hide(); + } else { + await getCurrentWindow().show(); + await getCurrentWindow().setFocus(); + } } -export async function useTray(init: boolean = false, beforExit) { - let tray - try { - tray = await TrayIcon.getById(DEFAULT_TRAY_NAME) - if (!tray) { - tray = await TrayIcon.new({ - tooltip: `EasyTier\n${pkg.version}`, - title: `EasyTier\n${pkg.version}`, - id: DEFAULT_TRAY_NAME, - menu: await Menu.new({ - id: 'main', - items: await generateMenuItem(beforExit), - }), - action: async (e) => { - toggleVisibility() - }, - }) - } - } - catch (error) { - console.warn('Error while creating tray icon:', error) - return null - } +export async function useTray(init: boolean = false, beforExit: Function) { + let tray; + try { + tray = await TrayIcon.getById(DEFAULT_TRAY_NAME); + if (!tray) { + tray = await TrayIcon.new({ + tooltip: `EasyTier\n${pkg.version}`, + title: `EasyTier\n${pkg.version}`, + id: DEFAULT_TRAY_NAME, + menu: await Menu.new({ + id: "main", + items: await generateMenuItem(beforExit) + }), + action: async e => { + toggleVisibility(); + } + }); + } + } catch (error) { + console.warn("Error while creating tray icon:", error); + return null; + } - if (init) { - tray.setTooltip(`EasyTier\n${pkg.version}`) - tray.setMenuOnLeftClick(false) - tray.setMenu(await Menu.new({ - id: 'main', - items: await generateMenuItem(beforExit), - })) - } + if (init) { + tray.setTooltip(`EasyTier\n${pkg.version}`); + tray.setMenuOnLeftClick(false); + tray.setMenu( + await Menu.new({ + id: "main", + items: await generateMenuItem(beforExit) + }) + ); + } - return tray + return tray; } export async function generateMenuItem(beforExit: Function) { - return [ - await MenuItemExit('退出', beforExit), - await PredefinedMenuItem.new({ item: 'Separator' }), - await MenuItemShow('显示 / 隐藏'), - ] + return [await MenuItemExit("退出", beforExit), await PredefinedMenuItem.new({ item: "Separator" }), await MenuItemShow("显示 / 隐藏")]; } export async function MenuItemExit(text: string, beforExit: Function) { - return await MenuItem.new({ - id: "quit", - text, - action: async () => { - if (beforExit) { - await beforExit(); - } - await getCurrentWindow().close(); - } - }) + return await MenuItem.new({ + id: "quit", + text, + action: async () => { + if (beforExit) { + await beforExit(); + } + await getCurrentWindow().close(); + } + }); } export async function MenuItemShow(text: string) { - return await MenuItem.new({ - id: 'show', - text, - action: async () => { - await toggleVisibility() - }, - }) + return await MenuItem.new({ + id: "show", + text, + action: async () => { + await toggleVisibility(); + } + }); } // export async function setTrayMenu(items: (MenuItem | PredefinedMenuItem)[] | undefined = undefined) { -// const tray = await useTray() +// // const tray = await useTray() +// const tray = await TrayIcon.getById(DEFAULT_TRAY_NAME) // if (!tray) // return // const menu = await Menu.new({ @@ -93,12 +90,10 @@ export async function MenuItemShow(text: string) { // tray.setMenu(menu) // } -// export async function setTrayRunState(isRunning: boolean = false) { -// const tray = await useTray() -// if (!tray) -// return -// tray.setIcon(isRunning ? 'icons/icon-inactive.ico' : 'icons/icon.ico') -// } +export async function setTrayRunState(tray: TrayIcon | null, isRunning: boolean = false) { + if (!tray) return; + tray.setIcon(isRunning ? "icons/icon-inactive.ico" : "icons/icon.ico"); +} // export async function setTrayTooltip(tooltip: string) { // if (tooltip) { @@ -108,4 +103,4 @@ export async function MenuItemShow(text: string) { // tray.setTooltip(`EasyTier\n${pkg.version}\n${tooltip}`) // tray.setTitle(`EasyTier\n${pkg.version}\n${tooltip}`) // } -// } \ No newline at end of file +// } diff --git a/nuxt.config.ts b/nuxt.config.ts index 02c16e3..fa96274 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -70,7 +70,7 @@ export default defineNuxtConfig({ sourcemap: !!process.env.TAURI_DEBUG }, esbuild: { - pure: ["console.log"], + // pure: ["console.log"], drop: ["debugger"] } }, diff --git a/package.json b/package.json index 71f5f59..583bb89 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "private": true, "author": "leizi97", "description": "A simple network initiator based on Easytier", - "version": "1.0.5", + "version": "1.0.6", "scripts": { "dev": "nuxt dev --dotenv env/.env.dev --host 0.0.0.0", "build": "nuxt generate --dotenv env/.env.prod" @@ -18,6 +18,7 @@ "@tauri-apps/cli": "^2.0.2", "@tauri-apps/plugin-cli": "^2.0.0", "@tauri-apps/plugin-http": "^2.0.0", + "@tauri-apps/plugin-shell": "~2", "@tinymce/tinymce-vue": "^5.1.1", "@types/lodash-es": "^4.17.12", "@types/qs": "^6.9.15", @@ -35,7 +36,9 @@ "typescript": "^5.4.5", "vue": "^3.4.24", "vue-clipboard3": "^2.0.0", - "xlsx": "^0.18.5", - "@tauri-apps/plugin-shell": "~2" + "xlsx": "^0.18.5" + }, + "dependencies": { + "@tauri-apps/plugin-autostart": "~2" } } diff --git a/pages/index.vue b/pages/index.vue index 6739f36..d7c9a0e 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -218,41 +218,50 @@ > 禁用ipv6 + + 开机自启 + 禁用端口监听 - - easytier发布页 -
+
+ + WinIPBroadcast + + + + + +
- WinIPBroadcast - - - + 主页 -
@@ -260,19 +269,20 @@ diff --git a/pages/member.vue b/pages/member.vue index 4787446..610d8df 100644 --- a/pages/member.vue +++ b/pages/member.vue @@ -40,7 +40,7 @@ const showTableHeader = [ ["ipv4", "虚拟网IP"], ["hostname", "主机名"], - ["cost", "方式"], + ["cost", "路由"], ["lat_ms", "延迟/ms"], ["loss_rate", "丢包率"], ["version", "版本"] diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e0d081d..6d387a7 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -7,6 +7,10 @@ settings: importers: .: + dependencies: + '@tauri-apps/plugin-autostart': + specifier: ~2 + version: 2.0.0 devDependencies: '@element-plus/icons-vue': specifier: ^2.3.1 @@ -1008,6 +1012,9 @@ packages: engines: {node: '>= 10'} hasBin: true + '@tauri-apps/plugin-autostart@2.0.0': + resolution: {integrity: sha512-NEwOQWVasZ8RczXkMLNJokRDujneuMH/UFA5t84DLkbNZUmiD3G7HZWhgSd1YQ0BFU9h9w+h2B/py3y6bzWg4Q==} + '@tauri-apps/plugin-cli@2.0.0': resolution: {integrity: sha512-glQmlL1IiCGEa1FHYa/PTPSeYhfu56omLRgHXWlJECDt6DbJyRuJWVgtkQfUxtqnVdYnnU+DGIGeiInoEqtjLw==} @@ -5568,6 +5575,10 @@ snapshots: '@tauri-apps/cli-win32-ia32-msvc': 2.0.2 '@tauri-apps/cli-win32-x64-msvc': 2.0.2 + '@tauri-apps/plugin-autostart@2.0.0': + dependencies: + '@tauri-apps/api': 2.0.2 + '@tauri-apps/plugin-cli@2.0.0': dependencies: '@tauri-apps/api': 2.0.2 diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index f358025..dc60fec 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -288,6 +288,17 @@ version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" +[[package]] +name = "auto-launch" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f012b8cc0c850f34117ec8252a44418f2e34a2cf501de89e29b241ae5f79471" +dependencies = [ + "dirs 4.0.0", + "thiserror", + "winreg 0.10.1", +] + [[package]] name = "autocfg" version = "1.4.0" @@ -979,13 +990,33 @@ dependencies = [ "subtle", ] +[[package]] +name = "dirs" +version = "4.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059" +dependencies = [ + "dirs-sys 0.3.7", +] + [[package]] name = "dirs" version = "5.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "44c45a9d03d6676652bcb5e724c7e988de1acad23a711b5217ab9cbecbec2225" dependencies = [ - "dirs-sys", + "dirs-sys 0.4.1", +] + +[[package]] +name = "dirs-sys" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6" +dependencies = [ + "libc", + "redox_users", + "winapi", ] [[package]] @@ -1078,7 +1109,7 @@ checksum = "0d6ef0072f8a535281e4876be788938b528e9a1d43900b82c2569af7da799125" [[package]] name = "easytier-game" -version = "1.0.5" +version = "1.0.6" dependencies = [ "log", "reqwest", @@ -1087,6 +1118,7 @@ dependencies = [ "sysinfo", "tauri", "tauri-build", + "tauri-plugin-autostart", "tauri-plugin-log", "tauri-plugin-shell", "tauri-plugin-single-instance", @@ -1110,7 +1142,7 @@ dependencies = [ "rustc_version", "toml 0.8.2", "vswhom", - "winreg", + "winreg 0.52.0", ] [[package]] @@ -4086,7 +4118,7 @@ checksum = "5920aad0804ea5e86808d4b6e8753d3bcbae7efc8f4e41a4da00b45427559868" dependencies = [ "anyhow", "bytes", - "dirs", + "dirs 5.0.1", "dunce", "embed_plist", "futures-util", @@ -4137,7 +4169,7 @@ checksum = "935f9b3c49b22b3e2e485a57f46d61cd1ae07b1cbb2ba87387a387caf2d8c4e7" dependencies = [ "anyhow", "cargo_toml", - "dirs", + "dirs 5.0.1", "glob", "heck 0.5.0", "json-patch", @@ -4209,6 +4241,21 @@ dependencies = [ "walkdir", ] +[[package]] +name = "tauri-plugin-autostart" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bba6bb936e0fd0a58ed958b49e2e423dd40949c9d9425cc991be996959e3838e" +dependencies = [ + "auto-launch", + "log", + "serde", + "serde_json", + "tauri", + "tauri-plugin", + "thiserror", +] + [[package]] name = "tauri-plugin-log" version = "2.0.1" @@ -4609,7 +4656,7 @@ checksum = "533fc2d4105e0e3d96ce1c71f2d308c9fbbe2ef9c587cab63dd627ab5bde218f" dependencies = [ "core-graphics", "crossbeam-channel", - "dirs", + "dirs 5.0.1", "libappindicator", "muda", "objc2", @@ -5430,6 +5477,15 @@ dependencies = [ "memchr", ] +[[package]] +name = "winreg" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80d0f4e272c85def139476380b12f9ac60926689dd2e01d4923222f40580869d" +dependencies = [ + "winapi", +] + [[package]] name = "winreg" version = "0.52.0" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index ae1458d..0a2a04c 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "easytier-game" -version = "1.0.5" +version = "1.0.6" homepage = "https://github.com/EasyTier/EasyTier" repository = "https://github.com/EasyTier/EasytierGame" description = "A simple network initiator based on Easytier" @@ -38,4 +38,5 @@ sysinfo = '0.32.0' # prost-types = "0.13" [target."cfg(not(any(target_os = \"android\", target_os = \"ios\")))".dependencies] +tauri-plugin-autostart = "2" tauri-plugin-single-instance = "2" diff --git a/src-tauri/capabilities/default.json b/src-tauri/capabilities/default.json index 5f6070b..bf51382 100644 --- a/src-tauri/capabilities/default.json +++ b/src-tauri/capabilities/default.json @@ -20,6 +20,12 @@ "core:webview:allow-webview-show", "core:webview:allow-webview-hide", "core:webview:allow-webview-close", + "autostart:allow-enable", + "autostart:allow-disable", + "autostart:allow-is-enabled", + "core:tray:allow-get-by-id", + "core:tray:allow-set-icon", + "core:tray:allow-new", { "identifier": "shell:allow-spawn", "allow": [ diff --git a/src-tauri/capabilities/desktop.json b/src-tauri/capabilities/desktop.json new file mode 100644 index 0000000..6fd3b4d --- /dev/null +++ b/src-tauri/capabilities/desktop.json @@ -0,0 +1,11 @@ +{ + "identifier": "desktop-capability", + "platforms": [ + "macOS", + "windows", + "linux" + ], + "permissions": [ + "autostart:default" + ] +} \ No newline at end of file diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 796a7d9..4b8efab 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -14,6 +14,8 @@ use sysinfo::System; use tauri::tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent}; use tauri::Emitter; use tauri::Manager; +use tauri_plugin_autostart::MacosLauncher; + // 定义GitHub Release的结构体 #[derive(Debug, Deserialize)] pub struct Release { @@ -267,7 +269,7 @@ fn stop_command(child_id: u32) { } else { eprintln!("Failed to terminate process {}.", child_id); } - }else { + } else { println!("child id is 0"); } } @@ -330,12 +332,18 @@ fn search_pid_by_pname(target_process_name: String) -> u32 { return 0; } +pub const AUTOSTART_ARG: &str = "--autostart"; + #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { let stop_signal = Arc::new(AtomicBool::new(false)); // 创建一个原子布尔值,用于控制命令的停止 let stop_signal_clone = Arc::clone(&stop_signal); // 创建一个原子布尔值的克隆,用于传递给命令 let context = tauri::generate_context!(); tauri::Builder::default() + .plugin(tauri_plugin_autostart::init( + MacosLauncher::LaunchAgent, + Some(vec![AUTOSTART_ARG]), + )) .plugin(tauri_plugin_shell::init()) .plugin(tauri_plugin_single_instance::init(|app, _args, _cwd| { let _ = app diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index af80c9d..87dc3d7 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -1,7 +1,7 @@ { "$schema": "../node_modules/@tauri-apps/cli/config.schema.json", "productName": "easytier-game", - "version": "1.0.5", + "version": "1.0.6", "identifier": "com.tauri.easytier-game", "build": { @@ -28,12 +28,18 @@ "csp": null } }, - + "bundle": { "active": false, "targets": "all", "externalBin": ["WinIPBroadcast"], + "resources": ["icons/icon-inactive.ico", "icons/icon.ico"], + "windows": { + "webviewInstallMode": { + "type": "embedBootstrapper" + } + }, "createUpdaterArtifacts": false, - "icon": ["icons/32x32.png", "icons/128x128.png", "icons/128x128@2x.png", "icons/icon.icns", "icons/icon.ico"] + "icon": ["icons/icon.png", "icons/icon.rgba", "icons/icon.icns", "icons/icon.ico", "icons/icon-inactive.ico"] } } diff --git a/stores/index.ts b/stores/index.ts index 7f97385..e316a77 100644 --- a/stores/index.ts +++ b/stores/index.ts @@ -9,9 +9,10 @@ export default defineStore("main", { networkPassword: "", hostname: "", ipv4: "", + autoStart: false, // 是否自动启动 disableIpv6: false, // 是否禁用IPv6 disbleListenner: false, // 是否禁用监听 - disbleP2p: true, // 是否使用P2P + disbleP2p: false, // 是否使用P2P dhcp: true, // 是否使用DHCP }, basePeers: ["public.easytier.top:11010"], @@ -25,6 +26,7 @@ export default defineStore("main", { "config.protocol", "config.networkPassword", "config.disbleP2p", + "config.autoStart", "config.disableIpv6", "config.disbleListenner", "config.hostname",