Skip to content

Commit c0836bb

Browse files
Merge pull request #195 from Elijah-Bodden/Elijah-Bodden-patch-669926
Merge fixes from debugging demo
2 parents 9dc25f6 + 213aa58 commit c0836bb

File tree

1 file changed

+82
-13
lines changed

1 file changed

+82
-13
lines changed

lib/index.js

Lines changed: 82 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,26 @@ var defaultConfig = {
5252
},
5353
},
5454
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,
5958
serverHeartbeatTimeout: 20000,
6059
},
6160
};
6261

6362
var CONFIG;
6463
const livePeers = {};
6564
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 = []
6675
const currentlyAuthenticating = [];
6776
const pubAliasLookup = {};
6877
const hiddenAliasLookup = {};
@@ -450,7 +459,7 @@ networkMap = new NetworkMap();
450459
// Try and stabilize link every second that we only have one peer
451460
var linkStabilizing = false;
452461
setInterval(async () => {
453-
if (Object.keys(livePeers).length === 1 && !linkStabilizing) {
462+
if (Object.keys(livePeers).length < 3 && !linkStabilizing) {
454463
linkStabilizing = true;
455464
try {
456465
await PeerConnection.prototype.stabilizeLink();
@@ -635,8 +644,12 @@ class PeerConnection {
635644
return this;
636645
}
637646
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 {
640653
var SDP = await connection.receiveOffer(JSON.parse(routePackage.SDP));
641654
} catch (error) {
642655
if (routePackage.sender) {
@@ -657,6 +670,7 @@ class PeerConnection {
657670
} else {
658671
this.rejectConnection(routePackage, connection);
659672
}
673+
consideringNonAuthConnections.splice(consideringNonAuthConnections.indexOf(routePackage.sender), 1)
660674
}
661675
async rejectConnection(routePackage, peerConnection) {
662676
peerConnection.connection.close();
@@ -787,7 +801,7 @@ class PeerConnection {
787801
[
788802
{
789803
sender: this.peerData.hiddenAlias,
790-
desiredPermissions: "advanced",
804+
wantAuth: true,
791805
},
792806
]
793807
)
@@ -979,7 +993,7 @@ async function makeConnection(destination, wantAuth) {
979993
if (Object.keys(livePeers).includes(destination) && wantAuth) {
980994
return await livePeers[destination].requestAuth();
981995
}
982-
const peerConnection = new PeerConnection(wantAuth);
996+
const peerConnection = new PeerConnection();
983997
const SDP = JSON.stringify(await peerConnection.makeOffer());
984998
const routeID = Math.random().toString().slice(2, 12);
985999

@@ -989,7 +1003,7 @@ async function makeConnection(destination, wantAuth) {
9891003
SDP,
9901004
sender: CONFIG.communication.hiddenAlias,
9911005
destination,
992-
wantAuth,
1006+
wantAuth: false,
9931007
routeID,
9941008
});
9951009
result = await Promise.race(
@@ -1026,6 +1040,12 @@ async function makeConnection(destination, wantAuth) {
10261040
);
10271041
case "routeAccepted":
10281042
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+
}
10291049
return peerConnection;
10301050
}
10311051
}
@@ -1294,8 +1314,11 @@ class ServerConnection {
12941314
let offer;
12951315
this.peer = new PeerConnection();
12961316
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) {
12991322
this.close();
13001323
return;
13011324
}
@@ -1324,6 +1347,7 @@ class ServerConnection {
13241347
}
13251348
async onClose() {
13261349
clearInterval(this.heartBeatTimeout);
1350+
eventHandler.dispatch("serverClosing")
13271351
this.server = undefined;
13281352
this.peer = undefined;
13291353
setTimeout(() => {
@@ -1353,7 +1377,7 @@ class ServerConnection {
13531377
break;
13541378
case "ERROR":
13551379
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)`
13571381
);
13581382
serverError(error, error.stack);
13591383
break;
@@ -1399,6 +1423,7 @@ async function loadConfig(configLoadFunction) {
13991423

14001424
async function init(configLoadFunction) {
14011425
await loadConfig(configLoadFunction);
1426+
eventHandler.dispatch("configLoaded")
14021427
addAlias(CONFIG.communication.publicAlias, CONFIG.communication.hiddenAlias);
14031428
serverConnection = new ServerConnection();
14041429
await serverConnection.connect();
@@ -1627,11 +1652,55 @@ class AuthPeerAddListener {
16271652
}
16281653
}
16291654

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+
16301693
const authPeerAddListener = new AuthPeerAddListener();
16311694
function onAuth(alias, callback) {
16321695
authPeerAddListener.addListener(alias, callback);
16331696
}
16341697

1698+
1699+
const peerAddListener = new PeerAddListener();
1700+
function onPeer(alias, callback) {
1701+
peerAddListener.addListener(alias, callback);
1702+
}
1703+
16351704
if (typeof process === "object") {
16361705
module.exports = {
16371706
init,

0 commit comments

Comments
 (0)