From de722039d6eb6a0437736c4531382a27d019f31a Mon Sep 17 00:00:00 2001 From: xhgit Date: Sat, 14 Nov 2026 09:17:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20Windows=20=E4=B8=8E=20Web?= =?UTF-8?q?=20=E7=AB=AF=E5=88=B0=E7=AB=AF=E5=8A=A0=E5=AF=86=E7=BE=A4?= =?UTF-8?q?=E8=AF=AD=E9=9F=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Desktop/src/main.ts | 12 +++++- README.md | 2 +- docs/progress.md | 8 +++- tests/group-chat.test.mjs | 69 ++++++++++++++++++++++++++++++++-- web/src/chat/GroupChat.vue | 28 +++++++++++++- web/src/chat/VoiceMessage.vue | 12 ++++-- web/src/chat/VoiceRecorder.vue | 7 ++-- web/src/chat/group-chat.ts | 3 +- 8 files changed, 125 insertions(+), 16 deletions(-) diff --git a/Desktop/src/main.ts b/Desktop/src/main.ts index 2143709..e8315ed 100644 --- a/Desktop/src/main.ts +++ b/Desktop/src/main.ts @@ -77,9 +77,14 @@ async function start() { frame.url.split('#')[0] === appURL } let microphoneUntil = 0 + let microphonePage = '' + function recordingPage(url: string) { + if (!url.startsWith(appURL)) return false + return /^#\/(chat|groups)\/[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$/.test(url.slice(appURL.length)) + } 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/') && + url.split('#')[0] === appURL && win.webContents.getURL() === microphonePage && Date.now() <= microphoneUntil && permission === 'media' && types.length === 1 && types[0] === 'audio' } trusted.setPermissionCheckHandler((contents, permission, _origin, details) => @@ -89,13 +94,16 @@ async function start() { 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/')) { + const page = event.senderFrame?.url ?? '' + if (!trustedSender(event) || !recordingPage(page)) { throw new Error('录音请求来源无效') } + microphonePage = page microphoneUntil = Date.now() + 30_000 }) ipcMain.handle('voice:end', (event) => { if (!trustedSender(event)) throw new Error('录音请求来源无效') + microphonePage = '' microphoneUntil = 0 }) ipcMain.handle('device:sign', (event, username: unknown, challenge: unknown) => { diff --git a/README.md b/README.md index 43360c2..4e01c86 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ | 加密文字聊天 | 在两个已核验设备之间发送 MLS 加密消息,支持离线接收。 | | 加密群聊 | 创建群组、邀请联系人和管理成员。每个账号选一台已核验设备参与 MLS 加密文字聊天。 | | 加密附件 | 两端可在私聊和群聊发送、下载最多 8 MiB 的文件,并在本机预览图片和视频。 | -| 加密语音 | 两端可录制和播放最长 60 秒的语音消息。 | +| 加密语音 | 两端可在私聊和群聊录制、取消或发送最长 60 秒的语音消息,接收方在本机解密播放。 | | 消息管理 | 支持会话查找、可选加密已读回执、引用回复、编辑、撤回、转发、本机搜索和收藏。 | | Chromium 浏览器 | Windows 端支持独立窗口、多标签、导航、搜索、书签、历史、下载和隐私窗口,网页运行在独立会话中。 | diff --git a/docs/progress.md b/docs/progress.md index 6fb0519..f5a4b7b 100644 --- a/docs/progress.md +++ b/docs/progress.md @@ -113,4 +113,10 @@ Windows 和 Web 已接入群设备核验、MLS Welcome 校验、文字加解密 ## 群附件 -Windows 和 Web 可发送和下载最多 8 MiB 的群附件,并在本机预览常见图片和视频。接口验收覆盖大小限制、上传幂等、未发布附件隔离、发送设备绑定、移除和重新入群后的历史下载隔离。双端验收覆盖 Windows 发送文件到 Edge、逐字节核对下载、拒绝篡改的密文,以及 Edge 发送 PNG 到 Windows 预览。群附件分片、断点续传和群语音仍待实现。 +Windows 和 Web 可发送和下载最多 8 MiB 的群附件,并在本机预览常见图片和视频。接口验收覆盖大小限制、上传幂等、未发布附件隔离、发送设备绑定、移除和重新入群后的历史下载隔离。双端验收覆盖 Windows 发送文件到 Edge、逐字节核对下载、拒绝篡改的密文,以及 Edge 发送 PNG 到 Windows 预览。群附件分片和断点续传仍待实现。 + +## 加密群语音 + +双端在群聊录制最长 60 秒的 Opus 语音。取消录音即释放麦克风,不产生上传。语音按群附件加密,群世代变更会取消当前录音或拒绝发送上一个世代的录音。接收设备下载并验证密文后,通过本机播放器播放、暂停或拖动进度。语音转写及未听提醒待实现。 + +Windows Electron 与 Edge 集成测试使用虚拟麦克风验证双向播放和暂停、取消录音与世代切换均不上传,以及群成员移除和设备撤销后拒绝下载。私聊语音和桌面浏览器隔离回归通过。 diff --git a/tests/group-chat.test.mjs b/tests/group-chat.test.mjs index a454c68..1c09864 100644 --- a/tests/group-chat.test.mjs +++ b/tests/group-chat.test.mjs @@ -16,8 +16,10 @@ async function register(page, username, password) { await page.getByRole('button', { name: '注册', exact: true }).click() await page.getByLabel('用户名').fill(username) await page.getByLabel('密码', { exact: true }).fill(password) + const registration = page.waitForResponse((item) => item.url().endsWith('/auth/register') && item.status() === 201) await page.locator('button.primary').click() await page.getByText('实时连接已建立').waitFor() + return (await registration).json() } async function connect(owner, member, ownerName, memberName) { @@ -81,24 +83,59 @@ async function sendFile(sender, name, mimeType, contents) { assert.ok(!Buffer.from(packet.wire, 'base64').includes(Buffer.from(name))) } -test('群聊跨 Windows 与 Web 加密投递文字和附件,移除成员后切换会话', { timeout: 120_000 }, async () => { - const browser = await chromium.launch({ channel: process.env.XHCHAT_TEST_BROWSER ?? 'msedge' }) +async function sendVoice(sender) { + const response = sender.waitForResponse((item) => /\/groups\/[^/]+\/attachments\/[^/]+\?epoch=/.test(item.url()) && + item.request().method() === 'POST' && item.status() === 204) + await chat(sender).getByRole('button', { name: '录制语音' }).click() + await chat(sender).getByText(/录音中 1 \/ 60 秒/).waitFor() + await chat(sender).getByRole('button', { name: '发送语音' }).click() + const path = new URL((await response).url()).pathname + return path +} + +async function playVoice(page, sender, downloadPath) { + const message = chat(page).getByRole('listitem').filter({ hasText: sender }).filter({ + has: page.getByRole('button', { name: '播放语音' }), + }) + const download = page.waitForResponse((item) => item.url().endsWith(downloadPath) && item.request().method() === 'GET') + await message.getByRole('button', { name: '播放语音' }).click() + const response = await download + assert.equal(response.status(), 200) + const ciphertext = await response.body() + assert.ok(ciphertext.length > 16) + assert.notDeepEqual(ciphertext.subarray(0, 4), Buffer.from([0x1a, 0x45, 0xdf, 0xa3])) + const player = chat(page).getByRole('listitem').filter({ hasText: sender }).locator('audio[aria-label="语音消息"]') + await player.waitFor() + const src = await player.getAttribute('src') + await page.waitForFunction((url) => [...document.querySelectorAll('.group-chat audio')].some((item) => + item.getAttribute('src') === url && item.readyState >= HTMLMediaElement.HAVE_CURRENT_DATA && item.currentTime > 0), src) + assert.match(src, /^blob:/) + await player.evaluate((element) => element.pause()) + assert.equal(await player.evaluate((element) => element.paused), true) +} + +test('Windows 与 Web 群聊加密投递文字、附件和语音,成员变更后隔离访问', { timeout: 120_000 }, async () => { + const browser = await chromium.launch({ channel: process.env.XHCHAT_TEST_BROWSER ?? 'msedge', args: ['--use-fake-device-for-media-stream'] }) const profile = await mkdtemp(join(tmpdir(), 'xhchat-group-test-')) let desktop const pages = [] try { const [owner, removed] = await Promise.all([browser.newPage(), browser.newPage()]) + await owner.context().grantPermissions(['microphone'], { origin: base }) await Promise.all([owner.goto(base), removed.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() pages.push(owner, windows, removed) + assert.equal(await windows.evaluate(async () => { + try { await window.xhchat.allowVoiceRecording(); return true } catch { return false } + }), false) const names = pages.map(() => 'groupqa_' + id()) const password = randomBytes(24).toString('hex') - await Promise.all(pages.map((page, index) => register(page, names[index], password))) + const [, , removedIdentity] = await Promise.all(pages.map((page, index) => register(page, names[index], password))) await connect(owner, windows, names[0], names[1]) await connect(owner, removed, names[0], names[2]) await work(owner).getByRole('link', { name: '群组' }).click() @@ -166,12 +203,36 @@ test('群聊跨 Windows 与 Web 加密投递文字和附件,移除成员后切 await chat(windows).getByRole('button', { name: '关闭预览' }).click() await image.waitFor({ state: 'detached' }) + const voiceUploads = [] + windows.on('request', (request) => { + if (request.method() === 'POST' && request.url().includes('/attachments/')) voiceUploads.push(request.url()) + }) + await chat(windows).getByRole('button', { name: '录制语音' }).click() + await chat(windows).getByText(/录音中 1 \/ 60 秒/).waitFor() + await chat(windows).getByRole('button', { name: '取消录音' }).click() + const desktopVoice = await sendVoice(windows) + assert.equal(voiceUploads.length, 1) + await playVoice(owner, names[1], desktopVoice) + await playVoice(removed, names[1], desktopVoice) + const webVoice = await sendVoice(owner) + await playVoice(windows, names[0], webVoice) + + await chat(windows).getByRole('button', { name: '录制语音' }).click() + await chat(windows).getByText(/录音中 1 \/ 60 秒/).waitFor() const member = owner.getByRole('list', { name: '群成员' }).getByRole('listitem').filter({ hasText: names[2] }) await member.getByRole('button', { name: '移除成员' }).click() await owner.getByRole('button', { name: '确认操作' }).click() await removed.getByRole('heading', { name: group }).waitFor({ state: 'hidden' }) + const fetchRemovedVoice = () => fetch(base + desktopVoice, { headers: { Authorization: 'Bearer ' + removedIdentity.token } }) + assert.equal((await fetchRemovedVoice()).status, 404) + assert.equal((await fetch(base + '/api/v1/devices/' + removedIdentity.device.id, { + method: 'DELETE', headers: { Authorization: 'Bearer ' + removedIdentity.token }, + })).status, 204) + assert.equal((await fetchRemovedVoice()).status, 401) await chat(windows).getByText('等待群主建立群加密会话。').waitFor() + assert.equal(await chat(windows).getByRole('button', { name: '发送语音' }).count(), 0) await publish(owner, [owner, windows]) + assert.equal(voiceUploads.length, 1) const second = '新世代 Web 群消息 ' + id() assert.notEqual(await send(owner, second), epoch) await chat(windows).getByText(second, { exact: true }).waitFor() diff --git a/web/src/chat/GroupChat.vue b/web/src/chat/GroupChat.vue index 1acca22..915c01c 100644 --- a/web/src/chat/GroupChat.vue +++ b/web/src/chat/GroupChat.vue @@ -4,6 +4,8 @@ import { useSession } from '../session' import { downloadAttachment } from './attachments' import type { Attachment } from './attachments' import MediaPreview from './MediaPreview.vue' +import VoiceMessage from './VoiceMessage.vue' +import VoiceRecorder from './VoiceRecorder.vue' import { enableChat, trust } from './chat' import { discardPendingGroupMessage, groupAttachmentPath, publishGroup, sendGroupMessage, synchronizeGroup } from './group-chat' import type { GroupDetail, GroupDevice } from './group-chat' @@ -16,9 +18,11 @@ const codes = ref>({}) const selected = ref>({}) const draft = ref('') const file = ref() +const pendingVoice = ref<{ file: File; epoch: string }>() const busy = ref(false) const error = ref('') const ready = computed(() => view.value?.ready && view.value.current?.membershipVersion === props.group.membershipVersion) +const recordingEpoch = computed(() => ready.value ? view.value?.current?.id : undefined) const members = computed(() => props.group.members.filter((member) => member.state === 'active' && member.id !== session.identity?.user.id)) const canPublish = computed(() => Boolean(view.value && !view.value.current && members.value.every((member) => view.value?.devices.some((device) => device.user === member.id && device.id === selected.value[member.id] && pinned(device))))) @@ -104,6 +108,10 @@ function sendFile() { }) } +function sendVoice(recording: File, epoch?: string) { + if (epoch) pendingVoice.value = { file: recording, epoch } +} + function download(attachment: Attachment) { void run(() => downloadAttachment(attachment, session.authorized, groupAttachmentPath(props.group, attachment.id))) } @@ -117,6 +125,21 @@ function discard() { } watch(() => props.group.membershipVersion, () => void run(refresh)) +watch([pendingVoice, busy, recordingEpoch, () => view.value?.hasPending], ([voice, inProgress, epoch, hasPending]) => { + const identity = session.identity + if (!voice || !identity) return + if (voice.epoch !== epoch) { + pendingVoice.value = undefined + error.value = '群加密会话已变更,请重新录音' + return + } + if (inProgress || hasPending) return + pendingVoice.value = undefined + void run(async () => { + await sendGroupMessage(identity, props.group, voice.file, session.authorized, voice.epoch) + await refresh() + }) +}) onMounted(() => { timer = setInterval(() => { if (code.value) void run(refresh, false) }, 2500) }) onBeforeUnmount(() => clearInterval(timer)) @@ -159,7 +182,8 @@ onBeforeUnmount(() => clearInterval(timer))
  • {{ line.name }} · {{ new Date(line.time).toLocaleString() }} -