WIP V improve UX [snapshot task 2]

This commit is contained in:
Schluffe committed 2026-10-17 14:51:48 +02:00
1 parent 5f8c6a1916
commit a73b3075b6
16 files changed
+238 -232

No files matched your search

+35 -37
View File
@@ -2,7 +2,7 @@
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, shallowRef, watch } from 'vue';
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';
@@ -11,7 +11,7 @@ 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';
import { createDownloadDestination, supportsStreamingDownload } from '../lib/download.ts';
import type { ConnectionActivity } from '../lib/connections.ts';
import type { ConnectionActivity, ConnectionUpdate, SavedConnection } from '../lib/connections.ts';
type Step = 'session-ready' | 'idle' | 'gathering' | 'offer-ready' | 'answer-ready' | TransferState;
interface Session {
@@ -28,20 +28,12 @@ interface Session {
timer?: ReturnType<typeof setTimeout>;
}
const props = defineProps<{ sending?: boolean; preferredDevice?: string }>();
const props = defineProps<{ sending?: boolean; connection: SavedConnection; loadTrust: () => Promise<SavedConnection> }>();
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()); emit('paired', session.remoteHello.key); canRemember.value = false; contactName.value = ''; }
catch (err) { error.value = asError(err).message; }
finally { savingContact.value = false; }
}
const identityStatus = ref('');
// Offered on the verification step whenever the verified peer would change
// this connection's paired device: 'new' starts ticked, 'replace' does not.
const pairing = ref<'' | 'new' | 'replace'>(''), rememberPeer = ref(false);
const file = ref<File | null>(null);
const selectedFiles = ref<File[]>([]);
const queuedFiles = ref<QueueItem[]>([]);
@@ -50,7 +42,7 @@ const receiveState = ref<TransferState | null>(null);
const outgoing = ref<FileMetadata | null>(null);
const receiveProgress = ref(0);
const lastDirection = ref<'send' | 'receive'>('send');
const emit = defineEmits<{ sessionChange: [active: boolean]; connectedChange: [connected: boolean]; activity: [item: ConnectionActivity]; paired: [key: string] }>();
const emit = defineEmits<{ sessionChange: [active: boolean]; connectedChange: [connected: boolean]; activity: [item: ConnectionActivity]; update: [change: ConnectionUpdate] }>();
const inputCode = ref('');
const outputCode = ref('');
const step = ref<Step>('idle');
@@ -133,7 +125,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 = '';
identityStatus.value = ''; pairing.value = ''; rememberPeer.value = false;
fps.value = incoming.value = null;
verified.value = copied.value = choosing.value = false;
progress.value = 0; receiveProgress.value = 0; step.value = 'idle'; streamedDownload.value = false;
@@ -187,10 +179,12 @@ function attach(session: Session, channel: RTCDataChannel) {
session.transfer = new FileSession(channel, !!props.sending, {
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);
if (!canRemember.value && session.remoteHello && (!props.preferredDevice || props.preferredDevice === session.remoteHello.key)) emit('paired', session.remoteHello.key);
// Ready implies both fresh proofs verified, so the key may now be saved.
if (session.identityContext && session.remoteHello) {
const key = session.remoteHello.key;
if (session.expected === key) identityStatus.value = 'Paired device authenticated for this connection.';
else if (rememberPeer.value) { emit('update', { peerKey: key }); identityStatus.value = `Device proof checked. Remembered for ${props.connection.name}.`; }
else identityStatus.value = 'Device proof checked. This device was not remembered.';
}
} },
onQueue: items => { if (active === session) queuedFiles.value = items; },
@@ -272,11 +266,13 @@ async function createCode() {
}
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;
// the paired peer or replaced the local identity since this page loaded.
if (props.connection.rememberDevice) {
const trust = await props.loadTrust();
const identity = trust.rememberDevice ? await devices.value?.refreshIdentity() : null;
if (active !== session) return;
if (trust.rememberDevice) session.expected = trust.peerKey;
if (identity) { session.identity = identity; session.localHello = hello(identity); }
}
const configured = await fetchTurnOptions(session.options, iceEndpoint, { signal: session.controller.signal });
if (active !== session) return;
@@ -291,7 +287,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); configureIdentity(session); }
if (!props.sending) { fps.value = getFingerprints(session.pc, options); offerPairing(session); configureIdentity(session); }
step.value = props.sending ? 'offer-ready' : 'answer-ready';
} catch (err) {
if (session) fail(session, err);
@@ -312,6 +308,7 @@ async function useAnswer() {
session.diagnostics.descriptions();
void session.diagnostics.capture();
fps.value = getFingerprints(session.pc, session.options);
offerPairing(session);
configureIdentity(session);
} catch (err) {
if (active !== session) return;
@@ -323,11 +320,16 @@ async function useAnswer() {
} else fail(session, err);
}
}
function offerPairing(session: Session) {
const key = session.localHello && session.remoteHello?.key;
pairing.value = !key || key === session.expected ? '' : session.expected ? 'replace' : 'new';
rememberPeer.value = pairing.value === 'new';
}
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.';
if (session.expected) identityStatus.value = 'A device identity is missing. Compare both fingerprints manually; the paired device is not being used.';
else if (session.localHello || session.remoteHello) identityStatus.value = 'Both people must use their device identity to remember each other. Compare fingerprints manually for this connection.';
return;
}
const initiator = !!props.sending;
@@ -341,9 +343,9 @@ function configureIdentity(session: Session) {
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();
identityStatus.value = 'Checking the paired 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.';
identityStatus.value = session.expected ? 'Device identity differs from the paired device. Compare both fingerprints manually before continuing. The paired device is only replaced if you choose to.' : 'New device. Compare both fingerprints manually before remembering it.';
}
}
function verify() {
@@ -418,7 +420,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" :preferred-device="preferredDevice" :disabled="busy || connected || savingContact" @change="deviceChange" />
<DeviceSettings ref="devices" :paired="connection.peerKey" :remember="connection.rememberDevice" :disabled="busy || connected" @update="emit('update', $event)" />
<details class="network-details">
<summary>Network settings</summary>
<NetworkSettings v-model="settings" :disabled="busy || connected" :managed-credentials="managedCredentials" />
@@ -445,13 +447,9 @@ onBeforeUnmount(() => { dispose(); revokeDownload(); clearTimeout(copyTimer); })
<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>
<label v-if="pairing"><input v-model="rememberPeer" type="checkbox" :disabled="verified" /> {{ pairing === 'new' ? `Remember this device for ${connection.name}` : `Replace the paired device of ${connection.name}` }}</label>
<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>
<p v-if="!incoming" class="status">Waiting for your peer to send a file. You can send files yourself using Send above.</p>