fix(client): 退出 Pangolin 时拆隧道(停内核),不再残留 sing-box/TUN
此前真退出(托盘退出 / Cmd+Q)走 windowManager.destroy(),从不先停内核, 而 sing-box 是 sudo root 子进程不随父进程退出 → 孤儿 + TUN 残留,得手动 pkill。 - system_tray:托盘「退出」前 await onBeforeQuit()(停内核)再 destroy。 - main:PangolinApp→ConsumerStatefulWidget,AppLifecycleListener.onExitRequested 在 Cmd+Q 时先停内核再退;_teardownVpn 调 vpnBridge.stop()(SIGTERM→sing-box, 拆 TUN),带 3s 超时兜底。 - 关窗口→隐藏托盘的路径不拆(保持连接,符合常驻语义)。 flutter analyze 0 error;116 tests passed。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
+39
-2
@@ -1,8 +1,11 @@
|
|||||||
// main.dart — 入口(演示态)
|
// main.dart — 入口(演示态)
|
||||||
// 串接:登录 → 引导 → 主框架(4 Tab);状态层统一走 Riverpod。
|
// 串接:登录 → 引导 → 主框架(4 Tab);状态层统一走 Riverpod。
|
||||||
|
import 'dart:ui' as ui;
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
|
|
||||||
|
import 'bridge/vpn_bridge_provider.dart';
|
||||||
import 'pangolin_theme.dart';
|
import 'pangolin_theme.dart';
|
||||||
import 'services/token_store.dart';
|
import 'services/token_store.dart';
|
||||||
import 'shell/home_shell.dart';
|
import 'shell/home_shell.dart';
|
||||||
@@ -22,11 +25,45 @@ Future<void> main() async {
|
|||||||
runApp(const ProviderScope(child: PangolinApp()));
|
runApp(const ProviderScope(child: PangolinApp()));
|
||||||
}
|
}
|
||||||
|
|
||||||
class PangolinApp extends ConsumerWidget {
|
class PangolinApp extends ConsumerStatefulWidget {
|
||||||
const PangolinApp({super.key});
|
const PangolinApp({super.key});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context, WidgetRef ref) {
|
ConsumerState<PangolinApp> createState() => _PangolinAppState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _PangolinAppState extends ConsumerState<PangolinApp> {
|
||||||
|
AppLifecycleListener? _lifecycle;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
if (isDesktop) {
|
||||||
|
// 真退出(Cmd+Q / 托盘退出)前先停内核拆隧道,避免 sing-box 孤儿 + TUN 残留。
|
||||||
|
_lifecycle = AppLifecycleListener(onExitRequested: () async {
|
||||||
|
await _teardownVpn();
|
||||||
|
return ui.AppExitResponse.exit;
|
||||||
|
});
|
||||||
|
TrayService.instance.onBeforeQuit = _teardownVpn;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _teardownVpn() async {
|
||||||
|
try {
|
||||||
|
await ref.read(vpnBridgeProvider).stop().timeout(const Duration(seconds: 3));
|
||||||
|
} catch (_) {
|
||||||
|
// 内核未启动/已退出:忽略。
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dispose() {
|
||||||
|
_lifecycle?.dispose();
|
||||||
|
super.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
final mode = ref.watch(themeModeProvider);
|
final mode = ref.watch(themeModeProvider);
|
||||||
return MaterialApp(
|
return MaterialApp(
|
||||||
title: '穿山甲',
|
title: '穿山甲',
|
||||||
|
|||||||
@@ -15,6 +15,9 @@ class TrayService with TrayListener, WindowListener {
|
|||||||
TrayService._();
|
TrayService._();
|
||||||
static final TrayService instance = TrayService._();
|
static final TrayService instance = TrayService._();
|
||||||
|
|
||||||
|
/// 真正退出前的清理钩子(由 app 注册,用于先停内核拆隧道再退出)。
|
||||||
|
Future<void> Function()? onBeforeQuit;
|
||||||
|
|
||||||
/// 在 runApp 前调用(仅桌面端)。设置窗口拦截关闭 + 托盘图标与菜单。
|
/// 在 runApp 前调用(仅桌面端)。设置窗口拦截关闭 + 托盘图标与菜单。
|
||||||
Future<void> init() async {
|
Future<void> init() async {
|
||||||
await windowManager.ensureInitialized();
|
await windowManager.ensureInitialized();
|
||||||
@@ -45,13 +48,15 @@ class TrayService with TrayListener, WindowListener {
|
|||||||
void onTrayIconRightMouseDown() => trayManager.popUpContextMenu();
|
void onTrayIconRightMouseDown() => trayManager.popUpContextMenu();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void onTrayMenuItemClick(MenuItem menuItem) {
|
void onTrayMenuItemClick(MenuItem menuItem) async {
|
||||||
switch (menuItem.key) {
|
switch (menuItem.key) {
|
||||||
case 'show':
|
case 'show':
|
||||||
_show();
|
_show();
|
||||||
case 'quit':
|
case 'quit':
|
||||||
windowManager.setPreventClose(false);
|
// 退出前先停内核拆隧道(避免 sudo sing-box 孤儿 + TUN 残留)。
|
||||||
windowManager.destroy();
|
await onBeforeQuit?.call();
|
||||||
|
await windowManager.setPreventClose(false);
|
||||||
|
await windowManager.destroy();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user