@@ -52,17 +52,26 @@ var defaultConfig = {
52
52
} ,
53
53
} ,
54
54
server : {
55
- //Change to "wss://..." for ssl-secured sockets
56
- initBindURL : `ws://${ window . location . hostname } :8777/bind?originatingSDP=*` ,
57
- reconnectURL : `ws://${ window . location . hostname } :8777/reconnect` ,
58
- reconnectInterval : 5000 ,
55
+ initBindURL : `wss://${ window . location . hostname } :8777/bind?originatingSDP=*` ,
56
+ reconnectURL : `wss://${ window . location . hostname } :8777/reconnect` ,
57
+ reconnectInterval : 5000 ,
59
58
serverHeartbeatTimeout : 20000 ,
60
59
} ,
61
60
} ;
62
61
63
62
var CONFIG ;
64
63
const livePeers = { } ;
65
64
const authPeers = [ ] ;
65
+ // All aliases with auth connections being considered
66
+ // (so we ignore subsequent non-auth requests)
67
+ // Ofc this blocks stabilization, see comment below about maybe eliminating auth requests alltogether
68
+ // This hack is incredibly fuck
69
+ // Will fix later
70
+ const consideringAuthConnections = [ ]
71
+ const consideringNonAuthConnections = [ ]
72
+ // For if a non-auth request is ongoing
73
+ // Escalate to auth once that connection is done
74
+ const authConnectionQueue = [ ]
66
75
const currentlyAuthenticating = [ ] ;
67
76
const pubAliasLookup = { } ;
68
77
const hiddenAliasLookup = { } ;
@@ -450,7 +459,7 @@ networkMap = new NetworkMap();
450
459
// Try and stabilize link every second that we only have one peer
451
460
var linkStabilizing = false ;
452
461
setInterval ( async ( ) => {
453
- if ( Object . keys ( livePeers ) . length === 1 && ! linkStabilizing ) {
462
+ if ( Object . keys ( livePeers ) . length < 3 && ! linkStabilizing ) {
454
463
linkStabilizing = true ;
455
464
try {
456
465
await PeerConnection . prototype . stabilizeLink ( ) ;
@@ -635,8 +644,12 @@ class PeerConnection {
635
644
return this ;
636
645
}
637
646
async considerConnectionRequest ( routePackage ) {
638
- var connection = new PeerConnection ( routePackage . wantAuth ) ;
639
- try {
647
+ if ( consideringNonAuthConnections . includes ( routePackage . sender ) ) {
648
+ return
649
+ }
650
+ consideringNonAuthConnections . push ( routePackage . sender )
651
+ var connection = new PeerConnection ( ) ;
652
+ try {
640
653
var SDP = await connection . receiveOffer ( JSON . parse ( routePackage . SDP ) ) ;
641
654
} catch ( error ) {
642
655
if ( routePackage . sender ) {
@@ -657,6 +670,7 @@ class PeerConnection {
657
670
} else {
658
671
this . rejectConnection ( routePackage , connection ) ;
659
672
}
673
+ consideringNonAuthConnections . splice ( consideringNonAuthConnections . indexOf ( routePackage . sender ) , 1 )
660
674
}
661
675
async rejectConnection ( routePackage , peerConnection ) {
662
676
peerConnection . connection . close ( ) ;
@@ -787,7 +801,7 @@ class PeerConnection {
787
801
[
788
802
{
789
803
sender : this . peerData . hiddenAlias ,
790
- desiredPermissions : "advanced" ,
804
+ wantAuth : true ,
791
805
} ,
792
806
]
793
807
)
@@ -979,7 +993,7 @@ async function makeConnection(destination, wantAuth) {
979
993
if ( Object . keys ( livePeers ) . includes ( destination ) && wantAuth ) {
980
994
return await livePeers [ destination ] . requestAuth ( ) ;
981
995
}
982
- const peerConnection = new PeerConnection ( wantAuth ) ;
996
+ const peerConnection = new PeerConnection ( ) ;
983
997
const SDP = JSON . stringify ( await peerConnection . makeOffer ( ) ) ;
984
998
const routeID = Math . random ( ) . toString ( ) . slice ( 2 , 12 ) ;
985
999
@@ -989,7 +1003,7 @@ async function makeConnection(destination, wantAuth) {
989
1003
SDP ,
990
1004
sender : CONFIG . communication . hiddenAlias ,
991
1005
destination,
992
- wantAuth,
1006
+ wantAuth : false ,
993
1007
routeID,
994
1008
} ) ;
995
1009
result = await Promise . race (
@@ -1026,6 +1040,12 @@ async function makeConnection(destination, wantAuth) {
1026
1040
) ;
1027
1041
case "routeAccepted" :
1028
1042
await peerConnection . receiveAnswer ( result . externalDetail . SDP ) ;
1043
+ // Sneakily make normal connection and then upgrade on connect instead of sending auth from the start
1044
+ if ( wantAuth ) {
1045
+ onPeer ( destination , ( ) => {
1046
+ makeConnection ( destination , true )
1047
+ } )
1048
+ }
1029
1049
return peerConnection ;
1030
1050
}
1031
1051
}
@@ -1294,8 +1314,11 @@ class ServerConnection {
1294
1314
let offer ;
1295
1315
this . peer = new PeerConnection ( ) ;
1296
1316
try {
1297
- offer = await this . peer . makeOffer ( ) ;
1298
- } catch ( error ) {
1317
+ offer = await Promise . race ( [ this . peer . makeOffer ( ) , eventHandler . acquireExpectedDispatch ( "serverClosing" ) ] ) ;
1318
+ if ( offer === "serverClosing" ) {
1319
+ return
1320
+ }
1321
+ } catch ( error ) {
1299
1322
this . close ( ) ;
1300
1323
return ;
1301
1324
}
@@ -1324,6 +1347,7 @@ class ServerConnection {
1324
1347
}
1325
1348
async onClose ( ) {
1326
1349
clearInterval ( this . heartBeatTimeout ) ;
1350
+ eventHandler . dispatch ( "serverClosing" )
1327
1351
this . server = undefined ;
1328
1352
this . peer = undefined ;
1329
1353
setTimeout ( ( ) => {
@@ -1353,7 +1377,7 @@ class ServerConnection {
1353
1377
break ;
1354
1378
case "ERROR" :
1355
1379
const error = new Error (
1356
- `Unidentified internal error on server ${ message [ 1 ] } `
1380
+ `Unidentified server error (likely hibernated tab or overloaded STUN server/network interface) `
1357
1381
) ;
1358
1382
serverError ( error , error . stack ) ;
1359
1383
break ;
@@ -1399,6 +1423,7 @@ async function loadConfig(configLoadFunction) {
1399
1423
1400
1424
async function init ( configLoadFunction ) {
1401
1425
await loadConfig ( configLoadFunction ) ;
1426
+ eventHandler . dispatch ( "configLoaded" )
1402
1427
addAlias ( CONFIG . communication . publicAlias , CONFIG . communication . hiddenAlias ) ;
1403
1428
serverConnection = new ServerConnection ( ) ;
1404
1429
await serverConnection . connect ( ) ;
@@ -1627,11 +1652,55 @@ class AuthPeerAddListener {
1627
1652
}
1628
1653
}
1629
1654
1655
+ class PeerAddListener {
1656
+
1657
+ constructor ( ) {
1658
+
1659
+ this . listening = { } ;
1660
+
1661
+ onNewPeer ( this . listener . bind ( this ) ) ;
1662
+
1663
+ }
1664
+
1665
+ async listener ( externalDetail ) {
1666
+
1667
+ if ( Object . keys ( this . listening ) . includes ( externalDetail ) ) {
1668
+
1669
+ this . listening [ externalDetail ] ( ) ;
1670
+
1671
+ delete this . listening [ externalDetail ] ;
1672
+
1673
+ }
1674
+
1675
+ }
1676
+
1677
+ async addListener ( alias , callback ) {
1678
+
1679
+ if ( Object . keys ( livePeers ) . includes ( alias ) ) {
1680
+
1681
+ callback ( ) ;
1682
+
1683
+ return ;
1684
+
1685
+ }
1686
+
1687
+ this . listening [ alias ] = callback ;
1688
+
1689
+ }
1690
+
1691
+ }
1692
+
1630
1693
const authPeerAddListener = new AuthPeerAddListener ( ) ;
1631
1694
function onAuth ( alias , callback ) {
1632
1695
authPeerAddListener . addListener ( alias , callback ) ;
1633
1696
}
1634
1697
1698
+
1699
+ const peerAddListener = new PeerAddListener ( ) ;
1700
+ function onPeer ( alias , callback ) {
1701
+ peerAddListener . addListener ( alias , callback ) ;
1702
+ }
1703
+
1635
1704
if ( typeof process === "object" ) {
1636
1705
module . exports = {
1637
1706
init,
0 commit comments