Files
piik/internal/server/room/store.go
T
AAAandAkazaAkalin 3707e48225 feat!: expand media controls and simplify App connections
## Problem and outcome / 问题与结果

Piik's media settings were coupled to a small preset list, and using native App capture on a Site required a separate launcher mode. This release separates numeric media requests from presets and bounded runtime admission, remembers successfully applied settings, and lets a running App serve an explicitly approved Site independently of Local/public room startup.

The same phase repairs confirmed capture-retirement, ICE discovery, media-recovery and storage failures. P2P-first routing, one room authority and the existing Browser/Native media owners remain. This phase targets **v2.0.0** with incompatible Web/App contracts. The owner authorized publication after final acceptance; the verified package and deployment records own delivery completion.

## Release notes

### 中文

Piik 2.0 扩展了画质设置,简化了 App 与站点的连接,并集中修复采集和断线恢复问题。**本次升级调整了通信协议,App 与所用站点需要一起升级。**

#### 新功能与改进

- **自定义画面设置:** 保留三档推荐预设,支持填写分辨率上限、1–240 帧的帧率上限和 0.1–240 Mbps 的视频码率上限。滑动条沿用常用范围,更高数值可直接填写并显示为“自定义”。帧率范围随画幅变化,可请求 1080p240、1440p144、4K60 和超宽画面;输出保留来源比例。
- **自定义音频质量:** 在原有三档音频预设之外,可填写 6–320 kbps 的来源音频码率上限。
- **双端 App 协同连接:** 启用增强 NAT 穿透后,适用的重试可协调双方 App 准备 UDP 连接;首次连接仍走普通 ICE,不适用时继续现有流程。已通过受控网络验证,实际连通性仍取决于双方网络。
- **记住分享设置:** 下次打开时恢复已应用的画面与音频质量。仅用浏览器时按站点保存;通过新版 App 使用时,更换临时公网邀请网址也能保留设置。
- **更直接地连接 App:** 启动页提供本地房间和公网邀请,首次默认选择公网邀请。使用已有站点时,保持 App 运行,在站点点击 **App** 并确认授权即可。App 打开的本地房主页自动获得授权,站点按钮会显示 App 就绪状态。启动页可查看或取消授权;从另一个站点连接并批准,会替换原授权。
- **置顶聊天窗口:** 支持的桌面浏览器可将聊天移入独立置顶窗口,保留当前消息、草稿和阅读位置;关闭窗口后回到原页面。

#### 修复

- **连接与恢复:** 完善备用 STUN 与 NAT 采样信息的处理;重新收集 ICE 候选时刷新网卡地址。修正观众重连的机会计数、本机媒体连接恢复,以及恢复后遗留的发送队列和带宽统计问题。保留视频编码对应的标准重传能力,改善网络地址变化后的画面恢复。
- **采集稳定性:** 修正 Windows 停止采集或预览时的异步资源释放问题,保留采集边框偏好,更快跳过无法正常输出的编码器。采集与应用音频能力按实际接口探测,不再按 Windows 版本号排除;修正 Linux Portal 来源识别和早期采集失败通知,改善摄像头与原生采集的画面比例处理。
- **状态与清理:** 房间失效时及时释放 App 连接和待完成的选源请求;本地或公网房间启动失败时,App 保持待机并保留其他已授权站点的连接;公网邀请启动失败保留底层错误。修正启动页旧查询覆盖新设置的问题,并限制原生连接准备的待完成任务。SFU 诊断区分房主发布和观众接收状态。
- **其他维护:** 收到尚不支持的表情时跳过该效果;修正 SQLite 写入失败后的事务恢复。Gitee 镜像可独立重试,官网交互、指南和依赖同步更新。App 连接失败时提供重试与下载入口;统一选源设置与自定义数值的反馈。减少重复的随机短语,仅在持续等待时定时轮换。

#### 升级须知

**2.0 与 1.x 不能混用。** 自建站点请更新 Server 和连接该站点的 App,并刷新所有分享与观看页面。本地房间或公网邀请用户请停止分享、关闭旧 App,完整解压新版程序包后重新发送邀请链接。

本次不变更房间数据库结构。重启会中断正在进行的分享;麦克风、采集权限和设备选择不会自动恢复。4K60 等设置是输出上限,实际清晰度与帧率取决于来源、设备和网络。macOS/Linux 采集及部分移动设备仍需更多实机反馈。

### English

Piik 2.0 adds more control over sharing quality, simplifies connecting the App to a Site, and fixes capture and connection-recovery failures. **The communication contracts change in this release: update the App and its Site together.**

#### Highlights

- **Custom picture settings:** Keep the three recommended presets or enter a resolution ceiling, a 1–240 fps limit and a 0.1–240 Mbps video bitrate limit. Sliders retain their familiar ranges; higher entered values are clearly marked as custom. Frame-rate limits depend on picture size, admitting requests such as 1080p240, 1440p144, 4K60 and ultrawide pictures while preserving source proportions.
- **Custom audio quality:** Enter a 6–320 kbps source-audio bitrate limit alongside the three existing audio presets.
- **Coordinated App connections:** With enhanced NAT traversal enabled, eligible retries can coordinate UDP preparation between two Apps. The first attempt remains ordinary ICE, and ineligible preparation continues through the existing path. Controlled network checks pass; real-world connectivity still depends on both networks.
- **Remember sharing settings:** Restore successfully applied picture and audio quality next time. Browser-only use saves settings per site; an updated App can retain them across changing temporary public-invitation addresses.
- **Connect the App from a Site:** The launcher offers Local room and Public invite, with Public invite selected on first use. For an existing Site, keep the App running, click **App** on the Site and confirm its authorization. Host pages opened locally by App are authorized automatically, and the Site button reflects verified App readiness. View or revoke approval in the launcher. Connecting and approving another Site replaces the previous approval.
- **Always-on-top chat:** Supported desktop browsers can move chat into a separate always-on-top window while retaining messages, drafts and reading position. Closing the window returns chat to the page.

