664 lines
24 KiB
Dart
664 lines
24 KiB
Dart
import 'dart:convert';
|
||
import 'package:flutter/material.dart';
|
||
import 'package:meshport_client_core/meshport_client_core.dart';
|
||
import 'proxy_node_editor.dart';
|
||
import 'configuration_pages.dart';
|
||
import 'proxy_source_pages.dart';
|
||
import 'detail_pages.dart';
|
||
|
||
class ProxyPanel extends StatefulWidget {
|
||
final ProxyProfile profile;
|
||
final String entry;
|
||
final void Function(String) entryChanged;
|
||
final bool busy, running;
|
||
final bool draftChanged, saving, saveFailed;
|
||
final Future<void> Function() save, connect;
|
||
final VoidCallback changed;
|
||
final VoidCallback? showLogs;
|
||
const ProxyPanel({
|
||
super.key,
|
||
required this.profile,
|
||
required this.entry,
|
||
required this.entryChanged,
|
||
required this.busy,
|
||
required this.running,
|
||
required this.save,
|
||
required this.connect,
|
||
required this.changed,
|
||
this.showLogs,
|
||
this.draftChanged = false,
|
||
this.saving = false,
|
||
this.saveFailed = false,
|
||
});
|
||
@override
|
||
State<ProxyPanel> createState() => _ProxyPanelState();
|
||
}
|
||
|
||
class _ProxyPanelState extends State<ProxyPanel> {
|
||
ProxyProfile get p => widget.profile;
|
||
bool working = false;
|
||
String error = '';
|
||
void change(VoidCallback work) {
|
||
setState(() {
|
||
work();
|
||
error = '';
|
||
});
|
||
widget.changed();
|
||
}
|
||
|
||
Future<void> act(Future<void> Function() work) async {
|
||
if (working || widget.busy) return;
|
||
setState(() {
|
||
working = true;
|
||
error = '';
|
||
});
|
||
try {
|
||
await work();
|
||
} catch (e) {
|
||
if (mounted) {
|
||
setState(
|
||
() => error = e is FormatException
|
||
? e.message
|
||
: '操作失败,请检查配置、网络及概览中的实际运行状态。',
|
||
);
|
||
}
|
||
} finally {
|
||
if (mounted) setState(() => working = false);
|
||
}
|
||
}
|
||
|
||
Future<bool> confirm(String title, String text) async =>
|
||
await showDialog<bool>(
|
||
context: context,
|
||
builder: (c) => AlertDialog(
|
||
title: Text(title),
|
||
content: Text(text),
|
||
actions: [
|
||
TextButton(
|
||
onPressed: () => Navigator.pop(c, false),
|
||
child: const Text('取消'),
|
||
),
|
||
FilledButton(
|
||
onPressed: () => Navigator.pop(c, true),
|
||
child: const Text('确认'),
|
||
),
|
||
],
|
||
),
|
||
) ??
|
||
false;
|
||
Future<String?> input(
|
||
String title, {
|
||
String initial = '',
|
||
String hint = '',
|
||
bool secret = false,
|
||
}) => showDialog<String>(
|
||
context: context,
|
||
builder: (_) => _TextEditorDialog(
|
||
title: title,
|
||
initial: initial,
|
||
hint: hint,
|
||
secret: secret,
|
||
),
|
||
);
|
||
|
||
Future<void> importNodes() async {
|
||
final preview = await Navigator.push<Object>(
|
||
context,
|
||
MaterialPageRoute(builder: (_) => const ProxyImportPage()),
|
||
);
|
||
if (preview == null || !mounted) return;
|
||
if (preview is ImportPreview) {
|
||
change(() => p.addNodes(preview.nodes));
|
||
}
|
||
if (preview is Map<String, dynamic>) {
|
||
final sub = await Navigator.push<Map<String, dynamic>>(
|
||
context,
|
||
MaterialPageRoute(
|
||
builder: (_) => SubscriptionEditor(subscription: preview),
|
||
),
|
||
);
|
||
if (sub != null && mounted) {
|
||
final next = Map<String, dynamic>.from(sub)..remove('update');
|
||
change(() => p.subscriptions.add(next));
|
||
if (sub['update'] == true) await updateSubscription(next);
|
||
}
|
||
}
|
||
}
|
||
|
||
Future<void> editNode([ProxyNode? existing]) async {
|
||
final transport = existing?.options['transport']?['type'];
|
||
if (transport != null && !{'ws', 'grpc'}.contains(transport)) {
|
||
final value = await input(
|
||
'高级节点 JSON',
|
||
initial: const JsonEncoder.withIndent(
|
||
' ',
|
||
).convert({...existing!.options, 'tag': existing.name}),
|
||
);
|
||
if (value == null) return;
|
||
final preview = importProxyNodes(value);
|
||
if (preview.nodes.length != 1) throw const FormatException('只接受一个节点');
|
||
final node = preview.nodes.single;
|
||
change(() {
|
||
if (existing.source == 'manual') {
|
||
p.nodes[p.nodes.indexWhere((n) => n.id == existing.id)] = ProxyNode(
|
||
id: existing.id,
|
||
name: node.name,
|
||
options: node.options,
|
||
);
|
||
} else {
|
||
p.addNodes([node]);
|
||
}
|
||
});
|
||
return;
|
||
}
|
||
final node = await Navigator.push<ProxyNode>(
|
||
context,
|
||
MaterialPageRoute(builder: (_) => ProxyNodeEditor(node: existing)),
|
||
);
|
||
if (node == null || !mounted) return;
|
||
change(
|
||
() => existing == null || existing.source != 'manual'
|
||
? p.addNodes([node])
|
||
: p.nodes[p.nodes.indexWhere((n) => n.id == existing.id)] = node,
|
||
);
|
||
}
|
||
|
||
Future<void> editSubscription([Map<String, dynamic>? existing]) async {
|
||
final result = await Navigator.push<Map<String, dynamic>>(
|
||
context,
|
||
MaterialPageRoute(
|
||
builder: (_) => SubscriptionEditor(subscription: existing),
|
||
),
|
||
);
|
||
if (result == null || !mounted) return;
|
||
final next = Map<String, dynamic>.from(result)..remove('update');
|
||
change(
|
||
() => existing == null
|
||
? p.subscriptions.add(next)
|
||
: p.subscriptions[p.subscriptions.indexOf(existing)] = next,
|
||
);
|
||
if (result['update'] == true) await updateSubscription(next);
|
||
}
|
||
|
||
Future<void> updateSubscription(Map<String, dynamic> sub) async {
|
||
final source = sub['id'] as String;
|
||
final preview = await fetchSubscription(
|
||
sub['url'] as String,
|
||
source: source,
|
||
);
|
||
if (preview.nodes.isEmpty) throw const FormatException('订阅没有可用节点,保留原配置');
|
||
final old = p.nodes.where((n) => n.source == source).toList();
|
||
final replacement = preview.nodes.map((n) {
|
||
final retained = old
|
||
.where((o) => jsonEncode(o.options) == jsonEncode(n.options))
|
||
.firstOrNull;
|
||
return ProxyNode(
|
||
id: retained?.id ?? n.id,
|
||
name: n.name,
|
||
source: source,
|
||
options: n.options,
|
||
);
|
||
}).toList();
|
||
final removed =
|
||
p.selectedId != null &&
|
||
old.any((n) => n.id == p.selectedId) &&
|
||
!replacement.any((n) => n.id == p.selectedId);
|
||
if (!mounted ||
|
||
!await confirm(
|
||
'订阅更新预览',
|
||
'${replacement.length} 个支持节点,忽略 ${preview.unsupported} 个项目。${removed ? '当前选中节点将被删除,需重新选择;正在运行的出口保持到显式重连。' : '相同节点保留选择。'}',
|
||
)) {
|
||
return;
|
||
}
|
||
change(() {
|
||
replaceSubscription(p, source, preview, DateTime.now());
|
||
});
|
||
}
|
||
|
||
Widget subscriptionsBody() {
|
||
final disabled = working || widget.busy;
|
||
return section('订阅', [
|
||
OutlinedButton(
|
||
onPressed: disabled ? null : () => act(() => editSubscription()),
|
||
child: const Text('添加订阅'),
|
||
),
|
||
for (final sub in p.subscriptions)
|
||
ListTile(
|
||
title: Text(sub['name'] as String),
|
||
subtitle: Text(
|
||
'${sub['updated'] == null ? '尚未更新' : '更新于 ${sub['updated']}'} · ${sub['auto_update'] == true ? '自动更新 ${sub['interval_minutes'] ?? 360} 分钟' : '手动更新'}',
|
||
),
|
||
trailing: Wrap(
|
||
children: [
|
||
IconButton(
|
||
tooltip: '编辑',
|
||
onPressed: disabled
|
||
? null
|
||
: () => act(() => editSubscription(sub)),
|
||
icon: const Icon(Icons.edit_outlined),
|
||
),
|
||
IconButton(
|
||
tooltip: '更新并预览',
|
||
onPressed: disabled
|
||
? null
|
||
: () => act(() => updateSubscription(sub)),
|
||
icon: const Icon(Icons.sync),
|
||
),
|
||
IconButton(
|
||
tooltip: '删除订阅及所属节点',
|
||
onPressed: disabled
|
||
? null
|
||
: () => act(() async {
|
||
if (await confirm(
|
||
'删除订阅',
|
||
'删除该来源的节点,手动节点保留;正在运行的出口保持到重连。',
|
||
)) {
|
||
change(() {
|
||
p.nodes.removeWhere((n) => n.source == sub['id']);
|
||
p.subscriptions.remove(sub);
|
||
if (!p.nodes.any((n) => n.id == p.selectedId)) {
|
||
p.selectedId = null;
|
||
}
|
||
});
|
||
}
|
||
}),
|
||
icon: const Icon(Icons.delete_outline),
|
||
),
|
||
],
|
||
),
|
||
),
|
||
]);
|
||
}
|
||
|
||
Widget section(String title, List<Widget> children) => Card(
|
||
child: Padding(
|
||
padding: const EdgeInsets.all(18),
|
||
child: Column(
|
||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||
children: [
|
||
Text(title, style: Theme.of(context).textTheme.titleMedium),
|
||
const SizedBox(height: 20),
|
||
for (final child in children) ...[child, const SizedBox(height: 18)],
|
||
],
|
||
),
|
||
),
|
||
);
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
final disabled = widget.busy || working;
|
||
return Column(
|
||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||
children: [
|
||
if (error.isNotEmpty)
|
||
Text(
|
||
error,
|
||
style: TextStyle(color: Theme.of(context).colorScheme.error),
|
||
),
|
||
section('连接', [
|
||
DropdownButtonFormField<String>(
|
||
isExpanded: true,
|
||
initialValue: p.feature,
|
||
decoration: const InputDecoration(labelText: '连接方式'),
|
||
items: const [
|
||
DropdownMenuItem(value: 'mesh', child: Text('仅组网')),
|
||
DropdownMenuItem(value: 'proxy', child: Text('仅代理')),
|
||
DropdownMenuItem(value: 'combined', child: Text('代理 + 组网')),
|
||
],
|
||
onChanged: disabled
|
||
? null
|
||
: (v) => change(() {
|
||
p.feature = v!;
|
||
if (widget.entry == 'system_vpn' && v != 'mesh') {
|
||
widget.entryChanged('unified_vpn');
|
||
}
|
||
}),
|
||
),
|
||
Text(
|
||
p.feature == 'mesh'
|
||
? '填写组网配置后连接。'
|
||
: p.mode == 'direct'
|
||
? '直连模式无需代理节点。'
|
||
: '导入节点后连接即可,已提供默认加密 DNS。',
|
||
),
|
||
if (p.feature != 'mesh')
|
||
DropdownButtonFormField<String>(
|
||
isExpanded: true,
|
||
initialValue: p.mode,
|
||
decoration: const InputDecoration(labelText: '上网模式'),
|
||
items: const [
|
||
DropdownMenuItem(value: 'bypass_cn', child: Text('绕过中国大陆')),
|
||
DropdownMenuItem(value: 'rule', child: Text('自定义规则')),
|
||
DropdownMenuItem(value: 'global', child: Text('全局代理')),
|
||
DropdownMenuItem(value: 'direct', child: Text('直连')),
|
||
],
|
||
onChanged: disabled || p.feature == 'mesh'
|
||
? null
|
||
: (v) => change(() => p.mode = v!),
|
||
),
|
||
if (p.mode == 'bypass_cn' && p.feature != 'mesh')
|
||
const Text('优先执行自定义分流规则,其余中国大陆 IP 和普通私网直连,其他流量通过代理。首次使用会下载分流规则。'),
|
||
Text(
|
||
widget.entry == 'application'
|
||
? '应用内代理:需要在使用它的软件中设置 SOCKS5 127.0.0.1:10818。'
|
||
: widget.entry == 'system_vpn'
|
||
? '连接后接入组网地址。'
|
||
: '连接后设备流量自动接入。',
|
||
),
|
||
Wrap(
|
||
spacing: 8,
|
||
runSpacing: 12,
|
||
children: [
|
||
FilledButton.icon(
|
||
onPressed: disabled ? null : () => act(widget.connect),
|
||
icon: Icon(
|
||
widget.running && !widget.draftChanged
|
||
? Icons.stop
|
||
: Icons.power_settings_new,
|
||
),
|
||
label: Text(
|
||
widget.running
|
||
? (widget.draftChanged ? '重连应用修改' : '断开连接')
|
||
: '连接',
|
||
),
|
||
),
|
||
if (widget.showLogs != null)
|
||
OutlinedButton.icon(
|
||
onPressed: widget.showLogs,
|
||
icon: const Icon(Icons.monitor_heart_outlined),
|
||
label: const Text('诊断与日志'),
|
||
),
|
||
if (widget.saveFailed)
|
||
OutlinedButton(
|
||
onPressed: disabled ? null : () => act(widget.save),
|
||
child: const Text('重试保存'),
|
||
),
|
||
],
|
||
),
|
||
Text(
|
||
widget.saveFailed
|
||
? '配置未保存,请重试。'
|
||
: widget.saving
|
||
? '正在自动保存…'
|
||
: widget.running && widget.draftChanged
|
||
? '修改已保存,重连后生效。'
|
||
: '配置修改后自动保存。',
|
||
),
|
||
]),
|
||
section('代理节点', [
|
||
Wrap(
|
||
spacing: 8,
|
||
runSpacing: 12,
|
||
children: [
|
||
OutlinedButton(
|
||
onPressed: disabled ? null : () => act(importNodes),
|
||
child: const Text('导入与预览'),
|
||
),
|
||
OutlinedButton(
|
||
onPressed: disabled ? null : () => act(() => editNode()),
|
||
child: const Text('添加节点'),
|
||
),
|
||
],
|
||
),
|
||
if (p.nodes.isEmpty) const Text('导入第一个节点即可开始;多个节点时请选择一个出口。'),
|
||
for (final node in p.nodes)
|
||
ListTile(
|
||
leading: IconButton(
|
||
tooltip: '选择出口',
|
||
icon: Icon(
|
||
p.selectedId == node.id
|
||
? Icons.radio_button_checked
|
||
: Icons.radio_button_unchecked,
|
||
),
|
||
onPressed: disabled
|
||
? null
|
||
: () => change(() => p.selectedId = node.id),
|
||
),
|
||
title: Text(
|
||
node.name,
|
||
maxLines: 2,
|
||
overflow: TextOverflow.ellipsis,
|
||
),
|
||
onTap: () => Navigator.push<void>(
|
||
context,
|
||
MaterialPageRoute(
|
||
builder: (_) => ProxyNodeDetails(
|
||
node: node,
|
||
selected: node.id == p.selectedId,
|
||
select: () => change(() => p.selectedId = node.id),
|
||
edit: () => act(() => editNode(node)),
|
||
),
|
||
),
|
||
),
|
||
subtitle: Text(
|
||
'${node.options['type']} · ${node.options['server']}:${node.options['server_port']} · ${node.source == 'manual' ? '手动' : '订阅'}',
|
||
),
|
||
trailing: PopupMenuButton<String>(
|
||
enabled: !disabled,
|
||
onSelected: (v) => act(() async {
|
||
if (v == 'edit') {
|
||
await editNode(node);
|
||
} else if (await confirm('删除节点', '删除草稿中的节点,不会立即改变正在运行的出口。')) {
|
||
change(() {
|
||
p.nodes.remove(node);
|
||
if (p.selectedId == node.id) p.selectedId = null;
|
||
});
|
||
}
|
||
}),
|
||
itemBuilder: (_) => [
|
||
PopupMenuItem(
|
||
value: 'edit',
|
||
child: Text(node.source == 'manual' ? '编辑' : '复制为手动节点'),
|
||
),
|
||
const PopupMenuItem(value: 'delete', child: Text('删除')),
|
||
],
|
||
),
|
||
),
|
||
]),
|
||
section('订阅', [
|
||
ListTile(
|
||
title: const Text('订阅管理'),
|
||
subtitle: Text('${p.subscriptions.length} 个来源'),
|
||
trailing: const Icon(Icons.chevron_right),
|
||
onTap: () => Navigator.push<void>(
|
||
context,
|
||
MaterialPageRoute(
|
||
builder: (_) =>
|
||
LiveDetailPage(title: '订阅管理', builder: subscriptionsBody),
|
||
),
|
||
),
|
||
),
|
||
]),
|
||
Card(
|
||
child: ExpansionTile(
|
||
key: const PageStorageKey('proxy-advanced'),
|
||
title: const Text('高级设置'),
|
||
subtitle: const Text('网络入口、DNS 与分流规则'),
|
||
children: [
|
||
Padding(
|
||
padding: const EdgeInsets.fromLTRB(18, 20, 18, 24),
|
||
child: Column(
|
||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||
children: [
|
||
DropdownButtonFormField<String>(
|
||
key: ValueKey('entry-${widget.entry}'),
|
||
isExpanded: true,
|
||
initialValue: widget.entry,
|
||
decoration: const InputDecoration(labelText: '网络入口'),
|
||
items: const [
|
||
DropdownMenuItem(
|
||
value: 'application',
|
||
child: Text('应用内 SOCKS5'),
|
||
),
|
||
DropdownMenuItem(
|
||
value: 'system_vpn',
|
||
child: Text('独立系统组网'),
|
||
),
|
||
DropdownMenuItem(
|
||
value: 'unified_vpn',
|
||
child: Text('统一 VPN · 单一 TUN'),
|
||
),
|
||
],
|
||
onChanged: disabled
|
||
? null
|
||
: (v) => widget.entryChanged(v!),
|
||
),
|
||
const SizedBox(height: 16),
|
||
const Text('修改连接设置后,重连使其生效。'),
|
||
const Divider(height: 48),
|
||
Text('分流规则', style: Theme.of(context).textTheme.titleSmall),
|
||
const SizedBox(height: 12),
|
||
const Text(
|
||
'组网地址优先走组网;已失效的已知组网地址拒绝访问。',
|
||
style: TextStyle(height: 1.5),
|
||
),
|
||
const SizedBox(height: 16),
|
||
if (p.mode == 'rule' && p.feature != 'mesh') ...[
|
||
DropdownButtonFormField<String>(
|
||
isExpanded: true,
|
||
initialValue: p.defaultAction,
|
||
decoration: const InputDecoration(
|
||
labelText: '规则模式默认出口',
|
||
),
|
||
items: const [
|
||
DropdownMenuItem(value: 'proxy', child: Text('代理')),
|
||
DropdownMenuItem(value: 'direct', child: Text('直连')),
|
||
DropdownMenuItem(value: 'mesh', child: Text('组网')),
|
||
DropdownMenuItem(value: 'reject', child: Text('拒绝')),
|
||
],
|
||
onChanged: disabled
|
||
? null
|
||
: (v) => change(() => p.defaultAction = v!),
|
||
),
|
||
const SizedBox(height: 16),
|
||
],
|
||
if (p.mode == 'bypass_cn' && p.feature != 'mesh') ...[
|
||
const Text(
|
||
'自定义规则优先,其余私网及中国大陆 IP 直连,其他流量走代理。',
|
||
style: TextStyle(height: 1.5),
|
||
),
|
||
const SizedBox(height: 12),
|
||
],
|
||
Text(
|
||
'${p.rules.length} 条普通规则 · '
|
||
'${p.ruleSets.where((set) => set['enabled'] == true).length} 个启用规则集 · '
|
||
'${{'rule', 'bypass_cn'}.contains(p.mode) && p.feature != 'mesh' ? '按顺序执行' : '已暂停'}',
|
||
),
|
||
const SizedBox(height: 12),
|
||
Align(
|
||
alignment: Alignment.centerLeft,
|
||
child: OutlinedButton(
|
||
onPressed: disabled
|
||
? null
|
||
: () => Navigator.push<void>(
|
||
context,
|
||
MaterialPageRoute(
|
||
builder: (_) => RulesPage(
|
||
profile: p,
|
||
changed: widget.changed,
|
||
),
|
||
),
|
||
),
|
||
child: const Text('编辑普通规则'),
|
||
),
|
||
),
|
||
const Divider(height: 48),
|
||
Text(
|
||
'DNS 设置',
|
||
style: Theme.of(context).textTheme.titleSmall,
|
||
),
|
||
const SizedBox(height: 12),
|
||
Text(
|
||
'启动 DNS:${p.dnsServer}',
|
||
style: const TextStyle(height: 1.5),
|
||
),
|
||
const SizedBox(height: 8),
|
||
Text(
|
||
'直连 DNS:${p.directDnsServer.isEmpty ? '启动 DNS' : p.directDnsServer}',
|
||
style: const TextStyle(height: 1.5),
|
||
),
|
||
const SizedBox(height: 8),
|
||
Text(
|
||
'代理 DNS:${p.effectiveProxyDns}',
|
||
style: const TextStyle(height: 1.5),
|
||
),
|
||
const SizedBox(height: 16),
|
||
Align(
|
||
alignment: Alignment.centerLeft,
|
||
child: OutlinedButton(
|
||
onPressed: disabled
|
||
? null
|
||
: () => Navigator.push<void>(
|
||
context,
|
||
MaterialPageRoute(
|
||
builder: (_) => DnsPage(
|
||
profile: p,
|
||
changed: widget.changed,
|
||
),
|
||
),
|
||
),
|
||
child: const Text('编辑 DNS'),
|
||
),
|
||
),
|
||
const SizedBox(height: 16),
|
||
Text(
|
||
'FakeIP:${p.fakeIP ? '开启' : '关闭'} · 组网域名自动排除,解析失败无公共 DNS 回退。',
|
||
style: const TextStyle(height: 1.5),
|
||
),
|
||
],
|
||
),
|
||
),
|
||
],
|
||
),
|
||
),
|
||
],
|
||
);
|
||
}
|
||
}
|
||
|
||
class _TextEditorDialog extends StatefulWidget {
|
||
final String title, initial, hint;
|
||
final bool secret;
|
||
const _TextEditorDialog({
|
||
required this.title,
|
||
required this.initial,
|
||
required this.hint,
|
||
required this.secret,
|
||
});
|
||
@override
|
||
State<_TextEditorDialog> createState() => _TextEditorDialogState();
|
||
}
|
||
|
||
class _TextEditorDialogState extends State<_TextEditorDialog> {
|
||
late final controller = TextEditingController(text: widget.initial);
|
||
@override
|
||
void dispose() {
|
||
controller.dispose();
|
||
super.dispose();
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) => AlertDialog(
|
||
title: Text(widget.title),
|
||
content: SizedBox(
|
||
width: 520,
|
||
child: TextField(
|
||
controller: controller,
|
||
autofocus: true,
|
||
maxLines: widget.secret ? 1 : 8,
|
||
obscureText: widget.secret,
|
||
decoration: InputDecoration(hintText: widget.hint),
|
||
),
|
||
),
|
||
actions: [
|
||
TextButton(
|
||
onPressed: () => Navigator.pop(context),
|
||
child: const Text('取消'),
|
||
),
|
||
FilledButton(
|
||
onPressed: () => Navigator.pop(context, controller.text),
|
||
child: const Text('确定'),
|
||
),
|
||
],
|
||
);
|
||
}
|