feat: 新增 iPhone 客户端,含隔空投送式点对点互传

三条链路:
- 电脑 → iPhone:扫 QFTSEND2 码下载,流式落盘 + 增量 SHA-256,
  支持 Range 断点续传
- iPhone → 电脑:扫 QFTPAIR2 码后 init/chunk/finish 并行分块上传
- iPhone ↔ iPhone:QFTP2P1 扫码握手,Network.framework 开启
  includePeerToPeer(AirDrop 同款 BLE 发现 + AWDL 点对点 Wi-Fi),
  两台设备不需要连同一个 Wi-Fi,也不需要路由器

P2P 用 TLS-PSK 握手:32 字节预共享密钥由发送端随机生成并写进二维码,
扫码这个动作本身完成带外密钥交换与双向认证,无需证书体系与配对弹窗。
未扫到码的第三方连上来过不了握手。

核心层刻意不 import UIKit,可直接以 macOS 为目标编译测试,
不需要 Xcode 也不需要模拟器,已接入主自检套件,72 项全部通过:
- 端点白名单 11 种拒绝用例(域名、公网、172.32 边界、
  非规范八位组、IPv6、带凭据/路径/query)
- CoreTests/MockPCServer.py 严格复刻 receiver.rs / sender.rs 的线上语义
  (路径、header 名、JSON 字段、Range 行为、chunk 长度校验),
  构成 Swift 与 Rust 两端的线格式契约测试,任一端改协议即会失败
- 真实 TLS-PSK 握手下的 P2P 端到端传输,含错误密钥必须被拒

iOS 平台限制(已在 ios/README.md 说明):
NEHotspotConfiguration 需要付费账号 entitlement,故不实现 Direct AP
流程,与电脑互传要求同一局域网;AWDL 是苹果私有链路,Windows 无法参与。
上传块固定 4 MiB 而非 Android 的 4/8/16 MiB,因为 iOS 上
worker 数 × 块大小即内存峰值。

