增加 Windows 与 Web 端到端加密群语音

This commit is contained in:
xhgit committed 2026-11-14 09:17:52 +08:00
1 parent 9c84b38fd9
commit de722039d6
8 files changed
+125 -16

No files matched your search

+65 -4
View File
@@ -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()