支持私聊消息加密转发到群聊

This commit is contained in:
xhgit committed 2026-11-14 14:52:22 +08:00
1 parent 20e179a877
commit 9028ba0eed
8 files changed
+99 -62

No files matched your search

+1 -1
View File
@@ -22,7 +22,7 @@
| 加密群聊 | 创建群组、邀请联系人和管理成员。每个账号选一台已核验设备参与 MLS 加密文字聊天。 |
| 加密附件 | 两端可在私聊和群聊发送、下载最多 8 MiB 的文件,并在本机预览图片和视频。 |
| 加密语音 | 两端可在私聊和群聊录制、取消或发送最长 60 秒的语音消息,接收方在本机解密播放。 |
| 消息管理 | 私聊和群聊支持引用回复、编辑文字、撤回、本机搜索和收藏。群消息可转发到其他已建立加密会话的群或已核验的联系人设备。私聊支持转发给联系人及可选加密已读回执。 |
| 消息管理 | 私聊和群聊支持引用回复、编辑文字、撤回、本机搜索和收藏。消息可转发到已建立加密会话的群或已核验的联系人设备。私聊支持可选加密已读回执。 |
| Chromium 浏览器 | Windows 端支持独立窗口、多标签、导航、搜索、书签、历史、下载和隐私窗口,网页运行在独立会话中。 |
当前为开发预览。大文件分片、音视频和多设备历史同步仍在开发,Windows 安装包尚未发布。 [开发进度与验收记录](docs/progress.md)
+7 -1
View File
@@ -19,7 +19,7 @@ Web 和 Electron 共用 Vue 客户端,独立接入同一 Go API。真实 Postg
## 尚待实现
- 阶段一还需邮箱和手机号注册、验证码服务、找回密码和双重验证。真实联调需要 SMTP 与短信供应商配置。
- 阶段二与三还需分片附件、私聊转发至群聊、多设备可信授权、加密备份与跨设备历史同步。群聊和私聊的文字及小附件已进入开发预览。
- 阶段二与三还需分片附件、多设备可信授权、加密备份与跨设备历史同步。群聊和私聊的文字及小附件已进入开发预览。
- 阶段四还需 WebRTC 音视频、TURN、SFU、屏幕共享及媒体 E2EE。
- 阶段五还需反钓鱼及聊天联动。
- 阶段六到八还需社交、扩展生态、独立管理后台、推送、生产部署与备份演练、压力测试、安全评估、签名安装包和发布流程。
@@ -138,3 +138,9 @@ Windows Electron 与 Edge 集成测试覆盖搜索引用和附件名、离开群
双端可将群消息转发到其他已加入且已核验的群加密会话,或转发到已核验的联系人设备。转发携带文字正文或附件。图片、视频和语音保留原有媒体类型。附件经本机完整性校验和解密,再以新的附件标识、文件密钥与随机数加密上传。待投递的群转发可在目标群继续重试。原消息的引用片段和发送人信息不随转发发送。
Windows Electron 与 Edge 验证向尚无加密会话的群转发会被拒绝、转发群文件后附件标识和密文不同且下载内容逐字节相同,以及群文字转发到联系人私聊。私聊附件转发回归通过。
## 私聊转发至群聊
Windows 和 Web 的私聊与群聊共用转发目标选择。私聊支持把文字正文和附件转发给已核验的联系人设备,或转发到本机已加入并核验的群加密会话。客户端先核对目标群会话,再读取本机消息或下载附件密文。附件通过认证后使用新的标识和密钥二次加密,目标群世代变更会阻断转发。
集成测试验证 Web 转发私聊文字后 Windows 在群内接收,Windows 转发私聊文件后 Web 下载逐字节一致且密文及附件标识变化。未建立加密会话的目标群拒绝转发,测试确认没有群文件上传或群消息提交。原有私聊及群聊转发回归通过。
+51
View File
@@ -373,6 +373,57 @@ test('Windows 与 Web 加密群聊支持消息操作、附件、语音和成员
await owner.getByRole('link', { name: names[1] }).click()
await owner.getByRole('button', { name: '启用本机加密聊天' }).click()
await owner.getByText(edited, { exact: true }).waitFor()
await owner.getByRole('listitem').filter({ hasText: edited }).getByRole('button', { name: '转发', exact: true }).click()
const privateForward = owner.getByRole('form', { name: '转发私聊消息' })
await privateForward.getByLabel('转发给').selectOption({ label: targetGroup })
await privateForward.getByRole('button', { name: '确认转发' }).click()
await owner.getByText('已转发到群聊', { exact: true }).waitFor()
await windows.getByRole('link', { name: new RegExp(targetGroup) }).click()
await chat(windows).getByRole('button', { name: '启用本机群聊' }).click()
await chat(windows).getByText(edited, { exact: true }).waitFor()
const pendingGroup = '待加密群 ' + id()
await windows.getByLabel('群名称', { exact: true }).fill(pendingGroup)
await windows.getByRole('button', { name: '创建群组' }).click()
await windows.getByRole('heading', { name: pendingGroup }).waitFor()
await work(windows).getByRole('link', { name: '消息', exact: true }).click()
await windows.getByRole('link', { name: names[0] }).click()
await windows.getByRole('button', { name: '启用本机加密聊天' }).click()
const privateFile = '私聊文件-' + id() + '.txt'
const originalUpload = windows.waitForResponse((item) => /\/api\/v1\/attachments\//.test(item.url()) && item.request().method() === 'POST')
await windows.getByLabel('发送附件').setInputFiles({ name: privateFile, mimeType: 'text/plain', buffer: contents })
const originalFile = await originalUpload
assert.equal(originalFile.status(), 204)
const originalBytes = originalFile.request().postDataBuffer()
assert.ok(originalBytes && originalBytes.length === contents.length + 16)
await windows.getByRole('listitem').filter({ hasText: privateFile }).getByRole('button', { name: '转发', exact: true }).click()
const desktopForward = windows.getByRole('form', { name: '转发私聊消息' })
const writes = []
const recordWrite = (req) => { if (req.method() === 'POST' && req.url().includes('/groups/')) writes.push(req.url()) }
windows.on('request', recordWrite)
await desktopForward.getByLabel('转发给').selectOption({ label: pendingGroup })
await desktopForward.getByRole('button', { name: '确认转发' }).click()
await windows.getByRole('alert').getByText('请先在目标群加入加密会话').waitFor()
windows.off('request', recordWrite)
assert.deepEqual(writes, [])
await desktopForward.getByLabel('转发给').selectOption({ label: targetGroup })
const groupUpload = windows.waitForResponse((item) => item.url().includes('/groups/') && item.url().includes('/attachments/') && item.request().method() === 'POST')
await desktopForward.getByRole('button', { name: '确认转发' }).click()
const targetFile = await groupUpload
assert.equal(targetFile.status(), 204)
assert.notEqual(new URL(targetFile.url()).pathname.split('/').at(-1), new URL(originalFile.url()).pathname.split('/').at(-1))
await windows.getByText('已转发到群聊', { exact: true }).waitFor()
await work(owner).getByRole('link', { name: '群组' }).click()
await owner.getByRole('link', { name: new RegExp(targetGroup) }).click()
await chat(owner).getByRole('button', { name: '启用本机群聊' }).click()
const groupDownload = owner.waitForEvent('download')
const groupFileResponse = owner.waitForResponse((item) => item.url().endsWith(new URL(targetFile.url()).pathname) && item.request().method() === 'GET')
await chat(owner).getByRole('button', { name: '下载 ' + privateFile, exact: true }).click()
assert.notDeepEqual(await (await groupFileResponse).body(), originalBytes)
assert.deepEqual(await readFile(await (await groupDownload).path()), contents)
} catch (error) {
const status = await Promise.all(pages.map((page) => page.locator('[role=alert], .group-chat [role=status]').allTextContents()))
throw new Error(JSON.stringify(status), { cause: error })
@@ -6,7 +6,7 @@ import type { Peer } from './chat'
import type { Contact } from '../contacts'
import type { ForwardDestination, GroupSummary } from './group-chat'
const props = defineProps<{ groupId: string; pinned: Record<string, string>; busy: boolean }>()
const props = defineProps<{ sourceGroup?: string; pinned: Record<string, string>; busy: boolean }>()
const emit = defineEmits<{ cancel: []; confirm: [destination: ForwardDestination] }>()
const session = useSession()
const groups = ref<GroupSummary[]>([])
@@ -28,7 +28,7 @@ const target = computed<ForwardDestination | undefined>(() => {
onMounted(async () => {
try {
const [allGroups, allContacts] = await Promise.all([session.authorized<GroupSummary[]>('/groups'), session.contacts()])
groups.value = allGroups.filter((group) => group.state === 'active' && group.id !== props.groupId)
groups.value = allGroups.filter((group) => group.state === 'active' && group.id !== props.sourceGroup)
contacts.value = allContacts.filter((contact) => contact.state === 'friend')
} catch (cause) {
error.value = cause instanceof Error ? cause.message : '无法读取转发目标'
@@ -57,7 +57,7 @@ watch(destination, async (value, _previous, onCleanup) => {
</script>
<template>
<form class="confirmation group-forward" aria-label="转发群消息" @submit.prevent="target && emit('confirm', target)">
<form class="confirmation forward-dialog" :aria-label="sourceGroup ? '转发群消息' : '转发私聊消息'" @submit.prevent="target && emit('confirm', target)">
<p v-if="error" class="error" role="alert">{{ error }}</p>
<label>转发给
<select v-model="destination" :disabled="busy || loading">
@@ -83,5 +83,5 @@ watch(destination, async (value, _previous, onCleanup) => {
</template>
<style scoped>
.group-forward { display: grid; gap: .65rem; }
.forward-dialog { display: grid; gap: .65rem; }
</style>
+3 -3
View File
@@ -4,7 +4,7 @@ import { useSession } from '../session'
import { downloadAttachment } from './attachments'
import type { Attachment } from './attachments'
import MediaPreview from './MediaPreview.vue'
import GroupForward from './GroupForward.vue'
import ForwardDialog from './ForwardDialog.vue'
import VoiceMessage from './VoiceMessage.vue'
import VoiceRecorder from './VoiceRecorder.vue'
import { enableChat, trust } from './chat'
@@ -277,8 +277,8 @@ onBeforeUnmount(() => clearInterval(timer))
</div>
</li>
</ul>
<GroupForward v-if="forwarding && view" :key="[forwarding.epoch, forwarding.sender, forwarding.id].join('/')"
:group-id="group.id" :pinned="view.pinned" :busy="busy" @confirm="forward" @cancel="forwarding = undefined" />
<ForwardDialog v-if="forwarding && view" :key="[forwarding.epoch, forwarding.sender, forwarding.id].join('/')"
:source-group="group.id" :pinned="view.pinned" :busy="busy" @confirm="forward" @cancel="forwarding = undefined" />
<div v-if="ready && recalling" class="confirmation">
<p>撤回后,群成员的聊天界面会更新。成员可能已保存副本。</p>
<button type="button" class="plain" :disabled="busy || view?.hasPending" @click="recall">确认撤回</button>
+6 -4
View File
@@ -276,10 +276,12 @@ 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 content = line.attachment ? await readForwardedFile(line.attachment, call) : line.text
await send(identity, recipient, device, content, call)
export function readPrivateForward(identity: ChatIdentity, message: Pick<ChatLine, 'id' | 'peer' | 'own'>, call: Call) {
return update(identity, async (state) => {
const line = state.lines.find((item) => item.id === message.id && item.peer === message.peer && item.own === message.own)
if (!line || line.recalled) throw new Error('此私聊消息无法转发')
return line.attachment ? readForwardedFile(line.attachment, call) : line.text
})
}
export function recentConversations(identity: ChatIdentity) {
+7 -4
View File
@@ -284,14 +284,17 @@ export function discardPendingGroupMessage(identity: ChatIdentity, group: GroupD
}
export async function forwardGroupMessage(identity: ChatIdentity, source: GroupDetail, message: LineRef, destination: ForwardDestination, call: Call) {
const content = await update(identity, async (state) => {
return forwardContent(identity, destination, call, () => update(identity, async (state) => {
const item = state.groupChats?.[source.id]?.lines.find((entry) => entry.id === message.id &&
entry.epoch === message.epoch && entry.sender === message.sender)
if (!item || item.recalled) throw new Error('此群消息无法转发')
return item.attachment ? readForwardedFile(item.attachment, call, groupAttachmentPath(source, item.attachment.id)) : item.text
})
}))
}
export async function forwardContent(identity: ChatIdentity, destination: ForwardDestination, call: Call, readContent: () => Promise<string | File>) {
if ('user' in destination) {
await send(identity, destination.user, destination.device, content, call)
await send(identity, destination.user, destination.device, await readContent(), call)
return '已提交转发'
}
@@ -300,7 +303,7 @@ export async function forwardGroupMessage(identity: ChatIdentity, source: GroupD
const view = await synchronizeGroup(identity, target, call)
if (!view.ready || !view.current) throw new Error('请先在目标群加入加密会话')
if (view.hasPending) throw new Error('目标群还有待处理的消息,请先打开目标群')
await sendGroupMessage(identity, target, content, call, view.current.id)
await sendGroupMessage(identity, target, await readContent(), call, view.current.id)
// At this point the outbox is durable. The target can retry after a network failure.
try {
const result = await synchronizeGroup(identity, target, call)
+20 -45
View File
@@ -2,8 +2,11 @@
import { computed, nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { useSession } from '../session'
import { directory, enableChat, forwardMessage, localMessageAction, markRead, mutateMessage, recentConversations, send, setReadReceipts, synchronize, trust } from '../chat/chat'
import type { ChatLine, Peer } from '../chat/chat'
import { enableChat, localMessageAction, markRead, mutateMessage, readPrivateForward, recentConversations, send, setReadReceipts, synchronize, trust } from '../chat/chat'
import type { ChatLine } from '../chat/chat'
import { forwardContent } from '../chat/group-chat'
import type { ForwardDestination } from '../chat/group-chat'
import ForwardDialog from '../chat/ForwardDialog.vue'
import type { Contact } from '../contacts'
import { downloadAttachment } from '../chat/attachments'
import type { Attachment } from '../chat/attachments'
@@ -24,10 +27,7 @@ const deleting = ref<ChatLine>()
const editing = ref<ChatLine>()
const recalling = ref<ChatLine>()
const forwarding = ref<ChatLine>()
const destinations = ref<Contact[]>([])
const destination = ref('')
const destinationDevice = ref('')
const forwardDevices = ref<Peer[]>([])
const forwardStatus = ref('')
const error = ref('')
const busy = ref(false)
const contacts = ref<Contact[]>([])
@@ -249,31 +249,18 @@ function localAction(line: ChatLine, action: 'favorite' | 'remove') {
}
function beginForward(line: ChatLine) {
void execute(async () => {
destinations.value = (await session.contacts()).filter((contact) => contact.state === 'friend')
destination.value = ''
destinationDevice.value = ''
forwardDevices.value = []
forwarding.value = line
})
forwardStatus.value = ''
forwarding.value = line
}
function loadForwardDevices() {
destinationDevice.value = ''
forwardDevices.value = []
if (!destination.value) return
void execute(async () => {
const devices = await directory(session.authorized, destination.value)
forwardDevices.value = devices.filter((device) => device.mls && view.value?.pinned[device.id] === device.publicKey)
destinationDevice.value = forwardDevices.value[0]?.id ?? ''
})
}
function submitForward() {
void execute(async () => {
const device = forwardDevices.value.find((candidate) => candidate.id === destinationDevice.value)
if (!session.identity || !forwarding.value || !device) return
await forwardMessage(session.identity, forwarding.value, destination.value, device, session.authorized)
function submitForward(destination: ForwardDestination) {
const identity = session.identity
const message = forwarding.value
if (identity && message) void execute(async () => {
const status = await forwardContent(identity, destination, session.authorized,
() => readPrivateForward(identity, message, session.authorized))
if (forwarding.value !== message) return
forwardStatus.value = status
forwarding.value = undefined
await refresh()
})
@@ -298,6 +285,7 @@ watch(selected, () => {
editing.value = undefined
recalling.value = undefined
forwarding.value = undefined
forwardStatus.value = ''
pendingVoice.value = undefined
draft.value = ''
scheduleReceipt()
@@ -336,6 +324,7 @@ onBeforeUnmount(() => {
</aside>
<div class="device-content chat-page">
<p v-if="error" class="error" role="alert">{{ error }}</p>
<p v-if="forwardStatus" role="status">{{ forwardStatus }}</p>
<button v-if="!ownCode" type="button" class="primary" :disabled="busy" @click="enable">启用本机加密聊天</button>
<template v-else>
<details open>
@@ -405,22 +394,8 @@ onBeforeUnmount(() => {
<button type="button" class="plain" :disabled="busy" @click="recall">确认撤回</button>
<button type="button" class="plain" @click="recalling = undefined">取消</button>
</div>
<form v-if="forwarding" class="contact-preferences confirmation" @submit.prevent="submitForward">
<label>转发给
<select v-model="destination" :disabled="busy" @change="loadForwardDevices">
<option value="">选择联系人</option>
<option v-for="contact in destinations" :key="contact.id" :value="contact.id">{{ contact.alias || contact.username }}</option>
</select>
</label>
<label v-if="forwardDevices.length">接收设备
<select v-model="destinationDevice" :disabled="busy">
<option v-for="device in forwardDevices" :key="device.id" :value="device.id">{{ device.name }}</option>
</select>
</label>
<p v-else-if="destination && !busy" class="muted">请先在对方聊天中核验接收设备。</p>
<button class="primary" :disabled="busy || !destinationDevice">确认转发</button>
<button type="button" class="plain" :disabled="busy" @click="forwarding = undefined">取消</button>
</form>
<ForwardDialog v-if="forwarding && view" :key="[forwarding.peer, forwarding.id, forwarding.own].join('/')"
:pinned="view.pinned" :busy="busy" @confirm="submitForward" @cancel="forwarding = undefined" />
<form class="chat-compose" @submit.prevent="submit">
<div v-if="editing" class="chat-quote">
<span>正在编辑消息</span>