| 1 | (() => { |
| 2 | if (window.__rustyCallShadow || !window.RTCPeerConnection || !window.chrome?.webview) { |
| 3 | return; |
| 4 | } |
| 5 | window.__rustyCallShadow = true; |
| 6 | const STATS_INTERVAL_MS = 2000; |
| 7 | const SIGNALING_HOST = /(^|\.)skype\.com$/; |
| 8 | const post = (event, data) => { |
| 9 | try { |
| 10 | window.chrome.webview.postMessage(JSON.stringify({ kind: "callShadow", at: Date.now(), event, data })); |
| 11 | } catch (_) {} |
| 12 | }; |
| 13 | const sdpSummary = (sdp) => |
| 14 | (sdp || "") |
| 15 | .split(/\r?\n/) |
| 16 | .filter((line) => /^(m=|a=rtpmap:|a=mid:|a=(sendrecv|sendonly|recvonly|inactive)$)/.test(line)) |
| 17 | .map((line) => line.replace(/^m=(\w+) \d+/, "m=$1")); |
| 18 | const statsSummary = (report) => { |
| 19 | const rows = []; |
| 20 | const codecs = {}; |
| 21 | report.forEach((entry) => { |
| 22 | if (entry.type === "codec") { |
| 23 | codecs[entry.id] = entry.mimeType; |
| 24 | } |
| 25 | }); |
| 26 | report.forEach((entry) => { |
| 27 | if (entry.type === "inbound-rtp" || entry.type === "outbound-rtp") { |
| 28 | rows.push({ |
| 29 | type: entry.type, |
| 30 | kind: entry.kind, |
| 31 | mid: entry.mid, |
| 32 | codec: codecs[entry.codecId], |
| 33 | bytes: entry.bytesReceived ?? entry.bytesSent, |
| 34 | packets: entry.packetsReceived ?? entry.packetsSent, |
| 35 | packetsLost: entry.packetsLost, |
| 36 | jitter: entry.jitter, |
| 37 | framesPerSecond: entry.framesPerSecond, |
| 38 | frameWidth: entry.frameWidth, |
| 39 | frameHeight: entry.frameHeight, |
| 40 | qualityLimitationReason: entry.qualityLimitationReason, |
| 41 | concealedSamples: entry.concealedSamples, |
| 42 | }); |
| 43 | } |
| 44 | if (entry.type === "candidate-pair" && entry.nominated && entry.state === "succeeded") { |
| 45 | const local = report.get(entry.localCandidateId); |
| 46 | const remote = report.get(entry.remoteCandidateId); |
| 47 | rows.push({ |
| 48 | type: "pair", |
| 49 | roundTripTime: entry.currentRoundTripTime, |
| 50 | availableOutgoingBitrate: entry.availableOutgoingBitrate, |
| 51 | local: local && `${local.candidateType}/${local.protocol}`, |
| 52 | remote: remote && `${remote.candidateType}/${remote.protocol}`, |
| 53 | }); |
| 54 | } |
| 55 | }); |
| 56 | return rows; |
| 57 | }; |
| 58 | let nextPeer = 0; |
| 59 | const Native = window.RTCPeerConnection; |
| 60 | class ShadowPeer extends Native { |
| 61 | constructor(configuration, ...rest) { |
| 62 | super(configuration, ...rest); |
| 63 | const peer = ++nextPeer; |
| 64 | this.__shadowPeer = peer; |
| 65 | post("peer", { |
| 66 | peer, |
| 67 | bundlePolicy: configuration?.bundlePolicy, |
| 68 | iceTransportPolicy: configuration?.iceTransportPolicy, |
| 69 | iceServers: (configuration?.iceServers || []).flatMap((server) => |
| 70 | [].concat(server.urls || []).map((url) => String(url).split("?")[0]) |
| 71 | ), |
| 72 | }); |
| 73 | this.addEventListener("iceconnectionstatechange", () => post("ice", { peer, state: this.iceConnectionState })); |
| 74 | this.addEventListener("connectionstatechange", () => post("connection", { peer, state: this.connectionState })); |
| 75 | this.addEventListener("icegatheringstatechange", () => post("gathering", { peer, state: this.iceGatheringState })); |
| 76 | this.addEventListener("icecandidate", (event) => { |
| 77 | if (event.candidate) { |
| 78 | post("candidate", { peer, type: event.candidate.type, protocol: event.candidate.protocol }); |
| 79 | } |
| 80 | }); |
| 81 | this.addEventListener("track", (event) => post("track", { peer, kind: event.track.kind, mid: event.transceiver?.mid })); |
| 82 | const timer = setInterval(() => { |
| 83 | if (this.connectionState === "closed") { |
| 84 | clearInterval(timer); |
| 85 | return; |
| 86 | } |
| 87 | this.getStats() |
| 88 | .then((report) => post("stats", { peer, rows: statsSummary(report) })) |
| 89 | .catch(() => {}); |
| 90 | }, STATS_INTERVAL_MS); |
| 91 | } |
| 92 | async setLocalDescription(...args) { |
| 93 | const result = await super.setLocalDescription(...args); |
| 94 | post("local", { peer: this.__shadowPeer, type: this.localDescription?.type, lines: sdpSummary(this.localDescription?.sdp) }); |
| 95 | return result; |
| 96 | } |
| 97 | async setRemoteDescription(...args) { |
| 98 | const result = await super.setRemoteDescription(...args); |
| 99 | post("remote", { peer: this.__shadowPeer, type: this.remoteDescription?.type, lines: sdpSummary(this.remoteDescription?.sdp) }); |
| 100 | return result; |
| 101 | } |
| 102 | close() { |
| 103 | post("close", { peer: this.__shadowPeer }); |
| 104 | return super.close(); |
| 105 | } |
| 106 | } |
| 107 | window.RTCPeerConnection = ShadowPeer; |
| 108 | const nativeFetch = window.fetch; |
| 109 | window.fetch = function (input, init) { |
| 110 | const started = performance.now(); |
| 111 | let target; |
| 112 | try { |
| 113 | target = new URL(input instanceof Request ? input.url : String(input), location.href); |
| 114 | } catch (_) {} |
| 115 | const watched = target && SIGNALING_HOST.test(target.hostname); |
| 116 | const promise = nativeFetch.apply(this, arguments); |
| 117 | if (watched) { |
| 118 | const method = (init?.method || input?.method || "GET").toUpperCase(); |
| 119 | const path = target.hostname + target.pathname.replace(/[0-9a-f-]{16,}|[A-Za-z0-9_-]{32,}/g, "{id}"); |
| 120 | promise.then( |
| 121 | (response) => post("request", { method, path, status: response.status, ms: Math.round(performance.now() - started) }), |
| 122 | () => post("request", { method, path, status: 0, ms: Math.round(performance.now() - started) }) |
| 123 | ); |
| 124 | } |
| 125 | return promise; |
| 126 | }; |
| 127 | })(); |