Improve VPN video routing, profile storage and authorization reuse

This commit is contained in:
shen
2026-10-13 00:34:16 -07:00
parent c94143dfae
commit f94dab0555
30 changed files with 1303 additions and 139 deletions
+102 -17
View File
@@ -22,6 +22,11 @@ class LabScreen extends StatefulWidget {
class _LabScreenState extends State<LabScreen> {
MeshPortClient? client, applicationClient, systemClient;
MacSystemVpnSession? macVpnSession;
bool legacyMigrationPending = false;
String localProfilePath = Platform.isAndroid
? 'APP 私有目录/MeshPort/'
: '~/Library/Application Support/MeshPort/';
String runtimeMode = Platform.isMacOS || Platform.isAndroid
? 'unified_vpn'
: 'application';
@@ -83,8 +88,7 @@ class _LabScreenState extends State<LabScreen> {
proxySession?.state == 'Running' ||
proxySession?.state == 'Partial';
bool get profileSupported => Platform.isMacOS || Platform.isAndroid;
String get profileStorage =>
Platform.isAndroid ? 'Android 安全存储' : 'macOS 钥匙串';
String get profileStorage => '本地配置目录 $localProfilePath';
@override
void initState() {
super.initState();
@@ -182,6 +186,7 @@ class _LabScreenState extends State<LabScreen> {
singBoxEnabled = proxyProfile.proxyEnabled;
final diagnostics = await opened.enableDiagnostics();
log(diagnostics['enabled'] == true ? '穿透详细日志采集已开启' : '穿透日志未接管:宿主已有日志订阅器');
if (profileSupported) await checkLocalProfileMigration();
if (Platform.isAndroid) {
final snapshot = await androidVpnRequest({'op': 'status'});
final data = snapshot['data'] as Map?;
@@ -209,6 +214,7 @@ class _LabScreenState extends State<LabScreen> {
'startup_status',
);
if (preferences?['auto_connect'] == true &&
!legacyMigrationPending &&
!running &&
(restored ||
(singBoxEnabled && proxyProfile.feature == 'proxy'))) {
@@ -514,7 +520,11 @@ class _LabScreenState extends State<LabScreen> {
}
if (systemClient != null) {
try {
await systemClient!.close();
if (Platform.isMacOS && macVpnSession != null) {
await macVpnSession!.release(systemClient!);
} else {
await systemClient!.close();
}
} catch (_) {}
systemClient = null;
client = applicationClient;
@@ -549,18 +559,20 @@ class _LabScreenState extends State<LabScreen> {
},
request: androidVpnRequest,
)
: await openMacSystemVpn(
launch: (path, uid) async {
log('等待管理员授权以创建虚拟网卡');
await diagnosticStage(
'授权并启动 macOS VPN 辅助进程',
() => vpnChannel.invokeMethod<void>('launch', {
'socket': path,
'uid': uid,
}),
);
},
);
: await (macVpnSession ??= MacSystemVpnSession(
open: () => openMacSystemVpn(
launch: (path, uid) async {
log('等待管理员授权以创建虚拟网卡');
await diagnosticStage(
'授权并启动 macOS VPN 辅助进程',
() => vpnChannel.invokeMethod<void>('launch', {
'socket': path,
'uid': uid,
}),
);
},
),
)).acquire();
try {
await opened.enableDiagnostics();
if (unified) {
@@ -588,6 +600,51 @@ class _LabScreenState extends State<LabScreen> {
}
}
Future<void> checkLocalProfileMigration() async {
try {
final info = await vpnChannel.invokeMapMethod<String, dynamic>(
'profile_storage_status',
);
if (mounted) {
setState(() {
legacyMigrationPending = info?['migration_pending'] == true;
if (info?['path'] is String) {
localProfilePath = info!['path'] as String;
}
});
}
if (legacyMigrationPending) log('旧配置需要迁移,请在设置中点击“迁移旧配置”');
} catch (_) {
log('无法检查本地配置目录,请在设置中重试');
}
}
Future<void> migrateLegacyProfiles() async {
await vpnChannel.invokeMethod<void>('migrate_profiles');
final proxy = await vpnChannel.invokeMethod<String>('load_proxy_profile');
final network = (await vpnChannel.invokeMethod<String>(
'load_profile',
))?.replaceAll(RegExp(r'^subnet_routes\s*=.*$', multiLine: true), '');
if (network != null) await loadForm(network);
if (proxy != null) {
final imported = ProxyProfile.fromJson(
jsonDecode(proxy) as Map<String, dynamic>,
);
if (imported.entry == 'system_vpn') imported.feature = 'mesh';
proxyProfile = imported;
savedProxyJson = jsonEncode(imported.toJson());
runtimeMode = imported.entry;
singBoxEnabled = imported.proxyEnabled;
}
if (network != null) {
savedConfig = network;
rememberConfig = true;
}
await checkLocalProfileMigration();
if (mounted) setState(() {});
log('旧配置已迁移,后续读取和保存使用本地文件');
}
Future<void> saveProfile(String config, {bool explicit = false}) async {
if ((!rememberConfig && !explicit) || !profileSupported) return;
try {
@@ -611,11 +668,17 @@ class _LabScreenState extends State<LabScreen> {
if (c == applicationClient && proxySession != null) {
await proxySession!.stop();
}
await c.stop();
if (Platform.isMacOS && c == systemClient && macVpnSession != null) {
await macVpnSession!.release(c);
} else {
await c.stop();
}
} finally {
if (systemClient != null) {
try {
await systemClient!.close();
if (!Platform.isMacOS || macVpnSession == null) {
await systemClient!.close();
}
} finally {
systemClient = null;
client = applicationClient;
@@ -750,6 +813,9 @@ class _LabScreenState extends State<LabScreen> {
cancel = true;
timer?.cancel();
subscriptionTimer?.cancel();
unawaited(
macVpnSession?.close().catchError((Object _) {}) ?? Future<void>.value(),
);
if (client != null) {
final active = client!,
application = applicationClient,
@@ -1247,6 +1313,25 @@ class _LabScreenState extends State<LabScreen> {
Widget settings() => Column(
children: [
panel('应用设置', [
if (profileSupported) ...[
ListTile(
title: const Text('配置保存位置'),
subtitle: Text(localProfilePath),
),
if (legacyMigrationPending)
ListTile(
title: const Text('迁移旧配置'),
subtitle: Text(
Platform.isMacOS
? '点击后可能需要钥匙串授权;迁移后使用本地文件。'
: '读取旧版保存的配置并迁移到本地文件。',
),
trailing: const Icon(Icons.chevron_right),
onTap: busy || running
? null
: () => perform((_) => migrateLegacyProfiles()),
),
],
ListTile(
title: const Text('DNS 设置'),
trailing: const Icon(Icons.chevron_right),