Skip to content

Commit

Permalink
Merge pull request #195 from Elijah-Bodden/Elijah-Bodden-patch-669926
Browse files Browse the repository at this point in the history
Merge fixes from debugging demo
  • Loading branch information
Elijah-Bodden authored Oct 13, 2024
2 parents 9dc25f6 + 213aa58 commit c0836bb
Showing 1 changed file with 82 additions and 13 deletions.
95 changes: 82 additions & 13 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,26 @@ var defaultConfig = {
},
},
server: {
//Change to "wss://..." for ssl-secured sockets
initBindURL: `ws://${window.location.hostname}:8777/bind?originatingSDP=*`,
reconnectURL: `ws://${window.location.hostname}:8777/reconnect`,
reconnectInterval: 5000,
initBindURL: `wss://${window.location.hostname}:8777/bind?originatingSDP=*`,
reconnectURL: `wss://${window.location.hostname}:8777/reconnect`,
reconnectInterval: 5000,
serverHeartbeatTimeout: 20000,
},
};

var CONFIG;
const livePeers = {};
const authPeers = [];
// All aliases with auth connections being considered
// (so we ignore subsequent non-auth requests)
// Ofc this blocks stabilization, see comment below about maybe eliminating auth requests alltogether
// This hack is incredibly fuck
// Will fix later
const consideringAuthConnections = []
const consideringNonAuthConnections = []
// For if a non-auth request is ongoing
// Escalate to auth once that connection is done
const authConnectionQueue = []
const currentlyAuthenticating = [];
const pubAliasLookup = {};
const hiddenAliasLookup = {};
Expand Down Expand Up @@ -450,7 +459,7 @@ networkMap = new NetworkMap();
// Try and stabilize link every second that we only have one peer
var linkStabilizing = false;
setInterval(async () => {
if (Object.keys(livePeers).length === 1 && !linkStabilizing) {
if (Object.keys(livePeers).length < 3 && !linkStabilizing) {
linkStabilizing = true;
try {
await PeerConnection.prototype.stabilizeLink();
Expand Down Expand Up @@ -635,8 +644,12 @@ class PeerConnection {
return this;
}
async considerConnectionRequest(routePackage) {
var connection = new PeerConnection(routePackage.wantAuth);
try {
if (consideringNonAuthConnections.includes(routePackage.sender)) {
return
}
consideringNonAuthConnections.push(routePackage.sender)
var connection = new PeerConnection();
try {
var SDP = await connection.receiveOffer(JSON.parse(routePackage.SDP));
} catch (error) {
if (routePackage.sender) {
Expand All @@ -657,6 +670,7 @@ class PeerConnection {
} else {
this.rejectConnection(routePackage, connection);
}
consideringNonAuthConnections.splice(consideringNonAuthConnections.indexOf(routePackage.sender), 1)
}
async rejectConnection(routePackage, peerConnection) {
peerConnection.connection.close();
Expand Down Expand Up @@ -787,7 +801,7 @@ class PeerConnection {
[
{
sender: this.peerData.hiddenAlias,
desiredPermissions: "advanced",
wantAuth: true,
},
]
)
Expand Down Expand Up @@ -979,7 +993,7 @@ async function makeConnection(destination, wantAuth) {
if (Object.keys(livePeers).includes(destination) && wantAuth) {
return await livePeers[destination].requestAuth();
}
const peerConnection = new PeerConnection(wantAuth);
const peerConnection = new PeerConnection();
const SDP = JSON.stringify(await peerConnection.makeOffer());
const routeID = Math.random().toString().slice(2, 12);

Expand All @@ -989,7 +1003,7 @@ async function makeConnection(destination, wantAuth) {
SDP,
sender: CONFIG.communication.hiddenAlias,
destination,
wantAuth,
wantAuth: false,
routeID,
});
result = await Promise.race(
Expand Down Expand Up @@ -1026,6 +1040,12 @@ async function makeConnection(destination, wantAuth) {
);
case "routeAccepted":
await peerConnection.receiveAnswer(result.externalDetail.SDP);
// Sneakily make normal connection and then upgrade on connect instead of sending auth from the start
if (wantAuth) {
onPeer(destination, () => {
makeConnection(destination, true)
})
}
return peerConnection;
}
}
Expand Down Expand Up @@ -1294,8 +1314,11 @@ class ServerConnection {
let offer;
this.peer = new PeerConnection();
try {
offer = await this.peer.makeOffer();
} catch (error) {
offer = await Promise.race([this.peer.makeOffer(), eventHandler.acquireExpectedDispatch("serverClosing")]);
if (offer === "serverClosing") {
return
}
} catch (error) {
this.close();
return;
}
Expand Down Expand Up @@ -1324,6 +1347,7 @@ class ServerConnection {
}
async onClose() {
clearInterval(this.heartBeatTimeout);
eventHandler.dispatch("serverClosing")
this.server = undefined;
this.peer = undefined;
setTimeout(() => {
Expand Down Expand Up @@ -1353,7 +1377,7 @@ class ServerConnection {
break;
case "ERROR":
const error = new Error(
`Unidentified internal error on server ${message[1]}`
`Unidentified server error (likely hibernated tab or overloaded STUN server/network interface)`
);
serverError(error, error.stack);
break;
Expand Down Expand Up @@ -1399,6 +1423,7 @@ async function loadConfig(configLoadFunction) {

async function init(configLoadFunction) {
await loadConfig(configLoadFunction);
eventHandler.dispatch("configLoaded")
addAlias(CONFIG.communication.publicAlias, CONFIG.communication.hiddenAlias);
serverConnection = new ServerConnection();
await serverConnection.connect();
Expand Down Expand Up @@ -1627,11 +1652,55 @@ class AuthPeerAddListener {
}
}

class PeerAddListener {

constructor() {

this.listening = {};

onNewPeer(this.listener.bind(this));

}

async listener(externalDetail) {

if (Object.keys(this.listening).includes(externalDetail)) {

this.listening[externalDetail]();

delete this.listening[externalDetail];

}

}

async addListener(alias, callback) {

if (Object.keys(livePeers).includes(alias)) {

callback();

return;

}

this.listening[alias] = callback;

}

}

const authPeerAddListener = new AuthPeerAddListener();
function onAuth(alias, callback) {
authPeerAddListener.addListener(alias, callback);
}


const peerAddListener = new PeerAddListener();
function onPeer(alias, callback) {
peerAddListener.addListener(alias, callback);
}

if (typeof process === "object") {
module.exports = {
init,
Expand Down

0 comments on commit c0836bb

Please sign in to comment.