#### Fixes

- **Connections and recovery:** Improve fallback STUN discovery and NAT survey handling, and refresh interface addresses during ICE gathering. Fix Viewer recovery-opportunity accounting, local media-connection recovery, stale send queues and bandwidth accounting after recovery. Preserve standard video retransmission when selecting a codec to improve picture recovery after network-address changes.
- **Capture stability:** Fix asynchronous Windows resource retirement after capture or preview ends, retain the capture-border preference, and reject non-performing encoder candidates earlier. Detect capture and process-audio support through actual APIs rather than Windows-version gates. Correct Linux Portal target identification and early capture-failure delivery; improve source-aspect handling for camera and native capture.
- **State and cleanup:** Release App connections and pending source selection when room authority ends. Keep the App on standby and preserve other authorized Site connections when interactive Local/public room startup fails. Retain underlying public-invitation startup errors. Prevent stale launcher reads from replacing newer settings, bound pending native connection-preparation work, and distinguish Host publication from Viewer reception in SFU diagnostics.
- **Maintenance:** Skip unsupported incoming reaction effects, recover SQLite transactions after write failures, and retry Gitee mirroring independently. Update website interactions, guides and dependencies; clarify App connection recovery, source-selection settings and custom-value feedback. Reduce repeated playful captions, with timed rotation reserved for ongoing waits.

#### Upgrading

**2.0 and 1.x cannot be mixed.** For a hosted Site, update its Server and connected Apps, then refresh every Host and Viewer page. For Local rooms or Public invites, stop sharing, close the old App, extract the complete new package and send a fresh invitation.

The room database schema is unchanged. Restarting interrupts active shares; microphone activation, capture permission and device selection are not restored automatically. Settings such as 4K60 are output ceilings: actual resolution and frame rate depend on the source, device and network. macOS/Linux capture and some mobile devices still need more physical-device feedback.

## Verification / 验证

