- Flutter 桌面端通过 dart:ffi 调用 Rust Core cdylib - 实现 ECDH P-256 密钥协商、HKDF 子密钥派生、AES-256-GCM 控制帧 - Android 被控端用派生密钥做控制帧加密与媒体包 MAC - 新增 libopus JNI 骨架,默认回退 MediaCodec - 新增 aosp/zhanxun/neutral 签名变体与系统 sharedUserId - 本机独立信令服务与设置页开关 - 新增 .gitignore 排除所有编译产物与打包目录
175 lines
5.3 KiB
Dart
175 lines
5.3 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
import 'local_signaling.dart';
|
|
import 'models.dart';
|
|
import 'native_core.dart';
|
|
|
|
class SenderBridge {
|
|
SenderBridge({
|
|
MethodChannel? method,
|
|
EventChannel? events,
|
|
NativeCore? nativeCore,
|
|
}) : _method = method ?? const MethodChannel('ttstd/sender'),
|
|
_events = events ?? const EventChannel('ttstd/sender/events'),
|
|
_native = nativeCore;
|
|
|
|
final MethodChannel _method;
|
|
final EventChannel _events;
|
|
final NativeCore? _native;
|
|
final LocalSignalingProcess _localSignaling = LocalSignalingProcess();
|
|
|
|
NativeCore? get nativeCore => _native;
|
|
|
|
bool get isLocalSignalingRunning => _localSignaling.isRunning;
|
|
|
|
static const mockDevices = [
|
|
DeviceInfo(
|
|
id: 'demo-pixel',
|
|
name: '客厅 Pixel',
|
|
ip: '192.168.1.10',
|
|
mediaPort: 41000,
|
|
controlPort: 41001,
|
|
fingerprint: 'sha256:ab12cd34ef567890',
|
|
capabilities: ['h264', 'opus', 'uibc', 'clipboard', 'file'],
|
|
model: 'Pixel8',
|
|
resolution: '1920x1080',
|
|
),
|
|
DeviceInfo(
|
|
id: 'demo-tablet',
|
|
name: '书房平板',
|
|
ip: '192.168.1.23',
|
|
mediaPort: 42000,
|
|
controlPort: 42001,
|
|
fingerprint: 'sha256:9988776655443322',
|
|
capabilities: ['h265', 'opus', 'uibc'],
|
|
model: 'PadPro',
|
|
resolution: '2560x1600',
|
|
),
|
|
];
|
|
|
|
Stream<ConnectionPhase> watchPhase() {
|
|
return _events
|
|
.receiveBroadcastStream()
|
|
.handleError((Object _) => null)
|
|
.map<ConnectionPhase>(ConnectionPhaseX.fromWire)
|
|
.distinct();
|
|
}
|
|
|
|
Future<List<DeviceInfo>> discover() async {
|
|
try {
|
|
final raw = await _method.invokeListMethod<dynamic>('discover');
|
|
if (raw == null || raw.isEmpty) return mockDevices;
|
|
return DeviceInfo.listFromJson(raw);
|
|
} on MissingPluginException {
|
|
return mockDevices;
|
|
} on PlatformException {
|
|
return mockDevices;
|
|
}
|
|
}
|
|
|
|
Future<void> connect(DeviceInfo device) async {
|
|
await _invokeIgnoringMissing('connect', device.toJson());
|
|
}
|
|
|
|
Future<void> disconnect() async {
|
|
await _invokeIgnoringMissing('disconnect');
|
|
}
|
|
|
|
Future<void> sendControl(String type, Map<String, dynamic> payload) async {
|
|
await _invokeIgnoringMissing('control', {'type': type, ...payload});
|
|
}
|
|
|
|
Future<void> saveSettings(AppSettings settings) async {
|
|
await _invokeIgnoringMissing('settings', settings.toJson());
|
|
}
|
|
|
|
Future<bool> startLocalSignaling(int port) => _localSignaling.start(port: port);
|
|
|
|
Future<void> stopLocalSignaling() => _localSignaling.stop();
|
|
|
|
String localSignalingUrl(int port) => _localSignaling.url(port: port);
|
|
|
|
Future<String> resolveSignalingUrl(AppSettings settings) async {
|
|
if (settings.useLocalSignaling) {
|
|
return _localSignaling.url(port: settings.localSignalingPort);
|
|
}
|
|
return settings.signalingUrl;
|
|
}
|
|
|
|
/// 通过 Rust Core 跑一次真实 ECDH + HKDF + AES-GCM + 媒体 MAC 自检。
|
|
Map<String, dynamic> runCryptoSelfTest() {
|
|
final native = _native;
|
|
if (native == null) {
|
|
return <String, dynamic>{'available': false, 'reason': 'Rust Core 未加载'};
|
|
}
|
|
final alice = native.generateKeyPair();
|
|
final bob = native.generateKeyPair();
|
|
try {
|
|
final aliceSecret = alice.agree(bob.publicBytes);
|
|
final bobSecret = bob.agree(alice.publicBytes);
|
|
final controlKey = native.deriveSubkey(aliceSecret, 'lancast/control/v1');
|
|
final videoKey = native.deriveSubkey(aliceSecret, 'lancast/video/v1');
|
|
final channel = native.createSecureChannel(controlKey);
|
|
final frame = channel.encodePing(1, DateTime.now().millisecondsSinceEpoch);
|
|
final decoded = channel.decode(frame);
|
|
channel.dispose();
|
|
final media = native.mediaEncode(
|
|
1234,
|
|
1,
|
|
videoKey,
|
|
const <int>[1, 2, 3, 4, 5],
|
|
);
|
|
final mediaDecoded = native.mediaDecode(media, videoKey);
|
|
return <String, dynamic>{
|
|
'available': true,
|
|
'ecdhMatch': aliceSecret.join() == bobSecret.join(),
|
|
'controlFrameBytes': frame.length,
|
|
'controlKind': decoded['kind'],
|
|
'mediaPacketBytes': media.length,
|
|
'mediaPts': mediaDecoded['pts'],
|
|
'controlKey': _hexPrefix(controlKey),
|
|
'videoKey': _hexPrefix(videoKey),
|
|
};
|
|
} finally {
|
|
alice.free();
|
|
bob.free();
|
|
}
|
|
}
|
|
|
|
/// 启动 Rust Core 的本机 demo 服务。
|
|
bool startDemoServer() =>
|
|
_native?.startDemoServer() ?? false;
|
|
|
|
void stopDemoServer() => _native?.stopDemoServer();
|
|
|
|
Map<String, dynamic> demoStatus() =>
|
|
_native?.demoStatus() ?? <String, dynamic>{'running': false};
|
|
|
|
static String _hexPrefix(List<int> bytes) => bytes
|
|
.take(4)
|
|
.map((byte) => byte.toRadixString(16).padLeft(2, '0'))
|
|
.join();
|
|
|
|
Future<String> pairingUri() async {
|
|
try {
|
|
final value = await _method.invokeMethod<String>('pairingUri');
|
|
if (value != null && value.isNotEmpty) return value;
|
|
} on MissingPluginException {
|
|
// 桌面/测试环境没有原生桥时给出可扫描的示例 URI。
|
|
} on PlatformException {
|
|
// 同上。
|
|
}
|
|
return 'lancast://pair?ip=192.168.1.10&port=41001&fp=sha256:ab12cd34ef567890';
|
|
}
|
|
|
|
Future<void> _invokeIgnoringMissing(String method, [Object? argument]) async {
|
|
try {
|
|
await _method.invokeMethod<void>(method, argument);
|
|
} on MissingPluginException {
|
|
// 测试或尚未接入 Rust Core 时使用 UI 演示数据。
|
|
}
|
|
}
|
|
}
|