- 支持 Ctrl+V 粘贴配对文本和二维码图片,预览确认后识别校验 - 修复控制发送阻塞、TLS 读写争锁、设备切换沿用旧连接及串流误关窗 - 优化媒体加密上下文、UDP 接收缓冲和异步诊断日志 - 安装器按路径停止主控与信令进程,统一递增版本、打包和清理旧产物 - 发布 0.1.3+4,通过三端编译、20 项 Flutter 测试和 58 项 Rust 单元测试
121 lines
4.9 KiB
Dart
121 lines
4.9 KiB
Dart
import 'dart:io';
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:flutter/services.dart';
|
|
import 'package:image/image.dart' as img;
|
|
import 'package:pasteboard/pasteboard.dart';
|
|
import 'package:zxing2/qrcode.dart';
|
|
|
|
import 'pairing.dart';
|
|
|
|
const maxPairingImageBytes = 16 * 1024 * 1024;
|
|
|
|
class PairingClipboardContent {
|
|
const PairingClipboardContent({this.text, this.image});
|
|
final String? text;
|
|
final Uint8List? image;
|
|
}
|
|
|
|
Future<PairingClipboardContent> readPairingClipboard() async {
|
|
final image = await Pasteboard.image;
|
|
if (image != null && image.isNotEmpty) {
|
|
if (image.length > maxPairingImageBytes)
|
|
throw const FormatException('图片过大,请复制较小的二维码截图(不超过 16 MB)');
|
|
return PairingClipboardContent(image: image);
|
|
}
|
|
final text = (await Clipboard.getData(Clipboard.kTextPlain))?.text;
|
|
if (text != null && text.trim().isNotEmpty) {
|
|
if (text.length > 32768) throw const FormatException('配对文本过长,请只复制配对链接');
|
|
return PairingClipboardContent(text: text);
|
|
}
|
|
for (final path in await Pasteboard.files()) {
|
|
if (!RegExp(r'\.(png|jpe?g|bmp|webp|gif)$', caseSensitive: false)
|
|
.hasMatch(path)) continue;
|
|
final file = File(path);
|
|
if (await file.length() > maxPairingImageBytes)
|
|
throw const FormatException('图片过大,请使用不超过 16 MB 的图片');
|
|
final bytes = await file.readAsBytes();
|
|
if (bytes.length > maxPairingImageBytes)
|
|
throw const FormatException('图片过大');
|
|
return PairingClipboardContent(image: bytes);
|
|
}
|
|
throw const FormatException('剪贴板中没有配对文本或图片,请复制后重试');
|
|
}
|
|
|
|
PairingUri parsePairingText(String text) {
|
|
try {
|
|
final links =
|
|
RegExp(r'''lancast://pair\?[^\s<>"']+''', caseSensitive: false)
|
|
.allMatches(text)
|
|
.toList();
|
|
if (links.length > 1) throw const FormatException('检测到多个配对链接,请只粘贴一个设备的信息');
|
|
final pairing =
|
|
PairingUri.parse(links.isEmpty ? text : links.single.group(0)!);
|
|
if (pairing == null || InternetAddress.tryParse(pairing.ip) == null) {
|
|
throw const FormatException('配对链接无效,请检查 IP 地址和端口');
|
|
}
|
|
bool validDigest(String value) =>
|
|
RegExp(r'^(sha256:)?[0-9a-f]{64}$', caseSensitive: false)
|
|
.hasMatch(value);
|
|
if (!validDigest(pairing.fingerprint))
|
|
throw const FormatException('配对链接缺少有效的设备指纹,请重新复制被控端的配对信息');
|
|
if (pairing.tlsFingerprint.isNotEmpty &&
|
|
!validDigest(pairing.tlsFingerprint)) {
|
|
throw const FormatException('配对链接中的 TLS 指纹无效');
|
|
}
|
|
return pairing;
|
|
} on FormatException {
|
|
rethrow;
|
|
} on Object {
|
|
throw const FormatException('无法解析配对链接,请重新复制');
|
|
}
|
|
}
|
|
|
|
// Called in a background isolate only after the user confirms the preview.
|
|
PairingUri parsePairingImage(Uint8List bytes) {
|
|
if (bytes.isEmpty || bytes.length > maxPairingImageBytes)
|
|
throw const FormatException('图片大小无效');
|
|
final decoder = img.findDecoderForData(bytes);
|
|
final info = decoder?.startDecode(bytes);
|
|
if (info == null) throw const FormatException('无法读取图片,请粘贴 PNG、JPEG 或二维码截图');
|
|
if (info.width <= 0 ||
|
|
info.height <= 0 ||
|
|
info.width * info.height > 16000000) {
|
|
throw const FormatException('图片分辨率过大,请裁剪到二维码区域后重试');
|
|
}
|
|
var image = decoder!.decodeFrame(0);
|
|
if (image == null) throw const FormatException('图片已损坏或不受支持');
|
|
image = img.bakeOrientation(image);
|
|
if (image.width > 2048 || image.height > 2048) {
|
|
image = img.copyResize(image,
|
|
width: image.width >= image.height ? 2048 : null,
|
|
height: image.height > image.width ? 2048 : null);
|
|
}
|
|
// White background also handles transparent QR images correctly.
|
|
final flattened =
|
|
img.Image(width: image.width, height: image.height, numChannels: 4);
|
|
img.fill(flattened, color: img.ColorRgba8(255, 255, 255, 255));
|
|
img.compositeImage(flattened, image);
|
|
var oriented = flattened;
|
|
for (var rotation = 0; rotation < 4; rotation++) {
|
|
final pixels = oriented.getBytes(order: img.ChannelOrder.bgra);
|
|
final source = RGBLuminanceSource(
|
|
oriented.width,
|
|
oriented.height,
|
|
Int32List.view(
|
|
pixels.buffer, pixels.offsetInBytes, pixels.length ~/ 4));
|
|
String? text;
|
|
for (final candidate in [source, source.invert()]) {
|
|
try {
|
|
text = QRCodeReader()
|
|
.decode(BinaryBitmap(HybridBinarizer(candidate)))
|
|
.text;
|
|
break;
|
|
} on ReaderException {/* Try the other orientation/polarity. */}
|
|
}
|
|
if (text != null) return parsePairingText(text);
|
|
oriented = img.copyRotate(oriented, angle: 90);
|
|
}
|
|
throw const FormatException('未识别到二维码,请使用清晰、完整的单个配对二维码图片');
|
|
}
|