交付 Windows 与 Web 端到端加密附件预览
This commit is contained in:
1 parent
d1efdedc57
commit
c24960d22a
19 files changed
+421
-58
No files matched your search
+19
-6
@@ -3,6 +3,8 @@ import { mlsBinding, signMls } from '../identity'
|
||||
import { base64, decode, fingerprint, loadEngine } from './engine'
|
||||
import type { DeviceMls } from './engine'
|
||||
import { withVault } from './vault'
|
||||
import { decodeMessage, encodeAttachment, sealAttachment, uploadAttachment } from './attachments'
|
||||
import type { Attachment } from './attachments'
|
||||
|
||||
export interface Peer {
|
||||
id: string
|
||||
@@ -24,6 +26,7 @@ export interface ChatLine {
|
||||
id: string
|
||||
peer: string
|
||||
text: string
|
||||
attachment?: Attachment
|
||||
time: string
|
||||
own: boolean
|
||||
}
|
||||
@@ -40,7 +43,7 @@ interface ChatState {
|
||||
rejected: number
|
||||
}
|
||||
|
||||
export type Call = <T>(path: string, method?: string, body?: object) => Promise<T>
|
||||
export type Call = <T>(path: string, method?: string, body?: object, binary?: boolean) => Promise<T>
|
||||
const encoder = new TextEncoder()
|
||||
|
||||
function aad(id: string, sender: string, recipient: string) {
|
||||
@@ -146,19 +149,29 @@ async function flush(state: ChatState, save: (state: ChatState) => Promise<void>
|
||||
return unavailable
|
||||
}
|
||||
|
||||
export function send(identity: ChatIdentity, user: string, peer: Peer, text: string, call: Call) {
|
||||
export function send(identity: ChatIdentity, user: string, peer: Peer, content: string | File, call: Call) {
|
||||
return update(identity, async (state, save, factory) => {
|
||||
const current = (await directory(call, user)).find((item) => item.id === peer.id)
|
||||
if (!current || state.pinned[current.id] !== current.publicKey) throw new Error('发送前请核验对方设备安全码')
|
||||
const key = await verifiedPeer(current, user)
|
||||
const unavailable = await flush(state, save, call)
|
||||
if (unavailable.has(current.id)) throw new Error('该设备暂不可接收消息')
|
||||
const plaintext = encoder.encode(text.trim())
|
||||
if (!plaintext.length || plaintext.length > 8000) throw new Error('消息须为 1 至 8000 字节')
|
||||
const text = typeof content === 'string' ? content.trim() : ''
|
||||
let plaintext = encoder.encode(text)
|
||||
if (typeof content === 'string' && (!plaintext.length || plaintext.length > 8000)) {
|
||||
throw new Error('消息须为 1 至 8000 字节')
|
||||
}
|
||||
if (state.lines.length >= 1000) throw new Error('本机预览版消息容量已达上限')
|
||||
const engine = factory.DeviceMls.restore(decode(state.engine))
|
||||
try {
|
||||
let group = state.groups[current.id] ? decode(state.groups[current.id]) : undefined
|
||||
let attachment: Attachment | undefined
|
||||
if (typeof content !== 'string') {
|
||||
const sealed = await sealAttachment(content)
|
||||
attachment = sealed.attachment
|
||||
await uploadAttachment(attachment, sealed.ciphertext, current.id, call)
|
||||
plaintext = encodeAttachment(attachment)
|
||||
}
|
||||
if (!group) {
|
||||
const pack = await call<Package>('/contacts/' + user + '/devices/' + current.id + '/claim', 'POST')
|
||||
const wire = decode(pack.package)
|
||||
@@ -171,7 +184,7 @@ export function send(identity: ChatIdentity, user: string, peer: Peer, text: str
|
||||
const id = crypto.randomUUID()
|
||||
const ciphertext = engine.encrypt(group, aad(id, identity.device.id, current.id), plaintext)
|
||||
state.outbox.push({ messageId: id, recipientDevice: current.id, ciphertext: base64(ciphertext) })
|
||||
state.lines.push({ id, peer: current.id, text: text.trim(), time: new Date().toISOString(), own: true })
|
||||
state.lines.push({ id, peer: current.id, text, attachment, time: new Date().toISOString(), own: true })
|
||||
state.engine = base64(engine.snapshot())
|
||||
await save(state)
|
||||
await flush(state, save, call)
|
||||
@@ -207,7 +220,7 @@ export function synchronize(identity: ChatIdentity, user: string, call: Call) {
|
||||
} else {
|
||||
const plaintext = engine.decrypt(peer.id, key, aad(item.messageId, peer.id, identity.device.id), wire)
|
||||
if (state.lines.length < 1000) state.lines.push({
|
||||
id: item.messageId, peer: peer.id, text: new TextDecoder('utf-8', { fatal: true }).decode(plaintext),
|
||||
id: item.messageId, peer: peer.id, ...decodeMessage(plaintext),
|
||||
time: item.createdAt, own: false,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user