-
Notifications
You must be signed in to change notification settings - Fork 0
/
webrtc.js
94 lines (72 loc) · 2.16 KB
/
webrtc.js
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
// https://ipleak.net/
function renderText(text) {
const item = document.getElementById('text')
item.innerHTML = JSON.stringify(text)
}
detectRTC().then(res => {
res && getLocalIPs().then(ips => {
renderText(ips)
})
})
function getLocalIPs(callback) {
console.log("In getLocalIPs")
var ips = [];
var pc = new RTCPeerConnection();
console.log("before Promise")
return new Promise((resolve, reject) => {
// Candidate found!
pc.onicecandidate = function(e) {
if (!e.candidate) { // Candidate gathering completed.
pc.close();
resolve(ips);
return;
}
var ip = /^candidate:.+ (\S+) \d+ typ/.exec(e.candidate.candidate)[1];
console.log(e.candidate)
if (ips.indexOf(ip) == -1) // avoid duplicate entries (tcp/udp)
ips.push(ip);
};
console.log("before createDataChannel")
// Enable candidate gathering
pc.createDataChannel('');
console.log("before createoffer")
pc.createOffer()
.then(offer => {
return pc.setLocalDescription(offer);
})
.catch(reason => {
console.log(reason);
reject(reason)
});
});
}
/* https://github.com/muaz-khan/DetectRTC/blob/master/DetectRTC.js#L839:53 */
function detectRTC() {
console.log("Trying to detect RTC")
var isWebRTCSupported = false;
['RTCPeerConnection',
'webkitRTCPeerConnection',
'mozRTCPeerConnection',
'RTCIceGatherer'
].forEach(function(item) {
if (item in window) {
isWebRTCSupported = true;
}
});
// Make sure datachannel create works
try {
var pc = new RTCPeerConnection();
pc.createDataChannel('');
pc.close();
} catch (err) {
console.log("Looks like we're on Edge")
isWebRTCSupported = false;
}
return new Promise((resolve, reject) => {
if (isWebRTCSupported) {
resolve(isWebRTCSupported);
} else {
reject(isWebRTCSupported);
}
});
}