Initialize Android physical interfaces and preserve macOS scoped routes. Add redacted diagnostics, default DNS, ordered auto-save, simplified controls, scanner rotation settings, and native regression coverage. Restore vendored build sources and document validation and stability checks.
426 lines
15 KiB
Dart
426 lines
15 KiB
Dart
import 'dart:async';
|
|
import 'dart:io';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'network_telemetry.dart';
|
|
|
|
class LiveDetailPage extends StatefulWidget {
|
|
final String title;
|
|
final Widget Function() builder;
|
|
const LiveDetailPage({super.key, required this.title, required this.builder});
|
|
@override
|
|
State<LiveDetailPage> createState() => _LiveDetailPageState();
|
|
}
|
|
|
|
class _LiveDetailPageState extends State<LiveDetailPage> {
|
|
Timer? timer;
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
timer = Timer.periodic(const Duration(seconds: 2), (_) {
|
|
if (mounted) setState(() {});
|
|
});
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
timer?.cancel();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) => Scaffold(
|
|
appBar: AppBar(title: Text(widget.title)),
|
|
body: SingleChildScrollView(
|
|
padding: const EdgeInsets.all(20),
|
|
child: Center(
|
|
child: ConstrainedBox(
|
|
constraints: const BoxConstraints(maxWidth: 820),
|
|
child: widget.builder(),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
class NodeDetailBody extends StatelessWidget {
|
|
final Map<String, dynamic> node, status;
|
|
final Map<String, dynamic>? peer;
|
|
const NodeDetailBody({
|
|
super.key,
|
|
required this.node,
|
|
required this.peer,
|
|
required this.status,
|
|
});
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final direct =
|
|
peer?['direct'] == true &&
|
|
(node['next_hop_peer_id'] == null ||
|
|
node['next_hop_peer_id'] == node['peer_id']);
|
|
final measured = direct ? (peer?['latency_ms']) : null;
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
Text(
|
|
'${node['hostname'] ?? node['peer_id']}',
|
|
style: Theme.of(context).textTheme.headlineSmall,
|
|
),
|
|
ListTile(
|
|
title: const Text('当前路径'),
|
|
subtitle: Text(
|
|
'${direct ? '直连' : '中继'} · 下一跳 ${node['next_hop_peer_id'] ?? node['peer_id']}',
|
|
),
|
|
),
|
|
ListTile(
|
|
title: const Text('直连能力'),
|
|
subtitle: Text(
|
|
'${peer?['direct'] == true ? '有直连可用' : '未发现直连'} · ${peer?['hole_punched'] == true ? '打洞建立' : '未确认打洞'}',
|
|
),
|
|
),
|
|
ListTile(
|
|
title: Text(measured != null ? '链路 RTT' : '路由估算'),
|
|
subtitle: Text(
|
|
'${measured ?? node['estimated_latency_ms'] ?? '未测得'}${measured != null || node['estimated_latency_ms'] != null ? ' ms' : ''}',
|
|
),
|
|
),
|
|
ListTile(
|
|
title: const Text('虚拟地址 / NAT'),
|
|
subtitle: Text(
|
|
'${node['ipv4'] ?? '—'} · ${node['udp_nat_type'] ?? '未知'}',
|
|
),
|
|
),
|
|
ListTile(
|
|
title: const Text('当前链路上传 / 下载'),
|
|
subtitle: Text(
|
|
peer == null
|
|
? '通过下一跳,未单独计量'
|
|
: '↑ ${formatBytes(peer?['tx_bytes_per_second'], rate: true)} · ↓ ${formatBytes(peer?['rx_bytes_per_second'], rate: true)}',
|
|
),
|
|
),
|
|
ListTile(
|
|
title: const Text('链路累计'),
|
|
subtitle: Text(
|
|
'↑ ${formatBytes(peer?['tx_bytes'])} · ↓ ${formatBytes(peer?['rx_bytes'])}',
|
|
),
|
|
),
|
|
const Text('隧道统计包含控制流量。页面显示最近 SDK 采样;没有采样时间的值不保证是即时测量。'),
|
|
const Divider(),
|
|
const Text('发布子网'),
|
|
for (final subnet in node['advertised_subnets'] as List? ?? [])
|
|
ListTile(title: Text('$subnet')),
|
|
const Divider(),
|
|
Text('核心 ${node['version'] ?? '未知'} · 节点 ID ${node['peer_id']}'),
|
|
],
|
|
);
|
|
}
|
|
}
|
|
|
|
class SubnetListPageBody extends StatelessWidget {
|
|
final Map<String, dynamic> status;
|
|
final Map<String, dynamic> Function()? statusProvider;
|
|
const SubnetListPageBody({
|
|
super.key,
|
|
required this.status,
|
|
this.statusProvider,
|
|
});
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final platform = status['platform_network'] as Map? ?? {};
|
|
final accepted = (platform['subnet_routes'] as List? ?? [])
|
|
.cast<String>()
|
|
.toSet();
|
|
final installed = (platform['installed_subnet_routes'] as List? ?? [])
|
|
.cast<String>()
|
|
.toSet();
|
|
final protected = (platform['protected_subnets'] as List? ?? [])
|
|
.cast<String>()
|
|
.toSet();
|
|
final sources = <String, List<Map<String, dynamic>>>{};
|
|
for (final row in status['routes'] as List? ?? []) {
|
|
for (final subnet in row['advertised_subnets'] as List? ?? []) {
|
|
sources
|
|
.putIfAbsent('$subnet', () => [])
|
|
.add(Map<String, dynamic>.from(row));
|
|
}
|
|
}
|
|
final all = {...sources.keys, ...accepted, ...protected};
|
|
final system =
|
|
status['runtime_mode'] == 'system_vpn' ||
|
|
status['runtime_mode'] == 'unified_vpn';
|
|
String label(String subnet) {
|
|
if (accepted.contains(subnet)) {
|
|
return system
|
|
? (platform['ready'] == true &&
|
|
(installed.contains(subnet) ||
|
|
platform['route_installation'] == 'macos_unified_tun')
|
|
? '已生效 · 系统接入'
|
|
: '接入中 / 待确认')
|
|
: '已生效 · 应用内可用';
|
|
}
|
|
if (protected.contains(subnet)) return '已失效 · 保护中';
|
|
return '已学习 · 未接入(查看详情)';
|
|
}
|
|
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
Text(
|
|
'已学习 ${sources.length} · 当前接受 ${accepted.length} · 保护 ${protected.length}',
|
|
),
|
|
const Text('自动学习合规公告,无远端手动列表。合规判定由 SDK 执行;不接管所有私网。'),
|
|
if (all.isEmpty)
|
|
const Padding(padding: EdgeInsets.all(24), child: Text('暂无子网公告')),
|
|
for (final subnet in all)
|
|
Card(
|
|
child: ListTile(
|
|
title: Text(subnet),
|
|
subtitle: Text(label(subnet)),
|
|
trailing: const Icon(Icons.chevron_right),
|
|
onTap: () => Navigator.push<void>(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (_) => LiveDetailPage(
|
|
title: '子网详情',
|
|
builder: () => SubnetDetailBody(
|
|
subnet: subnet,
|
|
status: statusProvider?.call() ?? status,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|
|
|
|
class LogsPageBody extends StatelessWidget {
|
|
final List<String> logs;
|
|
final VoidCallback onClear;
|
|
const LogsPageBody({super.key, required this.logs, required this.onClear});
|
|
@override
|
|
Widget build(BuildContext context) => Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
const Text(
|
|
'显示本次应用会话最近 300 条事件。macOS 同步写入系统日志,可在 Console 中筛选 MeshPort / Runtime。复制保留节点地址,隐藏认证信息。',
|
|
),
|
|
Wrap(
|
|
spacing: 12,
|
|
children: [
|
|
OutlinedButton(
|
|
onPressed: () async {
|
|
await Clipboard.setData(ClipboardData(text: logs.join('\n')));
|
|
if (context.mounted) {
|
|
ScaffoldMessenger.of(
|
|
context,
|
|
).showSnackBar(const SnackBar(content: Text('已复制当前日志')));
|
|
}
|
|
},
|
|
child: const Text('复制当前范围'),
|
|
),
|
|
TextButton(onPressed: onClear, child: const Text('清空显示')),
|
|
],
|
|
),
|
|
for (final line in logs)
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(vertical: 6),
|
|
child: SelectableText(
|
|
line,
|
|
style: const TextStyle(fontFamily: 'monospace', fontSize: 12),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
class OnboardingBody extends StatelessWidget {
|
|
const OnboardingBody({super.key});
|
|
@override
|
|
Widget build(BuildContext context) => Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
Text('欢迎使用 MeshPort', style: Theme.of(context).textTheme.headlineMedium),
|
|
const ListTile(
|
|
leading: Icon(Icons.hub_outlined),
|
|
title: Text('仅组网'),
|
|
subtitle: Text('填写网络名称、密钥、虚拟 IP 和引导节点,访问游戏服务器及节点公告子网。'),
|
|
),
|
|
const ListTile(
|
|
leading: Icon(Icons.language),
|
|
title: Text('仅代理'),
|
|
subtitle: Text('导入或添加代理节点,选择规则 / 全局 / 直连。无需组网凭据。'),
|
|
),
|
|
const ListTile(
|
|
leading: Icon(Icons.route),
|
|
title: Text('代理+组网'),
|
|
subtitle: Text('两侧配置齐全后连接,组网目标优先。统一 VPN 由一个 TUN 分流。'),
|
|
),
|
|
const Text(
|
|
'应用内入口仅供 SOCKS/SDK 接入,统一 VPN 才接管系统应用。管理员或系统 VPN 授权在连接时请求;默认手动连接,可在启动与后台设置中开启自动连接。',
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
class StartupPageBody extends StatefulWidget {
|
|
final MethodChannel channel;
|
|
const StartupPageBody({super.key, required this.channel});
|
|
@override
|
|
State<StartupPageBody> createState() => _StartupPageBodyState();
|
|
}
|
|
|
|
class _StartupPageBodyState extends State<StartupPageBody> {
|
|
Map<String, dynamic> preferences = {};
|
|
bool loading = true;
|
|
String error = '';
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
unawaited(load());
|
|
}
|
|
|
|
Future<void> load() async {
|
|
try {
|
|
final status = await widget.channel.invokeMapMethod<String, dynamic>(
|
|
'startup_status',
|
|
);
|
|
if (mounted) setState(() => preferences = status ?? {});
|
|
} catch (_) {
|
|
if (mounted) setState(() => error = '此平台尚未接入启动管理');
|
|
} finally {
|
|
if (mounted) setState(() => loading = false);
|
|
}
|
|
}
|
|
|
|
Future<void> update(Map<String, dynamic> changes) async {
|
|
setState(() {
|
|
loading = true;
|
|
error = '';
|
|
});
|
|
try {
|
|
final status = await widget.channel.invokeMapMethod<String, dynamic>(
|
|
'set_startup',
|
|
changes,
|
|
);
|
|
if (mounted) setState(() => preferences = status ?? {});
|
|
} catch (_) {
|
|
if (mounted) {
|
|
setState(() => error = '设置失败。请将 APP 安装到 Applications,并检查系统登录项权限。');
|
|
}
|
|
} finally {
|
|
if (mounted) setState(() => loading = false);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) => Column(
|
|
children: [
|
|
if (error.isNotEmpty) Text(error),
|
|
const ListTile(
|
|
title: Text('启动回填已保存配置'),
|
|
subtitle: Text('网络及代理配置从安全存储读取。'),
|
|
),
|
|
SwitchListTile(
|
|
value: preferences['auto_connect'] == true,
|
|
onChanged: loading || !(Platform.isMacOS || Platform.isAndroid)
|
|
? null
|
|
: (v) => update({'auto_connect': v}),
|
|
title: const Text('自动连接'),
|
|
subtitle: const Text('下次启动按已保存入口与代理开关连接;需要时请求管理员授权。启动失败不反复重试。'),
|
|
),
|
|
SwitchListTile(
|
|
value: [
|
|
'enabled',
|
|
'requires_approval',
|
|
].contains(preferences['login_status']),
|
|
onChanged:
|
|
loading ||
|
|
!Platform.isMacOS ||
|
|
preferences['login_status'] == 'unsupported'
|
|
? null
|
|
: (v) => update({'login': v}),
|
|
title: const Text('登录时启动'),
|
|
subtitle: Text(
|
|
'macOS 13+ · 当前状态:${preferences['login_status'] ?? '读取中'}',
|
|
),
|
|
),
|
|
if (preferences['login_status'] == 'requires_approval')
|
|
TextButton(
|
|
onPressed: () =>
|
|
widget.channel.invokeMethod<void>('open_login_settings'),
|
|
child: const Text('打开系统登录项,允许 MeshPort'),
|
|
),
|
|
TextButton(onPressed: loading ? null : load, child: const Text('刷新系统状态')),
|
|
const Text('登录启动与自动连接可分别开启。默认关闭;退出后不会自动重新抢占 VPN。'),
|
|
ListTile(
|
|
title: const Text('后台运行'),
|
|
subtitle: Text(
|
|
Platform.isAndroid
|
|
? 'VPN 使用前台服务。被其他 VPN 替换时停止。'
|
|
: '退出应用会停止网络;订阅定时更新仅在应用运行期间执行。',
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
class SubnetDetailBody extends StatelessWidget {
|
|
final String subnet;
|
|
final Map<String, dynamic> status;
|
|
const SubnetDetailBody({
|
|
super.key,
|
|
required this.subnet,
|
|
required this.status,
|
|
});
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final platform = status['platform_network'] as Map? ?? {};
|
|
final accepted = (platform['subnet_routes'] as List? ?? []).contains(
|
|
subnet,
|
|
);
|
|
final protected = (platform['protected_subnets'] as List? ?? []).contains(
|
|
subnet,
|
|
);
|
|
final installed = (platform['installed_subnet_routes'] as List? ?? [])
|
|
.contains(subnet);
|
|
final system = [
|
|
'system_vpn',
|
|
'unified_vpn',
|
|
].contains(status['runtime_mode']);
|
|
final ready = platform['ready'] == true;
|
|
final sources = (status['routes'] as List? ?? []).where(
|
|
(r) => (r['advertised_subnets'] as List? ?? []).contains(subnet),
|
|
);
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
Text(subnet, style: Theme.of(context).textTheme.headlineSmall),
|
|
Text(
|
|
accepted
|
|
? (system
|
|
? (ready && installed ? '已生效 · 系统接入' : '接入中 / 待确认')
|
|
: '已生效 · 应用内可用')
|
|
: protected
|
|
? '已失效 · 保护中'
|
|
: '未接入',
|
|
),
|
|
for (final source in sources)
|
|
ListTile(
|
|
title: Text('发布者 ${source['hostname'] ?? source['peer_id']}'),
|
|
subtitle: Text(
|
|
'节点 ${source['peer_id']} · 下一跳 ${source['next_hop_peer_id'] ?? '未确定'}',
|
|
),
|
|
),
|
|
if (!accepted && !protected)
|
|
const Text('SDK 未接受或等待接入;可能为默认路由、公网、过宽子网、本地冲突或不可达。接口暂未提供具体拒绝原因。'),
|
|
if (!accepted && protected)
|
|
const Text('路径撤销后拒绝转发,不能回退互联网代理。显式停止会话解除保护,可能中断连接。'),
|
|
const Text('同 CIDR 发布者合并;实际下一跳由 SDK 选路。状态每 2 秒刷新。'),
|
|
],
|
|
);
|
|
}
|
|
}
|