Use compressed connection codes in the transfer panel
Offers and answers now go through the HC1 codec: the sender creates a ref per session and the answer must echo it, so pasting an offer as the answer or an answer for another code is rejected with a specific message while the live offer stays usable. Pasted input may be a full link. webrtc.ts drops its Base64 JSON signaling and exchanges validated SDP only; identity hellos travel in the code envelope. Tests, the browser check, compare-ice and the docs follow the new format. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
da2999ed5d
commit
8bd37ac8b1
10 files changed
+68
-89
No files matched your search
@@ -6,7 +6,8 @@ import { computed, onBeforeUnmount, ref, watch } from 'vue';
|
||||
import DeviceSettings from './DeviceSettings.vue';
|
||||
import { hello, signIdentity, verifyIdentity, type IdentityState, type IdentityHello, type IdentityContext } from '../lib/identity.ts';
|
||||
import NetworkSettings from './NetworkSettings.vue';
|
||||
import { acceptAnswerCode, createAnswerCode, createOfferCode, createPeerConnection, fetchTurnOptions, iceConfiguration, getFingerprints, decodeSignal, signalIdentity, MAX_SIGNAL_LENGTH, STUN_PROVIDERS } from '../lib/webrtc.ts';
|
||||
import { acceptAnswer, createAnswer, createOffer, createPeerConnection, fetchTurnOptions, iceConfiguration, getFingerprints, STUN_PROVIDERS } from '../lib/webrtc.ts';
|
||||
import { createRef, decodeCode, encodeCode, MAX_INPUT_LENGTH } from '../lib/codes.ts';
|
||||
import { MEMORY_LIMIT, validateMetadata } from '../lib/transfer.ts';
|
||||
import { FileSession, QUEUE_LIMIT, type QueueItem } from '../lib/session.ts';
|
||||
import { ConnectionDiagnostics } from '../lib/connection-diagnostics.ts';
|
||||
@@ -25,6 +26,8 @@ interface Session {
|
||||
identityContext?: IdentityContext;
|
||||
identity?: IdentityState['identity'];
|
||||
expected?: string;
|
||||
// Shared by this session's offer and answer codes.
|
||||
ref?: string;
|
||||
timer?: ReturnType<typeof setTimeout>;
|
||||
}
|
||||
|
||||
@@ -289,10 +292,14 @@ async function createCode() {
|
||||
if (active !== session) return;
|
||||
session.pc.setConfiguration(iceConfiguration(configured));
|
||||
managedCredentials.value = session.options.allowTurn && session.options.managedTurn ? configured : null;
|
||||
if (!props.sending) { decodeSignal(pasted, session.options); session.remoteHello = signalIdentity(pasted); }
|
||||
const options = { ...session.options, signal: session.controller.signal, identity: session.localHello };
|
||||
const offer = props.sending ? null : await decodeCode(pasted, { type: 'offer', allowTurn: session.options.allowTurn });
|
||||
if (active !== session) return;
|
||||
session.ref = offer ? offer.ref : createRef(); session.remoteHello = offer?.identity;
|
||||
const options = { ...session.options, signal: session.controller.signal };
|
||||
if (props.sending) attach(session, session.pc.createDataChannel('hellcon-v3'));
|
||||
const code = props.sending ? await createOfferCode(session.pc, options) : await createAnswerCode(session.pc, pasted, options);
|
||||
const sdp = offer ? await createAnswer(session.pc, offer.sdp, options) : await createOffer(session.pc, options);
|
||||
if (active !== session) return;
|
||||
const code = await encodeCode({ type: props.sending ? 'offer' : 'answer', ref: session.ref, sdp, identity: session.localHello });
|
||||
if (active !== session) return;
|
||||
outputCode.value = code;
|
||||
session.diagnostics.record(props.sending ? 'offer ready' : 'answer ready');
|
||||
@@ -311,9 +318,10 @@ async function useAnswer() {
|
||||
error.value = '';
|
||||
step.value = 'verification';
|
||||
try {
|
||||
decodeSignal(inputCode.value, session.options);
|
||||
session.remoteHello = signalIdentity(inputCode.value);
|
||||
await acceptAnswerCode(session.pc, inputCode.value, session.options);
|
||||
const answer = await decodeCode(inputCode.value, { type: 'answer', ref: session.ref, allowTurn: session.options.allowTurn });
|
||||
if (active !== session) return;
|
||||
session.remoteHello = answer.identity;
|
||||
await acceptAnswer(session.pc, answer.sdp, session.options);
|
||||
if (active !== session) return;
|
||||
session.diagnostics.record('answer applied');
|
||||
session.diagnostics.descriptions();
|
||||
@@ -439,7 +447,7 @@ onBeforeUnmount(() => { dispose(); revokeDownload(); clearTimeout(copyTimer); })
|
||||
<p v-if="!connected" class="status">Exchange codes privately. They contain your IP address and connection details.</p>
|
||||
<template v-if="!sending">
|
||||
<label class="step" for="offer-input">Paste the sender’s connection code</label>
|
||||
<textarea id="offer-input" v-model="inputCode" :maxlength="MAX_SIGNAL_LENGTH" :disabled="busy" />
|
||||
<textarea id="offer-input" v-model="inputCode" :maxlength="MAX_INPUT_LENGTH" :disabled="busy" />
|
||||
</template>
|
||||
<button class="primary" :disabled="busy || connected || (sending ? !file : !inputCode.trim())" @click="createCode">{{ sending ? 'Create code' : 'Accept & create answer' }}</button>
|
||||
<template v-if="outputCode">
|
||||
@@ -449,7 +457,7 @@ onBeforeUnmount(() => { dispose(); revokeDownload(); clearTimeout(copyTimer); })
|
||||
</template>
|
||||
<template v-if="sending && outputCode">
|
||||
<label class="step" for="answer-input">Paste the receiver’s answer</label>
|
||||
<textarea id="answer-input" v-model="inputCode" :maxlength="MAX_SIGNAL_LENGTH" :disabled="step !== 'offer-ready'" />
|
||||
<textarea id="answer-input" v-model="inputCode" :maxlength="MAX_INPUT_LENGTH" :disabled="step !== 'offer-ready'" />
|
||||
<button class="primary" :disabled="step !== 'offer-ready' || !inputCode.trim()" @click="useAnswer">Connect</button>
|
||||
</template>
|
||||
<p v-if="identityStatus" class="status" role="status">{{ identityStatus }}</p>
|
||||
|
||||
+3
-1
@@ -7,6 +7,8 @@ import { MAX_SIGNAL_LENGTH, validateDescription, type IceOptions } from './webrt
|
||||
// Codes carry connection details, not secrets; fingerprints authenticate peers.
|
||||
export const CODE_PREFIX = 'HC1.';
|
||||
export const MAX_CODE_LENGTH = MAX_SIGNAL_LENGTH;
|
||||
// Pasted text may be a full link around the code.
|
||||
export const MAX_INPUT_LENGTH = MAX_CODE_LENGTH * 2;
|
||||
const MAX_PAYLOAD = MAX_SIGNAL_LENGTH + 1024;
|
||||
const INVALID = 'Invalid connection code. Paste the complete code or link.';
|
||||
|
||||
@@ -72,7 +74,7 @@ export async function encodeCode({ type, ref, sdp, identity }: Signal) {
|
||||
}
|
||||
|
||||
export async function decodeCode(text: unknown, { type, ref, ...options }: DecodeOptions = {}): Promise<Signal> {
|
||||
if (typeof text !== 'string' || text.length > MAX_CODE_LENGTH * 2) throw new Error('Connection code is too large.');
|
||||
if (typeof text !== 'string' || text.length > MAX_INPUT_LENGTH) throw new Error('Connection code is too large.');
|
||||
const code = codeFromText(text);
|
||||
if (code.length > MAX_CODE_LENGTH) throw new Error('Connection code is too large.');
|
||||
if (!code.startsWith(CODE_PREFIX)) {
|
||||
|
||||
+13
-38
@@ -1,5 +1,4 @@
|
||||
import { parseHello, type IdentityHello } from './identity.ts';
|
||||
import { asError, isRecord, isStringArray } from './validation.ts';
|
||||
import { isRecord, isStringArray } from './validation.ts';
|
||||
|
||||
export interface IceOptions {
|
||||
stunUrl?: string | null;
|
||||
@@ -21,13 +20,13 @@ export interface NetworkSettings {
|
||||
}
|
||||
|
||||
export interface WaitOptions { signal?: AbortSignal; timeout?: number }
|
||||
type SignalOptions = WaitOptions & Pick<IceOptions, 'allowTurn'> & { identity?: IdentityHello };
|
||||
type SignalOptions = WaitOptions & Pick<IceOptions, 'allowTurn'>;
|
||||
export interface Fingerprints { local: string; remote: string }
|
||||
export type IcePeer = EventTarget & Pick<RTCPeerConnection, 'iceGatheringState' | 'connectionState'>;
|
||||
export type BufferedChannel = EventTarget & Pick<RTCDataChannel, 'readyState' | 'bufferedAmount' | 'bufferedAmountLowThreshold'>;
|
||||
type CredentialFetch = (url: string, init: RequestInit) => Promise<Pick<Response, 'ok' | 'json'>>;
|
||||
|
||||
// Manual signaling. Codes are encoded connection details, not encrypted secrets.
|
||||
// Manual signaling; codes.ts encodes the validated descriptions for exchange.
|
||||
export const CHUNK_SIZE = 16 * 1024;
|
||||
export const MAX_BUFFERED = 1024 * 1024;
|
||||
export const MAX_SIGNAL_LENGTH = 64 * 1024;
|
||||
@@ -132,47 +131,28 @@ export function getFingerprints(pc: { localDescription: { sdp: string } | null;
|
||||
};
|
||||
}
|
||||
|
||||
export function encodeSignal(sdp: string, identity?: IdentityHello) {
|
||||
const code = btoa(JSON.stringify(identity ? { version: 4, sdp, identity: parseHello(identity) } : { version: 3, sdp })).replace(/=+$/, '');
|
||||
if (code.length > MAX_SIGNAL_LENGTH) throw new Error('Connection code is too large. Reduce the configured network interfaces and try again.');
|
||||
return code;
|
||||
}
|
||||
|
||||
export function decodeSignal(text: unknown, options?: Pick<IceOptions, 'allowTurn'>): string {
|
||||
if (typeof text !== 'string' || text.length > MAX_SIGNAL_LENGTH) throw new Error('Connection code is too large.');
|
||||
try {
|
||||
const clean = text.trim().replace(/\s+/g, '');
|
||||
const data: unknown = JSON.parse(atob(clean));
|
||||
if (!isRecord(data) || ![3, 4].includes(data.version as number)) throw new Error('Both people need the current HellCon version.');
|
||||
if (typeof data.sdp !== 'string') throw new Error('Invalid connection description.');
|
||||
if (data.version === 4) parseHello(data.identity);
|
||||
validateDescription(data.sdp, options);
|
||||
return data.sdp;
|
||||
} catch (err) {
|
||||
if (err instanceof SyntaxError || asError(err).name === 'InvalidCharacterError') throw new Error('Invalid connection code. Paste the complete code.');
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
export async function createOfferCode(pc: RTCPeerConnection, options: SignalOptions = {}) {
|
||||
// Each step validates the description it produces or applies.
|
||||
export async function createOffer(pc: RTCPeerConnection, options: SignalOptions = {}) {
|
||||
await pc.setLocalDescription(await pc.createOffer());
|
||||
await waitForIceComplete(pc, options);
|
||||
if (!pc.localDescription) throw new Error('Missing local connection description.');
|
||||
validateDescription(pc.localDescription.sdp, options);
|
||||
return encodeSignal(pc.localDescription.sdp, options.identity);
|
||||
return pc.localDescription.sdp;
|
||||
}
|
||||
|
||||
export async function createAnswerCode(pc: RTCPeerConnection, text: string, options: SignalOptions = {}) {
|
||||
await pc.setRemoteDescription({ type: 'offer', sdp: decodeSignal(text, options) });
|
||||
export async function createAnswer(pc: RTCPeerConnection, offer: string, options: SignalOptions = {}) {
|
||||
validateDescription(offer, options);
|
||||
await pc.setRemoteDescription({ type: 'offer', sdp: offer });
|
||||
await pc.setLocalDescription(await pc.createAnswer());
|
||||
await waitForIceComplete(pc, options);
|
||||
if (!pc.localDescription) throw new Error('Missing local connection description.');
|
||||
validateDescription(pc.localDescription.sdp, options);
|
||||
return encodeSignal(pc.localDescription.sdp, options.identity);
|
||||
return pc.localDescription.sdp;
|
||||
}
|
||||
|
||||
export function acceptAnswerCode(pc: RTCPeerConnection, text: string, options?: SignalOptions) {
|
||||
return pc.setRemoteDescription({ type: 'answer', sdp: decodeSignal(text, options) });
|
||||
export function acceptAnswer(pc: RTCPeerConnection, answer: string, options?: SignalOptions) {
|
||||
validateDescription(answer, options);
|
||||
return pc.setRemoteDescription({ type: 'answer', sdp: answer });
|
||||
}
|
||||
|
||||
export function waitForBufferDrain(dc: BufferedChannel, { signal, timeout = 60000 }: WaitOptions = {}) {
|
||||
@@ -200,8 +180,3 @@ export function waitForBufferDrain(dc: BufferedChannel, { signal, timeout = 6000
|
||||
check();
|
||||
});
|
||||
}
|
||||
|
||||
export function signalIdentity(text: string): IdentityHello | undefined {
|
||||
const data = JSON.parse(atob(text.trim().replace(/\s+/g, '')));
|
||||
return data.version === 4 ? parseHello(data.identity) : undefined;
|
||||
}
|
||||
Reference in new issue
Block a user