- Product candidate: `24a28738241b9012c0476386356a515aab21f1a5`, built as `v2.0.0`. [Final full candidate CI](https://github.com/TNTcraftHIM/Piik/actions/runs/38555494851) covers core validation, vulnerabilities, races, five App packages and both Server/container targets without publishing. Later commits extend race coverage, correct the capture test fixture's inherited Go race-exit delay, and update documentation/staged website screenshots; shipped product code is unchanged. [Additional control/loopback race checks](https://github.com/TNTcraftHIM/Piik/actions/runs/38561191343) use that updated collector. Product shutdown deadlines and test assertions are unchanged.
- Local Web acceptance passes **91 files / 1,612 tests**, type checking and production build. Full Go/native checks, Windows capture compilation, five-repeat control regressions, website/documentation build and repository checks pass. Existing documentation-size and bundle-size warnings remain visible.
- The Windows candidate's original descriptor, revision and SHA-256 are checked before extraction and packaged Local/control acceptance. All candidate packages pass the release publisher's read-only identity/checksum verification.
- Serial Windows native H.264 Host acceptance passes live/paused/custom/4K settings, background/source recovery and stop/re-share. VP8 two-room SFU and H.264 SFU-only checks pass live quality/audio changes, subscription/page-reload recovery and retirement; stopping one room preserves the other room's media. Browser-to-App fanout and App-exit fallback also pass. Gates clean their processes, profiles and listeners. These checks do not establish sustained game/4K performance.
- The final network run passes **37 cases and six negative controls**, with **32 actual media outcomes and five unavailable observations**. It covers all Browser/App pairings, IPv6, remapping, STUN backup, outages, quality recovery, healthy siblings and SFU policies. Sequential-NAT App/App preparation delivers media; a mixed sequential case also connects in this run, without a claim of deterministic benefit. Fault controls reject absent faults/frozen video and verify cancellation/cleanup. No public success percentage is inferred.
- Twenty Viewer topology acceptance delivers increasing decoded frames to all Viewers, preserves the two-copy bound and recovers from relay loss. Invitation privacy checks find no grant leakage in requests, cookies, persistent storage or logs. Both are bounded synthetic-source checks, not public-network endurance.
- Current UI checks cover Chinese/English/visual, both themes and 320/360/768/1366-pixel layouts, numeric limits, source selection, App approval/revocation, architecture downloads and reader navigation. Chat/PiP retains draft/history/scroll and returns to the page on close; retirement and refused opening clean up. Real Host/Viewer UI chat continues after capture stops and while the Host is absent. Film controls, mute/seek/end/replay, mobile containment and pointer-close unloading pass. README stills and the staged launcher now use current navigation. Scoped axe checks report no violations; contrast and physical assistive-technology coverage are not certified.
- Physical macOS/Linux capture, limited decoders, mixed HDR displays, mobile permissions and sustained game/4K use retain their [evidence limits](https://github.com/TNTcraftHIM/Piik/blob/ci/gitee-mirror/docs/verification-status.md). Unmatched field reports remain open. Gitee activation and public-domain checks are publication postflight; original v1.9.2 artifact/mirror recovery already passed without remote writes.

## Boundaries and contributor credit / 边界与贡献

The candidate uses Browser/server v24 and Native control v10; capture v7 and SQLite schema 2 are unchanged. Numeric wire values remain separate from codec/allocation admission. H.264/VP8, framework-owned adaptation and automatic P2P-first routing remain; this candidate adds neither codecs, room voice, end-to-end HDR, surround audio nor Magicsock.

Local controls confirm specific repairs, not the causes of unmatched user reports or a measured increase in NAT success rate. The STUN report in #471 remains subject to paired endpoint/network evidence. Gitee's separate workflow activation still needs post-merge verification; its original-artifact and anonymous-download path has passed against v1.9.2.

The Linux Portal fix from @AkazaAkali in #470 is incorporated with original commit authorship and the co-author trailer below. #462 and #469 propose higher audio rates and 4K; neither patch is merged. Their preset-expansion implementation is superseded by the independently established numeric-media contract, so overlapping functionality alone does not warrant merging or co-authorship. #475 identifies the Linux desktop entry’s missing terminal controls; that defect is independently corrected without accepting tray or Browser-owned process-exit additions. The settings request in #460 is implemented; issue/PR closure remains part of release follow-up.

Co-authored-by: AkazaAkalin <478080766@qq.com>
2026-10-14 08:09:45 -04:00

1105 lines
33 KiB
Go

package room
import (
"bytes"
"crypto/rand"
"crypto/sha256"
"crypto/subtle"
"encoding/base64"
"encoding/binary"
"errors"
"fmt"
"math"
"slices"
"strconv"
"time"
"github.com/TNTcraftHIM/Piik/internal/server/ordered"
"github.com/TNTcraftHIM/Piik/internal/server/protocol"
)
// Capacity is the number of four-digit room codes without a leading zero.
const Capacity = 9_000
const roomCodeFirst = 1_000
// ErrorCode identifies a room authority or admission failure.
type ErrorCode string
// Room authority and admission errors.
const (
CodeInvalidToken ErrorCode = "INVALID_TOKEN"
CodeRoomAccessDenied ErrorCode = "ROOM_ACCESS_DENIED"
CodeRoomBusy ErrorCode = "ROOM_BUSY"
CodeRoomNotFound ErrorCode = "ROOM_NOT_FOUND"
CodeRoomFull ErrorCode = "ROOM_FULL"
CodeHostAlreadyConnected ErrorCode = "HOST_ALREADY_CONNECTED"
CodeRoomLimit ErrorCode = "ROOM_LIMIT"
)
// Error carries a room authority or admission error code.
type Error struct {
Code ErrorCode
}
// Error returns the code.
func (e *Error) Error() string { return string(e.Code) }
func codeError(code ErrorCode) error { return &Error{Code: code} }
func hasCode(err error, code ErrorCode) bool {
var roomError *Error
return errors.As(err, &roomError) && roomError.Code == code
}
// admittedBy values.
const (
admittedByGrant = "grant"
admittedByCode = "code"
)
type participant struct {
clientID string
peerID string
// An empty sessionID means offline; validated wire IDs are never empty.
sessionID string
admittedBy string
}
// Room is one room's authority plus its participants. It has no exported
// members: callers hold the pointer only to compare identity across the
// unlocked password derivation.
type Room struct {
roomID string
hostTokenDigest []byte
viewerGrantDigest []byte
viewerPasswordMaterial []byte
viewerAuthorizationGeneration string
codeEntryPolicy protocol.CodeEntryPolicy
host *participant
// Process-local: restored rooms receive a fresh reconnect window. Only the
// last authenticated departure updates this; failed admission cannot renew it.
emptySinceMs int64
// Insertion order decides viewer presence order, the
// closed-session list and the router's participant order.
viewers ordered.Map[string, *participant]
}
// CreatedRoom carries newly issued Host authority and an optional Viewer grant.
type CreatedRoom struct {
RoomID string
HostToken string
CodeEntryPolicy protocol.CodeEntryPolicy
ViewerGrant string
}
// ConnectParticipantInput uses Token for a Host and ViewerGrant for a Viewer.
// Empty credentials are absent.
type ConnectParticipantInput struct {
RoomID string
Role protocol.Role
Token string
ViewerGrant string
ClientID string
SessionID string
}
// ConnectViewerWithPasswordInput supplies Viewer password admission. Password is
// consumed by the caller's DeriveViewerPassword call, not by the commit.
type ConnectViewerWithPasswordInput struct {
RoomID string
Password string
ClientID string
SessionID string
}
// ConnectedParticipant records admission and any session it superseded.
// ReplacedSessionID is empty when no connected session was replaced.
type ConnectedParticipant struct {
RoomID string
Role protocol.Role
PeerID string
HostOnline bool
ReplacedSessionID string
CodeEntryPolicy protocol.CodeEntryPolicy
ViewerPasswordEnabled bool
ViewerAuthorizationGeneration string
}
type DisconnectedParticipant struct {
RoomID string
Role protocol.Role
PeerID string
}
type ConnectedPeer struct {
PeerID string
SessionID string
}
type ClosedRoom struct {
RoomID string
SessionIDs []string
}
type ReplacedRoom struct {
Created CreatedRoom
Closed ClosedRoom
}
// RevokedViewer identifies revoked admission. SessionID is empty for an offline Viewer.
type RevokedViewer struct {
PeerID string
SessionID string
}
// ViewerGrantUpdate carries a grant change and affected Viewers; ViewerGrant is
// empty when the invitation was revoked.
type ViewerGrantUpdate struct {
ViewerGrant string
ViewerAuthorizationGeneration string
PreviousViewerAuthorizationGeneration string
RevokedViewers []RevokedViewer
}
type CodeEntryUpdate struct {
CodeEntryPolicy protocol.CodeEntryPolicy
ViewerPasswordEnabled bool
}
// Options configures capacity, optional persistence and injectable entropy/time.
type Options struct {
MaxRooms int
MaxViewersPerRoom int
Database *Database
Random func(size int) []byte
Now func() int64
}
// Store owns room authority. See the package comment for the locking contract.
type Store struct {
// Insertion order determines the order returned by AbandonAllRooms.
rooms ordered.Map[string, *Room]
freeRoomCodes []string
random func(size int) []byte
now func() int64
database *Database
initialized bool
maxRooms int
maxViewersPerRoom int
// The derivation gate bounds work across every room in this store.
gate gate
}
// New validates limits and prepares a Store; Initialize loads persisted authority.
func New(options Options) (*Store, error) {
if options.MaxRooms <= 0 || options.MaxRooms > Capacity {
return nil, fmt.Errorf("Room limit must be an integer between 1 and %d", Capacity)
}
if options.MaxViewersPerRoom <= 0 ||
options.MaxViewersPerRoom > protocol.MaxViewersPerRoomLimit {
return nil, fmt.Errorf(
"Room viewer limit must be an integer between 1 and %d",
protocol.MaxViewersPerRoomLimit)
}
store := &Store{
freeRoomCodes: make([]string, Capacity),
random: options.Random,
now: options.Now,
database: options.Database,
initialized: options.Database == nil,
maxRooms: options.MaxRooms,
maxViewersPerRoom: options.MaxViewersPerRoom,
gate: gate{limit: gateActiveLimit, pendingLimit: gatePendingLimit},
}
for index := range store.freeRoomCodes {
store.freeRoomCodes[index] = strconv.Itoa(roomCodeFirst + index)
}
if store.random == nil {
store.random = cryptoRandom
}
if store.now == nil {
store.now = func() int64 { return time.Now().UnixMilli() }
}
return store, nil
}
func cryptoRandom(size int) []byte {
value := make([]byte, size)
// crypto/rand.Read never fails; it panics if the operating system source does.
_, _ = rand.Read(value)
return value
}
// MaxViewersPerRoom is the readonly RoomStore field of the same name.
func (s *Store) MaxViewersPerRoom() int { return s.maxViewersPerRoom }
// Size is the `size` getter.
func (s *Store) Size() int { return s.rooms.Len() }
// Close closes the database.
func (s *Store) Close() error {
if s.database == nil {
return nil
}
return s.database.Close()
}
// Initialize recovers the stable authority. It is a no-op without a database.
func (s *Store) Initialize() error {
if s.initialized {
return nil
}
if s.database == nil {
s.initialized = true
return nil
}
if err := s.restore(); err != nil {
s.rooms.Clear()
_ = s.database.Close()
return err
}
s.initialized = true
return nil
}
func (s *Store) restore() error {
storedRooms, err := s.database.Initialize()
if err != nil {
return err
}
if len(storedRooms) > s.maxRooms {
return errors.New("Room database exceeds the configured room limit")
}
roomIDs := make(map[string]struct{}, len(storedRooms))
for _, stored := range storedRooms {
roomIDs[stored.RoomID] = struct{}{}
}
if len(roomIDs) != len(storedRooms) {
return errors.New("Room database contains duplicate room IDs")
}
for _, stored := range storedRooms {
s.rooms.Set(stored.RoomID, &Room{
roomID: stored.RoomID,
hostTokenDigest: bytes.Clone(stored.HostTokenDigest),
viewerGrantDigest: bytes.Clone(stored.ViewerGrantDigest),
viewerPasswordMaterial: bytes.Clone(stored.ViewerPasswordMaterial),
viewerAuthorizationGeneration: stored.ViewerAuthorizationGeneration,
codeEntryPolicy: stored.CodeEntryPolicy,
emptySinceMs: s.now(),
})
}
available := s.freeRoomCodes[:0]
for _, code := range s.freeRoomCodes {
if _, taken := roomIDs[code]; !taken {
available = append(available, code)
}
}
s.freeRoomCodes = available
return nil
}
// BeginCreateRoom checks room capacity before password derivation.
func (s *Store) BeginCreateRoom() error {
if err := s.ensureInitialized(); err != nil {
return err
}
if s.rooms.Len() >= s.maxRooms {
return codeError(CodeRoomLimit)
}
return nil
}
// CreateRoom commits room creation. material and derived are the
// results of DeriveViewerPasswordMaterial (both zero when no password was
// given).
func (s *Store) CreateRoom(
policy protocol.CodeEntryPolicy,
material []byte,
derived error,
preferredRoomID string,
) (CreatedRoom, error) {
// Derivation failures precede admission checks; gate outcomes are resolved below.
if derived != nil && !isGateOutcome(derived) {
return CreatedRoom{}, derived
}
if err := s.ensureInitialized(); err != nil {
return CreatedRoom{}, err
}
if s.rooms.Len() >= s.maxRooms {
return CreatedRoom{}, codeError(CodeRoomLimit)
}
if hasCode(derived, CodeRoomBusy) {
return CreatedRoom{}, codeError(CodeRoomBusy)
}
if errors.Is(derived, errDerivationCancelled) {
return CreatedRoom{}, errors.New("Room creation password derivation was cancelled")
}
roomID, err := s.takeRoomCode(preferredRoomID)
if err != nil {
return CreatedRoom{}, err
}
room, created, err := s.newRoom(roomID, policy, material)
if err == nil {
err = s.writeDatabase(func() error { return s.database.InsertRoom(storedRoomAuthority(room)) })
}
if err != nil {
s.releaseRoomCode(roomID)
return CreatedRoom{}, err
}
s.rooms.Set(roomID, room)
return created, nil
}
// HostManagedRoom returns the room owned by the exact Host token. The caller
// retains its identity while deriving password material outside the lock.
func (s *Store) HostManagedRoom(roomID, hostToken string) (*Room, error) {
if err := s.ensureInitialized(); err != nil {
return nil, err
}
return s.getHostManagedRoom(roomID, hostToken)
}
// HostStillOwnsRoom is the mayStart closure ReplaceRoom and SetViewerPassword
// hand to the KDF gate: the room must still be the same object and the token
// must still own it. Call it with the lock held.
func (s *Store) HostStillOwnsRoom(roomID, hostToken string, current *Room) bool {
room, ok := s.rooms.Get(roomID)
return ok && room == current && verifyDigest(hostToken, room.hostTokenDigest)
}
// ReplaceRoom commits a replacement. current is the room
// HostManagedRoom returned before the derivation.
func (s *Store) ReplaceRoom(
roomID, hostToken string,
policy protocol.CodeEntryPolicy,
material []byte,
derived error,
current *Room,
) (ReplacedRoom, error) {
// Derivation failures return before rechecking authority; gate outcomes do not.
if derived != nil && !isGateOutcome(derived) {
return ReplacedRoom{}, derived
}
if err := s.ensureInitialized(); err != nil {
return ReplacedRoom{}, err
}
owned, err := s.getHostManagedRoom(roomID, hostToken)
if err != nil {
return ReplacedRoom{}, err
}
if owned != current || errors.Is(derived, errDerivationCancelled) {
return ReplacedRoom{}, codeError(CodeRoomAccessDenied)
}
if hasCode(derived, CodeRoomBusy) {
return ReplacedRoom{}, codeError(CodeRoomBusy)
}
replacementRoomID, err := s.takeRoomCode("")
if err != nil {
return ReplacedRoom{}, err
}
replaced, err := s.replaceRoom(roomID, replacementRoomID, policy, material, current)
if err != nil {
s.releaseRoomCode(replacementRoomID)
return ReplacedRoom{}, err
}
return replaced, nil
}
func (s *Store) replaceRoom(
roomID, replacementRoomID string,
policy protocol.CodeEntryPolicy,
material []byte,
current *Room,
) (ReplacedRoom, error) {
room, created, err := s.newRoom(replacementRoomID, policy, material)
if err != nil {
return ReplacedRoom{}, err
}
err = s.writeDatabase(func() error {
return s.database.ReplaceRoom(roomID, current.hostTokenDigest, storedRoomAuthority(room))
})
if err != nil {
return ReplacedRoom{}, err
}
closed := ClosedRoom{RoomID: roomID, SessionIDs: connectedSessionIDs(current)}
s.rooms.Delete(roomID)
s.rooms.Set(replacementRoomID, room)
s.releaseRoomCode(roomID)
return ReplacedRoom{Created: created, Closed: closed}, nil
}
// SetViewerPassword commits a password change; it reports whether
// a password is now set. current is the room HostManagedRoom returned before
// the derivation.
func (s *Store) SetViewerPassword(
roomID, hostToken string,
material []byte,
derived error,
current *Room,
) (bool, error) {
// Derivation failures return before rechecking authority; gate outcomes do not.
if derived != nil && !isGateOutcome(derived) {
return false, derived
}
if err := s.ensureInitialized(); err != nil {
return false, err
}
currentRoom, err := s.getHostManagedRoom(roomID, hostToken)
if err != nil {
return false, err
}
if currentRoom != current || errors.Is(derived, errDerivationCancelled) {
return false, codeError(CodeRoomAccessDenied)
}
if hasCode(derived, CodeRoomBusy) {
return false, codeError(CodeRoomBusy)
}
err = s.writeDatabase(func() error {
return s.database.SetViewerPassword(roomID, currentRoom.hostTokenDigest, material)
})
if err != nil {
return false, err
}
currentRoom.viewerPasswordMaterial = material
return material != nil, nil
}
// SetCodeEntryPolicy updates code admission under exact Host authority.
func (s *Store) SetCodeEntryPolicy(
roomID string,
policy protocol.CodeEntryPolicy,
hostToken string,
) (CodeEntryUpdate, error) {
if err := s.ensureInitialized(); err != nil {
return CodeEntryUpdate{}, err
}
room, err := s.getHostManagedRoom(roomID, hostToken)
if err != nil {
return CodeEntryUpdate{}, err
}
err = s.writeDatabase(func() error {
return s.database.SetCodeEntryPolicy(roomID, room.hostTokenDigest, policy)
})
if err != nil {
return CodeEntryUpdate{}, err
}
room.codeEntryPolicy = policy
return CodeEntryUpdate{
CodeEntryPolicy: policy,
ViewerPasswordEnabled: room.viewerPasswordMaterial != nil,
}, nil
}
// SetViewerGrant rotates or revokes invitations and removes grant-admitted Viewers.
func (s *Store) SetViewerGrant(
roomID, action, hostToken string,
) (ViewerGrantUpdate, error) {
if err := s.ensureInitialized(); err != nil {
return ViewerGrantUpdate{}, err
}
room, err := s.getHostManagedRoom(roomID, hostToken)
if err != nil {
return ViewerGrantUpdate{}, err
}
previousGeneration := room.viewerAuthorizationGeneration
viewerGrant := ""
if action == "rotate" {
if viewerGrant, err = s.createViewerGrant(); err != nil {
return ViewerGrantUpdate{}, err
}
}
var revokedViewers []RevokedViewer
for _, viewer := range room.viewers.Values() {
if viewer.admittedBy == admittedByGrant {
revokedViewers = append(revokedViewers, RevokedViewer{
PeerID: viewer.peerID,
SessionID: viewer.sessionID,
})
}
}
var viewerGrantDigest []byte
if viewerGrant != "" {
viewerGrantDigest = digest(viewerGrant)
} else {
if viewerGrantDigest, err = s.randomDigest(); err != nil {
return ViewerGrantUpdate{}, err
}
}
generation, err := s.newAuthorizationGeneration()
if err != nil {
return ViewerGrantUpdate{}, err
}
err = s.writeDatabase(func() error {
return s.database.SetViewerGrant(
roomID, room.hostTokenDigest, viewerGrantDigest, generation)
})
if err != nil {
return ViewerGrantUpdate{}, err
}
room.viewerGrantDigest = viewerGrantDigest
room.viewerAuthorizationGeneration = generation
wasOccupied := roomOccupied(room)
// ordered.Map.All permits removal without skipping surviving entries.
for clientID, viewer := range room.viewers.All() {
if viewer.admittedBy == admittedByGrant {
room.viewers.Delete(clientID)
}
}
if wasOccupied && !roomOccupied(room) {
room.emptySinceMs = s.now()
}
return ViewerGrantUpdate{
ViewerGrant: viewerGrant,
ViewerAuthorizationGeneration: room.viewerAuthorizationGeneration,
PreviousViewerAuthorizationGeneration: previousGeneration,
RevokedViewers: revokedViewers,
}, nil
}
// ConnectParticipant authenticates a Host token, Viewer grant or open code entry.
func (s *Store) ConnectParticipant(input ConnectParticipantInput) (ConnectedParticipant, error) {
if err := s.ensureInitialized(); err != nil {
return ConnectedParticipant{}, err
}
room, err := s.getAvailableRoom(input.RoomID)
if err != nil {
if input.Role == protocol.RoleViewer && input.ViewerGrant == "" &&
hasCode(err, CodeInvalidToken) {
return ConnectedParticipant{}, codeError(CodeRoomNotFound)
}
return ConnectedParticipant{}, err
}
if input.Role == protocol.RoleHost {
if !verifyDigest(input.Token, room.hostTokenDigest) {
return ConnectedParticipant{}, codeError(CodeInvalidToken)
}
return s.connectHost(room, input)
}
if input.ViewerGrant != "" {
if !viewerGrantIsValid(room, input.ViewerGrant) {
return ConnectedParticipant{}, codeError(CodeInvalidToken)
}
return s.connectViewer(room, input.ClientID, input.SessionID, admittedByGrant)
}
if room.codeEntryPolicy != protocol.CodeEntryOpen {
return ConnectedParticipant{}, codeError(CodeInvalidToken)
}
return s.connectViewer(room, input.ClientID, input.SessionID, admittedByCode)
}
// ViewerGrantMayEnter validates an invitation without admitting a participant.
func (s *Store) ViewerGrantMayEnter(roomID, grant string) (bool, error) {
if err := s.ensureInitialized(); err != nil {
return false, err
}
room, err := s.getAvailableRoom(roomID)
if err != nil {
var roomError *Error
if errors.As(err, &roomError) {
return false, nil
}
return false, err
}
return viewerGrantIsValid(room, grant), nil
}
// ViewerPasswordChallenge is the locked first half of password admission:
// it returns the salt to derive against, a copy of the material to compare with
// and the room reference to re-check afterwards. A missing room and a room
// without a private password both answer with the same 48 zero
// bytes so the derivation cost does not reveal which. An uninitialized store
// fails before this check.
func (s *Store) ViewerPasswordChallenge(
roomID string,
) (salt, expectedMaterial []byte, room *Room, err error) {
if err := s.ensureInitialized(); err != nil {
return nil, nil, nil, err
}
found, err := s.getAvailableRoom(roomID)
if err != nil {
found = nil
}
expectedMaterial = make([]byte, viewerPasswordMaterialBytes)
if found != nil && found.codeEntryPolicy == protocol.CodeEntryPrivate &&
found.viewerPasswordMaterial != nil {
copy(expectedMaterial, found.viewerPasswordMaterial)
}
return expectedMaterial[:viewerPasswordSaltBytes], expectedMaterial, found, nil
}
// ConnectViewerWithPassword commits password admission after derivation.
// derived is the DeriveViewerPassword result; expectedMaterial and room come
// from ViewerPasswordChallenge; mayConnect is evaluated with the lock held.
func (s *Store) ConnectViewerWithPassword(
input ConnectViewerWithPasswordInput,
derived, expectedMaterial []byte,
room *Room,
mayConnect func() bool,
) (ConnectedParticipant, error) {
if err := s.ensureInitialized(); err != nil {
return ConnectedParticipant{}, err
}
matches := subtle.ConstantTimeCompare(
derived, expectedMaterial[viewerPasswordSaltBytes:]) == 1
currentRoom, err := s.getAvailableRoom(input.RoomID)
if err != nil {
var roomError *Error
if errors.As(err, &roomError) {
return ConnectedParticipant{}, codeError(CodeRoomNotFound)
}
return ConnectedParticipant{}, err
}
if room == nil || currentRoom != room ||
currentRoom.codeEntryPolicy != protocol.CodeEntryPrivate ||
currentRoom.viewerPasswordMaterial == nil ||
!matches ||
subtle.ConstantTimeCompare(currentRoom.viewerPasswordMaterial, expectedMaterial) != 1 ||
!mayConnect() {
return ConnectedParticipant{}, codeError(CodeInvalidToken)
}
connected, err := s.connectViewer(
currentRoom, input.ClientID, input.SessionID, admittedByCode)
if hasCode(err, CodeRoomFull) {
return ConnectedParticipant{}, codeError(CodeInvalidToken)
}
return connected, err
}
// DisconnectParticipant clears only the matching session and returns nil when
// the room, peer or current session no longer matches.
func (s *Store) DisconnectParticipant(
roomID, peerID, sessionID string,
) (*DisconnectedParticipant, error) {
if err := s.ensureInitialized(); err != nil {
return nil, err
}
room, ok := s.rooms.Get(roomID)
if !ok {
return nil, nil
}
if room.host != nil && room.host.peerID == peerID {
if room.host.sessionID != sessionID {
return nil, nil
}
room.host.sessionID = ""
if !roomOccupied(room) {
room.emptySinceMs = s.now()
}
return &DisconnectedParticipant{
RoomID: roomID, Role: protocol.RoleHost, PeerID: peerID}, nil
}
viewer := findViewerByPeerID(room, peerID)
if viewer == nil || viewer.sessionID != sessionID {
return nil, nil
}
viewer.sessionID = ""
if !roomOccupied(room) {
room.emptySinceMs = s.now()
}
return &DisconnectedParticipant{
RoomID: roomID, Role: protocol.RoleViewer, PeerID: peerID}, nil
}
// RemoveDisconnectedViewer removes a Viewer only if it has no current session.
func (s *Store) RemoveDisconnectedViewer(roomID, peerID string) bool {
room, ok := s.rooms.Get(roomID)
if !ok {
return false
}
viewer := findViewerByPeerID(room, peerID)
if viewer == nil || viewer.sessionID != "" {
return false
}
return room.viewers.Delete(viewer.clientID)
}
// GetConnectedHost returns only a Host with a current session.
func (s *Store) GetConnectedHost(roomID string) (ConnectedPeer, bool) {
room, ok := s.rooms.Get(roomID)
if !ok || room.host == nil || room.host.sessionID == "" {
return ConnectedPeer{}, false
}
return ConnectedPeer{PeerID: room.host.peerID, SessionID: room.host.sessionID}, true
}
// GetConnectedViewer returns only a Viewer with a current session.
func (s *Store) GetConnectedViewer(roomID, peerID string) (ConnectedPeer, bool) {
room, ok := s.rooms.Get(roomID)
if !ok {
return ConnectedPeer{}, false
}
viewer := findViewerByPeerID(room, peerID)
if viewer == nil || viewer.sessionID == "" {
return ConnectedPeer{}, false
}
return ConnectedPeer{PeerID: peerID, SessionID: viewer.sessionID}, true
}
// GetViewerPeerIDs is getViewerPeerIds, in viewer insertion order.
func (s *Store) GetViewerPeerIDs(roomID string) []string {
room, ok := s.rooms.Get(roomID)
if !ok {
return nil
}
peerIDs := make([]string, 0, room.viewers.Len())
for _, viewer := range room.viewers.Values() {
peerIDs = append(peerIDs, viewer.peerID)
}
return peerIDs
}
// GetConnectedViewers is getConnectedViewers, in viewer insertion order.
func (s *Store) GetConnectedViewers(roomID string) []ConnectedPeer {
room, ok := s.rooms.Get(roomID)
if !ok {
return nil
}
var connected []ConnectedPeer
for _, viewer := range room.viewers.Values() {
if viewer.sessionID != "" {
connected = append(connected, ConnectedPeer{
PeerID: viewer.peerID, SessionID: viewer.sessionID})
}
}
return connected
}
// AbandonRoom retires room authority, returning nil if the room is already absent.
func (s *Store) AbandonRoom(roomID string) (*ClosedRoom, error) {
closed, err := s.abandonRooms([]string{roomID})
if err != nil || len(closed) == 0 {
return nil, err
}
return &closed[0], nil
}
// Retire a batch atomically before releasing codes. One SQLite transaction
// avoids thousands of fsyncs under the signaling lock after a restart.
func (s *Store) abandonRooms(roomIDs []string) ([]ClosedRoom, error) {
if err := s.ensureInitialized(); err != nil {
return nil, err
}
var authorities []StoredRoomAuthority
var closed []ClosedRoom
for _, roomID := range roomIDs {
if room, ok := s.rooms.Get(roomID); ok {
authorities = append(authorities, StoredRoomAuthority{RoomID: roomID, HostTokenDigest: room.hostTokenDigest})
closed = append(closed, ClosedRoom{RoomID: roomID, SessionIDs: connectedSessionIDs(room)})
}
}
if len(closed) == 0 {
return nil, nil
}
err := s.writeDatabase(func() error {
return s.database.deleteRooms(authorities)
})
if err != nil {
return nil, err
}
for _, room := range closed {
s.rooms.Delete(room.RoomID)
s.releaseRoomCode(room.RoomID)
}
return closed, nil
}
// AbandonAllRooms is abandonAllRooms, in room insertion order.
func (s *Store) AbandonAllRooms() ([]ClosedRoom, error) {
return s.abandonRooms(s.rooms.Keys())
}
func (s *Store) connectHost(
room *Room, input ConnectParticipantInput,
) (ConnectedParticipant, error) {
current := room.host
if current != nil && current.sessionID != "" && current.clientID != input.ClientID {
return ConnectedParticipant{}, codeError(CodeHostAlreadyConnected)
}
joining := current
if current == nil || current.clientID != input.ClientID {
peerID, err := s.newPeerID(room)
if err != nil {
return ConnectedParticipant{}, err
}
joining = &participant{
clientID: input.ClientID, peerID: peerID, admittedBy: admittedByCode}
}
replacedSessionID := joining.sessionID
joining.sessionID = input.SessionID
room.host = joining
return connectedParticipant(
room, joining.peerID, protocol.RoleHost, replacedSessionID, input.SessionID), nil
}
func (s *Store) connectViewer(
room *Room, clientID, sessionID, admittedBy string,
) (ConnectedParticipant, error) {
joining, ok := room.viewers.Get(clientID)
if !ok && room.viewers.Len() >= s.maxViewersPerRoom {
return ConnectedParticipant{}, codeError(CodeRoomFull)
}
if !ok {
peerID, err := s.newPeerID(room)
if err != nil {
return ConnectedParticipant{}, err
}
joining = &participant{clientID: clientID, peerID: peerID, admittedBy: admittedBy}
}
replacedSessionID := joining.sessionID
joining.sessionID = sessionID
joining.admittedBy = admittedBy
// ordering: Map.set keeps the position of an existing key.
room.viewers.Set(clientID, joining)
return connectedParticipant(
room, joining.peerID, protocol.RoleViewer, replacedSessionID, sessionID), nil
}
func connectedParticipant(
room *Room, peerID string, role protocol.Role, replacedSessionID, sessionID string,
) ConnectedParticipant {
if replacedSessionID == sessionID {
replacedSessionID = ""
}
return ConnectedParticipant{
RoomID: room.roomID,
Role: role,
PeerID: peerID,
HostOnline: room.host != nil && room.host.sessionID != "",
ReplacedSessionID: replacedSessionID,
CodeEntryPolicy: room.codeEntryPolicy,
ViewerPasswordEnabled: room.viewerPasswordMaterial != nil,
ViewerAuthorizationGeneration: room.viewerAuthorizationGeneration,
}
}
func (s *Store) getAvailableRoom(roomID string) (*Room, error) {
room, ok := s.rooms.Get(roomID)
if !ok {
return nil, codeError(CodeInvalidToken)
}
return room, nil
}
func (s *Store) getHostManagedRoom(roomID, hostToken string) (*Room, error) {
room, err := s.getAvailableRoom(roomID)
if err != nil {
return nil, err
}
if !verifyDigest(hostToken, room.hostTokenDigest) {
return nil, codeError(CodeInvalidToken)
}
return room, nil
}
func (s *Store) newPeerID(room *Room) (string, error) {
return s.uniqueID(16, func(candidate string) bool {
if room.host != nil && room.host.peerID == candidate {
return true
}
for _, viewer := range room.viewers.Values() {
if viewer.peerID == candidate {
return true
}
}
return false
})
}
func (s *Store) takeRoomCode(preferredRoomID string) (string, error) {
if len(s.freeRoomCodes) == 0 {
return "", codeError(CodeRoomLimit)
}
if preferredRoomID != "" {
if index := slices.Index(s.freeRoomCodes, preferredRoomID); index >= 0 {
return s.takeRoomCodeAt(index), nil
}
}
index, err := uniformIndex(len(s.freeRoomCodes), s.random)
if err != nil {
return "", err
}
return s.takeRoomCodeAt(index), nil
}
// uniformIndex draws a uniform index below count from 8 random bytes, rejecting
// the incomplete final bucket of the 2^64 possible values.
// (MaxUint64 % count + 1) % count computes 2^64 % count without overflowing.
func uniformIndex(count int, random func(size int) []byte) (int, error) {
span := uint64(count)
remainder := (math.MaxUint64%span + 1) % span
for attempt := 0; attempt < 16; attempt++ {
value := random(8)
if len(value) != 8 {
return 0, errors.New("Room code random source must return 8 bytes")
}
if remainder != 0 &&
binary.BigEndian.Uint64(value) >= math.MaxUint64-remainder+1 {
continue
}
return int(binary.BigEndian.Uint64(value) % span), nil
}
return 0, errors.New("Unable to select a room code uniformly")
}
// takeRoomCodeAt removes the code at index by swapping the last code into it.
func (s *Store) takeRoomCodeAt(index int) string {
roomID := s.freeRoomCodes[index]
last := len(s.freeRoomCodes) - 1
lastRoomID := s.freeRoomCodes[last]
s.freeRoomCodes = s.freeRoomCodes[:last]
if index < last {
s.freeRoomCodes[index] = lastRoomID
}
return roomID
}
func (s *Store) releaseRoomCode(roomID string) {
s.freeRoomCodes = append(s.freeRoomCodes, roomID)
}
func (s *Store) uniqueID(size int, exists func(candidate string) bool) (string, error) {
for attempt := 0; attempt < 8; attempt++ {
value := s.random(size)
if len(value) != size {
return "", fmt.Errorf("Identifier random source must return %d bytes", size)
}
candidate := base64.RawURLEncoding.EncodeToString(value)
if !exists(candidate) {
return candidate, nil
}
}
return "", errors.New("Unable to allocate a unique identifier")
}
func viewerGrantIsValid(room *Room, grant string) bool {
if grant == "" || !protocol.ValidViewerGrant(grant) {
return false
}
return verifyDigest(grant, room.viewerGrantDigest)
}
func (s *Store) createViewerGrant() (string, error) {
secret := s.random(16)
if len(secret) != 16 {
return "", errors.New("Viewer grant random source must return 16 bytes")
}
return base64.RawURLEncoding.EncodeToString(secret), nil
}
func (s *Store) newRoom(
roomID string,
policy protocol.CodeEntryPolicy,
material []byte,
) (*Room, CreatedRoom, error) {
hostTokenBytes := s.random(32)
if len(hostTokenBytes) != 32 {
return nil, CreatedRoom{}, errors.New("Host token random source must return 32 bytes")
}
hostToken := base64.RawURLEncoding.EncodeToString(hostTokenBytes)
viewerGrant, err := s.createViewerGrant()
if err != nil {
return nil, CreatedRoom{}, err
}
generation, err := s.newAuthorizationGeneration()
if err != nil {
return nil, CreatedRoom{}, err
}
room := &Room{
roomID: roomID,
hostTokenDigest: digest(hostToken),
viewerGrantDigest: digest(viewerGrant),
viewerPasswordMaterial: material,
viewerAuthorizationGeneration: generation,
codeEntryPolicy: policy,
emptySinceMs: s.now(),
}
return room, CreatedRoom{
RoomID: roomID,
HostToken: hostToken,
CodeEntryPolicy: policy,
ViewerGrant: viewerGrant,
}, nil
}
func (s *Store) randomDigest() ([]byte, error) {
value := s.random(32)
if len(value) != 32 {
return nil, errors.New("Viewer grant random source must return 32 bytes")
}
return bytes.Clone(value), nil
}
func (s *Store) newAuthorizationGeneration() (string, error) {
value := s.random(16)
if len(value) != 16 {
return "", errors.New("Authorization generation random source must return 16 bytes")
}
return base64.RawURLEncoding.EncodeToString(value), nil
}
func (s *Store) ensureInitialized() error {
if !s.initialized {
return errors.New("RoomStore stable authority is not initialized")
}
return nil
}
// writeDatabase runs a stable-authority write when a database is configured.
func (s *Store) writeDatabase(write func() error) error {
if s.database == nil {
return nil
}
return write()
}
func storedRoomAuthority(room *Room) StoredRoomAuthority {
return StoredRoomAuthority{
RoomID: room.roomID,
HostTokenDigest: room.hostTokenDigest,
ViewerGrantDigest: room.viewerGrantDigest,
ViewerAuthorizationGeneration: room.viewerAuthorizationGeneration,
CodeEntryPolicy: room.codeEntryPolicy,
ViewerPasswordMaterial: room.viewerPasswordMaterial,
}
}
func findViewerByPeerID(room *Room, peerID string) *participant {
for _, viewer := range room.viewers.Values() {
if viewer.peerID == peerID {
return viewer
}
}
return nil
}
func connectedSessionIDs(room *Room) []string {
var sessionIDs []string
if room.host != nil && room.host.sessionID != "" {
sessionIDs = append(sessionIDs, room.host.sessionID)
}
for _, viewer := range room.viewers.Values() {
if viewer.sessionID != "" {
sessionIDs = append(sessionIDs, viewer.sessionID)
}
}
return sessionIDs
}
func digest(value string) []byte {
sum := sha256.Sum256([]byte(value))
return sum[:]
}
func verifyDigest(value string, expectedDigest []byte) bool {
return subtle.ConstantTimeCompare(digest(value), expectedDigest) == 1
}