fix: harden WebRTC transfers for v0.0.1
Require mutual fingerprint confirmation before sharing metadata or files. Validate incoming messages, bound buffering, and confirm receipt before reporting success. Add direct-to-disk saving and explicit TURN opt-in. Fix reset and retry handling, clean up cancelled connections, and preserve completed Firefox downloads when the sender disconnects. Update dependencies, clarify security assumptions, and add protocol and cross-browser regression tests. Set version 0.0.1 and document the release in CHANGELOG.md, linked from the README. BREAKING CHANGE: both peers must use signaling and transfer protocol v2; older connection codes are no longer accepted.
This commit is contained in:
1 parent
d198332c92
commit
752d0cf3af
16 files changed
+1352
-603
No files matched your search
@@ -1,248 +1,4 @@
|
||||
<script setup>
|
||||
import { ref, computed } from 'vue';
|
||||
import {
|
||||
createPeerConnection,
|
||||
createOfferCode,
|
||||
acceptAnswerCode,
|
||||
getFingerprints,
|
||||
STUN_PROVIDERS,
|
||||
CHUNK_SIZE,
|
||||
MAX_BUFFERED,
|
||||
waitForBufferDrain,
|
||||
} from '../lib/webrtc';
|
||||
|
||||
const STATUS = {
|
||||
idle: 'Pick a file to start.',
|
||||
'creating-offer': 'Gathering connection info…',
|
||||
'offer-ready': 'Send the code below to the receiver (any chat works).',
|
||||
connecting: 'Answer accepted — waiting for the P2P connection…',
|
||||
sending: 'Transferring…',
|
||||
done: 'Transfer complete!',
|
||||
closed: 'Connection closed.',
|
||||
};
|
||||
|
||||
const file = ref(null);
|
||||
const step = ref('idle');
|
||||
const offerCode = ref('');
|
||||
const answerInput = ref('');
|
||||
const progress = ref(0);
|
||||
const error = ref('');
|
||||
const copied = ref(false);
|
||||
const dragover = ref(false);
|
||||
const fps = ref(null);
|
||||
const stunChoice = ref('google');
|
||||
const customStun = ref('');
|
||||
|
||||
let pc = null;
|
||||
let dc = null;
|
||||
|
||||
const statusText = computed(() => STATUS[step.value] ?? '');
|
||||
const resolvedStun = computed(() => {
|
||||
if (stunChoice.value === 'custom') {
|
||||
const raw = customStun.value.trim();
|
||||
return raw ? raw : null;
|
||||
}
|
||||
return STUN_PROVIDERS.find((p) => p.id === stunChoice.value)?.url ?? null;
|
||||
});
|
||||
|
||||
function reset() {
|
||||
pc?.close();
|
||||
pc = null;
|
||||
dc = null;
|
||||
offerCode.value = '';
|
||||
answerInput.value = '';
|
||||
progress.value = 0;
|
||||
error.value = '';
|
||||
copied.value = false;
|
||||
fps.value = null;
|
||||
step.value = 'idle';
|
||||
}
|
||||
|
||||
function pickFiles(e) {
|
||||
const f = e.target?.files?.[0] ?? e.dataTransfer?.files?.[0];
|
||||
if (f) {
|
||||
reset();
|
||||
file.value = f;
|
||||
}
|
||||
}
|
||||
|
||||
async function makeOffer() {
|
||||
if (!file.value) return;
|
||||
error.value = '';
|
||||
step.value = 'creating-offer';
|
||||
|
||||
pc = createPeerConnection(resolvedStun.value);
|
||||
pc.onconnectionstatechange = () => {
|
||||
if (pc && ['disconnected', 'failed', 'closed'].includes(pc.connectionState)) {
|
||||
step.value = 'closed';
|
||||
}
|
||||
};
|
||||
dc = pc.createDataChannel('file');
|
||||
dc.binaryType = 'arraybuffer';
|
||||
dc.onopen = () => sendFile();
|
||||
dc.onclose = () => {
|
||||
if (step.value === 'sending' && progress.value < 100) step.value = 'closed';
|
||||
};
|
||||
|
||||
try {
|
||||
offerCode.value = await createOfferCode(pc);
|
||||
step.value = 'offer-ready';
|
||||
} catch (err) {
|
||||
error.value = String(err);
|
||||
step.value = 'idle';
|
||||
}
|
||||
}
|
||||
|
||||
async function useAnswer() {
|
||||
if (!pc) return;
|
||||
error.value = '';
|
||||
try {
|
||||
await acceptAnswerCode(pc, answerInput.value);
|
||||
fps.value = getFingerprints(pc);
|
||||
step.value = 'connecting';
|
||||
} catch {
|
||||
error.value = 'Invalid answer code — paste the full code from the receiver.';
|
||||
}
|
||||
}
|
||||
|
||||
async function sendFile() {
|
||||
const f = file.value;
|
||||
if (!dc || !f) return;
|
||||
step.value = 'sending';
|
||||
progress.value = 0;
|
||||
|
||||
// First frame: metadata as JSON. Everything after is raw bytes.
|
||||
dc.send(JSON.stringify({ name: f.name, size: f.size, type: f.type }));
|
||||
|
||||
let sent = 0;
|
||||
try {
|
||||
for (let offset = 0; offset < f.size; offset += CHUNK_SIZE) {
|
||||
if (dc.readyState !== 'open') break;
|
||||
if (dc.bufferedAmount > MAX_BUFFERED) await waitForBufferDrain(dc);
|
||||
const buf = await f.slice(offset, offset + CHUNK_SIZE).arrayBuffer();
|
||||
dc.send(buf);
|
||||
sent += buf.byteLength;
|
||||
progress.value = Math.floor((sent / Math.max(f.size, 1)) * 100);
|
||||
}
|
||||
if (dc.readyState === 'open') {
|
||||
progress.value = 100;
|
||||
step.value = 'done';
|
||||
}
|
||||
} catch (err) {
|
||||
error.value = String(err);
|
||||
step.value = 'closed';
|
||||
}
|
||||
}
|
||||
|
||||
async function copy(text) {
|
||||
await navigator.clipboard.writeText(text);
|
||||
copied.value = true;
|
||||
setTimeout(() => (copied.value = false), 1500);
|
||||
}
|
||||
|
||||
function formatBytes(n) {
|
||||
if (n < 1024) return n + ' B';
|
||||
const u = ['KiB', 'MiB', 'GiB'];
|
||||
let v = n;
|
||||
let i = -1;
|
||||
do {
|
||||
v /= 1024;
|
||||
i++;
|
||||
} while (v >= 1024 && i < u.length - 1);
|
||||
return v.toFixed(1) + ' ' + u[i];
|
||||
}
|
||||
import TransferPanel from './TransferPanel.vue';
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="panel">
|
||||
<label class="step">1 — Choose a file</label>
|
||||
|
||||
<div
|
||||
class="dropzone"
|
||||
:class="{ dragover }"
|
||||
@dragover.prevent="dragover = true"
|
||||
@dragleave="dragover = false"
|
||||
@drop.prevent="dragover = false; pickFiles($event)"
|
||||
@click="$refs.input.click()"
|
||||
>
|
||||
<span v-if="!file">Drop a file here or click to browse</span>
|
||||
<div v-else class="file-chip" @click.stop>
|
||||
<span>{{ file.name }}</span>
|
||||
<span class="status">{{ formatBytes(file.size) }}</span>
|
||||
</div>
|
||||
<input
|
||||
ref="input"
|
||||
type="file"
|
||||
style="display: none"
|
||||
@change="pickFiles"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<template v-if="file">
|
||||
<label class="step">2 — Choose a STUN provider</label>
|
||||
<select v-model="stunChoice" class="stun-select">
|
||||
<option v-for="p in STUN_PROVIDERS" :key="p.id" :value="p.id">
|
||||
{{ p.label }}
|
||||
</option>
|
||||
<option value="custom">Custom…</option>
|
||||
</select>
|
||||
<input
|
||||
v-if="stunChoice === 'custom'"
|
||||
v-model="customStun"
|
||||
class="stun-input"
|
||||
type="text"
|
||||
placeholder="stun:your.domain:3478"
|
||||
/>
|
||||
<p class="status stun-hint">
|
||||
STUN is only needed for peers on different networks — a stateless UDP
|
||||
lookup of your public IP; it never sees the connection or the file.
|
||||
Same-network or VPN peers work with "None".
|
||||
</p>
|
||||
|
||||
<label class="step">3 — Create your connection code</label>
|
||||
<button class="primary" :disabled="step !== 'idle'" @click="makeOffer">
|
||||
Create code
|
||||
</button>
|
||||
|
||||
<template v-if="offerCode">
|
||||
<textarea :value="offerCode" readonly @focus="$event.target.select()" />
|
||||
<button @click="copy(offerCode)">{{ copied ? 'Copied!' : 'Copy code' }}</button>
|
||||
</template>
|
||||
|
||||
<template v-if="offerCode">
|
||||
<label class="step">4 — Paste the receiver's answer code</label>
|
||||
<textarea
|
||||
v-model="answerInput"
|
||||
placeholder="Paste the code the receiver sends back…"
|
||||
/>
|
||||
<button class="primary" :disabled="!answerInput.trim()" @click="useAnswer">
|
||||
Connect
|
||||
</button>
|
||||
</template>
|
||||
|
||||
<div class="progress" v-if="step === 'sending' || step === 'done'">
|
||||
<div :style="{ width: progress + '%' }"></div>
|
||||
</div>
|
||||
|
||||
<div v-if="fps" class="fingerprint">
|
||||
<label class="step">Security check</label>
|
||||
<div class="fp-row">
|
||||
<span>Your code</span>
|
||||
<code>{{ fps.local }}</code>
|
||||
</div>
|
||||
<div class="fp-row">
|
||||
<span>Their code</span>
|
||||
<code>{{ fps.remote }}</code>
|
||||
</div>
|
||||
<p class="status">
|
||||
To rule out an intercepted exchange, compare with your peer over a
|
||||
channel you trust: your "Your code" must equal their "Their code",
|
||||
and vice versa.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<p class="status">{{ statusText }}</p>
|
||||
<p class="error" v-if="error">{{ error }}</p>
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
<template><TransferPanel sending /></template>
|
||||
Reference in new issue
Block a user