接通双端加密语音录制与播放
This commit is contained in:
1 parent
149028267e
commit
87676ae983
13 files changed
+217
-10
No files matched your search
@@ -0,0 +1,41 @@
|
||||
<script setup lang="ts">
|
||||
import { nextTick, onBeforeUnmount, ref } from 'vue'
|
||||
import { readAttachment } from './attachments'
|
||||
import type { Attachment } from './attachments'
|
||||
import { useSession } from '../session'
|
||||
|
||||
const props = defineProps<{ attachment: Attachment }>()
|
||||
const session = useSession()
|
||||
const src = ref('')
|
||||
const player = ref<HTMLAudioElement>()
|
||||
const error = ref('')
|
||||
const busy = ref(false)
|
||||
let active = true
|
||||
|
||||
async function play() {
|
||||
busy.value = true
|
||||
error.value = ''
|
||||
try {
|
||||
const bytes = await readAttachment(props.attachment, session.authorized)
|
||||
if (!active) return
|
||||
src.value = URL.createObjectURL(new Blob([bytes], { type: 'audio/webm;codecs=opus' }))
|
||||
await nextTick()
|
||||
await player.value?.play().catch(() => {})
|
||||
} catch (cause) {
|
||||
if (active) error.value = cause instanceof Error ? cause.message : '语音加载失败'
|
||||
} finally {
|
||||
busy.value = false
|
||||
}
|
||||
}
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
active = false
|
||||
if (src.value) URL.revokeObjectURL(src.value)
|
||||
})
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<audio v-if="src" ref="player" controls preload="metadata" :src="src" aria-label="语音消息" />
|
||||
<button v-else type="button" class="plain" :disabled="busy" @click="play">{{ busy ? '正在解密语音…' : '播放语音' }}</button>
|
||||
<small v-if="error" class="error" role="alert">{{ error }}</small>
|
||||
</template>
|
||||
@@ -0,0 +1,92 @@
|
||||
<script setup lang="ts">
|
||||
import { onBeforeUnmount, ref } from 'vue'
|
||||
import { voiceFile } from './attachments'
|
||||
|
||||
defineProps<{ disabled?: boolean }>()
|
||||
const emit = defineEmits<{ send: [file: File]; error: [text: string] }>()
|
||||
|
||||
const mimeType = 'audio/webm;codecs=opus'
|
||||
const recording = ref(false)
|
||||
const opening = ref(false)
|
||||
const stopping = ref(false)
|
||||
const elapsed = ref(0)
|
||||
let stream: MediaStream | undefined
|
||||
let recorder: MediaRecorder | undefined
|
||||
let timer: ReturnType<typeof setInterval> | undefined
|
||||
let active = true
|
||||
let deliver = false
|
||||
|
||||
function stop(send: boolean) {
|
||||
deliver = send
|
||||
if (recorder?.state === 'recording') {
|
||||
stopping.value = true
|
||||
recorder.stop()
|
||||
}
|
||||
stream?.getTracks().forEach((track) => track.stop())
|
||||
clearInterval(timer)
|
||||
recording.value = false
|
||||
}
|
||||
|
||||
async function start() {
|
||||
if (!navigator.mediaDevices || !window.MediaRecorder || !MediaRecorder.isTypeSupported(mimeType)) {
|
||||
emit('error', '当前客户端不支持 Opus 录音')
|
||||
return
|
||||
}
|
||||
opening.value = true
|
||||
try {
|
||||
await window.xhchat?.allowVoiceRecording()
|
||||
stream = await navigator.mediaDevices.getUserMedia({ audio: true, video: false })
|
||||
if (!active) {
|
||||
stop(false)
|
||||
return
|
||||
}
|
||||
const parts: Blob[] = []
|
||||
recorder = new MediaRecorder(stream, { mimeType, audioBitsPerSecond: 64_000 })
|
||||
recorder.addEventListener('dataavailable', (event) => {
|
||||
if (event.data.size) parts.push(event.data)
|
||||
})
|
||||
recorder.addEventListener('stop', () => {
|
||||
if (active && deliver && parts.length) emit('send', voiceFile(parts))
|
||||
else if (active && deliver) emit('error', '录音没有采集到音频')
|
||||
recorder = undefined
|
||||
stopping.value = false
|
||||
}, { once: true })
|
||||
recorder.addEventListener('error', () => {
|
||||
stop(false)
|
||||
if (active) emit('error', '录音中断,请重试')
|
||||
}, { once: true })
|
||||
recorder.start(1000)
|
||||
elapsed.value = 0
|
||||
recording.value = true
|
||||
deliver = false
|
||||
timer = setInterval(() => {
|
||||
elapsed.value++
|
||||
if (elapsed.value >= 60) stop(true)
|
||||
}, 1000)
|
||||
} catch {
|
||||
stop(false)
|
||||
if (active) emit('error', '无法使用麦克风,请检查系统和客户端权限')
|
||||
} finally {
|
||||
opening.value = false
|
||||
void window.xhchat?.endVoiceRecording().catch(() => {})
|
||||
}
|
||||
}
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
active = false
|
||||
stop(false)
|
||||
})
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="chat-quote">
|
||||
<template v-if="recording">
|
||||
<span role="status">录音中 {{ elapsed }} / 60 秒</span>
|
||||
<button type="button" class="plain" @click="stop(true)">发送语音</button>
|
||||
<button type="button" class="plain" @click="stop(false)">取消录音</button>
|
||||
</template>
|
||||
<button v-else type="button" class="plain" :disabled="disabled || opening || stopping" @click="start">
|
||||
{{ opening ? '等待麦克风权限…' : '录制语音' }}
|
||||
</button>
|
||||
</div>
|
||||
</template>
|
||||
@@ -11,14 +11,24 @@ export interface Attachment {
|
||||
size: number
|
||||
key: string
|
||||
iv: string
|
||||
voice?: true
|
||||
}
|
||||
const voiceRecordings = new WeakSet<File>()
|
||||
|
||||
export function voiceFile(parts: BlobPart[]) {
|
||||
const file = new File(parts, '语音消息.webm', { type: 'audio/webm;codecs=opus' })
|
||||
voiceRecordings.add(file)
|
||||
return file
|
||||
}
|
||||
|
||||
function attachmentPath(id: string) {
|
||||
return '/attachments/' + encodeURIComponent(id)
|
||||
}
|
||||
|
||||
function authenticatedData(file: Pick<Attachment, 'id' | 'name' | 'size'>) {
|
||||
return encoder.encode(JSON.stringify(['XHChat attachment v1', file.id, file.name, file.size]))
|
||||
function authenticatedData(file: Pick<Attachment, 'id' | 'name' | 'size' | 'voice'>) {
|
||||
const fields = ['XHChat attachment v1', file.id, file.name, file.size]
|
||||
if (file.voice) fields.push('voice')
|
||||
return encoder.encode(JSON.stringify(fields))
|
||||
}
|
||||
|
||||
function safeName(name: string) {
|
||||
@@ -29,7 +39,8 @@ export async function sealAttachment(file: File) {
|
||||
if (file.size > maxSize) throw new Error('附件超过 8 MiB')
|
||||
const key = crypto.getRandomValues(new Uint8Array(32))
|
||||
const iv = crypto.getRandomValues(new Uint8Array(12))
|
||||
const attachment = { id: crypto.randomUUID(), name: safeName(file.name), size: file.size, key: base64(key), iv: base64(iv) }
|
||||
const attachment: Attachment = { id: crypto.randomUUID(), name: safeName(file.name), size: file.size, key: base64(key), iv: base64(iv) }
|
||||
if (voiceRecordings.has(file)) attachment.voice = true
|
||||
try {
|
||||
const imported = await crypto.subtle.importKey('raw', key, 'AES-GCM', false, ['encrypt'])
|
||||
const ciphertext = await crypto.subtle.encrypt({ name: 'AES-GCM', iv, additionalData: authenticatedData(attachment) }, imported, await file.arrayBuffer())
|
||||
@@ -87,6 +98,7 @@ export function decodeMessage(message: Uint8Array): ChatContent {
|
||||
if (!value || typeof value.id !== 'string' || !/^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$/.test(value.id) ||
|
||||
typeof value.name !== 'string' || value.name !== safeName(value.name) ||
|
||||
!Number.isInteger(value.size) || value.size < 0 || value.size > maxSize ||
|
||||
(value.voice !== undefined && value.voice !== true) ||
|
||||
typeof value.key !== 'string' || decode(value.key).length !== 32 ||
|
||||
typeof value.iv !== 'string' || decode(value.iv).length !== 12) {
|
||||
throw new Error('附件描述格式无效')
|
||||
|
||||
@@ -3,7 +3,7 @@ import { mlsBinding, signMls } from '../identity'
|
||||
import { base64, decode, fingerprint, loadEngine } from './engine'
|
||||
import type { DeviceMls } from './engine'
|
||||
import { withVault } from './vault'
|
||||
import { decodeMessage, encodeAttachment, encodeMutation, encodeRead, encodeReply, readAttachment, sealAttachment, uploadAttachment } from './attachments'
|
||||
import { decodeMessage, encodeAttachment, encodeMutation, encodeRead, encodeReply, readAttachment, sealAttachment, uploadAttachment, voiceFile } from './attachments'
|
||||
import type { Attachment, Mutation } from './attachments'
|
||||
|
||||
export interface Peer {
|
||||
@@ -265,7 +265,8 @@ export function localMessageAction(identity: ChatIdentity, line: ChatLine, actio
|
||||
export async function forwardMessage(identity: ChatIdentity, line: ChatLine, recipient: string, device: Peer, call: Call) {
|
||||
if (line.recalled) throw new Error('撤回的消息无法转发')
|
||||
const attachment = line.attachment
|
||||
const content = attachment ? new File([await readAttachment(attachment, call)], attachment.name) : line.text
|
||||
const bytes = attachment ? await readAttachment(attachment, call) : undefined
|
||||
const content = attachment && bytes ? attachment.voice ? voiceFile([bytes]) : new File([bytes], attachment.name) : line.text
|
||||
await send(identity, recipient, device, content, call)
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,8 @@ interface DesktopBridge {
|
||||
apiBase: string
|
||||
signChallenge: (username: string, challenge: string) => Promise<DeviceProof>
|
||||
openBrowser?: (url?: string) => Promise<void>
|
||||
allowVoiceRecording: () => Promise<void>
|
||||
endVoiceRecording: () => Promise<void>
|
||||
signMls: (username: string, kind: 'credential' | 'KeyPackage', user: string, device: string, value: Uint8Array<ArrayBuffer>) => Promise<Uint8Array<ArrayBuffer>>
|
||||
protectMls: (key: string) => Promise<string>
|
||||
unprotectMls: (key: string) => Promise<string>
|
||||
|
||||
@@ -83,6 +83,7 @@ input { width: 100%; color: var(--text); background: var(--base); border: 1px so
|
||||
.chat-lines li { align-self: start; border-radius: 9px; background: var(--base); padding: 8px 11px; max-width: 85%; white-space: pre-wrap; overflow-wrap: anywhere; }
|
||||
.chat-lines li.own { align-self: end; background: var(--green-soft); }
|
||||
.chat-lines small { display: block; text-align: end; }
|
||||
.chat-lines audio { display: block; width: min(260px, 100%); }
|
||||
.chat-compose { display: grid; gap: 8px; }
|
||||
.chat-compose button { justify-self: end; }
|
||||
.chat-filters, .chat-quote, .message-actions { display: flex; gap: 6px; flex-wrap: wrap; align-items: end; }
|
||||
|
||||
@@ -7,6 +7,8 @@ import type { ChatLine, Peer } from '../chat/chat'
|
||||
import type { Contact } from '../contacts'
|
||||
import { downloadAttachment } from '../chat/attachments'
|
||||
import type { Attachment } from '../chat/attachments'
|
||||
import VoiceRecorder from '../chat/VoiceRecorder.vue'
|
||||
import VoiceMessage from '../chat/VoiceMessage.vue'
|
||||
|
||||
const session = useSession()
|
||||
const route = useRoute()
|
||||
@@ -32,6 +34,7 @@ const recent = ref<Record<string, string>>({})
|
||||
const conversationQuery = ref('')
|
||||
const history = ref<HTMLOListElement>()
|
||||
const shareRead = ref(false)
|
||||
const pendingVoice = ref<File>()
|
||||
const view = ref<Awaited<ReturnType<typeof synchronize>>>()
|
||||
const target = computed(() => typeof route.params.user === 'string' ? route.params.user : '')
|
||||
const peer = computed(() => view.value?.peers.find((item) => item.id === selected.value))
|
||||
@@ -209,7 +212,10 @@ function attach(event: Event) {
|
||||
const input = event.target as HTMLInputElement
|
||||
const file = input.files?.[0]
|
||||
input.value = ''
|
||||
if (!file) return
|
||||
if (file) sendFile(file)
|
||||
}
|
||||
|
||||
function sendFile(file: File) {
|
||||
void execute(async () => {
|
||||
if (!session.identity || !peer.value) return
|
||||
await send(session.identity, target.value, peer.value, file, session.authorized)
|
||||
@@ -217,6 +223,16 @@ function attach(event: Event) {
|
||||
})
|
||||
}
|
||||
|
||||
function sendVoice(file: File) {
|
||||
pendingVoice.value = file
|
||||
}
|
||||
|
||||
watch([pendingVoice, busy], ([file, inProgress]) => {
|
||||
if (!file || inProgress) return
|
||||
pendingVoice.value = undefined
|
||||
sendFile(file)
|
||||
})
|
||||
|
||||
function saveAttachment(attachment: Attachment) {
|
||||
void execute(() => downloadAttachment(attachment, session.authorized))
|
||||
}
|
||||
@@ -281,6 +297,7 @@ watch(selected, () => {
|
||||
editing.value = undefined
|
||||
recalling.value = undefined
|
||||
forwarding.value = undefined
|
||||
pendingVoice.value = undefined
|
||||
draft.value = ''
|
||||
scheduleReceipt()
|
||||
})
|
||||
@@ -352,6 +369,7 @@ onBeforeUnmount(() => {
|
||||
:data-unread="!line.own && !line.read && !line.recalled ? line.id : undefined">
|
||||
<blockquote v-if="line.quote">{{ line.quote }}</blockquote>
|
||||
<span v-if="line.recalled" class="muted">消息已撤回</span>
|
||||
<VoiceMessage v-else-if="line.attachment?.voice" :attachment="line.attachment" />
|
||||
<button v-else-if="line.attachment" type="button" class="plain" :disabled="busy"
|
||||
@click="saveAttachment(line.attachment)">下载 {{ line.attachment.name }}</button>
|
||||
<span v-else><template v-for="(part, index) in messageParts(line.text)" :key="index"><button v-if="part.url && internalBrowser"
|
||||
@@ -409,6 +427,7 @@ onBeforeUnmount(() => {
|
||||
<button type="button" class="plain" @click="reply = undefined">取消引用</button>
|
||||
</div>
|
||||
<label v-if="!editing">发送附件<input type="file" :disabled="busy" @change="attach"></label>
|
||||
<VoiceRecorder v-if="!editing" :key="selected" :disabled="busy" @send="sendVoice" @error="error = $event" />
|
||||
<label>消息<textarea v-model="draft" required rows="3" maxlength="4000"></textarea></label>
|
||||
<button class="primary" :disabled="busy || !draft.trim()">{{ editing ? '保存编辑' : '发送' }}</button>
|
||||
</form>
|
||||
|
||||
Reference in new issue
Block a user