A few days ago, I created a simple service for my WebRTC Project: <a href="https://github.com/miroslavpejic85/mirotalk" rel="nofollow">https://github.com/miroslavpejic85/mirotalk</a> to test if the STUN/TURN servers are working properly.<p>After installing and configuring a STUN/TURN server, most of the developers that are new to this stuff will ask themselves, how do I know if it's working properly?<p>So I made a snippet :)<p>```<p><!DOCTYPE html><p><html><p><head>
<title>Test Stun/Turn Servers</title><p><pre><code> <meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /></code></pre>
</head><p><body>
<h1>Test Ice Servers</h1><p><pre><code> <hr />
<pre id='ice' style='overflow: auto'></pre>
<hr />
<p id='ip'></p>
<p id='stun'> The STUN server is NOT reachable!</p>
<p id='turn'> The TURN server is NOT reachable!</p>
<p id='err'></p>
<hr />
<script>
const Ice = document.getElementById('ice');
const IP = document.getElementById('ip');
const Stun = document.getElementById('stun');
const Turn = document.getElementById('turn');
const Err = document.getElementById('err');
const iceServers = [
// Test some STUN server
{
urls: 'stun:stun.l.google.com:19302',
},
// Test some TURN server
{
urls: 'turn:turnUrl',
username: 'turnUsername',
credential: 'turnPassword',
},
];
// Print iceServers config
Ice.innerHTML = JSON.stringify(iceServers, null, 4);
// Test the connections
const pc = new RTCPeerConnection({
iceServers
});
pc.onicecandidate = (e) => {
if (!e.candidate) return;
console.log(e.candidate.candidate);
// If a srflx candidate was found, notify that the STUN server works!
if (e.candidate.type == 'srflx' || e.candidate.candidate.includes('srflx')) {
let ip = /\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/;
let address = e.candidate.address
? e.candidate.address
: e.candidate.candidate.match(ip);
IP.innerHTML = ' Your Public IP Address is ' + address;
Stun.innerHTML = ' The STUN server is reachable!';
}
// If a relay candidate was found, notify that the TURN server works!
if (e.candidate.type == 'relay' || e.candidate.candidate.includes('relay')) {
Turn.innerHTML = ' The TURN server is reachable!';
}
};
// handle error
pc.onicecandidateerror = (e) => {
console.error(e);
Err.innerHTML = ' Error: ' + e.errorText;
};
pc.createDataChannel('test');
pc.createOffer().then(offer => pc.setLocalDescription(offer));
</script>
</code></pre>
</body><p></html><p>```<p>Alternative to <a href="https://webrtc.github.io/samples/src/content/peerconnection/trickle-ice/" rel="nofollow">https://webrtc.github.io/samples/src/content/peerconnection/...</a><p>Ref: <a href="https://www.reddit.com/r/selfhosted/comments/wayiev/how_to_test_whether_a_stunturn_server_is_working/" rel="nofollow">https://www.reddit.com/r/selfhosted/comments/wayiev/how_to_t...</a><p>I hope it will be useful to someone.