接入群消息实时提醒和重连补拉

This commit is contained in:
xhgit committed 2026-11-14 14:59:24 +08:00
1 parent 5ace82b0d7
commit b2e04e8174
10 files changed
+122 -33

No files matched your search

+2
View File
@@ -52,6 +52,8 @@ MLS 凭据由标准 Base64 的 publicKey 和 signature 组成,使用 UTF-8 前
群组密文投递使用 `/groups/{id}/epochs` 和 `/groups/{id}/messages`。群主发布带 membershipVersion 的世代,选取每个有效成员的一台已登记 MLS 设备,提交其他设备的 Welcome。GET epochs 返回当前有效世代的设备名单和本设备获准接收的 Welcome,旧世代仅向在本次成员资格期间加入过该世代的设备返回。POST messages 接受当前世代的 MLS PrivateMessage,GET messages 通过 after 游标返回本设备有权读取的密文。
群世代和密文提交后,WebSocket 向该世代的设备发送 `group_changed`,事件只包含 `type` 和 `groupId`。客户端收到提示、重连或到达兜底轮询周期时,按持久化的游标向授权接口补拉。提示由 Redis 尽力送达,数据库保存投递记录。
有效成员可通过 `GET /groups/{id}/devices` 获取群内有效设备凭据。群主通过 `POST /groups/{id}/devices/{device}/claim` 一次性领取其他有效成员的设备密钥包。邀请待接受的账号、已离群的账号和撤销设备无法访问群设备目录。
成员变更后旧世代不能发消息。绑定设备被撤销后需以替代设备发布新世代。重新加入的成员只能补拉本次加入以来的世代。服务端校验外层格式和投递授权,MLS 内容验证由客户端执行。
+6
View File
@@ -144,3 +144,9 @@ Windows Electron 与 Edge 验证向尚无加密会话的群转发会被拒绝、
Windows 和 Web 的私聊与群聊共用转发目标选择。私聊支持把文字正文和附件转发给已核验的联系人设备,或转发到本机已加入并核验的群加密会话。客户端先核对目标群会话,再读取本机消息或下载附件密文。附件通过认证后使用新的标识和密钥二次加密,目标群世代变更会阻断转发。
集成测试验证 Web 转发私聊文字后 Windows 在群内接收,Windows 转发私聊文件后 Web 下载逐字节一致且密文及附件标识变化。未建立加密会话的目标群拒绝转发,测试确认没有群文件上传或群消息提交。原有私聊及群聊转发回归通过。
## 群消息实时同步
群密文和新加密会话提交后,WebSocket 向群会话的设备发送仅含群标识的同步提示。正在查看该群的 Windows 和 Web 客户端合并密集提示,并按本机游标补拉。重新连接时立即补拉,另保留 15 秒兜底轮询。群成员列表和邀请仍使用原有刷新间隔。
服务端集成测试验证名单内设备收到提示、无关账号收不到提示。Windows Electron 和 Edge 测试验证在线收取及主动断开 WebSocket 后重连补收,私聊和群附件回归通过。
+1 -1
View File
@@ -15,7 +15,7 @@ func (s *Store) UploadAttachment(ctx context.Context, group, user, device, epoch
return err
}
defer tx.Rollback(ctx)
if err = lockSendingEpoch(ctx, tx, group, user, device, epoch); err != nil {
if _, err = lockSendingEpoch(ctx, tx, group, user, device, epoch); err != nil {
return err
}
inserted, err := tx.Exec(ctx, `
+23 -19
View File
@@ -150,10 +150,10 @@ func retryEpoch(ctx context.Context, tx pgx.Tx, group, sender string, input Epoc
return tx.Commit(ctx)
}
func lockSendingEpoch(ctx context.Context, tx pgx.Tx, group, user, device, epoch string) error {
func lockSendingEpoch(ctx context.Context, tx pgx.Tx, group, user, device, epoch string) ([]string, error) {
version, _, err := lockTransport(ctx, tx, group, user, device)
if err != nil {
return err
return nil, err
}
var enrolled bool
err = tx.QueryRow(ctx, `
@@ -162,42 +162,46 @@ func lockSendingEpoch(ctx context.Context, tx pgx.Tx, group, user, device, epoch
WHERE e.group_id=$1 AND e.epoch_id=$2 AND e.device_id=$3 AND g.membership_version=$4::bigint AND g.retired_at IS NULL)
`, group, epoch, device, version).Scan(&enrolled)
if err != nil {
return err
return nil, err
}
if !enrolled {
return ErrVersion
return nil, ErrVersion
}
// Match the device revocation transaction's row lock. Once a bound device is
// revoked the generation cannot accept any further application ciphertext.
rows, err := tx.Query(ctx, `
SELECT d.revoked_at IS NULL FROM group_epoch_devices e JOIN devices d ON d.id=e.device_id
SELECT d.id,d.revoked_at IS NULL FROM group_epoch_devices e JOIN devices d ON d.id=e.device_id
WHERE e.group_id=$1 AND e.epoch_id=$2 ORDER BY d.id FOR UPDATE OF d
`, group, epoch)
if err != nil {
return err
return nil, err
}
defer rows.Close()
devices := make([]string, 0, 32)
for rows.Next() {
var id string
var active bool
if err = rows.Scan(&active); err != nil {
return err
if err = rows.Scan(&id, &active); err != nil {
return nil, err
}
if !active {
return ErrVersion
return nil, ErrVersion
}
devices = append(devices, id)
}
return rows.Err()
return devices, rows.Err()
}
func (s *Store) Send(ctx context.Context, group, user, device string, message Message) (string, error) {
func (s *Store) Send(ctx context.Context, group, user, device string, message Message) (string, []string, error) {
tx, err := s.db.Begin(ctx)
if err != nil {
return "", err
return "", nil, err
}
defer tx.Rollback(ctx)
if err = lockSendingEpoch(ctx, tx, group, user, device, message.Epoch); err != nil {
return "", err
devices, err := lockSendingEpoch(ctx, tx, group, user, device, message.Epoch)
if err != nil {
return "", nil, err
}
if message.AttachmentID != nil {
var found string
@@ -205,10 +209,10 @@ func (s *Store) Send(ctx context.Context, group, user, device string, message Me
SELECT id FROM group_attachments WHERE group_id=$1 AND epoch_id=$2 AND sender_device=$3 AND id=$4 FOR KEY SHARE
`, group, message.Epoch, device, message.AttachmentID).Scan(&found)
if errors.Is(err, pgx.ErrNoRows) {
return "", ErrAttachment
return "", nil, ErrAttachment
}
if err != nil {
return "", err
return "", nil, err
}
}
@@ -224,13 +228,13 @@ func (s *Store) Send(ctx context.Context, group, user, device string, message Me
WHERE group_id=$1 AND epoch_id=$2 AND sender_device=$3 AND message_id=$4
`, group, message.Epoch, device, message.ID, message.Wire, message.AttachmentID).Scan(&sequence, &same)
if errors.Is(err, pgx.ErrNoRows) || err == nil && !same {
return "", ErrVersion
return "", nil, ErrVersion
}
}
if err != nil {
return "", err
return "", nil, err
}
return sequence, tx.Commit(ctx)
return sequence, devices, tx.Commit(ctx)
}
func (s *Store) Inbox(ctx context.Context, group, user, device string, after int64) ([]Message, error) {
+21 -2
View File
@@ -1,10 +1,12 @@
package httpapi
import (
"context"
"errors"
"net/http"
"strconv"
"strings"
"time"
"xhchat/server/internal/account"
"xhchat/server/internal/groups"
@@ -95,6 +97,11 @@ func (a *API) publishGroupEpoch(w http.ResponseWriter, r *http.Request, _ string
groupError(w, err)
return
}
devices := make([]string, 0, len(input.Devices))
for _, device := range input.Devices {
devices = append(devices, device.Device)
}
a.hintGroup(r.Context(), id, devices)
w.WriteHeader(204)
}
@@ -122,21 +129,33 @@ func (a *API) sendGroupMessage(w http.ResponseWriter, r *http.Request, _ string,
return
}
message := groups.Message{ID: input.ID, Epoch: input.Epoch, Wire: input.Wire, AttachmentID: input.AttachmentID}
sequence, err := a.groups.Send(r.Context(), id, actor.User.ID, actor.Device.ID, message)
sequence, devices, err := a.groups.Send(r.Context(), id, actor.User.ID, actor.Device.ID, message)
if err != nil {
groupError(w, err)
return
}
a.hintGroup(r.Context(), id, devices)
respond(w, 200, map[string]string{"sequence": sequence})
}
func (a *API) hintGroup(ctx context.Context, group string, devices []string) {
ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 250*time.Millisecond)
defer cancel()
pipe := a.redis.Pipeline()
for _, device := range devices {
pipe.Publish(ctx, "xhchat:inbox:"+device, "group:"+strings.ToLower(group))
}
// Hints are best effort. The durable cursor is also polled by clients.
_, _ = pipe.Exec(ctx)
}
func (a *API) groupInbox(w http.ResponseWriter, r *http.Request, _ string, actor account.Identity) {
id := r.PathValue("id")
if !uuidPattern.MatchString(id) {
groupError(w, groups.ErrUnavailable)
return
}
if !a.limit(w, r, "group-inbox:"+actor.Device.ID, 180) {
if !a.limit(w, r, "group-inbox:"+actor.Device.ID, 240) {
return
}
after := int64(0)
+7 -3
View File
@@ -100,11 +100,15 @@ func (a *API) realtime(w http.ResponseWriter, r *http.Request) {
return
}
writeCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
event := "contacts_changed"
event := map[string]string{"type": "contacts_changed"}
if change.Channel == inboxChannel {
event = "inbox_changed"
event["type"] = "inbox_changed"
if group, found := strings.CutPrefix(change.Payload, "group:"); found && uuidPattern.MatchString(group) {
event["type"] = "group_changed"
event["groupId"] = group
}
}
err := wsjson.Write(writeCtx, conn, map[string]string{"type": event})
err := wsjson.Write(writeCtx, conn, event)
cancel()
if err != nil {
return
+19 -1
View File
@@ -42,6 +42,15 @@ async function contact(actor, target, action) {
assert.equal(reply.status, 204, await reply.text())
}
async function watchRealtime(actor, t) {
const { ticket } = await json('/realtime/ticket', 'POST', actor.token)
const ws = new WebSocket(base.replace(/^http/, 'ws') + '/api/v1/realtime', ['xhchat.v1', 'ticket.' + ticket], { origin: base })
t.after(() => ws.terminate())
assert.deepEqual(JSON.parse((await once(ws, 'message', { signal: AbortSignal.timeout(4000) }))[0]),
{ type: 'ready', deviceId: actor.device.id })
return ws
}
// Opaque fixture with the MLS header. Crypto interoperability is tested separately in crypto/.
function frame() {
return Buffer.concat([Buffer.from([0, 1, 0, 2]), randomBytes(96)]).toString('base64')
@@ -89,7 +98,7 @@ test('群组邀请、授权、成员移除与版本变更', { timeout: 25_000 },
await json(path, 'GET', owner.token, undefined, 404)
})
test('群密文只投递到当前名单中的设备,离开或撤销后阻断旧世代', { timeout: 25_000 }, async () => {
test('群密文和实时提示只投递到名单设备,移除或撤销阻断旧世代', { timeout: 25_000 }, async (t) => {
const enroll = async () => {
const keys = generateKeyPairSync('ed25519')
const actor = await register(keys)
@@ -145,7 +154,13 @@ test('群密文只投递到当前名单中的设备,离开或撤销后阻断
] }
await publish(owner, { ...second, devices: [second.devices[0], { device: outsider.device.id, wire: welcome }] }, 409)
await publish(member, { ...second, devices: [{ device: member.device.id }, { device: owner.device.id, wire: welcome }] }, 409)
const [ownerSocket, memberSocket, outsiderSocket] = await Promise.all([owner, member, outsider].map((actor) => watchRealtime(actor, t)))
const outsiderEvents = []
outsiderSocket.on('message', (data) => outsiderEvents.push(JSON.parse(data)))
const hint = { type: 'group_changed', groupId: group.id }
const epochHint = once(memberSocket, 'message', { signal: AbortSignal.timeout(4000) })
await publish(owner, second)
assert.deepEqual(JSON.parse((await epochHint)[0]), hint)
await publish(owner, second)
await publish(owner, { ...second, id: randomUUID() }, 409)
const epochs = await json(path + '/epochs', 'GET', member.token)
@@ -153,7 +168,9 @@ test('群密文只投递到当前名单中的设备,离开或撤销后阻断
roster: [owner.device.id, member.device.id].sort() }])
await json(path + '/messages', 'GET', outsider.token, undefined, 404)
const message = { id: randomUUID(), epoch: second.id, wire: frame() }
const messageHint = once(ownerSocket, 'message', { signal: AbortSignal.timeout(4000) })
const sequence = await json(path + '/messages', 'POST', member.token, message)
assert.deepEqual(JSON.parse((await messageHint)[0]), hint)
assert.deepEqual(await json(path + '/messages', 'POST', member.token, message), sequence)
const received = await json(path + '/messages', 'GET', member.token)
assert.deepEqual(received.map((item) => item.wire), [message.wire])
@@ -203,6 +220,7 @@ test('群密文只投递到当前名单中的设备,离开或撤销后阻断
assert.equal((await json(path + '/epochs', 'GET', owner.token)).every((epoch) => !epoch.current), true)
await change(owner, 'disband')
await json(attachment, 'GET', owner.token, undefined, 404)
assert.deepEqual(outsiderEvents, [])
})
test('设备密文投递、幂等、离线补拉、实时提示和撤销', { timeout: 25000 }, async () => {
+14 -2
View File
@@ -121,6 +121,13 @@ test('Windows 与 Web 加密群聊支持消息操作、附件、语音和成员
const pages = []
try {
const [owner, removed] = await Promise.all([browser.newPage(), browser.newPage()])
let ownerSocket
let holdConnection = false
await owner.routeWebSocket('**/api/v1/realtime', (route) => {
if (holdConnection) { void route.close({ code: 1012 }); return }
ownerSocket = route
route.connectToServer()
})
await owner.context().grantPermissions(['microphone'], { origin: base })
await Promise.all([owner.goto(base), removed.goto(base)])
desktop = await _electron.launch({
@@ -163,8 +170,13 @@ test('Windows 与 Web 加密群聊支持消息操作、附件、语音和成员
await Promise.all(pages.map((page) => chat(page).getByText('群加密已建立', { exact: true }).waitFor()))
const first = 'Windows 群消息 ' + id()
await work(removed).getByRole('link', { name: '联系人' }).click()
holdConnection = true
await ownerSocket.close({ code: 1012 })
await owner.getByText('正在重连', { exact: true }).waitFor()
const epoch = await send(windows, first)
await Promise.all([owner, windows].map((page) => chat(page).getByText(first, { exact: true }).waitFor()))
holdConnection = false
await owner.getByText('实时连接已建立', { exact: true }).waitFor()
await Promise.all([owner, windows].map((page) => chat(page).getByText(first, { exact: true }).waitFor({ timeout: 3500 })))
await work(removed).getByRole('link', { name: '群组' }).click()
await removed.getByRole('link', { name: new RegExp(group) }).click()
await chat(removed).getByRole('button', { name: '启用本机群聊' }).click()
@@ -177,7 +189,7 @@ test('Windows 与 Web 加密群聊支持消息操作、附件、语音和成员
const reply = '加密引用回复 ' + id()
assert.equal(await send(owner, reply), epoch)
const receivedReply = chat(windows).getByRole('listitem').filter({ hasText: reply })
await receivedReply.locator('blockquote').getByText(first, { exact: true }).waitFor()
await receivedReply.locator('blockquote').getByText(first, { exact: true }).waitFor({ timeout: 3500 })
await work(removed).getByRole('link', { name: '联系人' }).click()
const own = chat(windows).getByRole('listitem').filter({ has: windows.getByText(first, { exact: true }) }).first()
await own.getByRole('button', { name: '编辑', exact: true }).click()
+24 -3
View File
@@ -41,6 +41,8 @@ const members = computed(() => props.group.members.filter((member) => member.sta
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)))))
let timer: ReturnType<typeof setInterval>
let refreshTimer: ReturnType<typeof setTimeout> | undefined
let refreshPending = false
function pinned(device: GroupDevice) {
return view.value?.pinned[device.id] === device.publicKey
@@ -68,9 +70,22 @@ async function run(action: () => Promise<void>, manual = true) {
error.value = cause instanceof Error ? cause.message : '群消息操作失败'
} finally {
busy.value = false
if (refreshPending) queueRefresh()
}
}
function queueRefresh() {
if (!code.value) return
refreshPending = true
if (busy.value || refreshTimer) return
refreshTimer = setTimeout(() => {
refreshTimer = undefined
if (busy.value) return
refreshPending = false
void run(refresh, false)
}, 350)
}
function enable() {
const identity = session.identity
if (identity) void run(async () => {
@@ -183,7 +198,9 @@ function discard() {
})
}
watch(() => props.group.membershipVersion, () => void run(refresh))
watch(() => props.group.membershipVersion, queueRefresh)
watch(() => session.groupChange, (event) => { if (event?.groupId === props.group.id) queueRefresh() })
watch(() => session.connection, (connection) => { if (connection === '实时连接已建立') queueRefresh() })
watch(currentEpoch, (epoch, previous) => {
if (!previous || epoch === previous) return
if (editing.value || replying.value) draft.value = ''
@@ -206,8 +223,12 @@ watch([pendingVoice, busy, currentEpoch, () => view.value?.hasPending], ([voice,
await refresh()
})
})
onMounted(() => { timer = setInterval(() => { if (code.value) void run(refresh, false) }, 2500) })
onBeforeUnmount(() => clearInterval(timer))
onMounted(() => { timer = setInterval(queueRefresh, 15_000) })
onBeforeUnmount(() => {
refreshPending = false
clearTimeout(refreshTimer)
clearInterval(timer)
})
</script>
<template>
+5 -2
View File
@@ -26,6 +26,7 @@ export const useSession = defineStore('session', () => {
const connection = ref('离线')
const contactChanges = ref(0)
const inboxChanges = ref(0)
const groupChange = shallowRef<{ groupId: string }>()
let token = ''
let socket: WebSocket | undefined
let retry: ReturnType<typeof setTimeout> | undefined
@@ -34,6 +35,7 @@ export const useSession = defineStore('session', () => {
function clear() {
token = ''
identity.value = undefined
groupChange.value = undefined
connection.value = '离线'
clearTimeout(retry)
const previous = socket
@@ -51,13 +53,14 @@ export const useSession = defineStore('session', () => {
socket = active
active.onmessage = (event) => {
if (socket !== active) return
const { type } = JSON.parse(event.data)
const { type, groupId } = JSON.parse(event.data)
if (type === 'ready') {
connection.value = '实时连接已建立'
attempts = 0
}
if (type === 'contacts_changed') contactChanges.value++
if (type === 'inbox_changed') inboxChanges.value++
if (type === 'group_changed' && typeof groupId === 'string') groupChange.value = { groupId }
}
active.onclose = (event) => {
if (socket !== active) return
@@ -109,7 +112,7 @@ export const useSession = defineStore('session', () => {
}
return {
identity, connection, contactChanges, inboxChanges, authenticate, logout, authorized,
identity, connection, contactChanges, inboxChanges, groupChange, authenticate, logout, authorized,
devices: () => authorized<Device[]>('/devices'),
revoke: (id: string) => authorized<void>('/devices/' + encodeURIComponent(id), 'DELETE'),
contacts: () => authorized<Contact[]>('/contacts'),