-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathwebrtc.js
More file actions
187 lines (178 loc) · 7.58 KB
/
webrtc.js
File metadata and controls
187 lines (178 loc) · 7.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//@ts-check
/**
* @typedef {{
* type: 'connected',
* candidate: RTCIceCandidate,
* } | {
* type: 'executionError',
* error: any
* } | {
* type: 'noCandidates',
* candidates: string,
* }} IceLeakTestResult
*/
/**
* @returns {Promise<IceLeakTestResult>}
*/
function tryIceLeak(RTCPeerConnectionClass) {
return new Promise(async resolve => {
try {
const pc = new RTCPeerConnectionClass({
iceServers: [
{ urls: ['stun:stun.l.google.com:19302'] },
// stun.l.google.com, but by IP, in case DNS doesn't work.
{ urls: ['stun:173.194.76.127:19302'] },
{ urls: ['stun:stun.voipgate.com:3478'] },
// This will not generate candidates,
// because it's not a TURN server.
// But this helps see e.g. with Wireshark
// whether the browser will attempt to gather
// relay (TURN) candidates.
{
urls: ['turn:173.194.76.127:19302'],
username: 'foo',
credential: 'bar',
},
]
});
/**
* @param {RTCIceCandidate} candidate
*/
function isCandidateReflexive(candidate) {
// This includes `prflx`, `srflx`, `relay`.
// `candidate.type` is not implemented in e.g. Firefox.
return (
(candidate.type && candidate.type !== 'host')
|| candidate.candidate.includes(' srflx')
|| candidate.candidate.includes(' relay')
|| candidate.candidate.includes(' prflx')
);
}
/**
* @param {RTCIceCandidate} candidate
*/
function resolveConnected(candidate) {
resolve({ type: 'connected', candidate });
}
const allCandidates = [];
pc.addEventListener('icecandidate', e => {
allCandidates.push(e.candidate && e.candidate.candidate);
if (e.candidate == null || e.candidate.candidate === '') {
// Gathering completed.
// If `resolve` hasn't been called yet, resolve with `noCandidates`.
resolve({
type: 'noCandidates',
candidates: allCandidates.join('\n'),
});
return;
}
if (isCandidateReflexive(e.candidate)) {
return resolveConnected(e.candidate);
}
});
// So that an offer is actually generated.
pc.createDataChannel('dummyName');
const offer = await pc.createOffer();
await pc.setLocalDescription(offer);
} catch (error) {
resolve({ type: 'executionError', error })
}
});
}
window.addEventListener("load", () => {
// TODO try to do this inside an iframe, in case someone simply did
// `window.RTCPeerConnection = null`.
// A fresh iframe should have its `window.RTCPeerConnection` untouched.
// https://docs.ipdata.co/docs/how-to-get-a-clients-ip-address-using-javascript
/** @type {HTMLIFrameElement} */
const iframeRegularEl = document.getElementById('iframe-regular');
const iframeRegularWindow = iframeRegularEl?.contentWindow;
/** @type {HTMLIFrameElement} */
const iframeAllowSameOrigin = document.getElementById('iframe-allow-same-origin');
const iframeAllowSameOriginWindow = iframeAllowSameOrigin?.contentWindow;
// This test is specifically designed to get the RTCPeerConnection object
// before the injected script on android is run.
// The script injected into Android's WebView from Tauri
// only runs once the page is loaded, compared to other targets
// where the injected script is run immediately
// after the iframe is constructed, before yeilding back to the JS
// on the parent page which created the iframe.
const iframeContainer = document.getElementById("iframe-container");
/** @type {Window | undefined} */
let iframeNotInitedWindow
if (iframeContainer) {
iframeContainer.innerHTML += "<iframe id=uninitiframe></iframe>"
iframeNotInitedWindow = uninitiframe.contentWindow;
}
const tests = [
["RTCPeerConnection", window.RTCPeerConnection],
["mozRTCPeerConnection", window.mozRTCPeerConnection],
["webkitRTCPeerConnection", window.webkitRTCPeerConnection],
["iframe allow-same-origin RTCPeerConnection", iframeAllowSameOriginWindow?.RTCPeerConnection],
["iframe allow-same-origin mozRTCPeerConnection", iframeAllowSameOriginWindow?.mozRTCPeerConnection],
["iframe allow-same-origin webkitRTCPeerConnection", iframeAllowSameOriginWindow?.webkitRTCPeerConnection],
["iframe regular RTCPeerConnection", iframeRegularWindow?.RTCPeerConnection],
["iframe regular mozRTCPeerConnection", iframeRegularWindow?.mozRTCPeerConnection],
["iframe regular webkitRTCPeerConnection", iframeRegularWindow?.webkitRTCPeerConnection],
["iframe regular uninitialized RTCPeerConnection", iframeNotInitedWindow?.RTCPeerConnection],
["iframe regular uninitialized mozRTCPeerConnection", iframeNotInitedWindow?.mozRTCPeerConnection],
["iframe regular uninitialized webkitRTCPeerConnection", iframeNotInitedWindow?.webkitRTCPeerConnection],
];
const elements = [];
const testPromises = [];
let allPassed = true;
for (const [RTCPCName, RTCPeerConnectionClass] of tests) {
const resultEl = document.createElement('span');
const wrapperEl = h("p", {},
h("strong", {}, `${RTCPCName}: `),
resultEl,
);
wrapperEl.style.display = 'none';
elements.push(wrapperEl);
resultEl.innerText = 'checking...';
const p = tryIceLeak(RTCPeerConnectionClass).then(res => {
switch (res.type) {
case 'connected': {
resultEl.style.color = 'red';
const str = (
res.candidate.address
&& res.candidate.type
)
? `your IP: ${res.candidate.address}, ${res.candidate.type}`
: res.candidate.candidate;
resultEl.innerText = `BAD: connected: ${str}`;
wrapperEl.style.display = '';
allPassed = false;
break;
}
case 'executionError': {
resultEl.innerText = `GOOD: execution error: ${res.error}`;
break;
}
case 'noCandidates': {
resultEl.innerText = `GOOD: got no reflexive candidates:\n${res.candidates}`;
break;
}
}
});
testPromises.push(p);
}
const resultsHeader = h("strong", {}, "checking...")
Promise.all(testPromises).then(() => {
resultsHeader.innerText = allPassed
? `GOOD: passed all ${tests.length} tests`
: "Leaking via ...";
});
document.getElementById("webrtc-output").append(
createHeader("webrtc-sidechannel"),
h(
"p",
{},
"RTCPeerConnection object: " + globalThis.__capturedWebRTCObj,
),
h("div", {class: "container"},
resultsHeader,
...elements
)
);
});