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
@@ -47,6 +47,9 @@ Map<String, dynamic> _call(String path, int handle, String request) {
}
}
/// Lowest native capability revision this binding drives (`abi_info`).
const int minimumCapabilityRevision = 2;
class MeshPortClient {
final String libraryPath;
final int _handle;
@@ -79,9 +82,25 @@ class MeshPortClient {
)();
});
if (handle == 0) throw StateError('Could not create MeshPort context');
return MeshPortClient._(path, handle);
final client = MeshPortClient._(path, handle);
int? revision;
try {
revision = (await client.abiInfo())['revision'] as int?;
} catch (_) {}
if (revision == null || revision < minimumCapabilityRevision) {
await client.close();
throw StateError(
'MeshPort native library at $path is too old '
'(capability revision ${revision ?? 'unknown'}, '
'need $minimumCapabilityRevision); rebuild meshport-ffi',
);
}
return client;
}
/// `{abi, revision, operations}` of the loaded native library.
Future<Map<String, dynamic>> abiInfo() => request({'op': 'abi_info'});
Future<Map<String, dynamic>> request(Map<String, dynamic> request) async {
if (_closed) throw StateError('MeshPort context is closed');
final path = libraryPath, handle = _handle, text = jsonEncode(request);
@@ -95,18 +114,72 @@ class MeshPortClient {
return response['data'] as Map<String, dynamic>;
}
Future<void> validate(String toml) async {
await request({'op': 'validate', 'toml': toml});
/// Returns the overlay projection. [checkLocalOverlap] also rejects an
/// overlay that collides with an existing interface on this device.
Future<Map<String, dynamic>> validate(
String toml, {
bool checkLocalOverlap = false,
}) => request({
'op': 'validate',
'toml': toml,
if (checkLocalOverlap) 'check_local_overlap': true,
});
/// Builds the SDK-accepted TOML from a structured profile draft. Mode,
/// transport and encryption invariants are applied by the SDK.
Future<String> buildConfig(Map<String, dynamic> profile) async =>
(await request({'op': 'build_config', 'profile': profile}))['toml']
as String;
/// Removes options earlier releases saved; returns [toml] unchanged when
/// nothing needed migrating.
Future<String> migrateConfig(String toml) async =>
(await request({'op': 'migrate_config', 'toml': toml}))['toml'] as String;
Future<void> start(String toml, {bool checkLocalOverlap = false}) async {
await request({
'op': 'start',
'toml': toml,
if (checkLocalOverlap) 'check_local_overlap': true,
});
}
Future<void> start(String toml) async {
await request({'op': 'start', 'toml': toml});
}
/// Starts an application-mode network and its local TCP/UDP bridge as one
/// operation; a failure leaves neither running. Returns `listen`, `overlay`,
/// `mesh_cidrs` and `route_generation`.
Future<Map<String, dynamic>> startBridged(
String toml, {
int port = 0,
int maxSessions = 128,
bool checkLocalOverlap = false,
bool requireStaticOverlay = false,
}) => request({
'op': 'start_bridged',
'toml': toml,
'port': port,
'max_sessions': maxSessions,
if (checkLocalOverlap) 'check_local_overlap': true,
if (requireStaticOverlay) 'require_static_overlay': true,
});
Future<void> stop() async {
await request({'op': 'stop'});
}
/// Long-polls mesh routes: returns as soon as `route_generation` differs
/// from [afterGeneration] or the network stops, otherwise after [timeout]
/// with `changed: false`. Runs outside the context's serial lock, so other
/// requests proceed meanwhile. Returns `running`, `changed`,
/// `route_generation`, `mesh_cidrs` and `subnet_routes`.
Future<Map<String, dynamic>> waitRoutes({
int? afterGeneration,
Duration timeout = const Duration(seconds: 25),
}) => request({
'op': 'wait_routes',
if (afterGeneration != null) 'after_generation': afterGeneration,
'timeout_ms': timeout.inMilliseconds,
});
/// Local IPv4 TCP/UDP bridge for a host proxy engine. Application mode only.
/// Data travels on native sockets, outside the serial JSON I/O interface.
Future<Map<String, dynamic>> startSocksBridge({