From 08a5cbf794e80e999860db4a1ff3a80d25337935 Mon Sep 17 00:00:00 2001 From: Joachim Bauch Date: Wed, 5 Sep 2018 12:43:25 +0200 Subject: [PATCH] Add button to test TURN settings. This implements #679. Signed-off-by: Joachim Bauch --- css/settings-admin.scss | 1 + js/admin/sha1.js | 23 +++++ js/admin/turn-server.js | 114 +++++++++++++++++++++++ templates/settings/admin/turn-server.php | 1 + 4 files changed, 139 insertions(+) create mode 100644 js/admin/sha1.js diff --git a/css/settings-admin.scss b/css/settings-admin.scss index 051e7d37f22e..5eb5ec07c734 100644 --- a/css/settings-admin.scss +++ b/css/settings-admin.scss @@ -5,6 +5,7 @@ .icon-delete, .icon-checkmark-color, + .icon-category-monitoring, div.stun-server:last-child .icon-add, div.signaling-server:last-child .icon-add, div.turn-server:last-child .icon-add { diff --git a/js/admin/sha1.js b/js/admin/sha1.js new file mode 100644 index 000000000000..e46e531cb05b --- /dev/null +++ b/js/admin/sha1.js @@ -0,0 +1,23 @@ +/* + A JavaScript implementation of the SHA family of hashes, as + defined in FIPS PUB 180-4 and FIPS PUB 202, as well as the corresponding + HMAC implementation as defined in FIPS PUB 198a + + Copyright 2008-2018 Brian Turek, 1998-2009 Paul Johnston & Contributors + Distributed under the BSD License + See http://caligatio.github.com/jsSHA/ for more information +*/ +'use strict';(function(G){function r(d,b,c){var h=0,a=[],f=0,g,m,k,e,l,p,q,t,w=!1,n=[],u=[],v,r=!1;c=c||{};g=c.encoding||"UTF8";v=c.numRounds||1;if(v!==parseInt(v,10)||1>v)throw Error("numRounds must a integer >= 1");if("SHA-1"===d)l=512,p=z,q=H,e=160,t=function(a){return a.slice()};else throw Error("Chosen SHA variant is not supported");k=A(b,g);m=x(d);this.setHMACKey=function(a,f,b){var c;if(!0===w)throw Error("HMAC key already set");if(!0===r)throw Error("Cannot set HMAC key after calling update"); +g=(b||{}).encoding||"UTF8";f=A(f,g)(a);a=f.binLen;f=f.value;c=l>>>3;b=c/4-1;if(ca/8){for(;f.length<=b;)f.push(0);f[b]&=4294967040}for(a=0;a<=b;a+=1)n[a]=f[a]^909522486,u[a]=f[a]^1549556828;m=p(n,m);h=l;w=!0};this.update=function(b){var e,g,c,d=0,q=l>>>5;e=k(b,a,f);b=e.binLen;g=e.value;e=b>>>5;for(c=0;c>>5);f=b%l;r=!0};this.getHash=function(b,g){var c,k,l,p;if(!0=== +w)throw Error("Cannot call getHash after setting HMAC key");l=B(g);switch(b){case "HEX":c=function(a){return C(a,e,l)};break;case "B64":c=function(a){return D(a,e,l)};break;case "BYTES":c=function(a){return E(a,e)};break;case "ARRAYBUFFER":try{k=new ArrayBuffer(0)}catch(I){throw Error("ARRAYBUFFER not supported by this environment");}c=function(a){return F(a,e)};break;default:throw Error("format must be HEX, B64, BYTES, or ARRAYBUFFER");}p=q(a.slice(),f,h,t(m),e);for(k=1;k>>2]>>>8*(3+a%4*-1),h+="0123456789abcdef".charAt(f>>>4&15)+"0123456789abcdef".charAt(f&15);return c.outputUpper?h.toUpperCase():h}function D(d,b,c){var h="",a=b/8,f,g,m;for(f=0;f>>2]:0,m=f+2>>2]:0,m=(d[f>>>2]>>>8*(3+f%4*-1)&255)<<16|(g>>>8*(3+(f+1)%4*-1)&255)<<8|m>>>8*(3+(f+2)%4*-1)&255,g=0;4>g;g+=1)8*f+6*g<=b?h+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(m>>> +6*(3-g)&63):h+=c.b64Pad;return h}function E(d,b){var c="",h=b/8,a,f;for(a=0;a>>2]>>>8*(3+a%4*-1)&255,c+=String.fromCharCode(f);return c}function F(d,b){var c=b/8,h,a=new ArrayBuffer(c),f;f=new Uint8Array(a);for(h=0;h>>2]>>>8*(3+h%4*-1)&255;return a}function B(d){var b={outputUpper:!1,b64Pad:"=",shakeLen:-1};d=d||{};b.outputUpper=d.outputUpper||!1;!0===d.hasOwnProperty("b64Pad")&&(b.b64Pad=d.b64Pad);if("boolean"!==typeof b.outputUpper)throw Error("Invalid outputUpper formatting option"); +if("string"!==typeof b.b64Pad)throw Error("Invalid b64Pad formatting option");return b}function A(d,b){var c;switch(b){case "UTF8":case "UTF16BE":case "UTF16LE":break;default:throw Error("encoding must be UTF8, UTF16BE, or UTF16LE");}switch(d){case "HEX":c=function(b,a,f){var g=b.length,c,d,e,l,p;if(0!==g%2)throw Error("String of HEX type must be in byte increments");a=a||[0];f=f||0;p=f>>>3;for(c=0;c>>1)+p;for(e=l>>>2;a.length<=e;)a.push(0);a[e]|=d<<8*(3+l%4*-1)}return{value:a,binLen:4*g+f}};break;case "TEXT":c=function(c,a,f){var g,d,k=0,e,l,p,q,t,n;a=a||[0];f=f||0;p=f>>>3;if("UTF8"===b)for(n=3,e=0;eg?d.push(g):2048>g?(d.push(192|g>>>6),d.push(128|g&63)):55296>g||57344<=g?d.push(224|g>>>12,128|g>>>6&63,128|g&63):(e+=1,g=65536+((g&1023)<<10|c.charCodeAt(e)&1023),d.push(240|g>>>18,128|g>>>12&63,128|g>>>6&63,128|g&63)),l=0;l>>2;a.length<=q;)a.push(0);a[q]|=d[l]<<8*(n+t%4*-1);k+=1}else if("UTF16BE"===b||"UTF16LE"===b)for(n=2,d="UTF16LE"===b&&!0||"UTF16LE"!==b&&!1,e=0;e>>8);t=k+p;for(q=t>>>2;a.length<=q;)a.push(0);a[q]|=g<<8*(n+t%4*-1);k+=2}return{value:a,binLen:8*k+f}};break;case "B64":c=function(b,a,f){var c=0,d,k,e,l,p,q,n;if(-1===b.search(/^[a-zA-Z0-9=+\/]+$/))throw Error("Invalid character in base-64 string");k=b.indexOf("=");b=b.replace(/\=/g, +"");if(-1!==k&&k' + t('spreed', 'TCP only') + '' + ' {{/select}}' + ' ' + + ' ' + ' ' + ' ' + ' ' + @@ -72,6 +73,118 @@ } }, + notifyTurnResult: function($candidates, $timeout) { + console.log("Received candidates", $candidates); + const $types = $candidates.map(function($cand) { + return $cand.type; + }); + var $result; + if ($types.indexOf('relay') === -1) { + $result = t('spreed', 'TURN candidate generation failed, please check the settings.'); + } else { + $result = t('spreed', 'The TURN server settings are valid.'); + } + OC.Notification.showTemporary($result); + clearTimeout($timeout); + }, + + // Parse a candidate:foo string into an object, for easier use by other methods. + parseCandidate: function($text) { + const $candidateStr = 'candidate:'; + const $pos = $text.indexOf($candidateStr) + $candidateStr.length; + var [$foundation, $component, $protocol, $priority, $address, $port, , $type] = + $text.substr($pos).split(' '); + return { + 'component': $component, + 'type': $type, + 'foundation': $foundation, + 'protocol': $protocol, + 'address': $address, + 'port': $port, + 'priority': $priority + }; + }, + + iceCallback: function($pc, $candidates, $timeout, e) { + if (e.candidate) { + $candidates.push(this.parseCandidate(e.candidate.candidate)); + } else if (!('onicegatheringstatechange' in RTCPeerConnection.prototype)) { + $pc.close(); + this.notifyTurnResult($candidates, $timeout); + } + }, + + gatheringStateChange: function($pc, $candidates, $timeout) { + if ($pc.iceGatheringState !== 'complete') { + return; + } + + $pc.close(); + this.notifyTurnResult($candidates, $timeout); + }, + + testServer: function(e) { + e.stopPropagation(); + + var $row = $(e.currentTarget).parents('div.turn-server').first(); + var $server = $row.find('input.server').val(); + var $secret = $row.find('input.secret').val(); + var $protocols = $row.find('select.protocols').val().split(','); + + var $urls = []; + var i; + for (i = 0; i < $protocols.length; i++) { + $urls.push('turn:' + $server + '?transport=' + $protocols[i]); + } + + var $now = new Date(); + var $expires = Math.round($now.getTime() / 1000) + (5 * 60); + var $username = $expires + ':turn-test-user'; + var $hmac = new jsSHA("SHA-1", "TEXT"); + $hmac.setHMACKey($secret, "TEXT"); + $hmac.update($username); + var $password = $hmac.getHMAC("B64"); + var $iceServer = { + 'username': $username, + 'credential': $password, + 'urls': $urls + }; + + // Create a PeerConnection with no streams, but force a m=audio line. + var $iceTransports = []; + const $config = { + iceServers: [ + $iceServer + ], + iceTransportPolicy: 'relay' + }; + const $offerOptions = { + offerToReceiveAudio: 1 + }; + console.log('Creating PeerConnection with', $config); + var $candidates = []; + OC.Notification.showTemporary(t('spreed', 'Checking TURN server {server}', {'server': $server})); + var $pc = new RTCPeerConnection($config); + var $timeout = setTimeout(function() { + this.notifyTurnResult($candidates, $timeout); + $pc.close(); + }.bind(this), 10000); + $pc.onicecandidate = this.iceCallback.bind(this, $pc, $candidates, $timeout); + $pc.onicegatheringstatechange = this.gatheringStateChange.bind(this, $pc, $candidates, $timeout); + $pc.createOffer( + $offerOptions + ).then( + function(description) { + $pc.setLocalDescription(description); + }, + function(error) { + console.log("Error creating offer", error); + this.notifyTurnResult($candidates, $timeout); + $pc.close(); + }.bind(this) + ); + }, + saveServers: function() { var servers = [], $error = [], @@ -140,6 +253,7 @@ $template.find('a.icon-add').on('click', this.addNewTemplate.bind(this)); $template.find('a.icon-delete').on('click', this.deleteServer.bind(this)); + $template.find('a.icon-category-monitoring').on('click', this.testServer.bind(this)); $template.find('input').on('change', this.saveServers.bind(this)); $template.find('select').on('change', this.saveServers.bind(this)); diff --git a/templates/settings/admin/turn-server.php b/templates/settings/admin/turn-server.php index ef2376d8fb68..d50c6618d320 100644 --- a/templates/settings/admin/turn-server.php +++ b/templates/settings/admin/turn-server.php @@ -2,6 +2,7 @@ /** @var array $_ */ /** @var \OCP\IL10N $l */ script('spreed', ['admin/turn-server']); +script('spreed', ['admin/sha1']); style('spreed', ['settings-admin']); ?>