troubleshoot.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 setupChecks() {
  15. var start = document.querySelector('#start');
  16. var helpText = document.querySelector('#help-text');
  17. var checks = document.querySelector('#checks');
  18. start.addEventListener('click', function(e) {
  19. start.classList.add('hidden');
  20. helpText.classList.add('hidden');
  21. checks.classList.remove('hidden');
  22. doChecks();
  23. });
  24. }
  25. function doChecks() {
  26. // Check for JS
  27. switchTo('js', 'yes');
  28. // Check for RTCPeerConnection
  29. if (window.RTCPeerConnection) {
  30. switchTo('pc', 'yes');
  31. } else {
  32. switchTo('pc', 'no');
  33. }
  34. // Check for RTCDataChannel
  35. if (window.RTCPeerConnection && (new RTCPeerConnection()).createDataChannel) {
  36. switchTo('dc', 'yes');
  37. switchTo('turn', 'test');
  38. } else {
  39. switchTo('dc', 'no');
  40. switchTo('turn', 'no');
  41. }
  42. // Check for LocalStorage
  43. function localStorageAvailable(){
  44. var test = 'test';
  45. try {
  46. localStorage.setItem(test, test);
  47. localStorage.removeItem(test);
  48. return true;
  49. } catch(e) {
  50. return false;
  51. }
  52. }
  53. if (localStorageAvailable()) {
  54. switchTo('ls', 'yes');
  55. } else {
  56. switchTo('ls', 'no');
  57. }
  58. // Check for desktop notifications
  59. if ('Notification' in window) {
  60. switchTo('dn', 'yes');
  61. } else {
  62. switchTo('dn', 'no');
  63. }
  64. // Check for TURN connectivity
  65. var timeout = null;
  66. function turnSuccess() {
  67. switchTo('turn', 'yes');
  68. clearTimeout(timeout);
  69. }
  70. function turnFail() {
  71. switchTo('turn', 'no');
  72. document.querySelector('#status-turn .results').classList.add('hidden');
  73. document.querySelector('#status-turn .status-no .small').classList.remove('hidden');
  74. }
  75. function testTurn() {
  76. timeout = setTimeout(function() {
  77. turnFail();
  78. }, 10000);
  79. var noop = function() {};
  80. var pc = new RTCPeerConnection({iceServers: [{
  81. urls: [
  82. 'turn:ds-turn.threema.ch:443?transport=udp',
  83. 'turn:ds-turn.threema.ch:443?transport=tcp',
  84. 'turns:ds-turn.threema.ch:443',
  85. ],
  86. username: 'threema-angular-test',
  87. credential: 'VaoVnhxKGt2wD20F9bTOgiew6yHQmj4P7y7SE4lrahAjTQC0dpnG32FR4fnrlpKa',
  88. }]});
  89. document.querySelector('#status-turn .results').classList.remove('hidden');
  90. var resultData = document.querySelector('#status-turn .result-data');
  91. pc.createDataChannel('test');
  92. console.info('Creating offer...');
  93. pc.createOffer(function(sdp) { pc.setLocalDescription(sdp, noop, noop) }, noop);
  94. pc.onicecandidate = function(ice) {
  95. if (ice.candidate === null) {
  96. console.info('Done collecting candidates.');
  97. } else if (ice.candidate.candidate) {
  98. var candidate = SDPUtils.parseCandidate(ice.candidate.candidate);
  99. console.debug(candidate);
  100. if (candidate.type === 'relay') {
  101. var info = '[' + candidate.type + '] ' + candidate.ip + ':' + candidate.port + ' (' + candidate.protocol + ')';
  102. if (candidate.relatedAddress.indexOf(':') !== -1) {
  103. info += ' (ipv6)';
  104. } else if (candidate.relatedAddress.indexOf('.') !== -1) {
  105. info += ' (ipv4)';
  106. } else {
  107. info += ' (?)';
  108. }
  109. resultData.innerHTML += info + '<br>';
  110. turnSuccess();
  111. }
  112. } else {
  113. console.warn('Invalid candidate:', ice.candidate.candidate);
  114. }
  115. }
  116. }
  117. testTurn();
  118. }
  119. if (document.readyState != 'loading') {
  120. setupChecks();
  121. } else {
  122. document.addEventListener('DOMContentLoaded', setupChecks);
  123. }