Skip to content

Commit

Permalink
[mirotalk] - add test Stun-Turn simple page
Browse files Browse the repository at this point in the history
  • Loading branch information
miroslavpejic85 committed Jul 23, 2022
1 parent 7a0bbfb commit 3d0ab2e
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
11 changes: 11 additions & 0 deletions app/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ const turnUrls = process.env.TURN_URLS;
const turnUsername = process.env.TURN_USERNAME;
const turnCredential = process.env.TURN_PASSWORD;

// Test Stun and Turn connection URL
const testStunTurn = host + '/test';

// Sentry config
const Sentry = require('@sentry/node');
const { CaptureConsole } = require('@sentry/integrations');
Expand Down Expand Up @@ -151,6 +154,7 @@ const views = {
notFound: path.join(__dirname, '../../', 'public/views/404.html'),
permission: path.join(__dirname, '../../', 'public/views/permission.html'),
privacy: path.join(__dirname, '../../', 'public/views/privacy.html'),
stunTurn: path.join(__dirname, '../../', 'public/views/testStunTurn.html'),
};

let channels = {}; // collect channels
Expand Down Expand Up @@ -206,6 +210,11 @@ app.get(['/privacy'], (req, res) => {
res.sendFile(views.privacy);
});

// test Stun Turn connections
app.get(['/test'], (req, res) => {
res.sendFile(views.stunTurn);
});

// no room name specified to join
app.get('/join/', (req, res) => {
if (Object.keys(req.query).length > 0) {
Expand Down Expand Up @@ -366,6 +375,7 @@ async function ngrokStart() {
},
server: host,
server_tunnel: tunnelHttps,
stun_turn_test: testStunTurn,
api_docs: api_docs,
api_key_secret: api_key_secret,
sentry_enabled: sentryEnabled,
Expand Down Expand Up @@ -403,6 +413,7 @@ server.listen(port, null, () => {
log.debug('settings', {
iceServers: iceServers,
server: host,
stun_turn_test: testStunTurn,
api_docs: api_docs,
api_key_secret: api_key_secret,
sentry_enabled: sentryEnabled,
Expand Down
67 changes: 67 additions & 0 deletions public/views/testStunTurn.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<!DOCTYPE html>
<html>
<head>
<title>Test Stun/Turn Servers</title>
</head>

<body>
<h1>Test Stun/Turn Servers</h1>

<p id="stun">Stun: The STUN server is NOT reachable!</p>
<p id="turn">Turn: The TURN server is NOT reachable!</p>
<p id="err"></p>

<script>
const Stun = document.getElementById('stun');
const Turn = document.getElementById('turn');
const Err = document.getElementById('err');

// MiroTalk P2P default STUN/TURN if not set

const iceServers = [
// Test some STUN server
{
urls: 'stun:stun.l.google.com:19302',
},
// Test some TURN server
{
urls: 'turn:numb.viagenie.ca',
username: 'miroslav.pejic.85@gmail.com',
credential: 'mirotalkp2p',
},
];

// 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);
Stun.innerHTML = 'Stun: The STUN server is reachable! Your Public IP Address is ' + address;
}

// If a relay candidate was found, notify that the TURN server works!
if (e.candidate.type == 'relay' || e.candidate.candidate.includes('relay')) {
Turn.innerHTML = 'Turn: 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>
</body>
</html>

0 comments on commit 3d0ab2e

Please sign in to comment.