2023-02-10 20:22:36 +01:00
|
|
|
window.URL = window.URL || window.webkitURL;
|
2023-10-13 18:15:55 +02:00
|
|
|
window.isRtcSupported = !!(window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection);
|
2023-02-10 20:22:36 +01:00
|
|
|
|
2023-05-10 21:21:06 +02:00
|
|
|
window.hiddenProperty = 'hidden' in document ? 'hidden' :
|
|
|
|
'webkitHidden' in document ? 'webkitHidden' :
|
|
|
|
'mozHidden' in document ? 'mozHidden' :
|
|
|
|
null;
|
|
|
|
window.visibilityChangeEvent = 'visibilitychange' in document ? 'visibilitychange' :
|
|
|
|
'webkitvisibilitychange' in document ? 'webkitvisibilitychange' :
|
|
|
|
'mozvisibilitychange' in document ? 'mozvisibilitychange' :
|
|
|
|
null;
|
|
|
|
|
2023-02-10 20:22:36 +01:00
|
|
|
class ServerConnection {
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this._connect();
|
|
|
|
Events.on('pagehide', _ => this._disconnect());
|
2023-05-10 21:21:06 +02:00
|
|
|
document.addEventListener(window.visibilityChangeEvent, _ => this._onVisibilityChange());
|
2023-02-10 20:22:36 +01:00
|
|
|
if (navigator.connection) navigator.connection.addEventListener('change', _ => this._reconnect());
|
2023-05-11 21:04:10 +02:00
|
|
|
Events.on('room-secrets', e => this.send({ type: 'room-secrets', roomSecrets: e.detail }));
|
|
|
|
Events.on('join-ip-room', e => this.send({ type: 'join-ip-room'}));
|
2023-05-04 17:38:51 +02:00
|
|
|
Events.on('room-secrets-deleted', e => this.send({ type: 'room-secrets-deleted', roomSecrets: e.detail}));
|
|
|
|
Events.on('regenerate-room-secret', e => this.send({ type: 'regenerate-room-secret', roomSecret: e.detail}));
|
2023-02-10 20:22:36 +01:00
|
|
|
Events.on('pair-device-initiate', _ => this._onPairDeviceInitiate());
|
|
|
|
Events.on('pair-device-join', e => this._onPairDeviceJoin(e.detail));
|
|
|
|
Events.on('pair-device-cancel', _ => this.send({ type: 'pair-device-cancel' }));
|
2023-09-13 19:00:48 +02:00
|
|
|
|
|
|
|
Events.on('create-public-room', _ => this._onCreatePublicRoom());
|
|
|
|
Events.on('join-public-room', e => this._onJoinPublicRoom(e.detail.roomId, e.detail.createIfInvalid));
|
|
|
|
Events.on('leave-public-room', _ => this._onLeavePublicRoom());
|
|
|
|
|
2023-02-10 20:22:36 +01:00
|
|
|
Events.on('offline', _ => clearTimeout(this._reconnectTimer));
|
|
|
|
Events.on('online', _ => this._connect());
|
|
|
|
}
|
|
|
|
|
2023-03-01 21:35:00 +01:00
|
|
|
_connect() {
|
2023-02-10 20:22:36 +01:00
|
|
|
clearTimeout(this._reconnectTimer);
|
2023-10-12 18:57:26 +02:00
|
|
|
if (this._isConnected() || this._isConnecting() || this._isOffline()) return;
|
2023-10-12 03:39:37 +02:00
|
|
|
if (this._isReconnect) {
|
|
|
|
Events.fire('notify-user', {
|
|
|
|
message: Localization.getTranslation("notifications.connecting"),
|
|
|
|
persistent: true
|
|
|
|
});
|
|
|
|
}
|
2023-03-01 21:35:00 +01:00
|
|
|
const ws = new WebSocket(this._endpoint());
|
2023-02-10 20:22:36 +01:00
|
|
|
ws.binaryType = 'arraybuffer';
|
|
|
|
ws.onopen = _ => this._onOpen();
|
|
|
|
ws.onmessage = e => this._onMessage(e.data);
|
|
|
|
ws.onclose = _ => this._onDisconnect();
|
|
|
|
ws.onerror = e => this._onError(e);
|
|
|
|
this._socket = ws;
|
|
|
|
}
|
|
|
|
|
|
|
|
_onOpen() {
|
|
|
|
console.log('WS: server connected');
|
|
|
|
Events.fire('ws-connected');
|
2023-07-06 21:29:36 +02:00
|
|
|
if (this._isReconnect) Events.fire('notify-user', Localization.getTranslation("notifications.connected"));
|
2023-02-10 20:22:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_onPairDeviceInitiate() {
|
|
|
|
if (!this._isConnected()) {
|
2023-09-13 19:00:48 +02:00
|
|
|
Events.fire('notify-user', Localization.getTranslation("notifications.online-requirement-pairing"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.send({ type: 'pair-device-initiate' });
|
|
|
|
}
|
|
|
|
|
|
|
|
_onPairDeviceJoin(pairKey) {
|
|
|
|
if (!this._isConnected()) {
|
|
|
|
setTimeout(_ => this._onPairDeviceJoin(pairKey), 1000);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.send({ type: 'pair-device-join', pairKey: pairKey });
|
|
|
|
}
|
|
|
|
|
|
|
|
_onCreatePublicRoom() {
|
|
|
|
if (!this._isConnected()) {
|
|
|
|
Events.fire('notify-user', Localization.getTranslation("notifications.online-requirement-public-room"));
|
2023-02-10 20:22:36 +01:00
|
|
|
return;
|
|
|
|
}
|
2023-09-13 19:00:48 +02:00
|
|
|
this.send({ type: 'create-public-room' });
|
2023-02-10 20:22:36 +01:00
|
|
|
}
|
|
|
|
|
2023-09-13 19:00:48 +02:00
|
|
|
_onJoinPublicRoom(roomId, createIfInvalid) {
|
2023-02-10 20:22:36 +01:00
|
|
|
if (!this._isConnected()) {
|
2023-09-13 19:00:48 +02:00
|
|
|
setTimeout(_ => this._onJoinPublicRoom(roomId), 1000);
|
2023-02-10 20:22:36 +01:00
|
|
|
return;
|
|
|
|
}
|
2023-09-13 19:00:48 +02:00
|
|
|
this.send({ type: 'join-public-room', publicRoomId: roomId, createIfInvalid: createIfInvalid });
|
|
|
|
}
|
|
|
|
|
|
|
|
_onLeavePublicRoom() {
|
|
|
|
if (!this._isConnected()) {
|
|
|
|
setTimeout(_ => this._onLeavePublicRoom(), 1000);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.send({ type: 'leave-public-room' });
|
2023-02-10 20:22:36 +01:00
|
|
|
}
|
|
|
|
|
2023-02-24 18:08:48 +01:00
|
|
|
_setRtcConfig(config) {
|
|
|
|
window.rtcConfig = config;
|
|
|
|
}
|
|
|
|
|
2023-02-10 20:22:36 +01:00
|
|
|
_onMessage(msg) {
|
|
|
|
msg = JSON.parse(msg);
|
2023-05-04 17:38:51 +02:00
|
|
|
if (msg.type !== 'ping') console.log('WS receive:', msg);
|
2023-02-10 20:22:36 +01:00
|
|
|
switch (msg.type) {
|
2023-02-24 18:08:48 +01:00
|
|
|
case 'rtc-config':
|
|
|
|
this._setRtcConfig(msg.config);
|
|
|
|
break;
|
2023-02-10 20:22:36 +01:00
|
|
|
case 'peers':
|
2023-05-04 17:38:51 +02:00
|
|
|
this._onPeers(msg);
|
2023-02-10 20:22:36 +01:00
|
|
|
break;
|
|
|
|
case 'peer-joined':
|
|
|
|
Events.fire('peer-joined', msg);
|
|
|
|
break;
|
|
|
|
case 'peer-left':
|
2023-02-10 23:47:39 +01:00
|
|
|
Events.fire('peer-left', msg);
|
2023-02-10 20:22:36 +01:00
|
|
|
break;
|
|
|
|
case 'signal':
|
|
|
|
Events.fire('signal', msg);
|
|
|
|
break;
|
|
|
|
case 'ping':
|
|
|
|
this.send({ type: 'pong' });
|
|
|
|
break;
|
|
|
|
case 'display-name':
|
|
|
|
this._onDisplayName(msg);
|
|
|
|
break;
|
|
|
|
case 'pair-device-initiated':
|
|
|
|
Events.fire('pair-device-initiated', msg);
|
|
|
|
break;
|
|
|
|
case 'pair-device-joined':
|
2023-02-11 00:52:37 +01:00
|
|
|
Events.fire('pair-device-joined', msg);
|
2023-02-10 20:22:36 +01:00
|
|
|
break;
|
|
|
|
case 'pair-device-join-key-invalid':
|
|
|
|
Events.fire('pair-device-join-key-invalid');
|
|
|
|
break;
|
|
|
|
case 'pair-device-canceled':
|
2023-09-13 19:00:48 +02:00
|
|
|
Events.fire('pair-device-canceled', msg.pairKey);
|
2023-02-10 20:22:36 +01:00
|
|
|
break;
|
2023-09-13 19:00:48 +02:00
|
|
|
case 'join-key-rate-limit':
|
2023-07-06 21:29:36 +02:00
|
|
|
Events.fire('notify-user', Localization.getTranslation("notifications.rate-limit-join-key"));
|
2023-02-10 20:22:36 +01:00
|
|
|
break;
|
|
|
|
case 'secret-room-deleted':
|
|
|
|
Events.fire('secret-room-deleted', msg.roomSecret);
|
|
|
|
break;
|
2023-05-09 03:17:08 +02:00
|
|
|
case 'room-secret-regenerated':
|
|
|
|
Events.fire('room-secret-regenerated', msg);
|
|
|
|
break;
|
2023-09-13 19:00:48 +02:00
|
|
|
case 'public-room-id-invalid':
|
|
|
|
Events.fire('public-room-id-invalid', msg.publicRoomId);
|
|
|
|
break;
|
|
|
|
case 'public-room-created':
|
|
|
|
Events.fire('public-room-created', msg.roomId);
|
|
|
|
break;
|
|
|
|
case 'public-room-left':
|
|
|
|
Events.fire('public-room-left');
|
|
|
|
break;
|
2023-02-10 20:22:36 +01:00
|
|
|
case 'request':
|
|
|
|
case 'header':
|
|
|
|
case 'partition':
|
|
|
|
case 'partition-received':
|
|
|
|
case 'progress':
|
|
|
|
case 'files-transfer-response':
|
|
|
|
case 'file-transfer-complete':
|
|
|
|
case 'message-transfer-complete':
|
|
|
|
case 'text':
|
2023-03-01 21:35:00 +01:00
|
|
|
case 'display-name-changed':
|
2023-02-10 20:22:36 +01:00
|
|
|
case 'ws-chunk':
|
|
|
|
Events.fire('ws-relay', JSON.stringify(msg));
|
|
|
|
break;
|
|
|
|
default:
|
2023-05-04 17:38:51 +02:00
|
|
|
console.error('WS receive: unknown message type', msg);
|
2023-02-10 20:22:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
send(msg) {
|
|
|
|
if (!this._isConnected()) return;
|
2023-05-04 17:38:51 +02:00
|
|
|
if (msg.type !== 'pong') console.log("WS send:", msg)
|
2023-02-10 20:22:36 +01:00
|
|
|
this._socket.send(JSON.stringify(msg));
|
|
|
|
}
|
|
|
|
|
2023-05-04 17:38:51 +02:00
|
|
|
_onPeers(msg) {
|
|
|
|
Events.fire('peers', msg);
|
|
|
|
}
|
|
|
|
|
2023-02-10 20:22:36 +01:00
|
|
|
_onDisplayName(msg) {
|
2023-05-04 17:38:51 +02:00
|
|
|
// Add peerId and peerIdHash to sessionStorage to authenticate as the same device on page reload
|
2023-10-31 18:32:23 +01:00
|
|
|
sessionStorage.setItem('peer_id', msg.peerId);
|
|
|
|
sessionStorage.setItem('peer_id_hash', msg.peerIdHash);
|
2023-05-04 17:38:51 +02:00
|
|
|
|
2023-05-11 21:04:10 +02:00
|
|
|
// Add peerId to localStorage to mark it for other PairDrop tabs on the same browser
|
2023-05-04 17:38:51 +02:00
|
|
|
BrowserTabsConnector.addPeerIdToLocalStorage().then(peerId => {
|
2023-05-11 21:04:10 +02:00
|
|
|
if (!peerId) return;
|
|
|
|
console.log("successfully added peerId to localStorage");
|
|
|
|
|
|
|
|
// Only now join rooms
|
|
|
|
Events.fire('join-ip-room');
|
|
|
|
PersistentStorage.getAllRoomSecrets().then(roomSecrets => {
|
|
|
|
Events.fire('room-secrets', roomSecrets);
|
|
|
|
});
|
2023-05-04 17:38:51 +02:00
|
|
|
});
|
|
|
|
|
2023-02-10 20:22:36 +01:00
|
|
|
Events.fire('display-name', msg);
|
|
|
|
}
|
|
|
|
|
2023-03-01 21:35:00 +01:00
|
|
|
_endpoint() {
|
2023-02-10 20:22:36 +01:00
|
|
|
// hack to detect if deployment or development environment
|
|
|
|
const protocol = location.protocol.startsWith('https') ? 'wss' : 'ws';
|
|
|
|
const webrtc = window.isRtcSupported ? '/webrtc' : '/fallback';
|
|
|
|
let ws_url = new URL(protocol + '://' + location.host + location.pathname + 'server' + webrtc);
|
2023-09-13 19:00:48 +02:00
|
|
|
const peerId = sessionStorage.getItem('peer_id');
|
|
|
|
const peerIdHash = sessionStorage.getItem('peer_id_hash');
|
2023-03-06 00:06:57 +01:00
|
|
|
if (peerId && peerIdHash) {
|
|
|
|
ws_url.searchParams.append('peer_id', peerId);
|
|
|
|
ws_url.searchParams.append('peer_id_hash', peerIdHash);
|
|
|
|
}
|
2023-02-10 20:22:36 +01:00
|
|
|
return ws_url.toString();
|
|
|
|
}
|
|
|
|
|
2023-03-06 03:36:46 +01:00
|
|
|
_disconnect() {
|
|
|
|
this.send({ type: 'disconnect' });
|
2023-05-04 17:38:51 +02:00
|
|
|
|
2023-09-13 19:00:48 +02:00
|
|
|
const peerId = sessionStorage.getItem('peer_id');
|
2023-05-04 17:38:51 +02:00
|
|
|
BrowserTabsConnector.removePeerIdFromLocalStorage(peerId).then(_ => {
|
|
|
|
console.log("successfully removed peerId from localStorage");
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!this._socket) return;
|
|
|
|
|
|
|
|
this._socket.onclose = null;
|
|
|
|
this._socket.close();
|
|
|
|
this._socket = null;
|
|
|
|
Events.fire('ws-disconnected');
|
|
|
|
this._isReconnect = true;
|
2023-02-10 20:22:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_onDisconnect() {
|
|
|
|
console.log('WS: server disconnected');
|
2023-10-06 02:57:46 +02:00
|
|
|
setTimeout(() => {
|
2023-10-12 03:39:37 +02:00
|
|
|
this._isReconnect = true;
|
|
|
|
Events.fire('ws-disconnected');
|
|
|
|
this._reconnectTimer = setTimeout(_ => this._connect(), 1000);
|
2023-10-06 02:57:46 +02:00
|
|
|
}, 100); //delay for 100ms to prevent flickering on page reload
|
2023-02-10 20:22:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_onVisibilityChange() {
|
2023-05-10 21:21:06 +02:00
|
|
|
if (window.hiddenProperty) return;
|
2023-02-10 20:22:36 +01:00
|
|
|
this._connect();
|
|
|
|
}
|
|
|
|
|
|
|
|
_isConnected() {
|
|
|
|
return this._socket && this._socket.readyState === this._socket.OPEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
_isConnecting() {
|
|
|
|
return this._socket && this._socket.readyState === this._socket.CONNECTING;
|
|
|
|
}
|
|
|
|
|
2023-10-12 18:57:26 +02:00
|
|
|
_isOffline() {
|
|
|
|
return !navigator.onLine;
|
|
|
|
}
|
|
|
|
|
2023-02-10 20:22:36 +01:00
|
|
|
_onError(e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
_reconnect() {
|
|
|
|
this._disconnect();
|
|
|
|
this._connect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Peer {
|
|
|
|
|
2023-09-13 19:00:48 +02:00
|
|
|
constructor(serverConnection, isCaller, peerId, roomType, roomId) {
|
2023-02-10 20:22:36 +01:00
|
|
|
this._server = serverConnection;
|
2023-05-11 21:04:10 +02:00
|
|
|
this._isCaller = isCaller;
|
2023-02-10 20:22:36 +01:00
|
|
|
this._peerId = peerId;
|
2023-09-13 19:00:48 +02:00
|
|
|
|
|
|
|
this._roomIds = {};
|
|
|
|
this._updateRoomIds(roomType, roomId);
|
2023-05-04 17:38:51 +02:00
|
|
|
|
2023-02-10 20:22:36 +01:00
|
|
|
this._filesQueue = [];
|
|
|
|
this._busy = false;
|
2023-05-04 17:38:51 +02:00
|
|
|
|
|
|
|
// evaluate auto accept
|
|
|
|
this._evaluateAutoAccept();
|
2023-02-10 20:22:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sendJSON(message) {
|
|
|
|
this._send(JSON.stringify(message));
|
|
|
|
}
|
|
|
|
|
2023-03-06 11:24:19 +01:00
|
|
|
sendDisplayName(displayName) {
|
|
|
|
this.sendJSON({type: 'display-name-changed', displayName: displayName});
|
|
|
|
}
|
|
|
|
|
2023-05-11 21:04:10 +02:00
|
|
|
_isSameBrowser() {
|
|
|
|
return BrowserTabsConnector.peerIsSameBrowser(this._peerId);
|
|
|
|
}
|
|
|
|
|
2023-09-13 19:00:48 +02:00
|
|
|
_isPaired() {
|
|
|
|
return !!this._roomIds['secret'];
|
|
|
|
}
|
|
|
|
|
|
|
|
_getPairSecret() {
|
|
|
|
return this._roomIds['secret'];
|
|
|
|
}
|
|
|
|
|
|
|
|
_getRoomTypes() {
|
|
|
|
return Object.keys(this._roomIds);
|
|
|
|
}
|
|
|
|
|
|
|
|
_updateRoomIds(roomType, roomId) {
|
2023-05-04 17:38:51 +02:00
|
|
|
// if peer is another browser tab, peer is not identifiable with roomSecret as browser tabs share all roomSecrets
|
2023-05-11 21:04:10 +02:00
|
|
|
// -> do not delete duplicates and do not regenerate room secrets
|
2023-09-13 19:00:48 +02:00
|
|
|
if (!this._isSameBrowser() && roomType === "secret" && this._isPaired() && this._getPairSecret() !== roomId) {
|
|
|
|
// multiple roomSecrets with same peer -> delete old roomSecret
|
|
|
|
PersistentStorage.deleteRoomSecret(this._getPairSecret())
|
|
|
|
.then(deletedRoomSecret => {
|
|
|
|
if (deletedRoomSecret) console.log("Successfully deleted duplicate room secret with same peer: ", deletedRoomSecret);
|
|
|
|
});
|
2023-05-04 17:38:51 +02:00
|
|
|
}
|
|
|
|
|
2023-09-13 19:00:48 +02:00
|
|
|
this._roomIds[roomType] = roomId;
|
2023-05-04 17:38:51 +02:00
|
|
|
|
2023-09-13 19:00:48 +02:00
|
|
|
if (!this._isSameBrowser() && roomType === "secret" && this._isPaired() && this._getPairSecret().length !== 256 && this._isCaller) {
|
|
|
|
// increase security by initiating the increase of the roomSecret length from 64 chars (<v1.7.0) to 256 chars (v1.7.0+)
|
2023-05-04 17:38:51 +02:00
|
|
|
console.log('RoomSecret is regenerated to increase security')
|
2023-09-13 19:00:48 +02:00
|
|
|
Events.fire('regenerate-room-secret', this._getPairSecret());
|
2023-05-04 17:38:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-13 19:00:48 +02:00
|
|
|
_removeRoomType(roomType) {
|
|
|
|
delete this._roomIds[roomType];
|
|
|
|
|
|
|
|
Events.fire('room-type-removed', {
|
|
|
|
peerId: this._peerId,
|
|
|
|
roomType: roomType
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-05-04 17:38:51 +02:00
|
|
|
_evaluateAutoAccept() {
|
2023-09-13 19:00:48 +02:00
|
|
|
if (!this._isPaired()) {
|
2023-05-04 17:38:51 +02:00
|
|
|
this._setAutoAccept(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-13 19:00:48 +02:00
|
|
|
PersistentStorage.getRoomSecretEntry(this._getPairSecret())
|
2023-05-04 17:38:51 +02:00
|
|
|
.then(roomSecretEntry => {
|
2023-09-13 19:00:48 +02:00
|
|
|
const autoAccept = roomSecretEntry
|
|
|
|
? roomSecretEntry.entry.auto_accept
|
|
|
|
: false;
|
2023-05-04 17:38:51 +02:00
|
|
|
this._setAutoAccept(autoAccept);
|
|
|
|
})
|
|
|
|
.catch(_ => {
|
|
|
|
this._setAutoAccept(false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_setAutoAccept(autoAccept) {
|
2023-09-13 19:00:48 +02:00
|
|
|
this._autoAccept = !this._isSameBrowser()
|
|
|
|
? autoAccept
|
|
|
|
: false;
|
2023-02-10 20:22:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getResizedImageDataUrl(file, width = undefined, height = undefined, quality = 0.7) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let image = new Image();
|
|
|
|
image.src = URL.createObjectURL(file);
|
|
|
|
image.onload = _ => {
|
|
|
|
let imageWidth = image.width;
|
|
|
|
let imageHeight = image.height;
|
|
|
|
let canvas = document.createElement('canvas');
|
|
|
|
|
|
|
|
// resize the canvas and draw the image data into it
|
|
|
|
if (width && height) {
|
|
|
|
canvas.width = width;
|
|
|
|
canvas.height = height;
|
|
|
|
} else if (width) {
|
|
|
|
canvas.width = width;
|
|
|
|
canvas.height = Math.floor(imageHeight * width / imageWidth)
|
|
|
|
} else if (height) {
|
|
|
|
canvas.width = Math.floor(imageWidth * height / imageHeight);
|
|
|
|
canvas.height = height;
|
|
|
|
} else {
|
|
|
|
canvas.width = imageWidth;
|
|
|
|
canvas.height = imageHeight
|
|
|
|
}
|
|
|
|
|
|
|
|
var ctx = canvas.getContext("2d");
|
|
|
|
ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
|
|
|
|
|
|
|
|
let dataUrl = canvas.toDataURL("image/jpeg", quality);
|
|
|
|
resolve(dataUrl);
|
|
|
|
}
|
|
|
|
image.onerror = _ => reject(`Could not create an image thumbnail from type ${file.type}`);
|
|
|
|
}).then(dataUrl => {
|
|
|
|
return dataUrl;
|
|
|
|
}).catch(e => console.error(e));
|
|
|
|
}
|
|
|
|
|
|
|
|
async requestFileTransfer(files) {
|
|
|
|
let header = [];
|
|
|
|
let totalSize = 0;
|
|
|
|
let imagesOnly = true
|
|
|
|
for (let i=0; i<files.length; i++) {
|
|
|
|
Events.fire('set-progress', {peerId: this._peerId, progress: 0.8*i/files.length, status: 'prepare'})
|
2023-05-04 17:38:51 +02:00
|
|
|
header.push({
|
|
|
|
name: files[i].name,
|
|
|
|
mime: files[i].type,
|
|
|
|
size: files[i].size
|
|
|
|
});
|
2023-02-10 20:22:36 +01:00
|
|
|
totalSize += files[i].size;
|
|
|
|
if (files[i].type.split('/')[0] !== 'image') imagesOnly = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Events.fire('set-progress', {peerId: this._peerId, progress: 0.8, status: 'prepare'})
|
|
|
|
|
|
|
|
let dataUrl = '';
|
|
|
|
if (files[0].type.split('/')[0] === 'image') {
|
|
|
|
dataUrl = await this.getResizedImageDataUrl(files[0], 400, null, 0.9);
|
|
|
|
}
|
|
|
|
|
|
|
|
Events.fire('set-progress', {peerId: this._peerId, progress: 1, status: 'prepare'})
|
|
|
|
|
|
|
|
this._filesRequested = files;
|
|
|
|
|
|
|
|
this.sendJSON({type: 'request',
|
|
|
|
header: header,
|
|
|
|
totalSize: totalSize,
|
|
|
|
imagesOnly: imagesOnly,
|
|
|
|
thumbnailDataUrl: dataUrl
|
|
|
|
});
|
|
|
|
Events.fire('set-progress', {peerId: this._peerId, progress: 0, status: 'wait'})
|
|
|
|
}
|
|
|
|
|
|
|
|
async sendFiles() {
|
|
|
|
for (let i=0; i<this._filesRequested.length; i++) {
|
|
|
|
this._filesQueue.push(this._filesRequested[i]);
|
|
|
|
}
|
|
|
|
this._filesRequested = null
|
|
|
|
if (this._busy) return;
|
|
|
|
this._dequeueFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
_dequeueFile() {
|
|
|
|
this._busy = true;
|
|
|
|
const file = this._filesQueue.shift();
|
|
|
|
this._sendFile(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
async _sendFile(file) {
|
|
|
|
this.sendJSON({
|
|
|
|
type: 'header',
|
|
|
|
size: file.size,
|
|
|
|
name: file.name,
|
|
|
|
mime: file.type
|
|
|
|
});
|
|
|
|
this._chunker = new FileChunker(file,
|
|
|
|
chunk => this._send(chunk),
|
|
|
|
offset => this._onPartitionEnd(offset));
|
|
|
|
this._chunker.nextPartition();
|
|
|
|
}
|
|
|
|
|
|
|
|
_onPartitionEnd(offset) {
|
|
|
|
this.sendJSON({ type: 'partition', offset: offset });
|
|
|
|
}
|
|
|
|
|
|
|
|
_onReceivedPartitionEnd(offset) {
|
|
|
|
this.sendJSON({ type: 'partition-received', offset: offset });
|
|
|
|
}
|
|
|
|
|
|
|
|
_sendNextPartition() {
|
|
|
|
if (!this._chunker || this._chunker.isFileEnd()) return;
|
|
|
|
this._chunker.nextPartition();
|
|
|
|
}
|
|
|
|
|
|
|
|
_sendProgress(progress) {
|
|
|
|
this.sendJSON({ type: 'progress', progress: progress });
|
|
|
|
}
|
|
|
|
|
2023-03-01 21:35:00 +01:00
|
|
|
_onMessage(message) {
|
2023-02-10 20:22:36 +01:00
|
|
|
if (typeof message !== 'string') {
|
|
|
|
this._onChunkReceived(message);
|
|
|
|
return;
|
|
|
|
}
|
2023-03-03 16:36:55 +01:00
|
|
|
const messageJSON = JSON.parse(message);
|
|
|
|
switch (messageJSON.type) {
|
2023-02-10 20:22:36 +01:00
|
|
|
case 'request':
|
2023-03-03 16:36:55 +01:00
|
|
|
this._onFilesTransferRequest(messageJSON);
|
2023-02-10 20:22:36 +01:00
|
|
|
break;
|
|
|
|
case 'header':
|
2023-03-13 12:15:55 +01:00
|
|
|
this._onFileHeader(messageJSON);
|
2023-02-10 20:22:36 +01:00
|
|
|
break;
|
|
|
|
case 'partition':
|
2023-03-03 16:36:55 +01:00
|
|
|
this._onReceivedPartitionEnd(messageJSON);
|
2023-02-10 20:22:36 +01:00
|
|
|
break;
|
|
|
|
case 'partition-received':
|
|
|
|
this._sendNextPartition();
|
|
|
|
break;
|
|
|
|
case 'progress':
|
2023-03-03 16:36:55 +01:00
|
|
|
this._onDownloadProgress(messageJSON.progress);
|
2023-02-10 20:22:36 +01:00
|
|
|
break;
|
|
|
|
case 'files-transfer-response':
|
2023-03-03 16:36:55 +01:00
|
|
|
this._onFileTransferRequestResponded(messageJSON);
|
2023-02-10 20:22:36 +01:00
|
|
|
break;
|
|
|
|
case 'file-transfer-complete':
|
|
|
|
this._onFileTransferCompleted();
|
|
|
|
break;
|
|
|
|
case 'message-transfer-complete':
|
|
|
|
this._onMessageTransferCompleted();
|
|
|
|
break;
|
|
|
|
case 'text':
|
2023-03-03 16:36:55 +01:00
|
|
|
this._onTextReceived(messageJSON);
|
2023-02-10 20:22:36 +01:00
|
|
|
break;
|
2023-03-01 21:35:00 +01:00
|
|
|
case 'display-name-changed':
|
2023-03-03 17:40:10 +01:00
|
|
|
this._onDisplayNameChanged(messageJSON);
|
2023-03-01 21:35:00 +01:00
|
|
|
break;
|
2023-02-10 20:22:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_onFilesTransferRequest(request) {
|
|
|
|
if (this._requestPending) {
|
2023-05-04 17:38:51 +02:00
|
|
|
// Only accept one request at a time per peer
|
2023-02-10 20:22:36 +01:00
|
|
|
this.sendJSON({type: 'files-transfer-response', accepted: false});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (window.iOS && request.totalSize >= 200*1024*1024) {
|
|
|
|
// iOS Safari can only put 400MB at once to memory.
|
|
|
|
// Request to send them in chunks of 200MB instead:
|
|
|
|
this.sendJSON({type: 'files-transfer-response', accepted: false, reason: 'ios-memory-limit'});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._requestPending = request;
|
2023-05-04 17:38:51 +02:00
|
|
|
|
|
|
|
if (this._autoAccept) {
|
|
|
|
// auto accept if set via Edit Paired Devices Dialog
|
|
|
|
this._respondToFileTransferRequest(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// default behavior: show user transfer request
|
2023-02-10 20:22:36 +01:00
|
|
|
Events.fire('files-transfer-request', {
|
|
|
|
request: request,
|
|
|
|
peerId: this._peerId
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_respondToFileTransferRequest(accepted) {
|
|
|
|
this.sendJSON({type: 'files-transfer-response', accepted: accepted});
|
|
|
|
if (accepted) {
|
|
|
|
this._requestAccepted = this._requestPending;
|
|
|
|
this._totalBytesReceived = 0;
|
|
|
|
this._busy = true;
|
|
|
|
this._filesReceived = [];
|
|
|
|
}
|
|
|
|
this._requestPending = null;
|
|
|
|
}
|
|
|
|
|
2023-03-13 12:15:55 +01:00
|
|
|
_onFileHeader(header) {
|
2023-03-13 14:21:26 +01:00
|
|
|
if (this._requestAccepted && this._requestAccepted.header.length) {
|
2023-02-10 20:22:36 +01:00
|
|
|
this._lastProgress = 0;
|
|
|
|
this._digester = new FileDigester({size: header.size, name: header.name, mime: header.mime},
|
|
|
|
this._requestAccepted.totalSize,
|
|
|
|
this._totalBytesReceived,
|
|
|
|
fileBlob => this._onFileReceived(fileBlob)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_abortTransfer() {
|
|
|
|
Events.fire('set-progress', {peerId: this._peerId, progress: 1, status: 'wait'});
|
2023-07-06 21:29:36 +02:00
|
|
|
Events.fire('notify-user', Localization.getTranslation("notifications.files-incorrect"));
|
2023-02-10 20:22:36 +01:00
|
|
|
this._filesReceived = [];
|
|
|
|
this._requestAccepted = null;
|
|
|
|
this._digester = null;
|
|
|
|
throw new Error("Received files differ from requested files. Abort!");
|
|
|
|
}
|
|
|
|
|
|
|
|
_onChunkReceived(chunk) {
|
|
|
|
if(!this._digester || !(chunk.byteLength || chunk.size)) return;
|
|
|
|
|
|
|
|
this._digester.unchunk(chunk);
|
|
|
|
const progress = this._digester.progress;
|
|
|
|
|
|
|
|
if (progress > 1) {
|
|
|
|
this._abortTransfer();
|
|
|
|
}
|
|
|
|
|
|
|
|
this._onDownloadProgress(progress);
|
|
|
|
|
|
|
|
// occasionally notify sender about our progress
|
|
|
|
if (progress - this._lastProgress < 0.005 && progress !== 1) return;
|
|
|
|
this._lastProgress = progress;
|
|
|
|
this._sendProgress(progress);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onDownloadProgress(progress) {
|
|
|
|
Events.fire('set-progress', {peerId: this._peerId, progress: progress, status: 'transfer'});
|
|
|
|
}
|
|
|
|
|
|
|
|
async _onFileReceived(fileBlob) {
|
|
|
|
const acceptedHeader = this._requestAccepted.header.shift();
|
|
|
|
this._totalBytesReceived += fileBlob.size;
|
|
|
|
|
|
|
|
this.sendJSON({type: 'file-transfer-complete'});
|
|
|
|
|
|
|
|
const sameSize = fileBlob.size === acceptedHeader.size;
|
|
|
|
const sameName = fileBlob.name === acceptedHeader.name
|
|
|
|
if (!sameSize || !sameName) {
|
|
|
|
this._abortTransfer();
|
|
|
|
}
|
|
|
|
|
2023-07-06 21:29:36 +02:00
|
|
|
// include for compatibility with 'Snapdrop & PairDrop for Android' app
|
2023-03-13 12:15:55 +01:00
|
|
|
Events.fire('file-received', fileBlob);
|
|
|
|
|
2023-02-10 20:22:36 +01:00
|
|
|
this._filesReceived.push(fileBlob);
|
|
|
|
if (!this._requestAccepted.header.length) {
|
|
|
|
this._busy = false;
|
|
|
|
Events.fire('set-progress', {peerId: this._peerId, progress: 0, status: 'process'});
|
2023-09-13 19:00:48 +02:00
|
|
|
Events.fire('files-received', {peerId: this._peerId, files: this._filesReceived, imagesOnly: this._requestAccepted.imagesOnly, totalSize: this._requestAccepted.totalSize});
|
2023-02-10 20:22:36 +01:00
|
|
|
this._filesReceived = [];
|
|
|
|
this._requestAccepted = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_onFileTransferCompleted() {
|
|
|
|
this._chunker = null;
|
|
|
|
if (!this._filesQueue.length) {
|
|
|
|
this._busy = false;
|
2023-07-06 21:29:36 +02:00
|
|
|
Events.fire('notify-user', Localization.getTranslation("notifications.file-transfer-completed"));
|
|
|
|
Events.fire('files-sent'); // used by 'Snapdrop & PairDrop for Android' app
|
2023-02-10 20:22:36 +01:00
|
|
|
} else {
|
|
|
|
this._dequeueFile();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_onFileTransferRequestResponded(message) {
|
|
|
|
if (!message.accepted) {
|
|
|
|
Events.fire('set-progress', {peerId: this._peerId, progress: 1, status: 'wait'});
|
|
|
|
this._filesRequested = null;
|
|
|
|
if (message.reason === 'ios-memory-limit') {
|
2023-07-06 21:29:36 +02:00
|
|
|
Events.fire('notify-user', Localization.getTranslation("notifications.ios-memory-limit"));
|
2023-02-10 20:22:36 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Events.fire('file-transfer-accepted');
|
|
|
|
Events.fire('set-progress', {peerId: this._peerId, progress: 0, status: 'transfer'});
|
|
|
|
this.sendFiles();
|
|
|
|
}
|
|
|
|
|
|
|
|
_onMessageTransferCompleted() {
|
2023-07-06 21:29:36 +02:00
|
|
|
Events.fire('notify-user', Localization.getTranslation("notifications.message-transfer-completed"));
|
2023-02-10 20:22:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sendText(text) {
|
|
|
|
const unescaped = btoa(unescape(encodeURIComponent(text)));
|
|
|
|
this.sendJSON({ type: 'text', text: unescaped });
|
|
|
|
}
|
|
|
|
|
|
|
|
_onTextReceived(message) {
|
|
|
|
if (!message.text) return;
|
|
|
|
const escaped = decodeURIComponent(escape(atob(message.text)));
|
|
|
|
Events.fire('text-received', { text: escaped, peerId: this._peerId });
|
|
|
|
this.sendJSON({ type: 'message-transfer-complete' });
|
|
|
|
}
|
2023-03-01 21:35:00 +01:00
|
|
|
|
|
|
|
_onDisplayNameChanged(message) {
|
2023-05-04 17:38:51 +02:00
|
|
|
const displayNameHasChanged = this._displayName !== message.displayName
|
|
|
|
|
|
|
|
if (message.displayName && displayNameHasChanged) {
|
|
|
|
this._displayName = message.displayName;
|
|
|
|
}
|
|
|
|
|
2023-03-01 21:35:00 +01:00
|
|
|
Events.fire('peer-display-name-changed', {peerId: this._peerId, displayName: message.displayName});
|
2023-05-04 17:38:51 +02:00
|
|
|
|
|
|
|
if (!displayNameHasChanged) return;
|
|
|
|
Events.fire('notify-peer-display-name-changed', this._peerId);
|
2023-03-01 21:35:00 +01:00
|
|
|
}
|
2023-02-10 20:22:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class RTCPeer extends Peer {
|
|
|
|
|
2023-09-13 19:00:48 +02:00
|
|
|
constructor(serverConnection, isCaller, peerId, roomType, roomId) {
|
|
|
|
super(serverConnection, isCaller, peerId, roomType, roomId);
|
2023-03-03 13:09:59 +01:00
|
|
|
this.rtcSupported = true;
|
2023-05-04 17:38:51 +02:00
|
|
|
if (!this._isCaller) return; // we will listen for a caller
|
2023-05-11 21:04:10 +02:00
|
|
|
this._connect();
|
2023-02-10 20:22:36 +01:00
|
|
|
}
|
|
|
|
|
2023-05-11 21:04:10 +02:00
|
|
|
_connect() {
|
|
|
|
if (!this._conn || this._conn.signalingState === "closed") this._openConnection();
|
2023-02-10 20:22:36 +01:00
|
|
|
|
2023-05-04 17:38:51 +02:00
|
|
|
if (this._isCaller) {
|
2023-02-10 20:22:36 +01:00
|
|
|
this._openChannel();
|
|
|
|
} else {
|
|
|
|
this._conn.ondatachannel = e => this._onChannelOpened(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-11 21:04:10 +02:00
|
|
|
_openConnection() {
|
2023-02-24 18:08:48 +01:00
|
|
|
this._conn = new RTCPeerConnection(window.rtcConfig);
|
2023-02-10 20:22:36 +01:00
|
|
|
this._conn.onicecandidate = e => this._onIceCandidate(e);
|
2023-03-10 22:21:19 +01:00
|
|
|
this._conn.onicecandidateerror = e => this._onError(e);
|
2023-02-10 20:22:36 +01:00
|
|
|
this._conn.onconnectionstatechange = _ => this._onConnectionStateChange();
|
|
|
|
this._conn.oniceconnectionstatechange = e => this._onIceConnectionStateChange(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
_openChannel() {
|
2023-02-10 23:41:04 +01:00
|
|
|
if (!this._conn) return;
|
2023-09-13 19:00:48 +02:00
|
|
|
|
2023-02-10 20:22:36 +01:00
|
|
|
const channel = this._conn.createDataChannel('data-channel', {
|
|
|
|
ordered: true,
|
|
|
|
reliable: true // Obsolete. See https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel/reliable
|
|
|
|
});
|
|
|
|
channel.onopen = e => this._onChannelOpened(e);
|
2023-02-10 23:41:04 +01:00
|
|
|
channel.onerror = e => this._onError(e);
|
2023-09-13 19:00:48 +02:00
|
|
|
|
|
|
|
this._conn.createOffer()
|
|
|
|
.then(d => this._onDescription(d))
|
|
|
|
.catch(e => this._onError(e));
|
2023-02-10 20:22:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_onDescription(description) {
|
|
|
|
// description.sdp = description.sdp.replace('b=AS:30', 'b=AS:1638400');
|
|
|
|
this._conn.setLocalDescription(description)
|
|
|
|
.then(_ => this._sendSignal({ sdp: description }))
|
|
|
|
.catch(e => this._onError(e));
|
|
|
|
}
|
|
|
|
|
|
|
|
_onIceCandidate(event) {
|
|
|
|
if (!event.candidate) return;
|
|
|
|
this._sendSignal({ ice: event.candidate });
|
|
|
|
}
|
|
|
|
|
|
|
|
onServerMessage(message) {
|
2023-05-11 21:04:10 +02:00
|
|
|
if (!this._conn) this._connect();
|
2023-02-10 20:22:36 +01:00
|
|
|
|
|
|
|
if (message.sdp) {
|
|
|
|
this._conn.setRemoteDescription(message.sdp)
|
|
|
|
.then( _ => {
|
2023-02-10 23:41:04 +01:00
|
|
|
if (message.sdp.type === 'offer') {
|
|
|
|
return this._conn.createAnswer()
|
|
|
|
.then(d => this._onDescription(d));
|
|
|
|
}
|
2023-02-10 20:22:36 +01:00
|
|
|
})
|
|
|
|
.catch(e => this._onError(e));
|
|
|
|
} else if (message.ice) {
|
2023-02-10 23:41:04 +01:00
|
|
|
this._conn.addIceCandidate(new RTCIceCandidate(message.ice))
|
|
|
|
.catch(e => this._onError(e));
|
2023-02-10 20:22:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_onChannelOpened(event) {
|
|
|
|
console.log('RTC: channel opened with', this._peerId);
|
|
|
|
const channel = event.channel || event.target;
|
|
|
|
channel.binaryType = 'arraybuffer';
|
|
|
|
channel.onmessage = e => this._onMessage(e.data);
|
2023-03-06 03:36:46 +01:00
|
|
|
channel.onclose = _ => this._onChannelClosed();
|
2023-02-10 20:22:36 +01:00
|
|
|
this._channel = channel;
|
2023-03-01 21:35:00 +01:00
|
|
|
Events.on('beforeunload', e => this._onBeforeUnload(e));
|
|
|
|
Events.on('pagehide', _ => this._onPageHide());
|
|
|
|
Events.fire('peer-connected', {peerId: this._peerId, connectionHash: this.getConnectionHash()});
|
2023-02-10 20:22:36 +01:00
|
|
|
}
|
|
|
|
|
2023-03-03 12:38:34 +01:00
|
|
|
_onMessage(message) {
|
|
|
|
if (typeof message === 'string') {
|
2023-03-03 16:36:55 +01:00
|
|
|
console.log('RTC:', JSON.parse(message));
|
2023-03-03 12:38:34 +01:00
|
|
|
}
|
|
|
|
super._onMessage(message);
|
|
|
|
}
|
|
|
|
|
2023-02-16 02:19:14 +01:00
|
|
|
getConnectionHash() {
|
|
|
|
const localDescriptionLines = this._conn.localDescription.sdp.split("\r\n");
|
|
|
|
const remoteDescriptionLines = this._conn.remoteDescription.sdp.split("\r\n");
|
|
|
|
let localConnectionFingerprint, remoteConnectionFingerprint;
|
|
|
|
for (let i=0; i<localDescriptionLines.length; i++) {
|
|
|
|
if (localDescriptionLines[i].startsWith("a=fingerprint:")) {
|
|
|
|
localConnectionFingerprint = localDescriptionLines[i].substring(14);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (let i=0; i<remoteDescriptionLines.length; i++) {
|
|
|
|
if (remoteDescriptionLines[i].startsWith("a=fingerprint:")) {
|
|
|
|
remoteConnectionFingerprint = remoteDescriptionLines[i].substring(14);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const combinedFingerprints = this._isCaller
|
|
|
|
? localConnectionFingerprint + remoteConnectionFingerprint
|
|
|
|
: remoteConnectionFingerprint + localConnectionFingerprint;
|
|
|
|
let hash = cyrb53(combinedFingerprints).toString();
|
|
|
|
while (hash.length < 16) {
|
|
|
|
hash = "0" + hash;
|
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2023-02-10 23:41:04 +01:00
|
|
|
_onBeforeUnload(e) {
|
|
|
|
if (this._busy) {
|
|
|
|
e.preventDefault();
|
2023-07-06 21:29:36 +02:00
|
|
|
return Localization.getTranslation("notifications.unfinished-transfers-warning");
|
2023-02-10 23:41:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-01 21:35:00 +01:00
|
|
|
_onPageHide() {
|
|
|
|
this._disconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
_disconnect() {
|
|
|
|
if (this._conn && this._channel) {
|
|
|
|
this._channel.onclose = null;
|
|
|
|
this._channel.close();
|
|
|
|
}
|
|
|
|
Events.fire('peer-disconnected', this._peerId);
|
2023-02-10 23:41:04 +01:00
|
|
|
}
|
|
|
|
|
2023-02-10 20:22:36 +01:00
|
|
|
_onChannelClosed() {
|
|
|
|
console.log('RTC: channel closed', this._peerId);
|
|
|
|
Events.fire('peer-disconnected', this._peerId);
|
|
|
|
if (!this._isCaller) return;
|
2023-05-11 21:04:10 +02:00
|
|
|
this._connect(); // reopen the channel
|
2023-02-10 20:22:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_onConnectionStateChange() {
|
|
|
|
console.log('RTC: state changed:', this._conn.connectionState);
|
|
|
|
switch (this._conn.connectionState) {
|
|
|
|
case 'disconnected':
|
2023-03-01 21:35:00 +01:00
|
|
|
Events.fire('peer-disconnected', this._peerId);
|
2023-02-10 20:22:36 +01:00
|
|
|
this._onError('rtc connection disconnected');
|
|
|
|
break;
|
|
|
|
case 'failed':
|
2023-03-01 21:35:00 +01:00
|
|
|
Events.fire('peer-disconnected', this._peerId);
|
2023-02-10 20:22:36 +01:00
|
|
|
this._onError('rtc connection failed');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_onIceConnectionStateChange() {
|
|
|
|
switch (this._conn.iceConnectionState) {
|
|
|
|
case 'failed':
|
|
|
|
this._onError('ICE Gathering failed');
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.log('ICE Gathering', this._conn.iceConnectionState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_onError(error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
_send(message) {
|
|
|
|
if (!this._channel) this.refresh();
|
|
|
|
this._channel.send(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
_sendSignal(signal) {
|
|
|
|
signal.type = 'signal';
|
|
|
|
signal.to = this._peerId;
|
2023-09-13 19:00:48 +02:00
|
|
|
signal.roomType = this._getRoomTypes()[0];
|
|
|
|
signal.roomId = this._roomIds[this._getRoomTypes()[0]];
|
2023-02-10 20:22:36 +01:00
|
|
|
this._server.send(signal);
|
|
|
|
}
|
|
|
|
|
|
|
|
refresh() {
|
|
|
|
// check if channel is open. otherwise create one
|
|
|
|
if (this._isConnected() || this._isConnecting()) return;
|
2023-05-04 17:38:51 +02:00
|
|
|
|
|
|
|
// only reconnect if peer is caller
|
|
|
|
if (!this._isCaller) return;
|
|
|
|
|
2023-05-11 21:04:10 +02:00
|
|
|
this._connect();
|
2023-02-10 20:22:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_isConnected() {
|
|
|
|
return this._channel && this._channel.readyState === 'open';
|
|
|
|
}
|
|
|
|
|
|
|
|
_isConnecting() {
|
|
|
|
return this._channel && this._channel.readyState === 'connecting';
|
|
|
|
}
|
2023-03-06 15:33:22 +01:00
|
|
|
|
|
|
|
sendDisplayName(displayName) {
|
|
|
|
if (!this._isConnected()) return;
|
|
|
|
super.sendDisplayName(displayName);
|
|
|
|
}
|
2023-02-10 20:22:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class WSPeer extends Peer {
|
|
|
|
|
2023-05-11 21:04:10 +02:00
|
|
|
constructor(serverConnection, isCaller, peerId, roomType, roomSecret) {
|
|
|
|
super(serverConnection, isCaller, peerId, roomType, roomSecret);
|
2023-03-03 13:09:59 +01:00
|
|
|
this.rtcSupported = false;
|
2023-05-04 17:38:51 +02:00
|
|
|
if (!this._isCaller) return; // we will listen for a caller
|
2023-02-10 20:22:36 +01:00
|
|
|
this._sendSignal();
|
|
|
|
}
|
|
|
|
|
|
|
|
_send(chunk) {
|
|
|
|
this.sendJSON({
|
|
|
|
type: 'ws-chunk',
|
|
|
|
chunk: arrayBufferToBase64(chunk)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
sendJSON(message) {
|
|
|
|
message.to = this._peerId;
|
2023-09-13 19:00:48 +02:00
|
|
|
message.roomType = this._getRoomTypes()[0];
|
|
|
|
message.roomId = this._roomIds[this._getRoomTypes()[0]];
|
2023-02-10 20:22:36 +01:00
|
|
|
this._server.send(message);
|
|
|
|
}
|
|
|
|
|
2023-03-03 12:40:41 +01:00
|
|
|
_sendSignal(connected = false) {
|
|
|
|
this.sendJSON({type: 'signal', connected: connected});
|
2023-02-10 20:22:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
onServerMessage(message) {
|
|
|
|
this._peerId = message.sender.id;
|
2023-03-01 21:35:00 +01:00
|
|
|
Events.fire('peer-connected', {peerId: message.sender.id, connectionHash: this.getConnectionHash()})
|
2023-03-03 12:40:41 +01:00
|
|
|
if (message.connected) return;
|
|
|
|
this._sendSignal(true);
|
2023-02-10 20:22:36 +01:00
|
|
|
}
|
2023-02-16 02:19:14 +01:00
|
|
|
|
|
|
|
getConnectionHash() {
|
|
|
|
// Todo: implement SubtleCrypto asymmetric encryption and create connectionHash from public keys
|
|
|
|
return "";
|
|
|
|
}
|
2023-02-10 20:22:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class PeersManager {
|
|
|
|
|
|
|
|
constructor(serverConnection) {
|
|
|
|
this.peers = {};
|
|
|
|
this._server = serverConnection;
|
|
|
|
Events.on('signal', e => this._onMessage(e.detail));
|
|
|
|
Events.on('peers', e => this._onPeers(e.detail));
|
|
|
|
Events.on('files-selected', e => this._onFilesSelected(e.detail));
|
|
|
|
Events.on('respond-to-files-transfer-request', e => this._onRespondToFileTransferRequest(e.detail))
|
|
|
|
Events.on('send-text', e => this._onSendText(e.detail));
|
|
|
|
Events.on('peer-left', e => this._onPeerLeft(e.detail));
|
2023-05-04 17:38:51 +02:00
|
|
|
Events.on('peer-joined', e => this._onPeerJoined(e.detail));
|
2023-03-01 21:35:00 +01:00
|
|
|
Events.on('peer-connected', e => this._onPeerConnected(e.detail.peerId));
|
2023-02-10 20:22:36 +01:00
|
|
|
Events.on('peer-disconnected', e => this._onPeerDisconnected(e.detail));
|
2023-09-13 19:00:48 +02:00
|
|
|
|
|
|
|
// this device closes connection
|
|
|
|
Events.on('room-secrets-deleted', e => this._onRoomSecretsDeleted(e.detail));
|
|
|
|
Events.on('leave-public-room', e => this._onLeavePublicRoom(e.detail));
|
|
|
|
|
|
|
|
// peer closes connection
|
2023-02-10 20:22:36 +01:00
|
|
|
Events.on('secret-room-deleted', e => this._onSecretRoomDeleted(e.detail));
|
2023-09-13 19:00:48 +02:00
|
|
|
|
2023-05-09 03:17:08 +02:00
|
|
|
Events.on('room-secret-regenerated', e => this._onRoomSecretRegenerated(e.detail));
|
2023-10-31 18:32:23 +01:00
|
|
|
Events.on('display-name', e => this._onDisplayName(e.detail.displayName));
|
2023-03-01 21:35:00 +01:00
|
|
|
Events.on('self-display-name-changed', e => this._notifyPeersDisplayNameChanged(e.detail));
|
2023-05-04 17:38:51 +02:00
|
|
|
Events.on('notify-peer-display-name-changed', e => this._notifyPeerDisplayNameChanged(e.detail));
|
|
|
|
Events.on('auto-accept-updated', e => this._onAutoAcceptUpdated(e.detail.roomSecret, e.detail.autoAccept));
|
2023-03-03 13:09:59 +01:00
|
|
|
Events.on('ws-disconnected', _ => this._onWsDisconnected());
|
2023-02-10 20:22:36 +01:00
|
|
|
Events.on('ws-relay', e => this._onWsRelay(e.detail));
|
|
|
|
}
|
|
|
|
|
|
|
|
_onMessage(message) {
|
2023-05-04 17:38:51 +02:00
|
|
|
const peerId = message.sender.id;
|
|
|
|
this.peers[peerId].onServerMessage(message);
|
|
|
|
}
|
|
|
|
|
2023-09-13 19:00:48 +02:00
|
|
|
_refreshPeer(peer, roomType, roomId) {
|
2023-05-11 21:04:10 +02:00
|
|
|
if (!peer) return false;
|
2023-05-04 17:38:51 +02:00
|
|
|
|
2023-09-13 19:00:48 +02:00
|
|
|
const roomTypesDiffer = Object.keys(peer._roomIds)[0] !== roomType;
|
|
|
|
const roomIdsDiffer = peer._roomIds[roomType] !== roomId;
|
2023-05-04 17:38:51 +02:00
|
|
|
|
2023-09-13 19:00:48 +02:00
|
|
|
// if roomType or roomId for roomType differs peer is already connected
|
|
|
|
// -> only update roomSecret and reevaluate auto accept
|
|
|
|
if (roomTypesDiffer || roomIdsDiffer) {
|
|
|
|
peer._updateRoomIds(roomType, roomId);
|
2023-05-11 21:04:10 +02:00
|
|
|
peer._evaluateAutoAccept();
|
2023-05-04 17:38:51 +02:00
|
|
|
|
2023-05-11 21:04:10 +02:00
|
|
|
return true;
|
|
|
|
}
|
2023-05-04 17:38:51 +02:00
|
|
|
|
2023-05-11 21:04:10 +02:00
|
|
|
return true;
|
2023-05-04 17:38:51 +02:00
|
|
|
}
|
|
|
|
|
2023-09-13 19:00:48 +02:00
|
|
|
_createOrRefreshPeer(isCaller, peerId, roomType, roomId, rtcSupported) {
|
2023-05-11 21:04:10 +02:00
|
|
|
const peer = this.peers[peerId];
|
|
|
|
if (peer) {
|
2023-09-13 19:00:48 +02:00
|
|
|
this._refreshPeer(peer, roomType, roomId);
|
2023-05-11 21:04:10 +02:00
|
|
|
return;
|
|
|
|
}
|
2023-05-04 17:38:51 +02:00
|
|
|
|
2023-05-11 21:04:10 +02:00
|
|
|
if (window.isRtcSupported && rtcSupported) {
|
2023-09-13 19:00:48 +02:00
|
|
|
this.peers[peerId] = new RTCPeer(this._server,isCaller, peerId, roomType, roomId);
|
2023-05-04 17:38:51 +02:00
|
|
|
} else {
|
2023-09-13 19:00:48 +02:00
|
|
|
this.peers[peerId] = new WSPeer(this._server, isCaller, peerId, roomType, roomId);
|
2023-02-10 20:22:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-11 21:04:10 +02:00
|
|
|
_onPeerJoined(message) {
|
2023-09-13 19:00:48 +02:00
|
|
|
this._createOrRefreshPeer(false, message.peer.id, message.roomType, message.roomId, message.peer.rtcSupported);
|
2023-02-10 20:22:36 +01:00
|
|
|
}
|
|
|
|
|
2023-05-04 17:38:51 +02:00
|
|
|
_onPeers(message) {
|
2023-05-11 21:04:10 +02:00
|
|
|
message.peers.forEach(peer => {
|
2023-09-13 19:00:48 +02:00
|
|
|
this._createOrRefreshPeer(true, peer.id, message.roomType, message.roomId, peer.rtcSupported);
|
2023-02-10 20:22:36 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-05-11 21:04:10 +02:00
|
|
|
_onWsRelay(message) {
|
|
|
|
const messageJSON = JSON.parse(message);
|
|
|
|
if (messageJSON.type === 'ws-chunk') message = base64ToArrayBuffer(messageJSON.chunk);
|
|
|
|
this.peers[messageJSON.sender.id]._onMessage(message);
|
|
|
|
}
|
|
|
|
|
2023-02-10 20:22:36 +01:00
|
|
|
_onRespondToFileTransferRequest(detail) {
|
|
|
|
this.peers[detail.to]._respondToFileTransferRequest(detail.accepted);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onFilesSelected(message) {
|
|
|
|
let inputFiles = Array.from(message.files);
|
|
|
|
delete message.files;
|
|
|
|
let files = [];
|
|
|
|
const l = inputFiles.length;
|
|
|
|
for (let i=0; i<l; i++) {
|
|
|
|
// when filetype is empty guess via suffix
|
|
|
|
const inputFile = inputFiles.shift();
|
|
|
|
const file = inputFile.type
|
|
|
|
? inputFile
|
|
|
|
: new File([inputFile], inputFile.name, {type: mime.getMimeByFilename(inputFile.name)});
|
|
|
|
files.push(file)
|
|
|
|
}
|
|
|
|
this.peers[message.to].requestFileTransfer(files);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onSendText(message) {
|
|
|
|
this.peers[message.to].sendText(message.text);
|
|
|
|
}
|
|
|
|
|
2023-05-04 17:38:51 +02:00
|
|
|
_onPeerLeft(message) {
|
|
|
|
if (this.peers[message.peerId] && (!this.peers[message.peerId].rtcSupported || !window.isRtcSupported)) {
|
|
|
|
console.log('WSPeer left:', message.peerId);
|
|
|
|
}
|
|
|
|
if (message.disconnect === true) {
|
2023-02-10 23:47:39 +01:00
|
|
|
// if user actively disconnected from PairDrop server, disconnect all peer to peer connections immediately
|
2023-09-13 19:00:48 +02:00
|
|
|
this._disconnectOrRemoveRoomTypeByPeerId(message.peerId, message.roomType);
|
2023-05-11 21:04:10 +02:00
|
|
|
|
|
|
|
// If no peers are connected anymore, we can safely assume that no other tab on the same browser is connected:
|
|
|
|
// Tidy up peerIds in localStorage
|
|
|
|
if (Object.keys(this.peers).length === 0) {
|
|
|
|
BrowserTabsConnector.removeOtherPeerIdsFromLocalStorage().then(peerIds => {
|
|
|
|
if (!peerIds) return;
|
|
|
|
console.log("successfully removed other peerIds from localStorage");
|
|
|
|
});
|
|
|
|
}
|
2023-02-10 23:47:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-01 21:35:00 +01:00
|
|
|
_onPeerConnected(peerId) {
|
|
|
|
this._notifyPeerDisplayNameChanged(peerId);
|
|
|
|
}
|
|
|
|
|
2023-03-03 13:09:59 +01:00
|
|
|
_onWsDisconnected() {
|
|
|
|
for (const peerId in this.peers) {
|
|
|
|
if (this.peers[peerId] && (!this.peers[peerId].rtcSupported || !window.isRtcSupported)) {
|
|
|
|
Events.fire('peer-disconnected', peerId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-10 20:22:36 +01:00
|
|
|
_onPeerDisconnected(peerId) {
|
|
|
|
const peer = this.peers[peerId];
|
|
|
|
delete this.peers[peerId];
|
2023-02-10 23:41:04 +01:00
|
|
|
if (!peer || !peer._conn) return;
|
|
|
|
if (peer._channel) peer._channel.onclose = null;
|
|
|
|
peer._conn.close();
|
|
|
|
peer._busy = false;
|
2023-09-13 19:00:48 +02:00
|
|
|
peer._roomIds = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
_onRoomSecretsDeleted(roomSecrets) {
|
|
|
|
for (let i=0; i<roomSecrets.length; i++) {
|
|
|
|
this._disconnectOrRemoveRoomTypeByRoomId('secret', roomSecrets[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_onLeavePublicRoom(publicRoomId) {
|
|
|
|
this._disconnectOrRemoveRoomTypeByRoomId('public-id', publicRoomId);
|
2023-02-10 20:22:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_onSecretRoomDeleted(roomSecret) {
|
2023-09-13 19:00:48 +02:00
|
|
|
this._disconnectOrRemoveRoomTypeByRoomId('secret', roomSecret);
|
|
|
|
}
|
|
|
|
|
|
|
|
_disconnectOrRemoveRoomTypeByRoomId(roomType, roomId) {
|
|
|
|
const peerIds = this._getPeerIdsFromRoomId(roomId);
|
|
|
|
|
|
|
|
if (!peerIds.length) return;
|
|
|
|
|
|
|
|
for (let i=0; i<peerIds.length; i++) {
|
|
|
|
this._disconnectOrRemoveRoomTypeByPeerId(peerIds[i], roomType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_disconnectOrRemoveRoomTypeByPeerId(peerId, roomType) {
|
|
|
|
const peer = this.peers[peerId];
|
|
|
|
|
|
|
|
if (!peer) return;
|
|
|
|
|
|
|
|
if (peer._getRoomTypes().length > 1) {
|
|
|
|
peer._removeRoomType(roomType);
|
|
|
|
} else {
|
|
|
|
Events.fire('peer-disconnected', peerId);
|
2023-02-10 20:22:36 +01:00
|
|
|
}
|
|
|
|
}
|
2023-03-01 21:35:00 +01:00
|
|
|
|
2023-05-09 03:17:08 +02:00
|
|
|
_onRoomSecretRegenerated(message) {
|
|
|
|
PersistentStorage.updateRoomSecret(message.oldRoomSecret, message.newRoomSecret).then(_ => {
|
|
|
|
console.log("successfully regenerated room secret");
|
|
|
|
Events.fire("room-secrets", [message.newRoomSecret]);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-03-01 21:35:00 +01:00
|
|
|
_notifyPeersDisplayNameChanged(newDisplayName) {
|
|
|
|
this._displayName = newDisplayName ? newDisplayName : this._originalDisplayName;
|
|
|
|
for (const peerId in this.peers) {
|
|
|
|
this._notifyPeerDisplayNameChanged(peerId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_notifyPeerDisplayNameChanged(peerId) {
|
|
|
|
const peer = this.peers[peerId];
|
2023-03-06 03:36:46 +01:00
|
|
|
if (!peer) return;
|
2023-03-06 11:24:19 +01:00
|
|
|
this.peers[peerId].sendDisplayName(this._displayName);
|
2023-03-01 21:35:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_onDisplayName(displayName) {
|
|
|
|
this._originalDisplayName = displayName;
|
2023-05-04 17:38:51 +02:00
|
|
|
// if the displayName has not been changed (yet) set the displayName to the original displayName
|
|
|
|
if (!this._displayName) this._displayName = displayName;
|
|
|
|
}
|
|
|
|
|
|
|
|
_onAutoAcceptUpdated(roomSecret, autoAccept) {
|
2023-09-13 19:00:48 +02:00
|
|
|
const peerId = this._getPeerIdsFromRoomId(roomSecret)[0];
|
|
|
|
|
2023-05-04 17:38:51 +02:00
|
|
|
if (!peerId) return;
|
2023-09-13 19:00:48 +02:00
|
|
|
|
2023-05-04 17:38:51 +02:00
|
|
|
this.peers[peerId]._setAutoAccept(autoAccept);
|
|
|
|
}
|
|
|
|
|
2023-09-13 19:00:48 +02:00
|
|
|
_getPeerIdsFromRoomId(roomId) {
|
|
|
|
if (!roomId) return [];
|
|
|
|
|
|
|
|
let peerIds = []
|
2023-05-04 17:38:51 +02:00
|
|
|
for (const peerId in this.peers) {
|
|
|
|
const peer = this.peers[peerId];
|
2023-09-13 19:00:48 +02:00
|
|
|
|
|
|
|
// peer must have same roomId.
|
|
|
|
if (Object.values(peer._roomIds).includes(roomId)) {
|
|
|
|
peerIds.push(peer._peerId);
|
2023-05-04 17:38:51 +02:00
|
|
|
}
|
|
|
|
}
|
2023-09-13 19:00:48 +02:00
|
|
|
return peerIds;
|
2023-03-01 21:35:00 +01:00
|
|
|
}
|
2023-02-10 20:22:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class FileChunker {
|
|
|
|
|
|
|
|
constructor(file, onChunk, onPartitionEnd) {
|
|
|
|
this._chunkSize = 64000; // 64 KB
|
|
|
|
this._maxPartitionSize = 1e6; // 1 MB
|
|
|
|
this._offset = 0;
|
|
|
|
this._partitionSize = 0;
|
|
|
|
this._file = file;
|
|
|
|
this._onChunk = onChunk;
|
|
|
|
this._onPartitionEnd = onPartitionEnd;
|
|
|
|
this._reader = new FileReader();
|
|
|
|
this._reader.addEventListener('load', e => this._onChunkRead(e.target.result));
|
|
|
|
}
|
|
|
|
|
|
|
|
nextPartition() {
|
|
|
|
this._partitionSize = 0;
|
|
|
|
this._readChunk();
|
|
|
|
}
|
|
|
|
|
|
|
|
_readChunk() {
|
|
|
|
const chunk = this._file.slice(this._offset, this._offset + this._chunkSize);
|
|
|
|
this._reader.readAsArrayBuffer(chunk);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onChunkRead(chunk) {
|
|
|
|
this._offset += chunk.byteLength;
|
|
|
|
this._partitionSize += chunk.byteLength;
|
|
|
|
this._onChunk(chunk);
|
|
|
|
if (this.isFileEnd()) return;
|
|
|
|
if (this._isPartitionEnd()) {
|
|
|
|
this._onPartitionEnd(this._offset);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._readChunk();
|
|
|
|
}
|
|
|
|
|
|
|
|
repeatPartition() {
|
|
|
|
this._offset -= this._partitionSize;
|
|
|
|
this.nextPartition();
|
|
|
|
}
|
|
|
|
|
|
|
|
_isPartitionEnd() {
|
|
|
|
return this._partitionSize >= this._maxPartitionSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
isFileEnd() {
|
|
|
|
return this._offset >= this._file.size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class FileDigester {
|
|
|
|
|
|
|
|
constructor(meta, totalSize, totalBytesReceived, callback) {
|
|
|
|
this._buffer = [];
|
|
|
|
this._bytesReceived = 0;
|
|
|
|
this._size = meta.size;
|
|
|
|
this._name = meta.name;
|
|
|
|
this._mime = meta.mime;
|
|
|
|
this._totalSize = totalSize;
|
|
|
|
this._totalBytesReceived = totalBytesReceived;
|
|
|
|
this._callback = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
unchunk(chunk) {
|
|
|
|
this._buffer.push(chunk);
|
|
|
|
this._bytesReceived += chunk.byteLength || chunk.size;
|
|
|
|
this.progress = (this._totalBytesReceived + this._bytesReceived) / this._totalSize;
|
|
|
|
if (isNaN(this.progress)) this.progress = 1
|
|
|
|
|
|
|
|
if (this._bytesReceived < this._size) return;
|
|
|
|
// we are done
|
|
|
|
const blob = new Blob(this._buffer)
|
|
|
|
this._buffer = null;
|
|
|
|
this._callback(new File([blob], this._name, {
|
|
|
|
type: this._mime,
|
|
|
|
lastModified: new Date().getTime()
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class Events {
|
2023-09-13 19:00:48 +02:00
|
|
|
static fire(type, detail = {}) {
|
2023-02-10 20:22:36 +01:00
|
|
|
window.dispatchEvent(new CustomEvent(type, { detail: detail }));
|
|
|
|
}
|
|
|
|
|
2023-03-06 11:59:56 +01:00
|
|
|
static on(type, callback, options = false) {
|
|
|
|
return window.addEventListener(type, callback, options);
|
2023-02-10 20:22:36 +01:00
|
|
|
}
|
|
|
|
|
2023-03-06 11:59:56 +01:00
|
|
|
static off(type, callback, options = false) {
|
|
|
|
return window.removeEventListener(type, callback, options);
|
2023-02-10 20:22:36 +01:00
|
|
|
}
|
|
|
|
}
|