Move mesh networking logic into the SDK and fix lifecycle bugs

SDK:
- Add build_config/migrate_config, mesh_cidrs, route_generation, health
  and start_bridged; expose abi_info (capability revision 2).
- Add wait_routes long-poll; stop wakes waiters, and context_free now
  stops the network immediately instead of waiting for waiters to time out.

Hosts:
- Dart: waitRoutes(), MeshProxySession route watcher, ABI capability check.
- Android: fix a deadlock where every VPN request waited on its own
  single-thread worker; requests are now async, status is served from a
  snapshot, and combined mode reports Partial when the mesh stops.
- macOS helper: mesh-only unified mode stops when the mesh is lost;
  installed_subnet_routes excludes the overlay.
- Lab: restore Partial sessions, write migrated profiles back, remove the
  unreachable TOML import dialog; native test now connects via the form.

Docs updated accordingly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shenlei
2026-10-15 18:17:42 +09:00
co-authored by Claude Opus 5.5
parent 29d236f3fe
commit dc1bfc99d2
34 changed files with 1724 additions and 715 deletions
+125 -34
View File
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:io';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshport_flutter/meshport_flutter.dart';
@@ -47,29 +48,75 @@ class Fixture {
);
late final session = MeshProxySession(mesh: mesh, proxy: proxy);
String state = 'Stopped', mode = 'application';
String? ip = '10.77.0.11/24';
bool bridge = false;
// SDK projection: canonical overlay network plus accepted subnets.
String? overlay = '10.77.0.0/24';
List<String> routes = ['192.168.21.0/24'];
List<String> get cidrs => {?overlay, ...routes}.toList()..sort();
int generation = 1;
var _routesChanged = Completer<void>();
void _wake() {
_routesChanged.complete();
_routesChanged = Completer<void>();
}
/// Publishes a route change the way the SDK watch does.
void announceRoutes(List<String> next) {
routes = next;
generation++;
_wake();
}
Future<Map<String, dynamic>> request(Map<String, dynamic> input) async {
final op = input['op'] as String;
if (op == 'wait_routes') {
// Long poll: not logged so lifecycle event assertions stay exact.
if (state == 'Running' && input['after_generation'] == generation) {
await _routesChanged.future;
}
final running = state == 'Running';
return {
'ok': true,
'data': {
'running': running,
'changed': !running || input['after_generation'] != generation,
'route_generation': running ? generation : null,
'mesh_cidrs': running ? cidrs : [],
},
};
}
events.add('mesh.$op');
if (op == 'start_bridged') {
if (mode != 'application') {
return {'ok': false, 'error': 'bridged routing requires application'};
}
if (state != 'Stopped') return {'ok': false, 'error': 'already started'};
}
final data = switch (op) {
'validate' => {'runtime_mode': mode},
'start' => (() {
'start_bridged' => (() {
state = 'Running';
return <String, dynamic>{};
bridge = true;
return <String, dynamic>{
'listen': '127.0.0.1:50001',
'route_generation': generation,
};
})(),
'stop' => (() {
state = 'Stopped';
_wake();
return <String, dynamic>{};
})(),
'socks_stop' => (() {
bridge = false;
return <String, dynamic>{};
})(),
'socks_start' => {'listen': '127.0.0.1:50001'},
'socks_status' => {'running': true},
'status' => {
'state': state,
'ipv4': ip,
'platform_network': {
'subnet_routes_scope': 'accepted_projection',
'subnet_routes': routes,
'platform_network': {'mesh_cidrs': state == 'Running' ? cidrs : []},
'health': {
'engine_running': state == 'Running',
'bridge_running': state == 'Running' && bridge,
'mesh_routable': state == 'Running' && bridge,
},
},
_ => <String, dynamic>{},
@@ -87,7 +134,6 @@ void main() {
expect(f.session.state, 'Running');
expect(f.session.endpoint, '127.0.0.1:10818');
final starts = f.events.where((e) => e.startsWith('proxy.start')).length;
f.routes = ['192.168.21.17/24', '192.168.21.0/24'];
await f.session.refresh();
expect(f.events.where((e) => e.startsWith('proxy.start')).length, starts);
f.routes = ['192.168.22.0/24'];
@@ -107,6 +153,21 @@ void main() {
await expectLater(f.session.start('test'), throwsStateError);
},
);
test('route changes are applied from the SDK watch without a poll', () async {
final f = Fixture();
await f.session.start('test');
expect(f.events.last, 'proxy.start:10.77.0.0/24,192.168.21.0/24');
f.announceRoutes(['192.168.23.0/24']);
for (var i = 0; i < 50 && !f.events.last.contains('192.168.23.0'); i++) {
await Future<void>.delayed(const Duration(milliseconds: 10));
}
expect(f.events.last, 'proxy.start:10.77.0.0/24,192.168.23.0/24');
expect(f.session.state, 'Running');
f.events.clear();
await f.session.stop();
expect(f.events, ['proxy.stop', 'mesh.socks_stop', 'mesh.stop']);
await f.session.close();
});
test('failed engine startup rolls back and permits retry', () async {
final f = Fixture();
f.proxy.failStart = true;
@@ -130,10 +191,10 @@ void main() {
final duplicate = f.session.start('test');
await expectLater(duplicate, throwsStateError);
await first;
expect(f.events.where((e) => e == 'mesh.start').length, 1);
expect(f.events.where((e) => e == 'mesh.start_bridged').length, 1);
await f.session.stop();
f.mode = 'system_vpn';
await expectLater(f.session.start('test'), throwsUnsupportedError);
await expectLater(f.session.start('test'), throwsStateError);
await f.session.close();
},
);
@@ -141,11 +202,11 @@ void main() {
'wait for DHCP then start; unexpected engine failure cleans mesh',
() async {
final f = Fixture();
f.ip = null;
f.overlay = null;
f.routes = [];
await f.session.start('test');
expect(f.session.state, 'AwaitingAddress');
f.ip = '10.77.0.11/24';
f.overlay = '10.77.0.0/24';
await f.session.refresh();
expect(f.session.state, 'Running');
f.proxy.state = 'Failed';
@@ -155,26 +216,54 @@ void main() {
await f.session.close();
},
);
test('mesh prefixes are read from the SDK projection only', () {
expect(
meshPrefixes({
'ipv4': '10.77.0.11/24',
'platform_network': {
'subnet_routes': ['0.0.0.0/0'],
'mesh_cidrs': ['10.77.0.0/24', '192.168.21.0/24'],
},
}),
['10.77.0.0/24', '192.168.21.0/24'],
);
expect(meshPrefixes({'ipv4': '10.77.0.11/24'}), isEmpty);
expect(
() => meshPrefixes({
'platform_network': {
'mesh_cidrs': [7],
},
}),
throwsStateError,
);
});
test('a busy context is refused without stopping its network', () async {
final f = Fixture();
f.state = 'Running';
await expectLater(f.session.start('test'), throwsStateError);
expect(f.events.where((e) => e == 'mesh.stop'), isEmpty);
expect(f.state, 'Running');
await f.session.close();
});
test(
'reject public/overbroad projection and ignore untrusted route scope',
() {
expect(
() => effectiveMeshPrefixes({'ipv4': '8.8.8.8/24'}),
throwsStateError,
);
expect(
() => effectiveMeshPrefixes({'ipv4': '10.0.0.1/8'}),
throwsStateError,
);
expect(
effectiveMeshPrefixes({
'ipv4': '10.77.0.11/24',
'platform_network': {
'subnet_routes': ['0.0.0.0/0'],
},
}),
['10.77.0.0/24'],
'mesh-only session fails when the SDK reports the bridge down',
() async {
final f = Fixture();
await f.session.start('test');
f.bridge = false;
await expectLater(
f.session.refresh(),
throwsA(
isA<StateError>().having(
(e) => e.message,
'message',
'Mesh bridge stopped unexpectedly',
),
),
);
expect(f.session.state, 'Failed');
expect(f.state, 'Stopped');
await f.session.close();
},
);
test(
@@ -189,7 +278,9 @@ void main() {
await session.start('invalid mesh config');
expect(session.state, 'Running');
expect(
f.events.where((e) => e == 'mesh.start' || e == 'mesh.validate'),
f.events.where(
(e) => e.startsWith('mesh.start') || e == 'mesh.validate',
),
isEmpty,
);
expect(f.proxy.lastProfile?['feature'], 'proxy');