Merge pull request '增加 Windows 与 Web 端到端加密群语音' (#26) from xhgit/group-voice into main
Reviewed-on: http://git.xhmcl.com/xhgit/XHChat/pulls/26
This commit was merged in pull request #26.
This commit is contained in:
commit
8a92662e7f
8 files changed
+125
-16
No files matched your search
+10
-2
@@ -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) => {
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
| 加密文字聊天 | 在两个已核验设备之间发送 MLS 加密消息,支持离线接收。 |
|
||||
| 加密群聊 | 创建群组、邀请联系人和管理成员。每个账号选一台已核验设备参与 MLS 加密文字聊天。 |
|
||||
| 加密附件 | 两端可在私聊和群聊发送、下载最多 8 MiB 的文件,并在本机预览图片和视频。 |
|
||||
| 加密语音 | 两端可录制和播放最长 60 秒的语音消息。 |
|
||||
| 加密语音 | 两端可在私聊和群聊录制、取消或发送最长 60 秒的语音消息,接收方在本机解密播放。 |
|
||||
| 消息管理 | 支持会话查找、可选加密已读回执、引用回复、编辑、撤回、转发、本机搜索和收藏。 |
|
||||
| Chromium 浏览器 | Windows 端支持独立窗口、多标签、导航、搜索、书签、历史、下载和隐私窗口,网页运行在独立会话中。 |
|
||||
|
||||
|
||||
+7
-1
@@ -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 集成测试使用虚拟麦克风验证双向播放和暂停、取消录音与世代切换均不上传,以及群成员移除和设备撤销后拒绝下载。私聊语音和桌面浏览器隔离回归通过。
|
||||
@@ -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()
|
||||
|
||||
@@ -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<Record<string, string>>({})
|
||||
const selected = ref<Record<string, string>>({})
|
||||
const draft = ref('')
|
||||
const file = ref<File>()
|
||||
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))
|
||||
</script>
|
||||
@@ -159,7 +182,8 @@ onBeforeUnmount(() => clearInterval(timer))
|
||||
<ul v-if="view?.lines.length" class="group-chat-lines" aria-label="群消息记录">
|
||||
<li v-for="line in view.lines" :key="line.id">
|
||||
<small>{{ line.name }} · {{ new Date(line.time).toLocaleString() }}</small>
|
||||
<template v-if="line.attachment">
|
||||
<VoiceMessage v-if="line.attachment?.voice" :attachment="line.attachment" :path="groupAttachmentPath(group, line.attachment.id)" />
|
||||
<template v-else-if="line.attachment">
|
||||
<p>{{ line.attachment.name }} · {{ new Intl.NumberFormat().format(line.attachment.size) }} 字节</p>
|
||||
<MediaPreview v-if="line.attachment.media" :attachment="line.attachment" :path="groupAttachmentPath(group, line.attachment.id)" />
|
||||
<button type="button" class="plain" :disabled="busy" @click="download(line.attachment)">下载 {{ line.attachment.name }}</button>
|
||||
@@ -176,6 +200,8 @@ onBeforeUnmount(() => clearInterval(timer))
|
||||
<button class="primary" :disabled="busy || view?.hasPending || !draft.trim()">发送群消息</button>
|
||||
</form>
|
||||
<div v-if="ready" class="group-chat-composer">
|
||||
<VoiceRecorder v-if="recordingEpoch" :key="recordingEpoch" :epoch="recordingEpoch" :disabled="busy || view?.hasPending || !!pendingVoice"
|
||||
@send="sendVoice" @error="error = $event" />
|
||||
<label>选择群附件 <input type="file" :disabled="busy || view?.hasPending" @change="chooseFile"></label>
|
||||
<div v-if="file" class="message-actions">
|
||||
<span>{{ file.name }}</span>
|
||||
|
||||
@@ -4,7 +4,7 @@ import { readAttachment } from './attachments'
|
||||
import type { Attachment } from './attachments'
|
||||
import { useSession } from '../session'
|
||||
|
||||
const props = defineProps<{ attachment: Attachment }>()
|
||||
const props = defineProps<{ attachment: Attachment; path?: string }>()
|
||||
const session = useSession()
|
||||
const src = ref('')
|
||||
const player = ref<HTMLAudioElement>()
|
||||
@@ -16,7 +16,7 @@ async function play() {
|
||||
busy.value = true
|
||||
error.value = ''
|
||||
try {
|
||||
const bytes = await readAttachment(props.attachment, session.authorized)
|
||||
const bytes = await readAttachment(props.attachment, session.authorized, props.path)
|
||||
if (!active) return
|
||||
src.value = URL.createObjectURL(new Blob([bytes], { type: 'audio/webm;codecs=opus' }))
|
||||
await nextTick()
|
||||
@@ -28,6 +28,12 @@ async function play() {
|
||||
}
|
||||
}
|
||||
|
||||
function playbackError() {
|
||||
URL.revokeObjectURL(src.value)
|
||||
src.value = ''
|
||||
error.value = '客户端无法播放此语音文件'
|
||||
}
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
active = false
|
||||
if (src.value) URL.revokeObjectURL(src.value)
|
||||
@@ -35,7 +41,7 @@ onBeforeUnmount(() => {
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<audio v-if="src" ref="player" controls preload="metadata" :src="src" aria-label="语音消息" />
|
||||
<audio v-if="src" ref="player" controls preload="metadata" :src="src" aria-label="语音消息" @error="playbackError" />
|
||||
<button v-else type="button" class="plain" :disabled="busy" @click="play">{{ busy ? '正在解密语音…' : '播放语音' }}</button>
|
||||
<small v-if="error" class="error" role="alert">{{ error }}</small>
|
||||
</template>
|
||||
@@ -2,8 +2,8 @@
|
||||
import { onBeforeUnmount, ref } from 'vue'
|
||||
import { voiceFile } from './attachments'
|
||||
|
||||
defineProps<{ disabled?: boolean }>()
|
||||
const emit = defineEmits<{ send: [file: File]; error: [text: string] }>()
|
||||
const props = defineProps<{ disabled?: boolean; epoch?: string }>()
|
||||
const emit = defineEmits<{ send: [file: File, epoch?: string]; error: [text: string] }>()
|
||||
|
||||
const mimeType = 'audio/webm;codecs=opus'
|
||||
const recording = ref(false)
|
||||
@@ -33,6 +33,7 @@ async function start() {
|
||||
return
|
||||
}
|
||||
opening.value = true
|
||||
const epoch = props.epoch
|
||||
try {
|
||||
await window.xhchat?.allowVoiceRecording()
|
||||
stream = await navigator.mediaDevices.getUserMedia({ audio: true, video: false })
|
||||
@@ -46,7 +47,7 @@ async function start() {
|
||||
if (event.data.size) parts.push(event.data)
|
||||
})
|
||||
recorder.addEventListener('stop', () => {
|
||||
if (active && deliver && parts.length) emit('send', voiceFile(parts))
|
||||
if (active && deliver && parts.length) emit('send', voiceFile(parts), epoch)
|
||||
else if (active && deliver) emit('error', '录音没有采集到音频')
|
||||
recorder = undefined
|
||||
stopping.value = false
|
||||
|
||||
@@ -239,7 +239,7 @@ export function discardPendingGroupMessage(identity: ChatIdentity, group: GroupD
|
||||
})
|
||||
}
|
||||
|
||||
export function sendGroupMessage(identity: ChatIdentity, group: GroupDetail, message: string | File, call: Call) {
|
||||
export function sendGroupMessage(identity: ChatIdentity, group: GroupDetail, message: string | File, call: Call, expectedEpoch?: string) {
|
||||
return update(identity, async (state, save, factory) => {
|
||||
const text = typeof message === 'string' ? message.trim() : ''
|
||||
let content: Uint8Array = encoder.encode(text)
|
||||
@@ -248,6 +248,7 @@ export function sendGroupMessage(identity: ChatIdentity, group: GroupDetail, mes
|
||||
const epochs = await call<Epoch[]>(path(group) + '/epochs')
|
||||
const epoch = epochs.find((item) => item.current && item.membershipVersion === group.membershipVersion)
|
||||
if (!epoch || !matchesEpoch(chat.epochs[epoch.id], epoch, group)) throw new Error('群成员或设备已变更,请先建立新世代')
|
||||
if (expectedEpoch && epoch.id !== expectedEpoch) throw new Error('群加密会话已变更,请重新录音')
|
||||
if (chat.pendingMessage) throw new Error('请先刷新以重试待发消息')
|
||||
const id = crypto.randomUUID()
|
||||
const own = chat.epochs[epoch.id]!.roster[identity.device.id]
|
||||
|
||||
Reference in new issue
Block a user