Merge pull request '接通双端加密语音录制与播放' (#17) from xhgit/encrypted-voice-messages into main
Reviewed-on: http://git.xhmcl.com/xhgit/XHChat/pulls/17
This commit was merged in pull request #17.
This commit is contained in:
commit
f41d75e3d0
13 files changed
+217
-10
No files matched your search
+23
-1
@@ -49,7 +49,7 @@ async function start() {
|
||||
url.pathname.endsWith('.wasm') ? 'application/wasm' : 'application/javascript'
|
||||
return new Response(bytes, { headers: {
|
||||
'Content-Type': type,
|
||||
'Content-Security-Policy': browserAsset ? policy : policy + "; connect-src 'self' " + api + ' ' + websocket,
|
||||
'Content-Security-Policy': browserAsset ? policy : policy + "; media-src blob:; connect-src 'self' " + api + ' ' + websocket,
|
||||
'X-Content-Type-Options': 'nosniff',
|
||||
} })
|
||||
} catch {
|
||||
@@ -73,6 +73,28 @@ async function start() {
|
||||
return !win.isDestroyed() && event.sender === win.webContents && frame && frame === win.webContents.mainFrame &&
|
||||
frame.url.split('#')[0] === appURL
|
||||
}
|
||||
let microphoneUntil = 0
|
||||
function voicePermission(contents: Electron.WebContents | null, url: string, mainFrame: boolean, permission: string, types: string[]) {
|
||||
return !win.isDestroyed() && contents === win.webContents && mainFrame &&
|
||||
url.split('#')[0] === appURL && win.webContents.getURL().startsWith(appURL + '#/chat/') &&
|
||||
Date.now() <= microphoneUntil && permission === 'media' && types.length === 1 && types[0] === 'audio'
|
||||
}
|
||||
trusted.setPermissionCheckHandler((contents, permission, _origin, details) =>
|
||||
voicePermission(contents, details.requestingUrl ?? '', details.isMainFrame, permission,
|
||||
details.mediaType ? [details.mediaType] : []))
|
||||
trusted.setPermissionRequestHandler((contents, permission, callback, details) =>
|
||||
callback(voicePermission(contents, details.requestingUrl, details.isMainFrame, permission,
|
||||
'mediaTypes' in details ? details.mediaTypes ?? [] : [])))
|
||||
ipcMain.handle('voice:allow', (event) => {
|
||||
if (!trustedSender(event) || !event.senderFrame?.url.startsWith(appURL + '#/chat/')) {
|
||||
throw new Error('录音请求来源无效')
|
||||
}
|
||||
microphoneUntil = Date.now() + 30_000
|
||||
})
|
||||
ipcMain.handle('voice:end', (event) => {
|
||||
if (!trustedSender(event)) throw new Error('录音请求来源无效')
|
||||
microphoneUntil = 0
|
||||
})
|
||||
ipcMain.handle('device:sign', (event, username: unknown, challenge: unknown) => {
|
||||
if (!trustedSender(event) || typeof username !== 'string' || typeof challenge !== 'string') {
|
||||
throw new Error('设备签名请求来源无效')
|
||||
|
||||
@@ -6,6 +6,8 @@ contextBridge.exposeInMainWorld('xhchat', {
|
||||
apiBase: apiArgument?.slice('--xhchat-api='.length),
|
||||
signChallenge: (username: string, challenge: string) => ipcRenderer.invoke('device:sign', username, challenge),
|
||||
openBrowser: (url?: string) => ipcRenderer.invoke('browser:open', url),
|
||||
allowVoiceRecording: () => ipcRenderer.invoke('voice:allow'),
|
||||
endVoiceRecording: () => ipcRenderer.invoke('voice:end'),
|
||||
signMls: (username: string, kind: string, user: string, device: string, value: Uint8Array) =>
|
||||
ipcRenderer.invoke('device:sign-mls', username, kind, user, device, value),
|
||||
protectMls: (key: string) => ipcRenderer.invoke('device:protect-mls', key),
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
| 联系人 | 按用户名查找、处理好友申请、管理备注与分组,以及拉黑。变更可在双端实时同步。 |
|
||||
| 加密文字聊天 | 在两个已核验设备之间发送 MLS 加密消息,支持离线接收。 |
|
||||
| 加密附件 | 两端可发送和下载最多 8 MiB 的文件,下载时验证完整性。 |
|
||||
| 加密语音 | 两端可录制和播放最长 60 秒的语音消息。 |
|
||||
| 消息管理 | 支持会话查找、可选加密已读回执、引用回复、编辑、撤回、转发、本机搜索和收藏。 |
|
||||
| Chromium 浏览器 | Windows 端支持独立窗口、多标签、导航、搜索、书签、历史、下载和隐私窗口,网页运行在独立会话中。 |
|
||||
|
||||
|
||||
+2
-2
@@ -27,10 +27,10 @@ server {
|
||||
proxy_set_header Host $http_host;
|
||||
}
|
||||
|
||||
add_header Content-Security-Policy "default-src 'none'; script-src 'self' 'wasm-unsafe-eval'; style-src 'self'; connect-src 'self'; form-action 'self'; base-uri 'none'; frame-ancestors 'none'" always;
|
||||
add_header Content-Security-Policy "default-src 'none'; script-src 'self' 'wasm-unsafe-eval'; style-src 'self'; connect-src 'self'; media-src blob:; form-action 'self'; base-uri 'none'; frame-ancestors 'none'" always;
|
||||
add_header Referrer-Policy no-referrer always;
|
||||
add_header X-Content-Type-Options nosniff always;
|
||||
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
|
||||
add_header Permissions-Policy "camera=(), microphone=(self), geolocation=()" always;
|
||||
|
||||
location /api/ {
|
||||
proxy_pass http://api:8080;
|
||||
|
||||
@@ -80,3 +80,7 @@ Windows 浏览器新增可跨窗口保留的本地书签和历史,每类最多
|
||||
## 会话导航和已读回执
|
||||
|
||||
「消息」页支持按用户名或备注查找已接受的联系人,按本机最近消息时间排序。用户可按设备开启已读回执,默认关闭。开启后,仅在窗口聚焦且消息显示于可见区域时,通过 MLS 发送已读消息 ID。发送端根据对方设备身份和原消息 ID 更新状态。偏好、状态与待发回执保存在端侧加密记录中。
|
||||
|
||||
## 加密语音消息
|
||||
|
||||
Windows 和 Web 使用 MediaRecorder 录制 Opus,支持取消、计时和最长 60 秒录音。录音经附件加密链路发送,收件方验证密文后本地播放、暂停或拖动进度。Windows 的通讯渲染层只在主动录音时获得短期麦克风请求权限,第三方浏览器仍使用独立的逐站权限。集成测试使用 Chromium 虚拟麦克风完成 Windows 录音到 Web 播放。语音转写、按住录音和未听提醒尚待实现。
|
||||
@@ -50,7 +50,7 @@ test('Web 与 Windows 安全码校验、加密互发、离线接收和状态恢
|
||||
await web.goto(base)
|
||||
desktop = await _electron.launch({
|
||||
executablePath: electron,
|
||||
args: [join(import.meta.dirname, '..', 'Desktop'), '--user-data-dir=' + profile],
|
||||
args: [join(import.meta.dirname, '..', 'Desktop'), '--user-data-dir=' + profile, '--use-fake-device-for-media-stream'],
|
||||
env: { ...process.env, XHCHAT_SERVER_URL: base },
|
||||
})
|
||||
const windows = await desktop.firstWindow()
|
||||
@@ -185,6 +185,16 @@ test('Web 与 Windows 安全码校验、加密互发、离线接收和状态恢
|
||||
assert.notDeepEqual(forwarded.postDataBuffer(), uploaded)
|
||||
await windows.getByRole('button', { name: '下载 ' + fileName, exact: true }).nth(1).waitFor()
|
||||
|
||||
await windows.getByRole('button', { name: '录制语音', exact: true }).click()
|
||||
await windows.getByRole('button', { name: '发送语音', exact: true }).waitFor()
|
||||
await windows.getByText(/录音中 1 \/ 60 秒/).waitFor()
|
||||
await windows.getByRole('button', { name: '发送语音', exact: true }).click()
|
||||
await web.getByRole('button', { name: '播放语音', exact: true }).click()
|
||||
await web.waitForFunction(() => {
|
||||
const audio = document.querySelector('audio')
|
||||
return audio && audio.readyState >= HTMLMediaElement.HAVE_CURRENT_DATA && audio.currentTime > 0
|
||||
})
|
||||
|
||||
await windows.getByRole('button', { name: '退出登录' }).click()
|
||||
await windows.getByRole('heading', { name: '登录 XHChat' }).waitFor()
|
||||
const offline = '离线补拉 ' + randomBytes(8).toString('hex')
|
||||
|
||||
@@ -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