Improve VPN video routing, profile storage and authorization reuse
This commit is contained in:
@@ -4,6 +4,102 @@ import 'dart:ffi';
|
||||
import 'dart:io';
|
||||
import 'client.dart';
|
||||
|
||||
/// Reuses one authenticated helper while its owning app is running. Stopping
|
||||
/// the engine releases VPN resources; closing this session also closes IPC.
|
||||
/// No service is installed and no credentials are stored by this class.
|
||||
class MacSystemVpnSession {
|
||||
MacSystemVpnSession({
|
||||
required this.open,
|
||||
this.keepAlive = const Duration(seconds: 30),
|
||||
});
|
||||
final Future<MeshPortClient> Function() open;
|
||||
final Duration keepAlive;
|
||||
MeshPortClient? _client;
|
||||
Future<MeshPortClient>? _opening;
|
||||
Timer? _heartbeat;
|
||||
bool _disposed = false, _checking = false;
|
||||
|
||||
Future<MeshPortClient> acquire() async {
|
||||
if (_disposed) throw StateError('VPN authorization session is closed');
|
||||
_heartbeat?.cancel();
|
||||
final pending = _opening ??= _acquire();
|
||||
try {
|
||||
final client = await pending;
|
||||
if (_disposed) throw StateError('VPN authorization session is closed');
|
||||
return client;
|
||||
} finally {
|
||||
if (identical(_opening, pending)) _opening = null;
|
||||
}
|
||||
}
|
||||
|
||||
Future<MeshPortClient> _acquire() async {
|
||||
final cached = _client;
|
||||
if (cached != null) {
|
||||
try {
|
||||
await cached.status();
|
||||
return cached;
|
||||
} catch (_) {
|
||||
_client = null;
|
||||
try {
|
||||
await cached.close();
|
||||
} catch (_) {}
|
||||
}
|
||||
}
|
||||
final next = await open();
|
||||
if (_disposed) {
|
||||
await next.close();
|
||||
throw StateError('VPN authorization session is closed');
|
||||
}
|
||||
return _client = next;
|
||||
}
|
||||
|
||||
Future<void> release(MeshPortClient client) async {
|
||||
if (!identical(client, _client) || _disposed) {
|
||||
await client.close();
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await client.stop();
|
||||
} catch (_) {
|
||||
_client = null;
|
||||
try {
|
||||
await client.close();
|
||||
} catch (_) {}
|
||||
rethrow;
|
||||
}
|
||||
if (_disposed || !identical(_client, client)) return;
|
||||
_heartbeat?.cancel();
|
||||
_heartbeat = Timer.periodic(keepAlive, (_) async {
|
||||
if (_checking || _disposed || !identical(_client, client)) return;
|
||||
_checking = true;
|
||||
try {
|
||||
await client.status();
|
||||
} catch (_) {
|
||||
if (identical(_client, client)) {
|
||||
_client = null;
|
||||
_heartbeat?.cancel();
|
||||
}
|
||||
try {
|
||||
await client.close();
|
||||
} catch (_) {}
|
||||
} finally {
|
||||
_checking = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> close() async {
|
||||
_disposed = true;
|
||||
_heartbeat?.cancel();
|
||||
try {
|
||||
await _opening;
|
||||
} catch (_) {}
|
||||
final client = _client;
|
||||
_client = null;
|
||||
await client?.close();
|
||||
}
|
||||
}
|
||||
|
||||
/// Android's host supplies VpnService authorization and a serialized control
|
||||
/// channel. Keeping Flutter APIs out of this library permits non-Flutter hosts.
|
||||
Future<MeshPortClient> openAndroidSystemVpn({
|
||||
|
||||
Reference in New Issue
Block a user