WIP IV improve UX [snapshot task 2]

This commit is contained in:
Schluffe committed 2026-10-17 00:59:12 +02:00
1 parent 28c90d1d71
commit 58c3e2af86
16 files changed
+779 -36

No files matched your search

+75 -6
View File
@@ -2,9 +2,11 @@
import { asError } from '../lib/validation.ts';
import type { Fingerprints, IceOptions, NetworkSettings as NetworkSettingsModel } from '../lib/webrtc.ts';
import type { FileMetadata, TransferState, FileDestination } from '../lib/transfer.ts';
import { computed, onBeforeUnmount, ref, watch } from 'vue';
import { computed, onBeforeUnmount, ref, shallowRef, 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, MAX_SIGNAL_LENGTH, STUN_PROVIDERS } from '../lib/webrtc.ts';
import { acceptAnswerCode, createAnswerCode, createOfferCode, createPeerConnection, fetchTurnOptions, iceConfiguration, getFingerprints, decodeSignal, signalIdentity, MAX_SIGNAL_LENGTH, STUN_PROVIDERS } from '../lib/webrtc.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';
@@ -17,10 +19,28 @@ interface Session {
controller: AbortController;
transfer: FileSession | null;
diagnostics: ConnectionDiagnostics;
localHello?: IdentityHello;
remoteHello?: IdentityHello;
identityContext?: IdentityContext;
identity?: IdentityState['identity'];
expected?: string;
timer?: ReturnType<typeof setTimeout>;
}
const props = defineProps<{ sending?: boolean }>();
const devices = ref<InstanceType<typeof DeviceSettings> | null>(null);
const deviceState = shallowRef<IdentityState>({ identity: null, contacts: [] });
const identityStatus = ref(''), contactName = ref('');
const canRemember = ref(false), savingContact = ref(false);
function deviceChange(state: IdentityState) { deviceState.value = state; }
async function saveContact() {
const session = active;
if (!session?.remoteHello || !connected.value || !canRemember.value || savingContact.value) return;
savingContact.value = true;
try { await devices.value?.remember(session.remoteHello.key, contactName.value.trim()); canRemember.value = false; contactName.value = ''; }
catch (err) { error.value = asError(err).message; }
finally { savingContact.value = false; }
}
const file = ref<File | null>(null);
const selectedFiles = ref<File[]>([]);
const queuedFiles = ref<QueueItem[]>([]);
@@ -109,6 +129,7 @@ function reset(keepNetwork = false) {
managedCredentials.value = null;
if (!keepNetwork) settings.value.allowTurn = false;
inputCode.value = outputCode.value = error.value = '';
identityStatus.value = ''; canRemember.value = false; contactName.value = '';
fps.value = incoming.value = null;
verified.value = copied.value = choosing.value = false;
progress.value = 0; receiveProgress.value = 0; step.value = 'idle'; streamedDownload.value = false;
@@ -158,7 +179,13 @@ function attach(session: Session, channel: RTCDataChannel) {
channel.close(); fail(session, new Error('Unexpected data channel.')); return;
}
session.transfer = new FileSession(channel, !!props.sending, {
onReady: () => { if (active === session) { connected.value = true; step.value = 'session-ready'; clearTimeout(session.timer); } },
onReady: () => { if (active === session) {
connected.value = true; step.value = 'session-ready'; clearTimeout(session.timer);
if (session.identityContext) {
identityStatus.value = session.expected === session.remoteHello?.key ? 'Remembered device authenticated for this connection.' : 'Device proof checked. You can remember this manually verified device.';
canRemember.value = !deviceState.value.contacts.some(contact => contact.key === session.remoteHello?.key);
}
} },
onQueue: items => { if (active === session) queuedFiles.value = items; },
onState: (direction, state, meta) => {
if (active !== session) return;
@@ -194,6 +221,7 @@ function attach(session: Session, channel: RTCDataChannel) {
onError: err => fail(session, err),
});
if (props.sending) { session.transfer.add(selectedFiles.value); selectedFiles.value = []; file.value = null; }
configureIdentity(session);
if (verified.value) session.transfer.verify();
}
function start() {
@@ -236,11 +264,19 @@ async function createCode() {
validateMetadata({ name: file.value.name, size: file.value.size, mime: file.value.type || '' });
}
session = start();
// Re-read saved trust for each connection; another tab may have forgotten
// the expected peer or replaced the local identity since this page loaded.
const device = await devices.value?.refreshForConnection();
if (active !== session) return;
if (device?.identity) {
session.identity = device.identity; session.localHello = hello(device.identity); session.expected = device.expected;
}
const configured = await fetchTurnOptions(session.options, iceEndpoint, { signal: session.controller.signal });
if (active !== session) return;
session.pc.setConfiguration(iceConfiguration(configured));
managedCredentials.value = session.options.allowTurn && session.options.managedTurn ? configured : null;
const options = { ...session.options, signal: session.controller.signal };
if (!props.sending) { decodeSignal(pasted, session.options); session.remoteHello = signalIdentity(pasted); }
const options = { ...session.options, signal: session.controller.signal, identity: session.localHello };
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);
if (active !== session) return;
@@ -248,7 +284,7 @@ async function createCode() {
session.diagnostics.record(props.sending ? 'offer ready' : 'answer ready');
session.diagnostics.descriptions();
void session.diagnostics.capture();
if (!props.sending) fps.value = getFingerprints(session.pc, options);
if (!props.sending) { fps.value = getFingerprints(session.pc, options); configureIdentity(session); }
step.value = props.sending ? 'offer-ready' : 'answer-ready';
} catch (err) {
if (session) fail(session, err);
@@ -261,12 +297,15 @@ 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);
if (active !== session) return;
session.diagnostics.record('answer applied');
session.diagnostics.descriptions();
void session.diagnostics.capture();
fps.value = getFingerprints(session.pc, session.options);
configureIdentity(session);
} catch (err) {
if (active !== session) return;
// A rejected answer does not consume a valid local offer. Let the user
@@ -277,6 +316,29 @@ async function useAnswer() {
} else fail(session, err);
}
}
function configureIdentity(session: Session) {
if (!fps.value || !session.transfer || session.identityContext) return;
if (!session.identity || !session.localHello || !session.remoteHello) {
if (session.expected) identityStatus.value = 'Expected device identity is missing. Compare both fingerprints manually; saved trust is not being used.';
else if (session.localHello || session.remoteHello) identityStatus.value = 'Both people must enable device identity to remember each other. Compare fingerprints manually for this connection.';
return;
}
const initiator = !!props.sending;
const context: IdentityContext = {
offer: initiator ? session.localHello : session.remoteHello,
answer: initiator ? session.remoteHello : session.localHello,
offerFingerprint: initiator ? fps.value.local : fps.value.remote,
answerFingerprint: initiator ? fps.value.remote : fps.value.local,
};
session.identityContext = context;
const identity = session.identity;
session.transfer.authenticate({ sign: () => signIdentity(identity, context, initiator), verify: proof => verifyIdentity(proof, context, !initiator) });
if (session.expected === session.remoteHello.key) {
identityStatus.value = 'Checking the remembered device’s fresh proof…'; verify();
} else {
identityStatus.value = session.expected ? 'Device identity differs from the expected contact. Compare both fingerprints manually before continuing. The saved contact will not be replaced.' : 'New device. Compare both fingerprints manually before remembering it.';
}
}
function verify() {
if (!active || !fps.value || verified.value) return;
verified.value = true; step.value = 'verification';
@@ -349,6 +411,7 @@ onBeforeUnmount(() => { dispose(); revokeDownload(); clearTimeout(copyTimer); })
<p v-if="outgoing" class="status">Sending: {{ outgoing.name }} — {{ stateLabel(sendState) }} ({{ progress }}%)</p>
<div v-if="outgoing" class="progress" role="progressbar" :aria-valuenow="progress" aria-valuemin="0" aria-valuemax="100" aria-label="Sending progress"><div :style="{ width: progress + '%' }"></div></div>
</section>
<DeviceSettings ref="devices" :disabled="busy || connected || savingContact" @change="deviceChange" />
<NetworkSettings v-model="settings" :disabled="busy || connected" :managed-credentials="managedCredentials" />
<p class="status">Connection codes are not encrypted: they can reveal your IP address and connection details to anyone who sees them. Exchange them privately with your intended peer.</p>
<template v-if="!sending">
@@ -366,12 +429,18 @@ onBeforeUnmount(() => { dispose(); revokeDownload(); clearTimeout(copyTimer); })
<textarea id="answer-input" v-model="inputCode" :maxlength="MAX_SIGNAL_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>
<div v-if="fps" class="fingerprint">
<h2 class="step">Verify both full SHA-256 fingerprints</h2>
<div class="fp-row"><span>Your fingerprint</span><code>{{ fps.local }}</code></div>
<div class="fp-row"><span>Their fingerprint</span><code>{{ fps.remote }}</code></div>
<p class="status">Compare every byte over an authenticated channel an attacker cannot alter (for example, an in-person comparison). Your fingerprint must match their “Their fingerprint”, and vice versa. If either differs, cancel. Both people must confirm before the filename or contents are sent.</p>
<button class="primary" :disabled="verified || step === 'done'" @click="verify">{{ verified ? 'Fingerprints confirmed' : 'Both fingerprints match — confirm' }}</button>
<button class="primary" :disabled="verified || step === 'done'" @click="verify">{{ verified ? 'Verification submitted' : 'Both fingerprints match — confirm' }}</button>
</div>
<div v-if="connected && canRemember">
<label for="contact-name">Remember this device as</label>
<input id="contact-name" v-model="contactName" maxlength="80" />
<button :disabled="!contactName.trim() || savingContact" @click="saveContact">Remember device</button>
</div>
<section v-if="connected || incoming || downloadUrl" ref="receiveSection" class="transfer-section" tabindex="-1" aria-labelledby="receive-heading">
<h2 id="receive-heading" class="step">Receive files</h2>