From 621c525d11e0968c49892aee0dfa410f7dc0debf Mon Sep 17 00:00:00 2001 From: schlagmichdoch Date: Sat, 31 Dec 2022 18:03:58 +0100 Subject: [PATCH] readd persistent peerId via sessionStorage --- index.js | 14 ++++++++++---- public/scripts/network.js | 7 ++++++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 1ec8079..2e3e11d 100644 --- a/index.js +++ b/index.js @@ -102,7 +102,8 @@ class SnapdropServer { type: 'display-name', message: { displayName: peer.name.displayName, - deviceName: peer.name.deviceName + deviceName: peer.name.deviceName, + peerId: peer.id } }); } @@ -230,7 +231,7 @@ class Peer { this._setIP(request); // set peer id - this._setPeerId() + this._setPeerId(request) // is WebRTC supported ? this.rtcSupported = request.url.indexOf('webrtc') > -1; @@ -297,8 +298,13 @@ class Peer { return false; } - _setPeerId() { - this.id = Peer.uuid(); + _setPeerId(request) { + let peer_id = new URL(request.url, "http://server").searchParams.get("peer_id"); + if (peer_id && /^([0-9]|[a-f]){8}-(([0-9]|[a-f]){4}-){3}([0-9]|[a-f]){12}$/.test(peer_id)) { + this.id = peer_id; + } else { + this.id = Peer.uuid(); + } } toString() { diff --git a/public/scripts/network.js b/public/scripts/network.js index 0fb8af7..adbd381 100644 --- a/public/scripts/network.js +++ b/public/scripts/network.js @@ -43,6 +43,7 @@ class ServerConnection { this.send({ type: 'pong' }); break; case 'display-name': + sessionStorage.setItem("peer_id", msg.message.peerId); Events.fire('display-name', msg); break; default: @@ -59,7 +60,11 @@ class ServerConnection { // hack to detect if deployment or development environment const protocol = location.protocol.startsWith('https') ? 'wss' : 'ws'; const webrtc = window.isRtcSupported ? '/webrtc' : '/fallback'; - return protocol + '://' + location.host + location.pathname + 'server' + webrtc; + let url = new URL(protocol + '://' + location.host + location.pathname + 'server' + webrtc); + if (sessionStorage.getItem('peer_id')) { + url.searchParams.append('peer_id', sessionStorage.getItem('peer_id')) + } + return url.toString(); } _disconnect() {