From 165c942dc73e47d20e22916a03d63d572b49fd66 Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Mon, 11 Jan 2021 18:18:55 +0100 Subject: [PATCH] Add undocumented command /relay-test. --- static/galene.js | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/static/galene.js b/static/galene.js index d041120..311320e 100644 --- a/static/galene.js +++ b/static/galene.js @@ -2060,6 +2060,60 @@ commands.wall = { }, }; +async function relayTest() { + let conf = Object.assign({}, serverConnection.rtcConfiguration); + conf.iceTransportPolicy = 'relay'; + let pc1 = new RTCPeerConnection(conf); + let pc2 = new RTCPeerConnection(conf); + pc1.onicecandidate = e => {e.candidate && pc2.addIceCandidate(e.candidate);}; + pc2.onicecandidate = e => {e.candidate && pc1.addIceCandidate(e.candidate);}; + try { + await new Promise(async (resolve, reject) => { + let d1 = pc1.createDataChannel('loopbackTest'); + d1.onopen = e => { + d1.send('loopback'); + }; + + let offer = await pc1.createOffer(); + await pc1.setLocalDescription(offer); + await pc2.setRemoteDescription(offer); + let answer = await pc2.createAnswer(); + pc2.setLocalDescription(answer); + await pc1.setRemoteDescription(answer); + + pc2.ondatachannel = e => { + let d2 = e.channel; + d2.onmessage = e => { + if(e.data === 'loopback') + resolve(); + else + reject('unexpected data'); + } + } + + setTimeout(() => reject('timed out'), 5000); + }) + } finally { + pc1.close(); + pc2.close(); + } +} + +commands['relay-test'] = { + f: async (c, r) => { + addToChatbox(null, null, null, Date.now(), false, null, + `Relay test in progress...`); + try { + await relayTest(); + addToChatbox(null, null, null, Date.now(), false, null, + `Relay test successful.`); + } catch(e) { + addToChatbox(null, null, null, Date.now(), false, null, + 'Relay test failed: ' + e); + } + } +} + function handleInput() { let input = /** @type {HTMLTextAreaElement} */ (document.getElementById('input'));