未验证部分:本机只有 Command Line Tools,无 Xcode 与 iOS SDK,
UI 层(SwiftUI/UIKit/AVFoundation)未经编译;.xcodeproj 已通过
plutil 解析与引用完整性校验(42 个对象无悬空引用),
但未在 Xcode 中实际打开过。
This commit is contained in:
zhy200308
2026-08-11 09:08:37 +08:00
parent ea7b678b37
commit 4d77ba6c91
25 changed files with 3633 additions and 0 deletions
+197
View File
@@ -0,0 +1,197 @@
import SwiftUI
import UniformTypeIdentifiers
struct HomeView: View {
/// 同一个 view 上挂多个 .fileImporter 会互相覆盖,只能有一个,
/// 所以用这个枚举区分「选完文件之后要干什么」。
private enum PickPurpose: Equatable {
case peerSend
case uploadToPC(PairingPayload)
}
@StateObject private var model = TransferModel()
@State private var pickPurpose: PickPurpose?
var body: some View {
NavigationStack {
content
.navigationTitle("QFT 快传")
.navigationBarTitleDisplayMode(.inline)
}
.fullScreenCover(isPresented: isScanning) {
ScannerView(onFound: { model.handleScanned($0) }, onCancel: { model.cancel() })
.ignoresSafeArea()
}
.fileImporter(isPresented: isPicking,
allowedContentTypes: [UTType.item], allowsMultipleSelection: false) { result in
let purpose = pickPurpose
pickPurpose = nil
guard case .success(let urls) = result, let url = urls.first else {
model.reset()
return
}
switch purpose {
case .peerSend: model.startPeerSend(fileURL: url)
case .uploadToPC(let pairing): model.startUpload(pairing, fileURL: url)
case nil: break
}
}
.onAppear { model.refreshReceivedFiles() }
.onChange(of: model.stage) { _, stage in
// 扫到 QFTPAIR2 之后才知道要选文件,所以选择器由 stage 驱动
if case .pickFileForUpload(let pairing) = stage {
pickPurpose = .uploadToPC(pairing)
}
}
}
private var isScanning: Binding<Bool> {
Binding(get: { model.stage == .scanning }, set: { if !$0 { model.cancel() } })
}
private var isPicking: Binding<Bool> {
Binding(get: { pickPurpose != nil }, set: { if !$0 { pickPurpose = nil } })
}
@ViewBuilder
private var content: some View {
switch model.stage {
case .idle, .scanning, .pickFileForUpload:
landing
case .connecting(let message):
StatusCard(icon: "antenna.radiowaves.left.and.right", title: message) {
ProgressView()
Button("取消", role: .cancel) { model.cancel() }
}
case .running(let message):
StatusCard(icon: "arrow.left.arrow.right", title: message) {
if let progress = model.progress {
ProgressView(value: progress.fraction)
Text("\(progress.readableSize) · \(progress.readableRate)")
.font(.footnote.monospacedDigit())
.foregroundStyle(.secondary)
} else {
ProgressView()
}
Button("取消", role: .cancel) { model.cancel() }
}
case .showingPeerCode(let pairing):
peerCode(pairing)
case .finished(let message, let url):
StatusCard(icon: "checkmark.circle.fill", title: message) {
if let url {
Text(url.lastPathComponent).font(.footnote).foregroundStyle(.secondary)
ShareLink(item: url) { Label("分享", systemImage: "square.and.arrow.up") }
}
Button("完成") { model.reset() }.buttonStyle(.borderedProminent)
}
case .failed(let message):
StatusCard(icon: "exclamationmark.triangle.fill", title: "传输失败") {
Text(message).font(.footnote).multilineTextAlignment(.center)
.foregroundStyle(.secondary)
Button("返回") { model.reset() }.buttonStyle(.borderedProminent)
}
}
}
private var landing: some View {
List {
Section {
ActionRow(icon: "qrcode.viewfinder", title: "扫码",
detail: "扫电脑上的二维码收发文件,或扫另一台 iPhone 的码接收") {
model.beginScan()
}
ActionRow(icon: "iphone.gen3.radiowaves.left.and.right", title: "发给附近的 iPhone",
detail: "选文件后显示二维码,对方扫一下即可,无需连同一个 Wi-Fi") {
pickPurpose = .peerSend
}
} footer: {
Text("与电脑互传需要手机和电脑连在同一个 Wi-Fi。iPhone 之间使用点对点直连,不经过路由器。")
}
if !model.receivedFiles.isEmpty {
Section("已接收") {
ForEach(model.receivedFiles, id: \.self) { url in
ShareLink(item: url) {
Label(url.lastPathComponent, systemImage: "doc")
.lineLimit(1)
}
}
}
}
}
}
private func peerCode(_ pairing: PeerPairing) -> some View {
VStack(spacing: 20) {
Text("让对方用 QFT 扫这个码")
.font(.headline)
if let image = QRRenderer.image(from: pairing.encode()) {
Image(uiImage: image)
.interpolation(.none)
.resizable()
.scaledToFit()
.frame(maxWidth: 300)
.padding()
.background(.white)
.clipShape(RoundedRectangle(cornerRadius: 16))
}
VStack(spacing: 4) {
Text(pairing.fileName).font(.subheadline).lineLimit(1)
Text(TransferProgress.readable(pairing.fileSize))
.font(.footnote).foregroundStyle(.secondary)
}
if let progress = model.progress {
ProgressView(value: progress.fraction)
.padding(.horizontal, 40)
Text("\(progress.readableSize) · \(progress.readableRate)")
.font(.footnote.monospacedDigit()).foregroundStyle(.secondary)
} else {
Label("等待对方扫码…", systemImage: "hourglass")
.font(.footnote).foregroundStyle(.secondary)
}
Button("取消", role: .cancel) { model.cancel() }
}
.padding()
}
}
private struct ActionRow: View {
let icon: String
let title: String
let detail: String
let action: () -> Void
var body: some View {
Button(action: action) {
HStack(spacing: 14) {
Image(systemName: icon)
.font(.title2)
.frame(width: 34)
VStack(alignment: .leading, spacing: 3) {
Text(title).font(.body.weight(.medium))
Text(detail).font(.caption).foregroundStyle(.secondary)
}
}
.padding(.vertical, 4)
}
.buttonStyle(.plain)
}
}
private struct StatusCard<Content: View>: View {
let icon: String
let title: String
@ViewBuilder let content: Content
var body: some View {
VStack(spacing: 18) {
Image(systemName: icon).font(.system(size: 44))
Text(title).font(.headline).multilineTextAlignment(.center)
content
}
.padding(32)
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}