local-network.ts

1.2 KB · raw

1// Local network discovery — minimal helper used by chirp pairing.2//3// `getLocalSubnet` returns the /24 prefix (e.g. "192.168.1") that the4// browser is on, derived from a host-relative WebRTC ICE candidate. The5// extension uses this to scan the LAN for the peer's pairing-port6// WebSocket after the audio chirp exchange completes. Legacy v2.147// discovery/pair probes have been removed — see chirp-pair.ts for the8// v3 pairing-port scan and chirp-sync-transport.ts for the rate-limited9// Phase-2 subnet probe.1011// Returns e.g. "192.168.1" or null if unavailable.12export async function getLocalSubnet(): Promise<string | null> {13  return new Promise((resolve) => {14    try {15      const pc = new RTCPeerConnection({ iceServers: [] });16      pc.createDataChannel('');17      pc.onicecandidate = (e) => {18        if (!e.candidate) { pc.close(); resolve(null); return; }19        const m = e.candidate.candidate.match(/(\d+\.\d+\.\d+)\.\d+/);20        if (m) { pc.close(); resolve(m[1]); }21      };22      pc.createOffer().then(o => pc.setLocalDescription(o)).catch(() => { pc.close(); resolve(null); });23      setTimeout(() => { try { pc.close(); } catch { /* ignore */ } resolve(null); }, 2000);24    } catch {25      resolve(null);26    }27  });28}