troubleshoot.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. function switchTo(type, newStatus) {
  2. var unknown = document.querySelector('#status-' + type + ' .status-unknown');
  3. if (unknown) {
  4. unknown.classList.add('hidden');
  5. }
  6. var test = document.querySelector('#status-' + type + ' .status-test')
  7. if (test) {
  8. test.classList.add('hidden');
  9. }
  10. document.querySelector('#status-' + type + ' .status-no').classList.add('hidden');
  11. document.querySelector('#status-' + type + ' .status-yes').classList.add('hidden');
  12. document.querySelector('#status-' + type + ' .status-' + newStatus).classList.remove('hidden');
  13. }
  14. function doChecks() {
  15. // Check for JS
  16. switchTo('js', 'yes');
  17. // Check for RTCPeerConnection
  18. if (window.RTCPeerConnection) {
  19. switchTo('pc', 'yes');
  20. } else {
  21. switchTo('pc', 'no');
  22. }
  23. // Check for RTCDataChannel
  24. if (window.RTCPeerConnection && (new RTCPeerConnection()).createDataChannel) {
  25. switchTo('dc', 'yes');
  26. switchTo('turn', 'test');
  27. } else {
  28. switchTo('dc', 'no');
  29. switchTo('turn', 'no');
  30. }
  31. // Check for LocalStorage
  32. function localStorageAvailable(){
  33. var test = 'test';
  34. try {
  35. localStorage.setItem(test, test);
  36. localStorage.removeItem(test);
  37. return true;
  38. } catch(e) {
  39. return false;
  40. }
  41. }
  42. if (localStorageAvailable()) {
  43. switchTo('ls', 'yes');
  44. } else {
  45. switchTo('ls', 'no');
  46. }
  47. // Check for TURN connectivity
  48. var timeout = null;
  49. function turnSuccess() {
  50. switchTo('turn', 'yes');
  51. clearTimeout(timeout);
  52. }
  53. function turnFail() {
  54. switchTo('turn', 'no');
  55. document.querySelector('#status-turn .results').classList.add('hidden');
  56. document.querySelector('#status-turn .status-no .small').classList.remove('hidden');
  57. }
  58. var button = document.querySelector('#status-turn button');
  59. button.addEventListener('click', function(e) {
  60. button.outerHTML = '<img src="loading.gif" alt="Loading...">';
  61. timeout = setTimeout(function() {
  62. turnFail();
  63. }, 10000);
  64. var noop = function() {};
  65. var pc = new RTCPeerConnection({iceServers: [{
  66. urls: [
  67. 'turn:turn.threema.ch:443?transport=udp',
  68. 'turn:turn.threema.ch:443?transport=tcp',
  69. 'turns:turn.threema.ch:443',
  70. ],
  71. username: 'threema-angular-test',
  72. credential: 'VaoVnhxKGt2wD20F9bTOgiew6yHQmj4P7y7SE4lrahAjTQC0dpnG32FR4fnrlpKa',
  73. }]});
  74. document.querySelector('#status-turn .results').classList.remove('hidden');
  75. var resultData = document.querySelector('#status-turn .result-data');
  76. pc.createDataChannel('test');
  77. console.info('Creating offer...');
  78. pc.createOffer(function(sdp) { pc.setLocalDescription(sdp, noop, noop) }, noop);
  79. pc.onicecandidate = function(ice) {
  80. if (ice.candidate === null) {
  81. console.info('Done collecting candidates.');
  82. } else if (ice.candidate.candidate) {
  83. var candidate = SDPUtils.parseCandidate(ice.candidate.candidate);
  84. console.debug(candidate);
  85. if (candidate.type === 'relay') {
  86. var info = '[' + candidate.type + '] ' + candidate.ip + ':' + candidate.port + ' (' + candidate.protocol + ')';
  87. if (candidate.relatedAddress.indexOf(':') !== -1) {
  88. info += ' (ipv6)';
  89. } else if (candidate.relatedAddress.indexOf('.') !== -1) {
  90. info += ' (ipv4)';
  91. } else {
  92. info += ' (?)';
  93. }
  94. resultData.innerHTML += info + '<br>';
  95. turnSuccess();
  96. }
  97. } else {
  98. console.warn('Invalid candidate:', ice.candidate.candidate);
  99. }
  100. }
  101. });
  102. }
  103. if (document.readyState != 'loading') {
  104. doChecks();
  105. } else {
  106. document.addEventListener('DOMContentLoaded', doChecks);
  107. }