first commit
This commit is contained in:
commit
d198332c92
15 files changed
+1125
No files matched your search
@@ -0,0 +1,190 @@
|
||||
<script setup>
|
||||
import { ref, computed, onBeforeUnmount } from 'vue';
|
||||
import {
|
||||
createPeerConnection,
|
||||
createAnswerCode,
|
||||
getFingerprints,
|
||||
STUN_PROVIDERS,
|
||||
} from '../lib/webrtc';
|
||||
|
||||
const STATUS = {
|
||||
idle: 'Paste the sender\'s connection code to start.',
|
||||
'creating-answer': 'Gathering connection info…',
|
||||
'answer-ready': 'Send the code below back to the sender.',
|
||||
connecting: 'Waiting for the P2P connection…',
|
||||
receiving: 'Receiving…',
|
||||
done: 'File received!',
|
||||
closed: 'Connection closed.',
|
||||
};
|
||||
|
||||
const step = ref('idle');
|
||||
const offerInput = ref('');
|
||||
const answerCode = ref('');
|
||||
const progress = ref(0);
|
||||
const error = ref('');
|
||||
const copied = ref(false);
|
||||
const fps = ref(null);
|
||||
const stunChoice = ref('google');
|
||||
const customStun = ref('');
|
||||
|
||||
const fileName = ref('');
|
||||
const fileSize = ref(0);
|
||||
const downloadUrl = ref('');
|
||||
|
||||
let pc = 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;
|
||||
});
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
pc?.close();
|
||||
if (downloadUrl.value) URL.revokeObjectURL(downloadUrl.value);
|
||||
});
|
||||
|
||||
async function acceptOffer() {
|
||||
error.value = '';
|
||||
if (downloadUrl.value) {
|
||||
URL.revokeObjectURL(downloadUrl.value);
|
||||
downloadUrl.value = '';
|
||||
}
|
||||
progress.value = 0;
|
||||
fps.value = null;
|
||||
|
||||
pc?.close();
|
||||
pc = createPeerConnection(resolvedStun.value);
|
||||
pc.ondatachannel = (e) => setupReceiver(e.channel);
|
||||
pc.onconnectionstatechange = () => {
|
||||
if (['disconnected', 'failed', 'closed'].includes(pc.connectionState)) {
|
||||
if (step.value !== 'done') step.value = 'closed';
|
||||
}
|
||||
};
|
||||
|
||||
step.value = 'creating-answer';
|
||||
try {
|
||||
answerCode.value = await createAnswerCode(pc, offerInput.value);
|
||||
fps.value = getFingerprints(pc);
|
||||
step.value = 'answer-ready';
|
||||
} catch {
|
||||
step.value = 'idle';
|
||||
error.value = 'Invalid offer code — paste the full code from the sender.';
|
||||
}
|
||||
}
|
||||
|
||||
function setupReceiver(dc) {
|
||||
dc.binaryType = 'arraybuffer';
|
||||
let meta = null;
|
||||
let chunks = [];
|
||||
let received = 0;
|
||||
|
||||
dc.onopen = () => {
|
||||
if (step.value === 'answer-ready') step.value = 'connecting';
|
||||
};
|
||||
|
||||
dc.onmessage = (e) => {
|
||||
if (typeof e.data === 'string') {
|
||||
// Metadata frame
|
||||
meta = JSON.parse(e.data);
|
||||
fileName.value = meta.name;
|
||||
fileSize.value = meta.size;
|
||||
received = 0;
|
||||
if (meta.size === 0) finish(meta, []);
|
||||
else step.value = 'receiving';
|
||||
return;
|
||||
}
|
||||
chunks.push(e.data);
|
||||
received += e.data.byteLength;
|
||||
if (meta) {
|
||||
progress.value = Math.min(100, Math.floor((received / meta.size) * 100));
|
||||
if (received >= meta.size) finish(meta, chunks);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function finish(meta, chunks) {
|
||||
if (downloadUrl.value) URL.revokeObjectURL(downloadUrl.value);
|
||||
const blob = new Blob(chunks, { type: meta.type || 'application/octet-stream' });
|
||||
downloadUrl.value = URL.createObjectURL(blob);
|
||||
progress.value = 100;
|
||||
step.value = 'done';
|
||||
}
|
||||
|
||||
async function copy(text) {
|
||||
await navigator.clipboard.writeText(text);
|
||||
copied.value = true;
|
||||
setTimeout(() => (copied.value = false), 1500);
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="panel">
|
||||
<label class="step">1 — 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">2 — Paste the sender's connection code</label>
|
||||
<textarea
|
||||
v-model="offerInput"
|
||||
placeholder="Paste the code from the sender…"
|
||||
/>
|
||||
<button class="primary" :disabled="!offerInput.trim()" @click="acceptOffer">
|
||||
Accept & create answer
|
||||
</button>
|
||||
|
||||
<template v-if="answerCode">
|
||||
<label class="step">3 — Send this answer code back to the sender</label>
|
||||
<textarea :value="answerCode" readonly @focus="$event.target.select()" />
|
||||
<button @click="copy(answerCode)">{{ copied ? 'Copied!' : 'Copy code' }}</button>
|
||||
</template>
|
||||
|
||||
<div class="progress" v-if="step === 'receiving' || 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>
|
||||
|
||||
<div v-if="downloadUrl" class="file-chip">
|
||||
<span>{{ fileName }}</span>
|
||||
<a class="download" :href="downloadUrl" :download="fileName">Save file</a>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
Reference in new issue
Block a user