为双端群聊增加端到端加密附件
This commit is contained in:
1 parent
5988ad6ed6
commit
a95a007768
21 files changed
+405
-84
No files matched your search
+27
-13
@@ -2,6 +2,8 @@ import { APIError } from '../api'
|
||||
import { base64, decode } from './engine'
|
||||
import { replenishPackages, update, verifiedPeer, verify } from './chat'
|
||||
import type { Call, ChatIdentity, Package, Peer } from './chat'
|
||||
import { decodeMessage, encodeAttachment, sealAttachment } from './attachments'
|
||||
import type { Attachment } from './attachments'
|
||||
|
||||
export type GroupMember = { id: string; username: string; state: 'active' | 'invited'; role: 'owner' | 'admin' | 'member' }
|
||||
export type GroupDetail = { id: string; membershipVersion: string; members: GroupMember[]; role: GroupMember['role'] }
|
||||
@@ -9,8 +11,8 @@ export type GroupDevice = Peer & { user: string }
|
||||
type Epoch = { id: string; creatorDevice: string; membershipVersion: string; welcome?: string; current: boolean; roster: string[] }
|
||||
type Publishing = { id: string; membershipVersion: string; devices: { device: string; wire?: string }[] }
|
||||
type Outgoing = { id: string; epoch: string; wire: string }
|
||||
type Packet = Outgoing & { sequence: string; senderDevice: string; createdAt: string }
|
||||
type Line = { id: string; user: string; sender: string; name: string; text: string; time: string }
|
||||
type Packet = Outgoing & { sequence: string; senderDevice: string; createdAt: string; attachmentId?: string }
|
||||
type Line = { id: string; user: string; sender: string; name: string; text: string; time: string; attachment?: Attachment }
|
||||
type Verified = { user: string; key: string; name: string }
|
||||
type VerifiedEpoch = { version: string; roster: Record<string, Verified> }
|
||||
export type GroupChatState = {
|
||||
@@ -22,8 +24,8 @@ export type GroupChatState = {
|
||||
}
|
||||
|
||||
const encoder = new TextEncoder()
|
||||
const decoder = new TextDecoder('utf-8', { fatal: true })
|
||||
const path = (group: GroupDetail) => '/groups/' + encodeURIComponent(group.id)
|
||||
export const groupAttachmentPath = (group: GroupDetail, id: string) => path(group) + '/attachments/' + encodeURIComponent(id)
|
||||
const aad = (group: string, epoch: string, id: string, sender: string) =>
|
||||
encoder.encode(['XHChat group message v1', group, epoch, id, sender].join('\n'))
|
||||
|
||||
@@ -132,7 +134,9 @@ export function publishGroup(identity: ChatIdentity, group: GroupDetail, selecti
|
||||
async function sendPending(group: GroupDetail, chat: GroupChatState, call: Call) {
|
||||
const pending = chat.pendingMessage
|
||||
if (!pending) return
|
||||
await call(path(group) + '/messages', 'POST', { id: pending.id, epoch: pending.epoch, wire: pending.wire })
|
||||
await call(path(group) + '/messages', 'POST', {
|
||||
id: pending.id, epoch: pending.epoch, wire: pending.wire, attachmentId: pending.line.attachment?.id,
|
||||
})
|
||||
chat.lines.push(pending.line)
|
||||
delete chat.pendingMessage
|
||||
}
|
||||
@@ -186,7 +190,8 @@ export function synchronizeGroup(identity: ChatIdentity, group: GroupDetail, cal
|
||||
for (const item of packets) {
|
||||
if (item.senderDevice === identity.device.id) {
|
||||
const pending = chat.pendingMessage
|
||||
if (pending && pending.id === item.id && pending.epoch === item.epoch && pending.wire === item.wire) {
|
||||
if (pending && pending.id === item.id && pending.epoch === item.epoch && pending.wire === item.wire &&
|
||||
pending.line.attachment?.id === item.attachmentId) {
|
||||
chat.lines.push(pending.line)
|
||||
delete chat.pendingMessage
|
||||
}
|
||||
@@ -198,11 +203,12 @@ export function synchronizeGroup(identity: ChatIdentity, group: GroupDetail, cal
|
||||
try {
|
||||
const bytes = engine.decrypt_group(await groupID(group.id, item.epoch), item.senderDevice, decode(sender.key),
|
||||
aad(group.id, item.epoch, item.id, item.senderDevice), decode(item.wire))
|
||||
const text = decoder.decode(bytes)
|
||||
if (text.length > 0 && bytes.length <= 8000) {
|
||||
chat.lines.push({ id: item.id, user: sender.user, name: sender.name, sender: item.senderDevice, text, time: item.createdAt })
|
||||
}
|
||||
state.engine = base64(engine.snapshot())
|
||||
const { text, attachment, quote } = decodeMessage(bytes)
|
||||
if (attachment?.id !== item.attachmentId) throw new Error('群附件标识不匹配')
|
||||
if (attachment || (!quote && text.length > 0 && bytes.length <= 8000)) {
|
||||
chat.lines.push({ id: item.id, user: sender.user, name: sender.name, sender: item.senderDevice, text, attachment, time: item.createdAt })
|
||||
}
|
||||
} catch {
|
||||
warning ||= '已跳过无法通过验证的群消息'
|
||||
} finally {
|
||||
@@ -233,10 +239,11 @@ export function discardPendingGroupMessage(identity: ChatIdentity, group: GroupD
|
||||
})
|
||||
}
|
||||
|
||||
export function sendGroupMessage(identity: ChatIdentity, group: GroupDetail, text: string, call: Call) {
|
||||
export function sendGroupMessage(identity: ChatIdentity, group: GroupDetail, message: string | File, call: Call) {
|
||||
return update(identity, async (state, save, factory) => {
|
||||
const content = encoder.encode(text.trim())
|
||||
if (content.length === 0 || content.length > 8000) throw new Error('消息须为 1 至 8000 字节')
|
||||
const text = typeof message === 'string' ? message.trim() : ''
|
||||
let content: Uint8Array = encoder.encode(text)
|
||||
if (typeof message === 'string' && (content.length === 0 || content.length > 8000)) throw new Error('消息须为 1 至 8000 字节')
|
||||
const chat = local(state.groupChats ??= {}, group.id)
|
||||
const epochs = await call<Epoch[]>(path(group) + '/epochs')
|
||||
const epoch = epochs.find((item) => item.current && item.membershipVersion === group.membershipVersion)
|
||||
@@ -245,11 +252,18 @@ export function sendGroupMessage(identity: ChatIdentity, group: GroupDetail, tex
|
||||
const id = crypto.randomUUID()
|
||||
const own = chat.epochs[epoch.id]!.roster[identity.device.id]
|
||||
if (!own) throw new Error('当前设备未加入群加密世代')
|
||||
let attachment: Attachment | undefined
|
||||
if (typeof message !== 'string') {
|
||||
const sealed = await sealAttachment(message)
|
||||
attachment = sealed.attachment
|
||||
await call(groupAttachmentPath(group, attachment.id) + '?' + new URLSearchParams({ epoch: epoch.id }), 'POST', sealed.ciphertext)
|
||||
content = encodeAttachment(attachment)
|
||||
}
|
||||
const engine = factory.DeviceMls.restore(decode(state.engine))
|
||||
try {
|
||||
const wire = engine.encrypt(await groupID(group.id, epoch.id), aad(group.id, epoch.id, id, identity.device.id), content)
|
||||
chat.pendingMessage = { id, epoch: epoch.id, wire: base64(wire),
|
||||
line: { id, user: identity.user.id, name: own.name, sender: identity.device.id, text: text.trim(), time: new Date().toISOString() } }
|
||||
line: { id, user: identity.user.id, name: own.name, sender: identity.device.id, text, attachment, time: new Date().toISOString() } }
|
||||
state.engine = base64(engine.snapshot())
|
||||
await save(state)
|
||||
} finally {
|
||||
|
||||
Reference in new issue
Block a user