merged from gramsrv upstream

This commit is contained in:
onysd 2026-09-01 12:06:31 +03:00
parent 79c64ee916
commit 21a0856587
651 changed files with 54774 additions and 4590 deletions

View file

@ -25,15 +25,16 @@ var (
ErrSecretChatAlreadyAccepted = errors.New("secretchat: already accepted")
// ErrSecretChatAlreadyDeclined:accept 一个已销毁的密聊 → ENCRYPTION_ALREADY_DECLINED。
ErrSecretChatAlreadyDeclined = errors.New("secretchat: already declined")
// ErrSecretChatIDConflict:chat_id 主键撞键(计数器回退);调用方按 AtLeast 重分配重试。
ErrSecretChatIDConflict = errors.New("secretchat: chat id conflict")
// ErrSecretChatRandomIDDuplicate:requestEncryption.random_id 已被不同意图或其它
// auth key 占用。random_id 同时就是 chat_id,禁止另分配 ID 规避碰撞。
ErrSecretChatRandomIDDuplicate = errors.New("secretchat: random id duplicate")
)
// SecretChat 是一通私聊密聊的服务端权威态(durable,跨重启存活)。
// 字段命名对齐 TL encryptedChat*;ID 是 int32 量级,access_hash/admin_id/
// participant_id/key_fingerprint 是 int64。绑定维度是设备级(perm auth_key 的 int64 值)。
type SecretChat struct {
// ID 是 chat_id,全局单调 int32 序列;双方共享同一 id。
// ID 是 chat_id,必须逐位等于 requestEncryption.random_id(非零 int32);双方共享同一 id。
ID int
// AdminAccessHash / ParticipantAccessHash 双视角不同(TL "check sum depending on user ID")。
AdminAccessHash int64
@ -108,6 +109,19 @@ func (c SecretChat) PeerAuthKeyOf(userID int64) int64 {
}
}
// AuthKeyOf 返回 userID 自身绑定的 permanent auth key;非参与者或尚未绑定返回 0。
// 已建立密聊的所有读写授权必须同时匹配 user 与该 auth key,不能只依赖账号身份。
func (c SecretChat) AuthKeyOf(userID int64) int64 {
switch userID {
case c.AdminUserID:
return c.AdminAuthKeyID
case c.ParticipantUserID:
return c.ParticipantAuthKeyID
default:
return 0
}
}
// AccessHashFor 返回 userID 视角的 access_hash(双方不同);非参与者返回 0。
func (c SecretChat) AccessHashFor(userID int64) int64 {
switch userID {
@ -177,6 +191,12 @@ type SecretMessageDelivery struct {
Date int
}
// MaxSecretMessageDataBytes bounds the opaque encrypted DecryptedMessage payload persisted in
// the device queue. Secret-chat media bytes travel through the file service, so a 1 MiB metadata
// envelope is already well above the payload emitted by the supported clients while keeping one
// request independent from the process-wide MTProto admission budget.
const MaxSecretMessageDataBytes = 1 << 20
// SecretChatRequest 是 requestEncryption 受理入参(隐私/拉黑/self/bot 校验在 rpc 层先行)。
type SecretChatRequest struct {
AdminUserID int64