feat(media,server): media in the Shade container, owner-only uploads and channels
- createMediaService: sealed store + live relay + /watch in one mount, used by both `shade media serve` and standalone.ts - SHADE_MEDIA_DIR enables it (no in-memory fallback, like the vault); SHADE_MEDIA_OWNERS lists who may upload and open live channels - live publishers co-sign their hello with an owner key; a relay with an owner list refuses everyone else, and the client stops on that refusal instead of reconnecting forever - relay caps live channels held at once; `shade media whoami` Verified against the real standalone process: film round trip identical, stranger gets 403 / refused channel, live logs and browser playback. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -32,7 +32,7 @@ import { mkdir, readFile, rename, writeFile } from 'node:fs/promises';
|
||||
import { dirname } from 'node:path';
|
||||
import { enc, equalBytes, fromB64u, randomBytes, sha256, toB64u, toHex } from '../bytes.js';
|
||||
import { decodeFrame, FLAG_END, FLAG_KEYFRAME, FLAG_STICKY } from '../live/frame.js';
|
||||
import { MAX_SKEW_MS } from '../owner.js';
|
||||
import { MAX_SKEW_MS, verifyPublishOwner } from '../owner.js';
|
||||
import { Ed25519Verifier } from '../ed25519.js';
|
||||
|
||||
export interface LivePin {
|
||||
@@ -106,6 +106,13 @@ export class FileLivePinStore implements LivePinStore {
|
||||
|
||||
export interface LiveRelayOptions {
|
||||
pins: LivePinStore;
|
||||
/**
|
||||
* Owner keys (base64url) allowed to publish. A publisher then co-signs its
|
||||
* hello with one of them. Unset = anyone may open a channel.
|
||||
*/
|
||||
allowedOwners?: readonly string[];
|
||||
/** Channels held at once (live state, not pins). Default 1000. */
|
||||
maxChannels?: number;
|
||||
/** Keyframes of history kept for late joiners. Default 3. */
|
||||
backlogKeyframes?: number;
|
||||
/** Byte cap on a channel's backlog (sticky frames excluded). Default 32 MiB. */
|
||||
@@ -173,12 +180,18 @@ export function createLiveRelay(opts: LiveRelayOptions): LiveRelay {
|
||||
const idleMs = opts.idleRetentionMs ?? 10 * 60 * 1000;
|
||||
const maxFrameBytes = opts.maxFrameBytes ?? 16 * 1024 * 1024 + 256;
|
||||
const channels = new Map<string, Channel>();
|
||||
const allowedOwners = opts.allowedOwners !== undefined ? new Set(opts.allowedOwners) : null;
|
||||
const maxChannels = opts.maxChannels ?? 1000;
|
||||
|
||||
async function channelFor(id: string): Promise<Channel | null> {
|
||||
const have = channels.get(id);
|
||||
if (have !== undefined) return have;
|
||||
const pin = await opts.pins.get(id);
|
||||
if (pin === null) return null;
|
||||
if (channels.size >= maxChannels) {
|
||||
sweep();
|
||||
if (channels.size >= maxChannels) throw new Error('relay is at its channel limit');
|
||||
}
|
||||
const ch: Channel = {
|
||||
id,
|
||||
pin,
|
||||
@@ -335,6 +348,13 @@ export function createLiveRelay(opts: LiveRelayOptions): LiveRelay {
|
||||
} catch {
|
||||
return fail(ws, 'malformed hello');
|
||||
}
|
||||
if (allowedOwners !== null) {
|
||||
const owner = typeof msg.owner === 'string' ? msg.owner : '';
|
||||
const osig = typeof msg.ownerSignature === 'string' ? msg.ownerSignature : '';
|
||||
if (!allowedOwners.has(owner) || !verifyPublishOwner(owner, osig, msg.hello)) {
|
||||
return fail(ws, 'this relay only accepts channels from its owners');
|
||||
}
|
||||
}
|
||||
const existing = await opts.pins.get(channelId);
|
||||
if (existing !== null && existing.signingKey !== hello.signingKey) {
|
||||
return fail(ws, 'channel is pinned to another key');
|
||||
|
||||
Reference in New Issue
Block a user