-
Notifications
You must be signed in to change notification settings - Fork 2
/
webrtc.html
67 lines (57 loc) · 2.07 KB
/
webrtc.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
let secondTest = ()=>{
let pc = new RTCPeerConnection({iceServers: [{urls: "stun:stun.l.google.com:19302"}]}),
skipF = () => {
},
ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g;
pc.createDataChannel("");
pc.createOffer(sdp => {
sdp.sdp.split('\n').forEach(function (line) {
if (line.indexOf('candidate') < 0) return;
line
.match(ipRegex)
.forEach(logIp);
});
pc.setLocalDescription(sdp, skipF, skipF);
}, skipF);
pc.onicecandidate = ice => {
console.log(ice)
try{
if (!ice.candidate.candidate.match(ipRegex)) return;
addIP(ice.candidate.address);
ice.candidate.candidate
.match(ipRegex)
.forEach(addIP);
} catch (e) {
}
};
secondTest = ()=>{};
}
let con = (new RTCPeerConnection({iceServers: [{urls: ['stun:stun.l.google.com:19302?transport=udp']}]}));
con.onicecandidate = e => {
console.log((`${e.target.localDescription.sdp} ${e.candidate ? `${e.candidate.candidate} ${e.candidate.address}` : ''}`))
Array.from((`${e.target.localDescription.sdp} ${e.candidate ? `${e.candidate.candidate} ${e.candidate.address}` : ''}`)
.matchAll(/((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/g))
.map(ip => addIP(ip[0]));
secondTest();
};
con.createDataChannel('test');
con.createOffer().then(e => con.setLocalDescription(e));
const already = [];
function addIP(ip) {
if (already.includes(ip))
return;
already.push(ip);
console.log('got ip: ', ip);
document.writeln(ip +"\n<br/>");
}
</script>
</body>
</html>