| 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 WATCHED_HOST = /(^|\.)(skype\.com|teams\.microsoft\.com|teams\.cloud\.microsoft|office\.net)$/; |
| 8 | const STATIC_PATH = /\.(js|css|png|svg|jpg|jpeg|gif|woff2?|json|ico|mp3|wav)$/i; |
| 9 | const post = (event, data) => { |
| 10 | try { |
| 11 | window.chrome.webview.postMessage(JSON.stringify({ kind: "callShadow", at: Date.now(), event, data })); |
| 12 | } catch (_) {} |
| 13 | }; |
| 14 | const sdpSummary = (sdp) => { |
| 15 | const sections = []; |
| 16 | let current = null; |
| 17 | for (const line of (sdp || "").split(/\r?\n/)) { |
| 18 | if (line.startsWith("m=")) { |
| 19 | current = { kind: line.slice(2).split(" ")[0], mid: "", direction: "", codecs: [] }; |
| 20 | sections.push(current); |
| 21 | } else if (!current) { |
| 22 | continue; |
| 23 | } else if (line.startsWith("a=mid:")) { |
| 24 | current.mid = line.slice(6); |
| 25 | } else if (/^a=(sendrecv|sendonly|recvonly|inactive)$/.test(line)) { |
| 26 | current.direction = line.slice(2); |
| 27 | } else if (line.startsWith("a=rtpmap:")) { |
| 28 | const codec = (line.split(" ")[1] || "").split("/")[0]; |
| 29 | if (codec && codec.toLowerCase() !== "rtx" && !current.codecs.includes(codec)) { |
| 30 | current.codecs.push(codec); |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | return sections.map((section) => `${section.kind} mid ${section.mid} ${section.direction} [${section.codecs.join(",")}]`); |
| 35 | }; |
| 36 | const statsSummary = (report) => { |
| 37 | const rows = []; |
| 38 | const codecs = {}; |
| 39 | report.forEach((entry) => { |
| 40 | if (entry.type === "codec") { |
| 41 | codecs[entry.id] = entry.mimeType; |
| 42 | } |
| 43 | }); |
| 44 | report.forEach((entry) => { |
| 45 | if (entry.type === "inbound-rtp" || entry.type === "outbound-rtp") { |
| 46 | rows.push({ |
| 47 | type: entry.type, |
| 48 | kind: entry.kind, |
| 49 | mid: entry.mid, |
| 50 | codec: codecs[entry.codecId], |
| 51 | bytes: entry.bytesReceived ?? entry.bytesSent, |
| 52 | packets: entry.packetsReceived ?? entry.packetsSent, |
| 53 | packetsLost: entry.packetsLost, |
| 54 | jitter: entry.jitter, |
| 55 | framesPerSecond: entry.framesPerSecond, |
| 56 | frameWidth: entry.frameWidth, |
| 57 | frameHeight: entry.frameHeight, |
| 58 | qualityLimitationReason: entry.qualityLimitationReason, |
| 59 | concealedSamples: entry.concealedSamples, |
| 60 | }); |
| 61 | } |
| 62 | if (entry.type === "candidate-pair" && entry.nominated && entry.state === "succeeded") { |
| 63 | const local = report.get(entry.localCandidateId); |
| 64 | const remote = report.get(entry.remoteCandidateId); |
| 65 | rows.push({ |
| 66 | type: "pair", |
| 67 | roundTripTime: entry.currentRoundTripTime, |
| 68 | availableOutgoingBitrate: entry.availableOutgoingBitrate, |
| 69 | local: local && `${local.candidateType}/${local.protocol}`, |
| 70 | remote: remote && `${remote.candidateType}/${remote.protocol}`, |
| 71 | }); |
| 72 | } |
| 73 | }); |
| 74 | return rows; |
| 75 | }; |
| 76 | let nextPeer = 0; |
| 77 | const Native = window.RTCPeerConnection; |
| 78 | class ShadowPeer extends Native { |
| 79 | constructor(configuration, ...rest) { |
| 80 | super(configuration, ...rest); |
| 81 | const peer = ++nextPeer; |
| 82 | this.__shadowPeer = peer; |
| 83 | post("peer", { |
| 84 | peer, |
| 85 | bundlePolicy: configuration?.bundlePolicy, |
| 86 | iceTransportPolicy: configuration?.iceTransportPolicy, |
| 87 | iceServers: (configuration?.iceServers || []).flatMap((server) => |
| 88 | [].concat(server.urls || []).map((url) => String(url).split("?")[0]) |
| 89 | ), |
| 90 | }); |
| 91 | this.addEventListener("iceconnectionstatechange", () => post("ice", { peer, state: this.iceConnectionState })); |
| 92 | this.addEventListener("connectionstatechange", () => post("connection", { peer, state: this.connectionState })); |
| 93 | this.addEventListener("icegatheringstatechange", () => post("gathering", { peer, state: this.iceGatheringState })); |
| 94 | this.addEventListener("icecandidate", (event) => { |
| 95 | if (event.candidate) { |
| 96 | post("candidate", { peer, type: event.candidate.type, protocol: event.candidate.protocol }); |
| 97 | } |
| 98 | }); |
| 99 | this.addEventListener("track", (event) => post("track", { peer, kind: event.track.kind, mid: event.transceiver?.mid })); |
| 100 | const timer = setInterval(() => { |
| 101 | if (this.connectionState === "closed") { |
| 102 | clearInterval(timer); |
| 103 | return; |
| 104 | } |
| 105 | this.getStats() |
| 106 | .then((report) => post("stats", { peer, rows: statsSummary(report) })) |
| 107 | .catch(() => {}); |
| 108 | }, STATS_INTERVAL_MS); |
| 109 | } |
| 110 | async setLocalDescription(...args) { |
| 111 | const result = await super.setLocalDescription(...args); |
| 112 | post("local", { peer: this.__shadowPeer, type: this.localDescription?.type, lines: sdpSummary(this.localDescription?.sdp) }); |
| 113 | return result; |
| 114 | } |
| 115 | async setRemoteDescription(...args) { |
| 116 | const result = await super.setRemoteDescription(...args); |
| 117 | post("remote", { peer: this.__shadowPeer, type: this.remoteDescription?.type, lines: sdpSummary(this.remoteDescription?.sdp) }); |
| 118 | return result; |
| 119 | } |
| 120 | close() { |
| 121 | post("close", { peer: this.__shadowPeer }); |
| 122 | return super.close(); |
| 123 | } |
| 124 | } |
| 125 | window.RTCPeerConnection = ShadowPeer; |
| 126 | const watchedTarget = (url) => { |
| 127 | try { |
| 128 | const target = new URL(url, location.href); |
| 129 | if (!WATCHED_HOST.test(target.hostname) || STATIC_PATH.test(target.pathname)) { |
| 130 | return null; |
| 131 | } |
| 132 | return target.hostname + target.pathname.replace(/[0-9a-f-]{16,}|[A-Za-z0-9_%:@.-]{32,}/g, "{id}"); |
| 133 | } catch (_) { |
| 134 | return null; |
| 135 | } |
| 136 | }; |
| 137 | const nativeFetch = window.fetch; |
| 138 | window.fetch = function (input, init) { |
| 139 | const started = performance.now(); |
| 140 | const path = watchedTarget(input instanceof Request ? input.url : String(input)); |
| 141 | const promise = nativeFetch.apply(this, arguments); |
| 142 | if (path) { |
| 143 | const method = (init?.method || (input instanceof Request ? input.method : "GET")).toUpperCase(); |
| 144 | const done = (status) => post("request", { method, path, status, ms: Math.round(performance.now() - started) }); |
| 145 | promise.then((response) => done(response.status), () => done(0)); |
| 146 | } |
| 147 | return promise; |
| 148 | }; |
| 149 | const nativeOpen = XMLHttpRequest.prototype.open; |
| 150 | const nativeSend = XMLHttpRequest.prototype.send; |
| 151 | XMLHttpRequest.prototype.open = function (method, url) { |
| 152 | this.__shadowRequest = { method: String(method).toUpperCase(), path: watchedTarget(String(url)) }; |
| 153 | return nativeOpen.apply(this, arguments); |
| 154 | }; |
| 155 | XMLHttpRequest.prototype.send = function () { |
| 156 | const request = this.__shadowRequest; |
| 157 | if (request?.path) { |
| 158 | const started = performance.now(); |
| 159 | this.addEventListener("loadend", () => |
| 160 | post("request", { method: request.method, path: request.path, status: this.status, ms: Math.round(performance.now() - started) }) |
| 161 | ); |
| 162 | } |
| 163 | return nativeSend.apply(this, arguments); |
| 164 | }; |
| 165 | })(); |