File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Load diff This file was deleted.
Original file line number Diff line number Diff line change @@ -421,6 +421,7 @@ <h5 style="color: var(--color-h1); margin:0">
421421 < script src ="js/qrious.min.js " type ="text/javascript "> </ script >
422422 < script src ="js/crypto-js.js " type ="text/javascript "> </ script >
423423 < script src ="js/peerjs.min.js " type ="text/javascript "> </ script >
424+ < script src ="js/webrtc/turn.js " type ="text/javascript "> </ script >
424425 < script src ="js/webrtc/user.js " type ="text/javascript "> </ script >
425426 < script src ="js/webrtc/file.js " type ="text/javascript "> </ script >
426427 < script src ="js/script.js " type ="text/javascript "> </ script >
Original file line number Diff line number Diff line change 1+ class Turn {
2+ _url = "https://api.filesync.app/credentials/" ;
3+
4+ _decodeJwt ( token ) {
5+ const payload = token . split ( "." ) [ 1 ] ;
6+ const json = atob ( payload ) ;
7+ return JSON . parse ( json ) ;
8+ }
9+
10+ getToken = async ( ) => {
11+ const response = await fetch ( this . _url , { method : "GET" } ) ;
12+
13+ if ( ! response . ok ) {
14+ throw new Error ( "An issue occurred while getting the token." ) ;
15+ }
16+
17+ // Parse the response as JSON
18+ const data = await response . json ( ) ;
19+
20+ // Get JWT from cookie
21+ const token = data . token
22+ if ( ! token ) throw new Error ( "Token cookie not found" ) ;
23+
24+ // Decode JWT to get username and credential
25+ const payload = this . _decodeJwt ( token ) ;
26+ return { username : payload . username , credential : payload . credential } ;
27+ } ;
28+ }
Original file line number Diff line number Diff line change @@ -33,6 +33,20 @@ class User {
3333 }
3434
3535 async init ( peer_id = crypto . randomUUID ( ) ) {
36+ // Get Short Lived TURN credentials
37+ const turn = new Turn ( ) ;
38+ let username , credential ;
39+ try {
40+ ( { username, credential } = await turn . getToken ( ) ) ;
41+ } catch ( err ) {
42+ console . log ( err )
43+ transfer_div . style . display = 'none'
44+ connect_div . style . display = 'none'
45+ error_div . style . display = 'block'
46+ error_message . innerHTML = 'An error occurred getting the TURN credentials. Please try again later.'
47+ return
48+ }
49+
3650 await new Promise ( ( resolve ) => {
3751 // Create a new Peer instance
3852 this . _peer = new Peer ( peer_id , {
@@ -44,8 +58,8 @@ class User {
4458 iceServers : [
4559 {
4660 urls : 'turn:turn.filesync.app:3478' ,
47- username : 'peerjs' ,
48- credential : '6VF8LbHjZUPR4BdCAPPTQA=='
61+ username : username ,
62+ credential : credential
4963 }
5064 ]
5165 }
You can’t perform that action at this time.
0 commit comments