|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | + <title>Test ICE Servers</title> |
| 5 | + <meta charset="utf-8" /> |
| 6 | + <meta http-equiv="X-UA-Compatible" content="IE=edge" /> |
| 7 | + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 8 | +</head> |
| 9 | + |
| 10 | +<body> |
| 11 | + <h1>Test ICE Servers</h1> |
| 12 | + <hr /> |
| 13 | + <p id="ip"></p> |
| 14 | + <p id="stun">🔴 The STUN server is NOT reachable!</p> |
| 15 | + <p id="turn">🔴 The TURN server is NOT reachable!</p> |
| 16 | + <p id="err"></p> |
| 17 | + <hr /> |
| 18 | + <script> |
| 19 | + const IP = document.getElementById('ip'); |
| 20 | + const Stun = document.getElementById('stun'); |
| 21 | + const Turn = document.getElementById('turn'); |
| 22 | + const Err = document.getElementById('err'); |
| 23 | + |
| 24 | + document.addEventListener("DOMContentLoaded", main); |
| 25 | + |
| 26 | + async function main() { |
| 27 | + const token = await _getToken() |
| 28 | + const payload = await _decodeJwt(token); |
| 29 | + const iceServers = [ |
| 30 | + { |
| 31 | + urls: `stun:${window.location.hostname}:3478` |
| 32 | + }, |
| 33 | + { |
| 34 | + urls: `turn:${window.location.hostname}:3478`, |
| 35 | + username: payload.username, |
| 36 | + credential: payload.credential |
| 37 | + } |
| 38 | + ]; |
| 39 | + |
| 40 | + // Print iceServers config |
| 41 | + console.log("ICE Servers", iceServers) |
| 42 | + |
| 43 | + // Create a new RTCPeerConnection using the STUN and TURN servers |
| 44 | + const pc = new RTCPeerConnection({ iceServers }); |
| 45 | + |
| 46 | + // Handle each ICE candidate as it is discovered. |
| 47 | + pc.onicecandidate = (e) => { |
| 48 | + // Ignore the final empty candidate (null means gathering finished) |
| 49 | + if (!e.candidate || e.candidate.candidate === '') return; |
| 50 | + |
| 51 | + // Show candidate |
| 52 | + console.log(e.candidate.candidate); |
| 53 | + |
| 54 | + // Detect and indicate if a STUN candidate is found and gather the Public IP address |
| 55 | + if (e.candidate.type == 'srflx' || e.candidate.candidate.includes('srflx')) { |
| 56 | + let ip = /\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/; |
| 57 | + let address = e.candidate.address |
| 58 | + ? e.candidate.address |
| 59 | + : e.candidate.candidate.match(ip); |
| 60 | + IP.innerHTML = '🟢 Your Public IP Address is ' + address; |
| 61 | + Stun.innerHTML = '🟢 The STUN server is reachable!'; |
| 62 | + } |
| 63 | + |
| 64 | + // Detect and indicate if a TURN (relay) candidate is found |
| 65 | + if (e.candidate.type == 'relay' || e.candidate.candidate.includes('relay')) { |
| 66 | + Turn.innerHTML = '🟢 The TURN server is reachable!'; |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + // Close the connection when ICE gathering is complete. |
| 71 | + pc.onicegatheringstatechange = () => { |
| 72 | + if (pc.iceGatheringState === 'complete') { |
| 73 | + pc.close(); |
| 74 | + } |
| 75 | + }; |
| 76 | + |
| 77 | + // Handle any ICE candidate errors. |
| 78 | + pc.onicecandidateerror = (e) => { |
| 79 | + console.error(e); |
| 80 | + }; |
| 81 | + |
| 82 | + // Create a dummy data channel (required to initiate ICE gathering) |
| 83 | + pc.createDataChannel('test'); |
| 84 | + |
| 85 | + // Start ICE gathering by creating and setting a local offer |
| 86 | + pc.createOffer().then(offer => pc.setLocalDescription(offer)); |
| 87 | + } |
| 88 | + |
| 89 | + // Fetches a temporary token (JWT) from the server. |
| 90 | + async function _getToken() { |
| 91 | + try { |
| 92 | + // Request the credentials from the server API |
| 93 | + const response = await fetch('/api/credentials', { method: "GET" }); |
| 94 | + |
| 95 | + // Check if the response is successful (HTTP 2xx) |
| 96 | + if (!response.ok) { |
| 97 | + throw new Error(`Failed to fetch token: ${response.status} ${response.statusText}`); |
| 98 | + } |
| 99 | + |
| 100 | + // Parse the response as JSON |
| 101 | + const data = await response.json(); |
| 102 | + |
| 103 | + // Extract the token from the response |
| 104 | + const token = data?.token; |
| 105 | + if (!token) { |
| 106 | + throw new Error('No token found in server response.'); |
| 107 | + } |
| 108 | + |
| 109 | + // Return token |
| 110 | + return token; |
| 111 | + } |
| 112 | + catch (err) { |
| 113 | + // Log and rethrow for higher-level error handling |
| 114 | + console.error('Error fetching token:', err); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + // Decodes the payload of a JWT |
| 119 | + async function _decodeJwt(token) { |
| 120 | + const payload = token.split(".")[1]; |
| 121 | + const json = atob(payload); |
| 122 | + return JSON.parse(json); |
| 123 | + } |
| 124 | + </script> |
| 125 | +</body> |
| 126 | +</html> |
0 commit comments