first commit
This commit is contained in:
commit
d198332c92
15 files changed
+1125
No files matched your search
@@ -0,0 +1,98 @@
|
||||
// P2P with manual signaling: no server, no third party.
|
||||
// Peers exchange SDP blobs by copy/paste. ICE gathering runs to completion
|
||||
// so the full connection info fits in a single copyable string.
|
||||
|
||||
// Chunking / buffering for the data channel.
|
||||
export const CHUNK_SIZE = 16 * 1024; // 16 KiB per message
|
||||
export const MAX_BUFFERED = 1024 * 1024; // backpressure limit (bytes)
|
||||
|
||||
// Built-in public STUN providers. STUN is a stateless, unencrypted UDP
|
||||
// lookup of your public IP — the server never sees connection or file data.
|
||||
export const STUN_PROVIDERS = [
|
||||
{ id: 'google', label: 'Google', url: 'stun:stun.l.google.com:19302' },
|
||||
{ id: 'cloudflare', label: 'Cloudflare', url: 'stun:stun.cloudflare.com:3478' },
|
||||
{ id: 'none', label: 'None (same-network / VPN peers only)', url: null },
|
||||
];
|
||||
|
||||
// stunUrl: a URL from STUN_PROVIDERS, or a user-supplied "stun:host:port"
|
||||
// string, or null for host candidates only.
|
||||
export function createPeerConnection(stunUrl = null) {
|
||||
return new RTCPeerConnection({
|
||||
iceServers: stunUrl ? [{ urls: stunUrl }] : [],
|
||||
});
|
||||
}
|
||||
|
||||
function waitForIceComplete(pc) {
|
||||
if (pc.iceGatheringState === 'complete') return Promise.resolve();
|
||||
return new Promise((resolve, reject) => {
|
||||
const onChange = () => {
|
||||
if (pc.iceGatheringState === 'complete') {
|
||||
pc.removeEventListener('icegatheringstatechange', onChange);
|
||||
resolve();
|
||||
}
|
||||
};
|
||||
pc.addEventListener('icegatheringstatechange', onChange);
|
||||
setTimeout(() => {
|
||||
pc.removeEventListener('icegatheringstatechange', onChange);
|
||||
// Resolve anyway — partial candidate lists often still connect fine.
|
||||
resolve();
|
||||
}, 5000);
|
||||
});
|
||||
}
|
||||
|
||||
// Sender side: create an offer and return it as a copyable code.
|
||||
export async function createOfferCode(pc) {
|
||||
await pc.setLocalDescription(await pc.createOffer());
|
||||
await waitForIceComplete(pc);
|
||||
return encodeSignal(pc.localDescription.sdp);
|
||||
}
|
||||
|
||||
// Receiver side: accept an offer code, return the answer code to send back.
|
||||
export async function createAnswerCode(pc, offerText) {
|
||||
await pc.setRemoteDescription({ type: 'offer', sdp: decodeSignal(offerText) });
|
||||
await pc.setLocalDescription(await pc.createAnswer());
|
||||
await waitForIceComplete(pc);
|
||||
return encodeSignal(pc.localDescription.sdp);
|
||||
}
|
||||
|
||||
// Sender side: apply the answer code the receiver sent back.
|
||||
export function acceptAnswerCode(pc, answerText) {
|
||||
return pc.setRemoteDescription({ type: 'answer', sdp: decodeSignal(answerText) });
|
||||
}
|
||||
|
||||
// Anti-MITM check: extracts the DTLS certificate fingerprints from both
|
||||
// descriptions and shortens them for human comparison.
|
||||
// Rule: my "local" must equal the peer's "remote" (and vice versa).
|
||||
// If both match, nobody interposed on the code exchange.
|
||||
export function getFingerprints(pc) {
|
||||
const extract = (sdp) => {
|
||||
const m = sdp?.match(/a=fingerprint:(?:sha-256|sha-384|sha-512) ([0-9A-Fa-f:]+)/);
|
||||
return m ? m[1].toUpperCase() : null;
|
||||
};
|
||||
// The fingerprint is already a certificate hash — its first 5 bytes
|
||||
// (40 bits) are plenty for two humans to read to each other.
|
||||
const shorten = (fp) => (fp ? fp.split(':').slice(0, 5).join(' ') : '—');
|
||||
return {
|
||||
local: shorten(extract(pc.localDescription?.sdp)),
|
||||
remote: shorten(extract(pc.remoteDescription?.sdp)),
|
||||
};
|
||||
}
|
||||
|
||||
// SDP is ASCII plus CRLFs, so plain base64 is safe and compact enough (~2-4 KB).
|
||||
export function encodeSignal(sdp) {
|
||||
return btoa(sdp).replace(/=+$/, '');
|
||||
}
|
||||
|
||||
export function decodeSignal(text) {
|
||||
const clean = String(text).trim().replace(/\s+/g, '');
|
||||
const padded = clean + '='.repeat((4 - (clean.length % 4)) % 4);
|
||||
return atob(padded);
|
||||
}
|
||||
|
||||
// Waits until the channel's send buffer drains below MAX_BUFFERED.
|
||||
export function waitForBufferDrain(dc) {
|
||||
dc.bufferedAmountLowThreshold = MAX_BUFFERED / 2;
|
||||
return new Promise((resolve) => {
|
||||
dc.addEventListener('bufferedamountlow', resolve, { once: true });
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user