Initial MeshPort SDK and cross-platform client
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
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_flutter/meshport_flutter.dart';
|
||||
|
||||
const channel = MethodChannel('meshport/system_vpn');
|
||||
const target = '10.253.79.1:17007';
|
||||
String config(String mode) =>
|
||||
'''
|
||||
hostname = "Android-validation"
|
||||
ipv4 = "10.253.83.11/24"
|
||||
listeners = []
|
||||
stun_servers = []
|
||||
stun_servers_v6 = []
|
||||
[[peer]]
|
||||
uri = "tcp://127.0.0.1:18080"
|
||||
[network_identity]
|
||||
network_name = "android-validation"
|
||||
network_secret = "test-only"
|
||||
[runtime]
|
||||
mode = "$mode"
|
||||
''';
|
||||
|
||||
Future<void> waitForRoutes(
|
||||
MeshPortClient client, {
|
||||
bool withdrawn = false,
|
||||
bool installed = false,
|
||||
}) async {
|
||||
final deadline = DateTime.now().add(const Duration(seconds: 40));
|
||||
while (DateTime.now().isBefore(deadline)) {
|
||||
final state = await client.status();
|
||||
final platform = state['platform_network'] as Map? ?? {};
|
||||
final routes =
|
||||
platform[installed ? 'installed_subnet_routes' : 'subnet_routes']
|
||||
as List? ??
|
||||
[];
|
||||
if (withdrawn ? routes.isEmpty : routes.contains('10.253.79.0/24')) return;
|
||||
await Future<void>.delayed(const Duration(milliseconds: 200));
|
||||
}
|
||||
throw StateError('Automatic subnet route transition timed out');
|
||||
}
|
||||
|
||||
void main() {
|
||||
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
|
||||
testWidgets(
|
||||
'Android native SDK, secure storage and system VPN data plane',
|
||||
(tester) async {
|
||||
await tester.pumpWidget(
|
||||
const MaterialApp(
|
||||
home: Scaffold(body: Center(child: Text('MeshPort Android 验证中'))),
|
||||
),
|
||||
);
|
||||
expect(await channel.invokeMethod<bool>('debug_storage_self_test'), true);
|
||||
debugPrint('ANDROID_KEYSTORE_PASS');
|
||||
final client = await MeshPortClient.open();
|
||||
try {
|
||||
await client.start(config('application'));
|
||||
await waitForRoutes(client);
|
||||
final stream = await client.connectTcp(target);
|
||||
final payload = Uint8List.fromList(utf8.encode('Android SDK 测试'));
|
||||
try {
|
||||
await client.writeTcp(stream, payload);
|
||||
expect(await client.readTcp(stream), orderedEquals(payload));
|
||||
} finally {
|
||||
await client.closeResource(stream);
|
||||
}
|
||||
final reply = await client.probe(target, udp: true, echo: true);
|
||||
expect(reply['verified_bytes'], greaterThan(0));
|
||||
debugPrint('ANDROID_APPLICATION_TCP_UDP_PASS');
|
||||
} finally {
|
||||
await client.close();
|
||||
}
|
||||
|
||||
debugPrint('WAITING_FOR_ANDROID_VPN_PERMISSION');
|
||||
final vpn = await openAndroidSystemVpn(
|
||||
prepare: () async {
|
||||
await channel.invokeMethod<void>('prepare_android_vpn');
|
||||
},
|
||||
request: (request) async =>
|
||||
jsonDecode(
|
||||
(await channel.invokeMethod<String>(
|
||||
'android_vpn_request',
|
||||
jsonEncode(request),
|
||||
))!,
|
||||
)
|
||||
as Map<String, dynamic>,
|
||||
);
|
||||
try {
|
||||
await vpn.start(config('system_vpn'));
|
||||
await waitForRoutes(vpn, installed: true);
|
||||
// Plain OS sockets prove that traffic from other applications can use
|
||||
// the VPN. No SDK stream is used for these transfers.
|
||||
final socket = await Socket.connect(
|
||||
'10.253.79.1',
|
||||
17007,
|
||||
timeout: const Duration(seconds: 10),
|
||||
);
|
||||
try {
|
||||
socket.add(utf8.encode('OS TCP Android'));
|
||||
await socket.flush();
|
||||
expect(
|
||||
utf8.decode(
|
||||
await socket.first.timeout(const Duration(seconds: 10)),
|
||||
),
|
||||
'OS TCP Android',
|
||||
);
|
||||
} finally {
|
||||
socket.destroy();
|
||||
}
|
||||
final udp = await RawDatagramSocket.bind(InternetAddress.anyIPv4, 0);
|
||||
try {
|
||||
final reply = udp.firstWhere((event) => event == RawSocketEvent.read);
|
||||
udp.send(
|
||||
utf8.encode('OS UDP Android'),
|
||||
InternetAddress('10.253.79.1'),
|
||||
17007,
|
||||
);
|
||||
await reply.timeout(const Duration(seconds: 10));
|
||||
expect(utf8.decode(udp.receive()!.data), 'OS UDP Android');
|
||||
} finally {
|
||||
udp.close();
|
||||
}
|
||||
debugPrint('ANDROID_SYSTEM_VPN_TCP_UDP_PASS');
|
||||
final control = await Socket.connect('127.0.0.1', 18081);
|
||||
try {
|
||||
control.add(utf8.encode('withdraw'));
|
||||
await control.flush();
|
||||
expect(utf8.decode(await control.first), contains('withdrawn'));
|
||||
} finally {
|
||||
control.destroy();
|
||||
}
|
||||
await waitForRoutes(vpn, withdrawn: true);
|
||||
await waitForRoutes(vpn, withdrawn: true, installed: true);
|
||||
debugPrint('ANDROID_AUTOMATIC_ROUTE_WITHDRAW_PASS');
|
||||
} finally {
|
||||
await vpn.close();
|
||||
}
|
||||
},
|
||||
timeout: const Timeout(Duration(minutes: 10)),
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user