Fix VPN internet connectivity and simplify client setup

Initialize Android physical interfaces and preserve macOS scoped routes. Add redacted diagnostics, default DNS, ordered auto-save, simplified controls, scanner rotation settings, and native regression coverage. Restore vendored build sources and document validation and stability checks.
This commit is contained in:
shen
2026-10-12 06:51:25 -07:00
parent 2f185f914d
commit c7cc2aa865
41 changed files with 2841 additions and 247 deletions
@@ -0,0 +1,168 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:meshport_client_core/meshport_client_core.dart';
import 'package:meshport_flutter/meshport_flutter.dart';
const channel = MethodChannel('meshport/system_vpn');
const fixtureHost = String.fromEnvironment('ANDROID_PROXY_FIXTURE');
Future<void> httpsCheck() async {
final client = HttpClient()..connectionTimeout = const Duration(seconds: 20);
try {
final request = await client.getUrl(
Uri.parse('https://www.gstatic.com/generate_204'),
);
final reply = await request.close().timeout(const Duration(seconds: 30));
expect(reply.statusCode, 204);
await reply.drain<void>();
} finally {
client.close(force: true);
}
}
Future<void> proxyTcpCheck() async {
final client = HttpClient()..connectionTimeout = const Duration(seconds: 10);
try {
final request = await client.getUrl(
Uri.parse('http://198.51.100.45:18082/meshport-device-test'),
);
final reply = await request.close().timeout(const Duration(seconds: 15));
expect(reply.statusCode, 200);
expect(await utf8.decoder.bind(reply).join(), 'MeshPort proxy fixture');
} finally {
client.close(force: true);
}
}
Future<void> udpCheck() async {
final socket = await RawDatagramSocket.bind(InternetAddress.anyIPv4, 0);
final reply = Completer<Datagram>();
final subscription = socket.listen((event) {
if (event == RawSocketEvent.read) {
final packet = socket.receive();
if (packet != null && !reply.isCompleted) reply.complete(packet);
}
}, onError: reply.completeError);
final payload = utf8.encode('MeshPort Android UDP');
try {
socket.send(payload, InternetAddress('198.51.100.45'), 18083);
expect(
(await reply.future.timeout(const Duration(seconds: 15))).data,
orderedEquals(payload),
);
} finally {
await subscription.cancel();
socket.close();
}
}
Future<void> tunDnsCheck() async {
final socket = await RawDatagramSocket.bind(InternetAddress.anyIPv4, 0);
final reply = Completer<Datagram>();
final subscription = socket.listen((event) {
if (event == RawSocketEvent.read) {
final packet = socket.receive();
if (packet != null && !reply.isCompleted) reply.complete(packet);
}
}, onError: reply.completeError);
// An uncached name asks the VPN DNS server to use the default encrypted DNS.
final packet = BytesBuilder()
..add([0x4d, 0x50, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0]);
for (final label in 'example.com'.split('.')) {
packet.addByte(label.length);
packet.add(ascii.encode(label));
}
packet.add([0, 0, 1, 0, 1]);
try {
socket.send(packet.toBytes(), InternetAddress('172.19.0.2'), 53);
final data = (await reply.future.timeout(const Duration(seconds: 30))).data;
expect(data.length, greaterThan(12));
expect(data.take(2), [0x4d, 0x50]);
expect(data[3] & 15, 0, reason: 'DNS response must succeed');
expect((data[6] << 8) | data[7], greaterThan(0));
} finally {
await subscription.cancel();
socket.close();
}
}
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
testWidgets(
'Android unified VPN default DNS and proxy data plane',
(tester) async {
expect(fixtureHost, isNotEmpty);
await tester.pumpWidget(
const MaterialApp(
home: Scaffold(body: Center(child: Text('MeshPort Android 代理验证中'))),
),
);
expect(await channel.invokeMethod<bool>('debug_storage_self_test'), true);
await httpsCheck();
debugPrint('ANDROID_BASELINE_HTTPS_PASS');
final profile = ProxyProfile()
..feature = 'proxy'
..mode = 'global'
..entry = 'unified_vpn';
profile.addNodes(
importProxyNodes('socks5://$fixtureHost:18084#Device-test').nodes,
);
profile.validateConnection();
expect(profile.selectedId, profile.nodes.single.id);
expect(profile.effectiveProxyDns, ProxyProfile.defaultProxyDns);
debugPrint('WAITING_FOR_ANDROID_VPN_PERMISSION');
final vpn = await openAndroidSystemVpn(
prepare: () => channel.invokeMethod<void>('prepare_android_vpn'),
request: (request) async =>
jsonDecode(
(await channel.invokeMethod<String>(
'android_vpn_request',
jsonEncode(request),
))!,
)
as Map<String, dynamic>,
);
try {
for (var attempt = 0; attempt < 2; attempt++) {
await vpn.request({
'op': 'start',
'toml': '', // Proxy-only must not require a mesh configuration.
'unified_profile': profile.runtime(),
});
// Wait for system clients to observe the newly established VPN.
await Future<void>.delayed(const Duration(seconds: 2));
final state = await vpn.status();
expect(state['state'], 'Running');
expect(state['platform_network']['ready'], true);
await proxyTcpCheck();
debugPrint('ANDROID_UNIFIED_OS_TCP_PASS_$attempt');
await udpCheck();
debugPrint('ANDROID_UNIFIED_OS_UDP_PASS_$attempt');
await tunDnsCheck();
debugPrint('ANDROID_DEFAULT_DOH_PASS_$attempt');
await httpsCheck();
debugPrint('ANDROID_UNIFIED_HTTPS_PASS_$attempt');
await vpn.stop();
await Future<void>.delayed(const Duration(seconds: 2));
expect((await vpn.status())['state'], 'Stopped');
await httpsCheck();
debugPrint('ANDROID_NETWORK_RESTORED_PASS_$attempt');
}
} catch (_) {
debugPrint(
'ANDROID_UNIFIED_FAILURE_STATE ${jsonEncode(await vpn.status())}',
);
rethrow;
} finally {
await vpn.close();
}
},
timeout: const Timeout(Duration(minutes: 8)),
);
}