Import apps/android/src/services/secure-transport.ts

This commit is contained in:
QQ 2026-11-14 14:18:40 +08:00
parent 50efa30755
commit 5fb63a547f
1 changed files with 160 additions and 0 deletions

View File

@ -0,0 +1,160 @@
import { NoiseIkSession, createNoisePrologue } from '@dsh-remote/crypto'
import { SecureMessageCodec } from '@dsh-remote/protocol'
import type { RemoteTransport, SecureHandshakeTransport } from '@dsh-remote/webrtc'
import { connectionLog } from './connection-log'
import { strings } from '../locales/i18n'
import type { DeviceIdentity, RemoteDevice } from '../types'
export class SecureTransport implements RemoteTransport {
private noise?: NoiseIkSession
private unsubscribeInner?: () => void
private readonly incoming = new SecureMessageCodec()
private readonly outgoing = new SecureMessageCodec()
private closed = false
/** Noise + SecureMessageCodec counters are not re-entrant; serialize sends. */
private sendTail: Promise<void> = Promise.resolve()
constructor(
private readonly inner: SecureHandshakeTransport,
private readonly identity: DeviceIdentity,
private readonly host: RemoteDevice,
private readonly onHandshakeStarting?: () => void,
) {
if (host.identityKey.length === 0) throw new Error(strings.runtime.hostMissingKey)
}
async connect(): Promise<void> {
this.closed = false
this.incoming.reset()
this.outgoing.reset()
await this.inner.connect()
const info = this.inner.connectionInfo()
if (info.localDeviceId !== this.identity.deviceId || info.remoteDeviceId !== this.host.deviceId) {
await this.inner.close()
throw new Error(strings.runtime.unexpectedRelayDevice)
}
connectionLog('handshake', { phase: 'starting' })
this.onHandshakeStarting?.()
const noise = new NoiseIkSession({
role: 'initiator',
localPrivateKey: this.identity.privateKey,
localPublicKey: this.identity.publicKey,
remotePublicKey: this.host.identityKey,
prologue: createNoisePrologue(info.connectionId, this.host.deviceId, this.identity.deviceId),
})
this.noise = noise
try {
await waitForResponder(this.inner, noise)
this.inner.markSecureReady?.()
connectionLog('handshake', { phase: 'ready' })
} catch (error) {
noise.destroy()
this.noise = undefined
await this.inner.close()
throw error
}
}
async send(data: Uint8Array): Promise<void> {
const run = this.sendTail.then(() => this.sendNow(data))
this.sendTail = run.catch(() => undefined)
return run
}
private async sendNow(data: Uint8Array): Promise<void> {
const plaintextFrames = this.outgoing.encode(data)
try {
for (const plaintext of plaintextFrames) {
await this.inner.send(this.requireNoise().encrypt(plaintext))
}
} catch (error) {
connectionLog('secure-send-failed')
await this.close().catch(() => undefined)
throw error
}
}
onMessage(cb: (data: Uint8Array) => void): () => void {
this.unsubscribeInner?.()
this.unsubscribeInner = this.inner.onMessage(data => {
const noise = this.noise
if (noise === undefined || !noise.complete || this.closed) return
let stage = 'decrypt'
try {
const plaintext = noise.decrypt(data)
stage = 'reassembly'
const message = this.incoming.decode(plaintext)
if (message !== undefined) {
stage = 'message-handler'
cb(message)
}
} catch (error) {
// Only known error classes and numeric bundle positions. Error messages
// and arbitrary stack lines can contain decrypted application content.
const name = error instanceof Error
&& ['Error', 'TypeError', 'RangeError', 'SyntaxError', 'ReferenceError', 'ZodError'].includes(error.name)
? error.name : 'unknown'
connectionLog('secure-receive-failed', { stage, errorName: name })
void this.close()
}
})
return () => {
this.unsubscribeInner?.()
this.unsubscribeInner = undefined
}
}
onClose(handler: () => void): () => void {
return this.inner.onClose?.(handler) ?? (() => undefined)
}
async close(): Promise<void> {
if (this.closed) return
this.closed = true
this.unsubscribeInner?.()
this.unsubscribeInner = undefined
this.incoming.reset()
this.outgoing.reset()
this.noise?.destroy()
this.noise = undefined
await this.inner.close()
}
getStats() { return this.inner.getStats() }
private requireNoise(): NoiseIkSession {
if (this.noise === undefined || !this.noise.complete || this.closed) {
throw new Error(strings.runtime.secureChannelNotConnected)
}
return this.noise
}
}
async function waitForResponder(inner: SecureHandshakeTransport, noise: NoiseIkSession): Promise<void> {
await new Promise<void>((resolve, reject) => {
let settled = false
const timer = setTimeout(() => finish(new Error(strings.runtime.secureHandshakeTimedOut)), 10_000)
const unsubscribe = inner.onHandshake((step, data) => {
if (settled) return
try {
if (step !== 2) throw new Error(strings.runtime.secureHandshakeOrder)
noise.readHandshake(data)
if (!noise.complete) throw new Error(strings.runtime.secureHandshakeIncomplete)
finish()
} catch (error) {
finish(error instanceof Error ? error : new Error(strings.runtime.secureHandshakeFailed))
}
})
const finish = (error?: Error): void => {
if (settled) return
settled = true
clearTimeout(timer)
unsubscribe()
if (error === undefined) resolve()
else reject(error)
}
void inner.sendHandshake(1, noise.writeHandshake()).catch(error => {
finish(error instanceof Error ? error : new Error(strings.runtime.secureHandshakeFailed))
})
})
}