diff --git a/Desktop/src/main.ts b/Desktop/src/main.ts index 898e669..0396e94 100644 --- a/Desktop/src/main.ts +++ b/Desktop/src/main.ts @@ -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('设备签名请求来源无效') diff --git a/Desktop/src/preload.ts b/Desktop/src/preload.ts index 9855ddf..95af53d 100644 --- a/Desktop/src/preload.ts +++ b/Desktop/src/preload.ts @@ -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), diff --git a/README.md b/README.md index 888c408..fe95ab3 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ | 联系人 | 按用户名查找、处理好友申请、管理备注与分组,以及拉黑。变更可在双端实时同步。 | | 加密文字聊天 | 在两个已核验设备之间发送 MLS 加密消息,支持离线接收。 | | 加密附件 | 两端可发送和下载最多 8 MiB 的文件,下载时验证完整性。 | +| 加密语音 | 两端可录制和播放最长 60 秒的语音消息。 | | 消息管理 | 支持会话查找、可选加密已读回执、引用回复、编辑、撤回、转发、本机搜索和收藏。 | | Chromium 浏览器 | Windows 端支持独立窗口、多标签、导航、搜索、书签、历史、下载和隐私窗口,网页运行在独立会话中。 | diff --git a/deploy/nginx.conf b/deploy/nginx.conf index 1cceea4..aacf2c0 100644 --- a/deploy/nginx.conf +++ b/deploy/nginx.conf @@ -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; diff --git a/docs/progress.md b/docs/progress.md index d476613..bf92323 100644 --- a/docs/progress.md +++ b/docs/progress.md @@ -80,3 +80,7 @@ Windows 浏览器新增可跨窗口保留的本地书签和历史,每类最多 ## 会话导航和已读回执 「消息」页支持按用户名或备注查找已接受的联系人,按本机最近消息时间排序。用户可按设备开启已读回执,默认关闭。开启后,仅在窗口聚焦且消息显示于可见区域时,通过 MLS 发送已读消息 ID。发送端根据对方设备身份和原消息 ID 更新状态。偏好、状态与待发回执保存在端侧加密记录中。 + +## 加密语音消息 + +Windows 和 Web 使用 MediaRecorder 录制 Opus,支持取消、计时和最长 60 秒录音。录音经附件加密链路发送,收件方验证密文后本地播放、暂停或拖动进度。Windows 的通讯渲染层只在主动录音时获得短期麦克风请求权限,第三方浏览器仍使用独立的逐站权限。集成测试使用 Chromium 虚拟麦克风完成 Windows 录音到 Web 播放。语音转写、按住录音和未听提醒尚待实现。 diff --git a/tests/encrypted-chat.test.mjs b/tests/encrypted-chat.test.mjs index ebbb4da..96e8cb1 100644 --- a/tests/encrypted-chat.test.mjs +++ b/tests/encrypted-chat.test.mjs @@ -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') diff --git a/web/src/chat/VoiceMessage.vue b/web/src/chat/VoiceMessage.vue new file mode 100644 index 0000000..71d513e --- /dev/null +++ b/web/src/chat/VoiceMessage.vue @@ -0,0 +1,41 @@ + + + diff --git a/web/src/chat/VoiceRecorder.vue b/web/src/chat/VoiceRecorder.vue new file mode 100644 index 0000000..08fabf9 --- /dev/null +++ b/web/src/chat/VoiceRecorder.vue @@ -0,0 +1,92 @@ + + + diff --git a/web/src/chat/attachments.ts b/web/src/chat/attachments.ts index 1077905..b7baadc 100644 --- a/web/src/chat/attachments.ts +++ b/web/src/chat/attachments.ts @@ -11,14 +11,24 @@ export interface Attachment { size: number key: string iv: string + voice?: true +} +const voiceRecordings = new WeakSet() + +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) { - return encoder.encode(JSON.stringify(['XHChat attachment v1', file.id, file.name, file.size])) +function authenticatedData(file: Pick) { + 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('附件描述格式无效') diff --git a/web/src/chat/chat.ts b/web/src/chat/chat.ts index 5162e3b..381005e 100644 --- a/web/src/chat/chat.ts +++ b/web/src/chat/chat.ts @@ -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) } diff --git a/web/src/identity.ts b/web/src/identity.ts index 6f87416..bceeb5e 100644 --- a/web/src/identity.ts +++ b/web/src/identity.ts @@ -7,6 +7,8 @@ interface DesktopBridge { apiBase: string signChallenge: (username: string, challenge: string) => Promise openBrowser?: (url?: string) => Promise + allowVoiceRecording: () => Promise + endVoiceRecording: () => Promise signMls: (username: string, kind: 'credential' | 'KeyPackage', user: string, device: string, value: Uint8Array) => Promise> protectMls: (key: string) => Promise unprotectMls: (key: string) => Promise diff --git a/web/src/style.css b/web/src/style.css index bad54e6..fa6a7de 100644 --- a/web/src/style.css +++ b/web/src/style.css @@ -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; } diff --git a/web/src/views/ChatView.vue b/web/src/views/ChatView.vue index 551a60b..f47776a 100644 --- a/web/src/views/ChatView.vue +++ b/web/src/views/ChatView.vue @@ -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>({}) const conversationQuery = ref('') const history = ref() const shareRead = ref(false) +const pendingVoice = ref() const view = ref>>() 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">
{{ line.quote }}
消息已撤回 +