feat: support custom bypass rules and mesh routing policies

This commit is contained in:
shen
2026-10-15 07:06:34 -07:00
parent dc1bfc99d2
commit c1d0739b95
14 changed files with 621 additions and 89 deletions
@@ -4,6 +4,14 @@ import 'package:flutter/services.dart';
import 'package:flutter/material.dart';
import 'package:meshport_client_core/meshport_client_core.dart';
String routingPolicyLabel(Object? action) => switch (action) {
'proxy' => '代理',
'direct' => '直连',
'mesh' => '组网',
'reject' => '拒绝',
_ => '$action',
};
Future<bool> confirmDiscard(BuildContext context) async =>
await showDialog<bool>(
context: context,
@@ -130,6 +138,13 @@ class _RuleEditorState extends State<RuleEditor> {
setState(() => error = '请输入有效域名或 CIDR,不接受星号语法');
return;
}
if (action == 'mesh' &&
type == 'ip_cidr' &&
InternetAddress.tryParse(v.split('/').first)?.type !=
InternetAddressType.IPv4) {
setState(() => error = '组网目前仅支持 IPv4 网段');
return;
}
Navigator.pop(context, {
'type': type,
'value': v,
@@ -159,18 +174,28 @@ class _RuleEditorState extends State<RuleEditor> {
),
TextField(
controller: value,
decoration: const InputDecoration(labelText: '匹配值'),
decoration: InputDecoration(
labelText: '匹配值',
hintText: type == 'ip_cidr'
? '例如 192.168.50.0/24'
: '例如 example.com',
),
),
DropdownButtonFormField<String>(
isExpanded: true,
initialValue: action,
items: const [
DropdownMenuItem(value: 'proxy', child: Text('所选代理')),
DropdownMenuItem(value: 'proxy', child: Text('代理')),
DropdownMenuItem(value: 'direct', child: Text('直连')),
DropdownMenuItem(value: 'mesh', child: Text('组网')),
DropdownMenuItem(value: 'reject', child: Text('拒绝')),
],
onChanged: (v) => setState(() => action = v!),
),
if (action == 'mesh')
const Text(
'需启用代理+组网。目标必须属于已接入的 IPv4 组网网段;未接入时拒绝访问,不回退到代理或直连。此规则不会自动发布远端网段。',
),
const Text('按列表顺序匹配;组网优先,然后普通规则,最后规则集。'),
],
),
@@ -264,7 +289,11 @@ class _RulesPageState extends State<RulesPage> {
subtitle: Text('有效目标走组网;已知失效目标拒绝。不能移动或关闭。'),
),
Text(
widget.profile.mode == 'rule' && widget.profile.feature != 'mesh'
widget.profile.feature == 'mesh'
? '仅组网模式暂停普通规则'
: widget.profile.mode == 'bypass_cn'
? '自定义规则和规则集优先,其后执行私网及中国大陆 IP 直连,其余走代理'
: widget.profile.mode == 'rule'
? '普通规则按顺序执行'
: '当前模式暂停普通规则',
),
@@ -272,7 +301,9 @@ class _RulesPageState extends State<RulesPage> {
Card(
child: ListTile(
title: Text('${entry.$1 + 1}. ${entry.$2['value']}'),
subtitle: Text('${entry.$2['type']} → ${entry.$2['action']}'),
subtitle: Text(
'${entry.$2['type']} → ${routingPolicyLabel(entry.$2['action'])}',
),
onTap: () => edit(entry.$1),
trailing: Wrap(
children: [
@@ -303,7 +334,9 @@ class _RulesPageState extends State<RulesPage> {
const Text('规则集 · 普通规则之后按顺序匹配'),
for (final e in sets.indexed)
ListTile(
title: Text('${e.$2['tag']} → ${e.$2['action']}'),
title: Text(
'${e.$2['tag']} → ${routingPolicyLabel(e.$2['action'])}',
),
subtitle: Text(
e.$2['url'] == null
? '内联规则'
@@ -341,20 +374,25 @@ class _RulesPageState extends State<RulesPage> {
),
),
OutlinedButton(onPressed: () => editSet(), child: const Text('添加规则集')),
DropdownButtonFormField<String>(
isExpanded: true,
initialValue: outlet,
decoration: const InputDecoration(labelText: '固定默认出口'),
items: const [
DropdownMenuItem(value: 'proxy', child: Text('所选代理')),
DropdownMenuItem(value: 'direct', child: Text('直连')),
DropdownMenuItem(value: 'reject', child: Text('拒绝')),
],
onChanged: (v) => setState(() {
outlet = v!;
dirty = true;
}),
),
if (widget.profile.mode == 'bypass_cn')
const Text('预设默认出口:所选代理(未匹配自定义规则、私网及中国大陆 IP 时)'),
if (widget.profile.mode == 'rule')
DropdownButtonFormField<String>(
isExpanded: true,
initialValue: outlet,
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: (v) => setState(() {
outlet = v!;
dirty = true;
}),
),
if (outlet == 'mesh') const Text('组网默认策略仅允许已接入的组网目标,其他目标拒绝访问。'),
const Text('保存不改变当前会话;应用时需要重连。'),
],
),
@@ -1066,8 +1104,9 @@ class _RuleSetEditorState extends State<RuleSetEditor> {
initialValue: action,
decoration: const InputDecoration(labelText: '匹配后执行'),
items: const [
DropdownMenuItem(value: 'proxy', child: Text('代理')),
DropdownMenuItem(value: 'direct', child: Text('直连')),
DropdownMenuItem(value: 'proxy', child: Text('所选代理')),
DropdownMenuItem(value: 'mesh', child: Text('组网')),
DropdownMenuItem(value: 'reject', child: Text('拒绝')),
],
onChanged: (v) => setState(() {
@@ -1075,7 +1114,11 @@ class _RuleSetEditorState extends State<RuleSetEditor> {
dirty = true;
}),
),
const Text('远程集在规则模式连接期间自动更新并缓存;首次下载失败会阻止连接,后续失败保留已缓存版本。保存后重连生效。'),
if (action == 'mesh')
const Text('需启用代理+组网;规则集匹配到的目标仅在已有组网路由时可达,否则拒绝访问。'),
const Text(
'远程集在自定义规则或绕过中国大陆模式连接期间自动更新并缓存;首次下载失败会阻止连接,后续失败保留已缓存版本。保存后重连生效。',
),
],
),
);
+88 -43
View File
@@ -336,7 +336,7 @@ class _ProxyPanelState extends State<ProxyPanel> {
: (v) => change(() => p.mode = v!),
),
if (p.mode == 'bypass_cn' && p.feature != 'mesh')
const Text('中国大陆 IP 和普通私网直连,其余通过代理。首次使用会下载分流规则。'),
const Text('优先执行自定义分流规则,其余中国大陆 IP 和普通私网直连,其他流量通过代理。首次使用会下载分流规则。'),
Text(
widget.entry == 'application'
? '应用内代理:需要在使用它的软件中设置 SOCKS5 127.0.0.1:10818。'
@@ -475,7 +475,7 @@ class _ProxyPanelState extends State<ProxyPanel> {
subtitle: const Text('网络入口、DNS 与分流规则'),
children: [
Padding(
padding: const EdgeInsets.all(18),
padding: const EdgeInsets.fromLTRB(18, 20, 18, 24),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
@@ -502,62 +502,107 @@ class _ProxyPanelState extends State<ProxyPanel> {
? null
: (v) => widget.entryChanged(v!),
),
const SizedBox(height: 18),
const SizedBox(height: 16),
const Text('修改连接设置后,重连使其生效。'),
const SizedBox(height: 18),
const Divider(height: 48),
Text('分流规则', style: Theme.of(context).textTheme.titleSmall),
const SizedBox(height: 12),
const Text(
'受管理规则:有效组网地址 → 组网;已失效的已知组网地址 → 拒绝;随后才应用普通规则与默认出口。',
),
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: 'reject', child: Text('拒绝')),
],
onChanged:
disabled || p.mode != 'rule' || p.feature == 'mesh'
? null
: (v) => change(() => p.defaultAction = v!),
'组网地址优先走组网;已失效的已知组网地址拒绝访问。',
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.mode == 'rule' && p.feature != 'mesh' ? '规则模式执行' : '已暂停'}',
'${p.rules.length} 条普通规则 · '
'${p.ruleSets.where((set) => set['enabled'] == true).length} 个启用规则集 · '
'${{'rule', 'bypass_cn'}.contains(p.mode) && p.feature != 'mesh' ? '按顺序执行' : '已暂停'}',
),
OutlinedButton(
onPressed: disabled
? null
: () => Navigator.push<void>(
context,
MaterialPageRoute(
builder: (_) => RulesPage(
profile: p,
changed: widget.changed,
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('编辑普通规则'),
child: const Text('编辑普通规则'),
),
),
const Divider(height: 48),
Text(
'启动 DNS:${p.dnsServer} · 直连 DNS:${p.directDnsServer.isEmpty ? '启动 DNS' : p.directDnsServer} · 代理 DNS:${p.effectiveProxyDns}',
'DNS 设置',
style: Theme.of(context).textTheme.titleSmall,
),
OutlinedButton(
onPressed: disabled
? null
: () => Navigator.push<void>(
context,
MaterialPageRoute(
builder: (_) => DnsPage(
profile: p,
changed: widget.changed,
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'),
child: const Text('编辑 DNS'),
),
),
const SizedBox(height: 16),
Text(
'FakeIP:${p.fakeIP ? '开启' : '关闭'} · 组网域名自动排除,解析失败无公共 DNS 回退。',
style: const TextStyle(height: 1.5),
),
],
),
@@ -0,0 +1,56 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshport_client_core/meshport_client_core.dart';
import 'package:meshport_lab/features/configuration_pages.dart';
void main() {
testWidgets('bypass mode edits custom rules and retains preset fallback', (
tester,
) async {
final profile = ProxyProfile()
..feature = 'proxy'
..mode = 'bypass_cn'
..defaultAction = 'reject';
var saved = false;
await tester.pumpWidget(
MaterialApp(
home: Builder(
builder: (context) {
return TextButton(
onPressed: () => Navigator.push<void>(
context,
MaterialPageRoute(
builder: (_) =>
RulesPage(profile: profile, changed: () => saved = true),
),
),
child: const Text('open'),
);
},
),
),
);
await tester.tap(find.text('open'));
await tester.pumpAndSettle();
expect(find.textContaining('自定义规则和规则集优先'), findsOneWidget);
expect(find.text('当前模式暂停普通规则'), findsNothing);
expect(find.text('固定默认出口'), findsNothing);
expect(find.textContaining('预设默认出口:所选代理'), findsOneWidget);
await tester.tap(find.text('添加规则'));
await tester.pumpAndSettle();
await tester.enterText(find.byType(TextField), 'example.com');
await tester.tap(find.text('保存规则'));
await tester.pumpAndSettle();
await tester.tap(find.text('保存草稿'));
await tester.pumpAndSettle();
expect(saved, true);
expect(profile.rules.single, {
'type': 'domain_suffix',
'value': 'example.com',
'action': 'proxy',
});
// Keep the custom-mode fallback draft; the engine owns bypass mode's proxy fallback.
expect(profile.defaultAction, 'reject');
expect(profile.mode, 'bypass_cn');
});
}
@@ -0,0 +1,93 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshport_client_core/meshport_client_core.dart';
import 'package:meshport_lab/features/configuration_pages.dart';
void main() {
testWidgets('IPv4 subnet rule offers all four policies and saves mesh', (
tester,
) async {
Map<String, dynamic>? saved;
await tester.pumpWidget(
MaterialApp(
home: Builder(
builder: (context) {
return TextButton(
onPressed: () async {
saved = await Navigator.push<Map<String, dynamic>>(
context,
MaterialPageRoute(builder: (_) => const RuleEditor()),
);
},
child: const Text('open'),
);
},
),
),
);
await tester.tap(find.text('open'));
await tester.pumpAndSettle();
await tester.tap(find.byType(DropdownButtonFormField<String>).first);
await tester.pumpAndSettle();
await tester.tap(find.text('IP CIDR').last);
await tester.pumpAndSettle();
await tester.enterText(find.byType(TextField), '192.168.50.0/24');
await tester.tap(find.byType(DropdownButtonFormField<String>).last);
await tester.pumpAndSettle();
for (final label in ['代理', '直连', '组网', '拒绝']) {
expect(find.text(label), findsWidgets);
}
await tester.tap(find.text('组网').last);
await tester.pumpAndSettle();
expect(find.textContaining('不会自动发布远端网段'), findsOneWidget);
await tester.tap(find.text('保存规则'));
await tester.pumpAndSettle();
expect(saved, {
'type': 'ip_cidr',
'value': '192.168.50.0/24',
'action': 'mesh',
});
});
testWidgets('mesh rule refuses unsupported IPv6 subnet', (tester) async {
await tester.pumpWidget(
const MaterialApp(
home: RuleEditor(
rule: {'type': 'ip_cidr', 'value': 'fd00::/64', 'action': 'mesh'},
),
),
);
await tester.tap(find.text('保存规则'));
await tester.pumpAndSettle();
expect(find.text('组网目前仅支持 IPv4 网段'), findsOneWidget);
});
test('mesh policies and legacy reject survive profile persistence', () {
final profile = ProxyProfile()
..feature = 'combined'
..mode = 'bypass_cn'
..defaultAction = 'mesh'
..rules = [
{'type': 'ip_cidr', 'value': '192.168.50.0/24', 'action': 'mesh'},
{'type': 'domain', 'value': 'blocked.test', 'action': 'reject'},
]
..ruleSets = [
{
'tag': 'group-sites',
'enabled': true,
'action': 'mesh',
'rules': [
{
'domain': ['mesh.test'],
},
],
},
];
final restored = ProxyProfile.fromJson(profile.toJson());
expect(restored.runtime()['rules'], profile.rules);
expect(restored.runtime()['rule_sets'], profile.ruleSets);
expect(restored.defaultAction, 'mesh');
expect(routingPolicyLabel('reject'), '拒绝');
expect(routingPolicyLabel('mesh'), '组网');
});
}
@@ -31,6 +31,18 @@ void main() {
await tester.tap(find.text(label).last);
await tester.pumpAndSettle();
expect(tester.takeException(), isNull, reason: label);
if (label == '代理') {
await tester.ensureVisible(find.text('高级设置'));
await tester.tap(find.text('高级设置'));
await tester.pumpAndSettle();
await tester.ensureVisible(find.text('DNS 设置'));
expect(
tester.takeException(),
isNull,
reason: 'expanded advanced settings',
);
expect(find.textContaining('代理 DNS:'), findsOneWidget);
}
}
});
}