2018-10-09 15:45:07 +02:00
|
|
|
window.URL = window.URL || window.webkitURL;
|
|
|
|
window.isRtcSupported = !!(window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection);
|
2023-01-31 21:57:00 +01:00
|
|
|
|
2023-02-04 17:03:25 +01:00
|
|
|
if (!window.isRtcSupported) alert("WebRTC must be enabled for PairDrop to work");
|
|
|
|
|
2018-09-21 16:05:03 +02:00
|
|
|
class ServerConnection {
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this._connect();
|
2022-12-30 23:31:58 +01:00
|
|
|
Events.on('pagehide', _ => this._disconnect());
|
|
|
|
document.addEventListener('visibilitychange', _ => this._onVisibilityChange());
|
2023-01-23 04:51:22 +01:00
|
|
|
if (navigator.connection) navigator.connection.addEventListener('change', _ => this._reconnect());
|
2023-01-10 05:07:57 +01:00
|
|
|
Events.on('room-secrets', e => this._sendRoomSecrets(e.detail));
|
|
|
|
Events.on('room-secret-deleted', e => this.send({ type: 'room-secret-deleted', roomSecret: e.detail}));
|
|
|
|
Events.on('room-secrets-cleared', e => this.send({ type: 'room-secrets-cleared', roomSecrets: e.detail}));
|
|
|
|
Events.on('resend-peers', _ => this.send({ type: 'resend-peers'}));
|
|
|
|
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-01-17 10:41:50 +01:00
|
|
|
Events.on('offline', _ => clearTimeout(this._reconnectTimer));
|
|
|
|
Events.on('online', _ => this._connect());
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
|
2023-03-01 21:35:00 +01:00
|
|
|
_connect() {
|
2018-09-21 21:12:11 +02:00
|
|
|
clearTimeout(this._reconnectTimer);
|
2018-09-21 21:04:48 +02:00
|
|
|
if (this._isConnected() || this._isConnecting()) return;
|
2023-03-01 21:35:00 +01:00
|
|
|
const ws = new WebSocket(this._endpoint());
|
2018-09-21 16:05:03 +02:00
|
|
|
ws.binaryType = 'arraybuffer';
|
2023-01-07 01:45:52 +01:00
|
|
|
ws.onopen = _ => this._onOpen();
|
2018-09-21 16:05:03 +02:00
|
|
|
ws.onmessage = e => this._onMessage(e.data);
|
2022-12-23 02:38:56 +01:00
|
|
|
ws.onclose = _ => this._onDisconnect();
|
|
|
|
ws.onerror = e => this._onError(e);
|
2018-09-21 16:05:03 +02:00
|
|
|
this._socket = ws;
|
|
|
|
}
|
|
|
|
|
2023-01-07 01:45:52 +01:00
|
|
|
_onOpen() {
|
|
|
|
console.log('WS: server connected');
|
2023-01-10 05:07:57 +01:00
|
|
|
Events.fire('ws-connected');
|
2023-03-03 17:03:10 +01:00
|
|
|
if (this._isReconnect) Events.fire('notify-user', 'Connected.');
|
2023-01-10 05:07:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_sendRoomSecrets(roomSecrets) {
|
|
|
|
this.send({ type: 'room-secrets', roomSecrets: roomSecrets });
|
|
|
|
}
|
|
|
|
|
|
|
|
_onPairDeviceInitiate() {
|
|
|
|
if (!this._isConnected()) {
|
|
|
|
Events.fire('notify-user', 'You need to be online to pair devices.');
|
2023-01-07 01:45:52 +01:00
|
|
|
return;
|
|
|
|
}
|
2023-01-10 05:07:57 +01:00
|
|
|
this.send({ type: 'pair-device-initiate' })
|
|
|
|
}
|
|
|
|
|
|
|
|
_onPairDeviceJoin(roomKey) {
|
|
|
|
if (!this._isConnected()) {
|
2023-03-04 15:46:26 +01:00
|
|
|
setTimeout(_ => this._onPairDeviceJoin(roomKey), 1000);
|
2023-01-10 05:07:57 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.send({ type: 'pair-device-join', roomKey: roomKey })
|
2023-01-07 01:45:52 +01:00
|
|
|
}
|
|
|
|
|
2023-02-24 18:08:48 +01:00
|
|
|
_setRtcConfig(config) {
|
|
|
|
window.rtcConfig = config;
|
|
|
|
}
|
|
|
|
|
2018-09-21 16:05:03 +02:00
|
|
|
_onMessage(msg) {
|
|
|
|
msg = JSON.parse(msg);
|
2022-12-30 22:55:48 +01:00
|
|
|
if (msg.type !== 'ping') console.log('WS:', msg);
|
2018-09-21 16:05:03 +02:00
|
|
|
switch (msg.type) {
|
2023-02-24 18:08:48 +01:00
|
|
|
case 'rtc-config':
|
|
|
|
this._setRtcConfig(msg.config);
|
|
|
|
break;
|
2018-09-21 16:05:03 +02:00
|
|
|
case 'peers':
|
2023-01-10 05:07:57 +01:00
|
|
|
Events.fire('peers', msg);
|
2018-09-21 16:05:03 +02:00
|
|
|
break;
|
|
|
|
case 'peer-joined':
|
2023-01-10 05:07:57 +01:00
|
|
|
Events.fire('peer-joined', msg);
|
2018-09-21 16:05:03 +02:00
|
|
|
break;
|
|
|
|
case 'peer-left':
|
2023-02-10 23:47:39 +01:00
|
|
|
Events.fire('peer-left', msg);
|
2018-09-21 16:05:03 +02:00
|
|
|
break;
|
|
|
|
case 'signal':
|
|
|
|
Events.fire('signal', msg);
|
|
|
|
break;
|
|
|
|
case 'ping':
|
|
|
|
this.send({ type: 'pong' });
|
|
|
|
break;
|
2020-12-16 04:16:53 +01:00
|
|
|
case 'display-name':
|
2023-01-10 05:07:57 +01:00
|
|
|
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-01-10 05:07:57 +01:00
|
|
|
break;
|
|
|
|
case 'pair-device-join-key-invalid':
|
|
|
|
Events.fire('pair-device-join-key-invalid');
|
|
|
|
break;
|
|
|
|
case 'pair-device-canceled':
|
|
|
|
Events.fire('pair-device-canceled', msg.roomKey);
|
|
|
|
break;
|
|
|
|
case 'pair-device-join-key-rate-limit':
|
|
|
|
Events.fire('notify-user', 'Rate limit reached. Wait 10 seconds and try again.');
|
|
|
|
break;
|
|
|
|
case 'secret-room-deleted':
|
|
|
|
Events.fire('secret-room-deleted', msg.roomSecret);
|
2019-08-28 17:14:51 +02:00
|
|
|
break;
|
2018-09-21 16:05:03 +02:00
|
|
|
default:
|
2022-12-23 02:38:56 +01:00
|
|
|
console.error('WS: unknown message type', msg);
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-10 05:07:57 +01:00
|
|
|
send(msg) {
|
2018-10-09 15:45:07 +02:00
|
|
|
if (!this._isConnected()) return;
|
2023-01-10 05:07:57 +01:00
|
|
|
this._socket.send(JSON.stringify(msg));
|
|
|
|
}
|
|
|
|
|
|
|
|
_onDisplayName(msg) {
|
2023-03-06 00:06:57 +01:00
|
|
|
sessionStorage.setItem("peerId", msg.message.peerId);
|
|
|
|
sessionStorage.setItem("peerIdHash", msg.message.peerIdHash);
|
2023-01-10 05:07:57 +01:00
|
|
|
Events.fire('display-name', msg);
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
|
2023-03-01 21:35:00 +01:00
|
|
|
_endpoint() {
|
2018-09-21 16:05:03 +02:00
|
|
|
// hack to detect if deployment or development environment
|
|
|
|
const protocol = location.protocol.startsWith('https') ? 'wss' : 'ws';
|
|
|
|
const webrtc = window.isRtcSupported ? '/webrtc' : '/fallback';
|
2023-01-10 05:07:57 +01:00
|
|
|
let ws_url = new URL(protocol + '://' + location.host + location.pathname + 'server' + webrtc);
|
2023-03-06 00:06:57 +01:00
|
|
|
const peerId = sessionStorage.getItem("peerId");
|
|
|
|
const peerIdHash = sessionStorage.getItem("peerIdHash");
|
|
|
|
if (peerId && peerIdHash) {
|
|
|
|
ws_url.searchParams.append('peer_id', peerId);
|
|
|
|
ws_url.searchParams.append('peer_id_hash', peerIdHash);
|
|
|
|
}
|
2023-01-10 05:07:57 +01:00
|
|
|
return ws_url.toString();
|
|
|
|
}
|
|
|
|
|
2023-03-06 03:36:46 +01:00
|
|
|
_disconnect() {
|
|
|
|
this.send({ type: 'disconnect' });
|
2023-02-04 16:22:57 +01:00
|
|
|
if (this._socket) {
|
|
|
|
this._socket.onclose = null;
|
|
|
|
this._socket.close();
|
|
|
|
this._socket = null;
|
|
|
|
Events.fire('ws-disconnected');
|
2023-03-03 17:03:10 +01:00
|
|
|
this._isReconnect = true;
|
2023-02-04 16:22:57 +01:00
|
|
|
}
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_onDisconnect() {
|
|
|
|
console.log('WS: server disconnected');
|
2023-03-03 17:03:10 +01:00
|
|
|
Events.fire('notify-user', 'Connecting..');
|
2018-09-21 16:05:03 +02:00
|
|
|
clearTimeout(this._reconnectTimer);
|
2023-03-04 15:46:26 +01:00
|
|
|
this._reconnectTimer = setTimeout(_ => this._connect(), 1000);
|
2023-01-10 05:07:57 +01:00
|
|
|
Events.fire('ws-disconnected');
|
2023-03-03 17:03:10 +01:00
|
|
|
this._isReconnect = true;
|
2022-12-23 02:38:56 +01:00
|
|
|
}
|
|
|
|
|
2018-09-21 20:51:56 +02:00
|
|
|
_onVisibilityChange() {
|
2022-12-31 18:03:37 +01:00
|
|
|
if (document.hidden) return;
|
2018-09-21 20:51:56 +02:00
|
|
|
this._connect();
|
|
|
|
}
|
2018-09-22 04:44:17 +02:00
|
|
|
|
|
|
|
_isConnected() {
|
|
|
|
return this._socket && this._socket.readyState === this._socket.OPEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
_isConnecting() {
|
|
|
|
return this._socket && this._socket.readyState === this._socket.CONNECTING;
|
|
|
|
}
|
2022-12-23 02:38:56 +01:00
|
|
|
|
|
|
|
_onError(e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
2022-12-31 18:03:37 +01:00
|
|
|
|
|
|
|
_reconnect() {
|
|
|
|
this._disconnect();
|
|
|
|
this._connect();
|
|
|
|
}
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class Peer {
|
|
|
|
|
2023-01-10 05:07:57 +01:00
|
|
|
constructor(serverConnection, peerId, roomType, roomSecret) {
|
2018-09-21 16:05:03 +02:00
|
|
|
this._server = serverConnection;
|
|
|
|
this._peerId = peerId;
|
2023-01-10 05:07:57 +01:00
|
|
|
this._roomType = roomType;
|
|
|
|
this._roomSecret = roomSecret;
|
2018-09-21 16:05:03 +02:00
|
|
|
this._filesQueue = [];
|
|
|
|
this._busy = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-01-17 10:41:50 +01:00
|
|
|
async createHeader(file) {
|
|
|
|
return {
|
|
|
|
name: file.name,
|
|
|
|
mime: file.type,
|
|
|
|
size: file.size,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
getResizedImageDataUrl(file, width = undefined, height = undefined, quality = 0.7) {
|
2023-02-08 14:10:34 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2023-01-17 10:41:50 +01:00
|
|
|
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);
|
|
|
|
}
|
2023-02-08 14:10:34 +01:00
|
|
|
image.onerror = _ => reject(`Could not create an image thumbnail from type ${file.type}`);
|
2023-01-17 10:41:50 +01:00
|
|
|
}).then(dataUrl => {
|
|
|
|
return dataUrl;
|
2023-02-08 14:10:34 +01:00
|
|
|
}).catch(e => console.error(e));
|
2023-01-17 10:41:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async requestFileTransfer(files) {
|
|
|
|
let header = [];
|
2023-01-27 01:27:22 +01:00
|
|
|
let totalSize = 0;
|
|
|
|
let imagesOnly = true
|
2023-01-17 10:41:50 +01:00
|
|
|
for (let i=0; i<files.length; i++) {
|
2023-01-27 01:27:22 +01:00
|
|
|
Events.fire('set-progress', {peerId: this._peerId, progress: 0.8*i/files.length, status: 'prepare'})
|
2023-01-17 10:41:50 +01:00
|
|
|
header.push(await this.createHeader(files[i]));
|
2023-01-27 01:27:22 +01:00
|
|
|
totalSize += files[i].size;
|
|
|
|
if (files[i].type.split('/')[0] !== 'image') imagesOnly = false;
|
2023-01-17 10:41:50 +01:00
|
|
|
}
|
|
|
|
|
2023-01-27 01:27:22 +01:00
|
|
|
Events.fire('set-progress', {peerId: this._peerId, progress: 0.8, status: 'prepare'})
|
2023-01-17 10:41:50 +01:00
|
|
|
|
2023-01-27 01:27:22 +01:00
|
|
|
let dataUrl = '';
|
2023-01-18 15:34:11 +01:00
|
|
|
if (files[0].type.split('/')[0] === 'image') {
|
2023-01-27 01:27:22 +01:00
|
|
|
dataUrl = await this.getResizedImageDataUrl(files[0], 400, null, 0.9);
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
2023-01-27 01:27:22 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
});
|
2023-01-17 10:41:50 +01:00
|
|
|
Events.fire('set-progress', {peerId: this._peerId, progress: 0, status: 'wait'})
|
|
|
|
}
|
|
|
|
|
|
|
|
async sendFiles() {
|
2023-01-27 01:27:22 +01:00
|
|
|
for (let i=0; i<this._filesRequested.length; i++) {
|
|
|
|
this._filesQueue.push(this._filesRequested[i]);
|
|
|
|
}
|
|
|
|
this._filesRequested = null
|
2018-09-21 16:05:03 +02:00
|
|
|
if (this._busy) return;
|
|
|
|
this._dequeueFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
_dequeueFile() {
|
|
|
|
this._busy = true;
|
|
|
|
const file = this._filesQueue.shift();
|
|
|
|
this._sendFile(file);
|
|
|
|
}
|
|
|
|
|
2023-01-17 10:41:50 +01:00
|
|
|
async _sendFile(file) {
|
2018-09-21 16:05:03 +02:00
|
|
|
this.sendJSON({
|
|
|
|
type: 'header',
|
2023-01-27 01:27:22 +01:00
|
|
|
size: file.size,
|
|
|
|
name: file.name,
|
|
|
|
mime: file.type
|
2018-09-21 16:05:03 +02:00
|
|
|
});
|
2023-01-27 01:27:22 +01:00
|
|
|
this._chunker = new FileChunker(file,
|
2018-09-21 16:05:03 +02:00
|
|
|
chunk => this._send(chunk),
|
|
|
|
offset => this._onPartitionEnd(offset));
|
|
|
|
this._chunker.nextPartition();
|
|
|
|
}
|
|
|
|
|
|
|
|
_onPartitionEnd(offset) {
|
|
|
|
this.sendJSON({ type: 'partition', offset: offset });
|
|
|
|
}
|
|
|
|
|
|
|
|
_onReceivedPartitionEnd(offset) {
|
2018-09-22 08:47:40 +02:00
|
|
|
this.sendJSON({ type: 'partition-received', offset: offset });
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_sendNextPartition() {
|
|
|
|
if (!this._chunker || this._chunker.isFileEnd()) return;
|
|
|
|
this._chunker.nextPartition();
|
|
|
|
}
|
|
|
|
|
|
|
|
_sendProgress(progress) {
|
|
|
|
this.sendJSON({ type: 'progress', progress: progress });
|
|
|
|
}
|
|
|
|
|
|
|
|
_onMessage(message) {
|
|
|
|
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-01-17 10:41:50 +01:00
|
|
|
case 'request':
|
2023-03-03 16:36:55 +01:00
|
|
|
this._onFilesTransferRequest(messageJSON);
|
2023-01-17 10:41:50 +01:00
|
|
|
break;
|
2018-09-21 16:05:03 +02:00
|
|
|
case 'header':
|
2023-03-13 12:15:55 +01:00
|
|
|
this._onFileHeader(messageJSON);
|
2018-09-21 16:05:03 +02:00
|
|
|
break;
|
|
|
|
case 'partition':
|
2023-03-03 16:36:55 +01:00
|
|
|
this._onReceivedPartitionEnd(messageJSON);
|
2018-09-21 16:05:03 +02:00
|
|
|
break;
|
2018-09-22 08:47:40 +02:00
|
|
|
case 'partition-received':
|
2018-09-21 16:05:03 +02:00
|
|
|
this._sendNextPartition();
|
|
|
|
break;
|
|
|
|
case 'progress':
|
2023-03-03 16:36:55 +01:00
|
|
|
this._onDownloadProgress(messageJSON.progress);
|
2018-09-21 16:05:03 +02:00
|
|
|
break;
|
2023-01-17 10:41:50 +01:00
|
|
|
case 'files-transfer-response':
|
2023-03-03 16:36:55 +01:00
|
|
|
this._onFileTransferRequestResponded(messageJSON);
|
2023-01-17 10:41:50 +01:00
|
|
|
break;
|
2022-11-08 15:43:24 +01:00
|
|
|
case 'file-transfer-complete':
|
|
|
|
this._onFileTransferCompleted();
|
|
|
|
break;
|
|
|
|
case 'message-transfer-complete':
|
|
|
|
this._onMessageTransferCompleted();
|
2018-09-21 16:05:03 +02:00
|
|
|
break;
|
|
|
|
case 'text':
|
2023-03-03 16:36:55 +01:00
|
|
|
this._onTextReceived(messageJSON);
|
2018-09-21 16:05:03 +02: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;
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-17 10:41:50 +01:00
|
|
|
_onFilesTransferRequest(request) {
|
|
|
|
if (this._requestPending) {
|
|
|
|
// Only accept one request at a time
|
|
|
|
this.sendJSON({type: 'files-transfer-response', accepted: false});
|
|
|
|
return;
|
|
|
|
}
|
2023-01-27 01:27:22 +01:00
|
|
|
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-01-17 10:41:50 +01:00
|
|
|
Events.fire('files-transfer-request', {
|
|
|
|
request: request,
|
|
|
|
peerId: this._peerId
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-27 01:27:22 +01:00
|
|
|
_respondToFileTransferRequest(accepted) {
|
2023-01-17 10:41:50 +01:00
|
|
|
this.sendJSON({type: 'files-transfer-response', accepted: accepted});
|
2023-01-27 01:27:22 +01:00
|
|
|
if (accepted) {
|
|
|
|
this._requestAccepted = this._requestPending;
|
|
|
|
this._totalBytesReceived = 0;
|
|
|
|
this._busy = true;
|
|
|
|
this._filesReceived = [];
|
|
|
|
}
|
|
|
|
this._requestPending = null;
|
2023-01-17 10:41:50 +01:00
|
|
|
}
|
|
|
|
|
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-01-17 10:41:50 +01:00
|
|
|
this._lastProgress = 0;
|
2023-01-27 01:27:22 +01:00
|
|
|
this._digester = new FileDigester({size: header.size, name: header.name, mime: header.mime},
|
|
|
|
this._requestAccepted.totalSize,
|
|
|
|
this._totalBytesReceived,
|
|
|
|
fileBlob => this._onFileReceived(fileBlob)
|
|
|
|
);
|
2023-01-17 10:41:50 +01:00
|
|
|
}
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
|
2023-01-27 01:27:22 +01:00
|
|
|
_abortTransfer() {
|
|
|
|
Events.fire('set-progress', {peerId: this._peerId, progress: 1, status: 'wait'});
|
|
|
|
Events.fire('notify-user', 'Files are incorrect.');
|
|
|
|
this._filesReceived = [];
|
|
|
|
this._requestAccepted = null;
|
|
|
|
this._digester = null;
|
|
|
|
throw new Error("Received files differ from requested files. Abort!");
|
|
|
|
}
|
|
|
|
|
2018-09-21 16:05:03 +02:00
|
|
|
_onChunkReceived(chunk) {
|
2023-01-17 10:41:50 +01:00
|
|
|
if(!this._digester || !(chunk.byteLength || chunk.size)) return;
|
2022-12-22 20:28:45 +01:00
|
|
|
|
2018-09-21 16:05:03 +02:00
|
|
|
this._digester.unchunk(chunk);
|
|
|
|
const progress = this._digester.progress;
|
2023-01-27 01:27:22 +01:00
|
|
|
|
|
|
|
if (progress > 1) {
|
|
|
|
this._abortTransfer();
|
|
|
|
}
|
|
|
|
|
2018-09-21 16:05:03 +02:00
|
|
|
this._onDownloadProgress(progress);
|
|
|
|
|
2022-12-22 20:28:45 +01:00
|
|
|
// occasionally notify sender about our progress
|
2023-01-27 01:27:22 +01:00
|
|
|
if (progress - this._lastProgress < 0.005 && progress !== 1) return;
|
2018-09-21 16:05:03 +02:00
|
|
|
this._lastProgress = progress;
|
|
|
|
this._sendProgress(progress);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onDownloadProgress(progress) {
|
2023-01-27 01:27:22 +01:00
|
|
|
Events.fire('set-progress', {peerId: this._peerId, progress: progress, status: 'transfer'});
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
|
2023-01-27 01:27:22 +01:00
|
|
|
async _onFileReceived(fileBlob) {
|
|
|
|
const acceptedHeader = this._requestAccepted.header.shift();
|
|
|
|
this._totalBytesReceived += fileBlob.size;
|
|
|
|
|
2023-01-17 10:41:50 +01:00
|
|
|
this.sendJSON({type: 'file-transfer-complete'});
|
2023-01-18 15:34:11 +01:00
|
|
|
|
2023-01-27 01:27:22 +01:00
|
|
|
const sameSize = fileBlob.size === acceptedHeader.size;
|
|
|
|
const sameName = fileBlob.name === acceptedHeader.name
|
|
|
|
if (!sameSize || !sameName) {
|
|
|
|
this._abortTransfer();
|
|
|
|
}
|
2023-01-18 15:34:11 +01:00
|
|
|
|
2023-03-13 12:15:55 +01:00
|
|
|
// include for compatibility with Snapdrop for Android app
|
|
|
|
Events.fire('file-received', fileBlob);
|
|
|
|
|
2023-01-27 01:27:22 +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-03-03 12:01:43 +01:00
|
|
|
Events.fire('files-received', {sender: this._peerId, files: this._filesReceived, imagesOnly: this._requestAccepted.imagesOnly, totalSize: this._requestAccepted.totalSize});
|
2023-01-27 01:27:22 +01:00
|
|
|
this._filesReceived = [];
|
|
|
|
this._requestAccepted = null;
|
2023-01-17 10:41:50 +01:00
|
|
|
}
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
|
2022-11-08 15:43:24 +01:00
|
|
|
_onFileTransferCompleted() {
|
2023-01-27 01:27:22 +01:00
|
|
|
this._chunker = null;
|
|
|
|
if (!this._filesQueue.length) {
|
|
|
|
this._busy = false;
|
|
|
|
Events.fire('notify-user', 'File transfer completed.');
|
|
|
|
} else {
|
|
|
|
this._dequeueFile();
|
|
|
|
}
|
2023-01-17 10:41:50 +01:00
|
|
|
}
|
|
|
|
|
2023-01-17 14:00:01 +01:00
|
|
|
_onFileTransferRequestResponded(message) {
|
2023-01-17 10:41:50 +01:00
|
|
|
if (!message.accepted) {
|
|
|
|
Events.fire('set-progress', {peerId: this._peerId, progress: 1, status: 'wait'});
|
2023-01-27 01:27:22 +01:00
|
|
|
this._filesRequested = null;
|
|
|
|
if (message.reason === 'ios-memory-limit') {
|
|
|
|
Events.fire('notify-user', "Sending files to iOS is only possible up to 200MB at once");
|
|
|
|
}
|
2023-01-17 10:41:50 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
Events.fire('file-transfer-accepted');
|
2023-02-08 01:04:38 +01:00
|
|
|
Events.fire('set-progress', {peerId: this._peerId, progress: 0, status: 'transfer'});
|
2023-01-17 10:41:50 +01:00
|
|
|
this.sendFiles();
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
|
2022-11-08 15:43:24 +01:00
|
|
|
_onMessageTransferCompleted() {
|
|
|
|
Events.fire('notify-user', 'Message transfer completed.');
|
|
|
|
}
|
|
|
|
|
2018-09-21 16:05:03 +02:00
|
|
|
sendText(text) {
|
2018-09-22 04:44:17 +02:00
|
|
|
const unescaped = btoa(unescape(encodeURIComponent(text)));
|
|
|
|
this.sendJSON({ type: 'text', text: unescaped });
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_onTextReceived(message) {
|
2023-02-10 03:26:08 +01:00
|
|
|
if (!message.text) return;
|
2018-09-22 04:44:17 +02:00
|
|
|
const escaped = decodeURIComponent(escape(atob(message.text)));
|
2023-02-10 03:26:08 +01:00
|
|
|
Events.fire('text-received', { text: escaped, peerId: this._peerId });
|
2022-11-08 15:43:24 +01:00
|
|
|
this.sendJSON({ type: 'message-transfer-complete' });
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
2023-03-01 21:35:00 +01:00
|
|
|
|
|
|
|
_onDisplayNameChanged(message) {
|
2023-03-06 11:24:19 +01:00
|
|
|
if (!message.displayName || this._displayName === message.displayName) return;
|
|
|
|
this._displayName = message.displayName;
|
2023-03-01 21:35:00 +01:00
|
|
|
Events.fire('peer-display-name-changed', {peerId: this._peerId, displayName: message.displayName});
|
|
|
|
}
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class RTCPeer extends Peer {
|
|
|
|
|
2023-01-10 05:07:57 +01:00
|
|
|
constructor(serverConnection, peerId, roomType, roomSecret) {
|
|
|
|
super(serverConnection, peerId, roomType, roomSecret);
|
2023-03-03 13:09:59 +01:00
|
|
|
this.rtcSupported = true;
|
2018-09-21 16:05:03 +02:00
|
|
|
if (!peerId) return; // we will listen for a caller
|
2018-09-22 04:44:17 +02:00
|
|
|
this._connect(peerId, true);
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
|
2018-09-22 04:44:17 +02:00
|
|
|
_connect(peerId, isCaller) {
|
2023-02-10 18:56:13 +01:00
|
|
|
if (!this._conn || this._conn.signalingState === "closed") this._openConnection(peerId, isCaller);
|
2018-09-21 16:05:03 +02:00
|
|
|
|
|
|
|
if (isCaller) {
|
2018-09-22 04:44:17 +02:00
|
|
|
this._openChannel();
|
2018-09-21 16:05:03 +02:00
|
|
|
} else {
|
2018-09-22 04:44:17 +02:00
|
|
|
this._conn.ondatachannel = e => this._onChannelOpened(e);
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-22 04:44:17 +02:00
|
|
|
_openConnection(peerId, isCaller) {
|
|
|
|
this._isCaller = isCaller;
|
|
|
|
this._peerId = peerId;
|
2023-02-24 18:08:48 +01:00
|
|
|
this._conn = new RTCPeerConnection(window.rtcConfig);
|
2018-09-22 04:44:17 +02:00
|
|
|
this._conn.onicecandidate = e => this._onIceCandidate(e);
|
2023-03-10 22:21:19 +01:00
|
|
|
this._conn.onicecandidateerror = e => this._onError(e);
|
2023-01-10 05:07:57 +01:00
|
|
|
this._conn.onconnectionstatechange = _ => this._onConnectionStateChange();
|
2018-09-25 18:55:47 +02:00
|
|
|
this._conn.oniceconnectionstatechange = e => this._onIceConnectionStateChange(e);
|
2018-09-22 04:44:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_openChannel() {
|
2023-02-10 23:41:04 +01:00
|
|
|
if (!this._conn) return;
|
2022-12-22 20:28:45 +01:00
|
|
|
const channel = this._conn.createDataChannel('data-channel', {
|
2021-01-11 02:24:27 +01:00
|
|
|
ordered: true,
|
|
|
|
reliable: true // Obsolete. See https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel/reliable
|
|
|
|
});
|
2018-09-21 19:14:25 +02:00
|
|
|
channel.onopen = e => this._onChannelOpened(e);
|
2023-02-10 23:41:04 +01:00
|
|
|
channel.onerror = e => this._onError(e);
|
2018-10-09 23:00:18 +02:00
|
|
|
this._conn.createOffer().then(d => this._onDescription(d)).catch(e => this._onError(e));
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_onDescription(description) {
|
|
|
|
// description.sdp = description.sdp.replace('b=AS:30', 'b=AS:1638400');
|
2018-10-09 23:00:18 +02:00
|
|
|
this._conn.setLocalDescription(description)
|
|
|
|
.then(_ => this._sendSignal({ sdp: description }))
|
|
|
|
.catch(e => this._onError(e));
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_onIceCandidate(event) {
|
|
|
|
if (!event.candidate) return;
|
|
|
|
this._sendSignal({ ice: event.candidate });
|
|
|
|
}
|
|
|
|
|
|
|
|
onServerMessage(message) {
|
2023-02-10 20:22:36 +01:00
|
|
|
if (!this._conn) this._connect(message.sender.id, false);
|
2018-09-21 16:05:03 +02:00
|
|
|
|
|
|
|
if (message.sdp) {
|
2023-01-23 19:53:14 +01:00
|
|
|
this._conn.setRemoteDescription(message.sdp)
|
2019-02-12 01:54:44 +01:00
|
|
|
.then( _ => {
|
2023-02-10 23:41:04 +01:00
|
|
|
if (message.sdp.type === 'offer') {
|
|
|
|
return this._conn.createAnswer()
|
|
|
|
.then(d => this._onDescription(d));
|
|
|
|
}
|
2019-02-12 01:54:44 +01:00
|
|
|
})
|
2018-10-09 23:00:18 +02:00
|
|
|
.catch(e => this._onError(e));
|
2018-09-21 16:05:03 +02:00
|
|
|
} else if (message.ice) {
|
2023-02-10 23:41:04 +01:00
|
|
|
this._conn.addIceCandidate(new RTCIceCandidate(message.ice))
|
|
|
|
.catch(e => this._onError(e));
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_onChannelOpened(event) {
|
|
|
|
console.log('RTC: channel opened with', this._peerId);
|
|
|
|
const channel = event.channel || event.target;
|
2022-10-23 06:17:39 +02:00
|
|
|
channel.binaryType = 'arraybuffer';
|
2018-09-21 16:05:03 +02:00
|
|
|
channel.onmessage = e => this._onMessage(e.data);
|
2023-03-06 03:36:46 +01:00
|
|
|
channel.onclose = _ => this._onChannelClosed();
|
2018-09-21 16:05:03 +02: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()});
|
2018-09-21 16:05:03 +02: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();
|
|
|
|
return "There are unfinished transfers. Are you sure you want to close?";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-09-21 16:05:03 +02:00
|
|
|
_onChannelClosed() {
|
2018-09-21 19:24:01 +02:00
|
|
|
console.log('RTC: channel closed', this._peerId);
|
2022-12-22 22:41:26 +01:00
|
|
|
Events.fire('peer-disconnected', this._peerId);
|
2023-01-06 16:05:17 +01:00
|
|
|
if (!this._isCaller) return;
|
2018-09-22 04:44:17 +02:00
|
|
|
this._connect(this._peerId, true); // reopen the channel
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
|
2023-01-10 05:07:57 +01:00
|
|
|
_onConnectionStateChange() {
|
2018-09-22 04:44:17 +02:00
|
|
|
console.log('RTC: state changed:', this._conn.connectionState);
|
|
|
|
switch (this._conn.connectionState) {
|
2018-09-21 20:51:56 +02:00
|
|
|
case 'disconnected':
|
2023-03-01 21:35:00 +01:00
|
|
|
Events.fire('peer-disconnected', this._peerId);
|
2023-02-10 18:56:13 +01:00
|
|
|
this._onError('rtc connection disconnected');
|
2018-09-21 20:51:56 +02:00
|
|
|
break;
|
|
|
|
case 'failed':
|
2023-03-01 21:35:00 +01:00
|
|
|
Events.fire('peer-disconnected', this._peerId);
|
2023-02-10 18:56:13 +01:00
|
|
|
this._onError('rtc connection failed');
|
2018-09-21 20:51:56 +02:00
|
|
|
break;
|
2018-09-21 19:24:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-25 18:58:52 +02:00
|
|
|
_onIceConnectionStateChange() {
|
|
|
|
switch (this._conn.iceConnectionState) {
|
|
|
|
case 'failed':
|
2023-02-10 18:56:13 +01:00
|
|
|
this._onError('ICE Gathering failed');
|
2018-09-25 18:58:52 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.log('ICE Gathering', this._conn.iceConnectionState);
|
|
|
|
}
|
2018-09-25 18:55:47 +02:00
|
|
|
}
|
|
|
|
|
2018-09-22 04:44:17 +02:00
|
|
|
_onError(error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
|
2018-09-21 16:05:03 +02:00
|
|
|
_send(message) {
|
2023-01-23 04:51:22 +01:00
|
|
|
if (!this._channel) this.refresh();
|
2018-09-21 16:05:03 +02:00
|
|
|
this._channel.send(message);
|
|
|
|
}
|
|
|
|
|
2018-09-22 04:44:17 +02:00
|
|
|
_sendSignal(signal) {
|
|
|
|
signal.type = 'signal';
|
|
|
|
signal.to = this._peerId;
|
2023-01-10 05:07:57 +01:00
|
|
|
signal.roomType = this._roomType;
|
|
|
|
signal.roomSecret = this._roomSecret;
|
2018-09-22 04:44:17 +02:00
|
|
|
this._server.send(signal);
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
refresh() {
|
2018-09-22 04:44:17 +02:00
|
|
|
// check if channel is open. otherwise create one
|
|
|
|
if (this._isConnected() || this._isConnecting()) return;
|
|
|
|
this._connect(this._peerId, this._isCaller);
|
|
|
|
}
|
|
|
|
|
|
|
|
_isConnected() {
|
|
|
|
return this._channel && this._channel.readyState === 'open';
|
|
|
|
}
|
|
|
|
|
|
|
|
_isConnecting() {
|
|
|
|
return this._channel && this._channel.readyState === 'connecting';
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
2023-03-06 15:33:22 +01:00
|
|
|
|
|
|
|
sendDisplayName(displayName) {
|
|
|
|
if (!this._isConnected()) return;
|
|
|
|
super.sendDisplayName(displayName);
|
|
|
|
}
|
2018-09-21 16:05:03 +02: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));
|
2023-01-17 10:41:50 +01:00
|
|
|
Events.on('respond-to-files-transfer-request', e => this._onRespondToFileTransferRequest(e.detail))
|
2018-09-21 16:05:03 +02:00
|
|
|
Events.on('send-text', e => this._onSendText(e.detail));
|
2023-02-10 23:47:39 +01:00
|
|
|
Events.on('peer-left', e => this._onPeerLeft(e.detail));
|
2023-03-01 21:35:00 +01:00
|
|
|
Events.on('peer-connected', e => this._onPeerConnected(e.detail.peerId));
|
2023-02-10 18:56:13 +01:00
|
|
|
Events.on('peer-disconnected', e => this._onPeerDisconnected(e.detail));
|
2023-01-10 05:07:57 +01:00
|
|
|
Events.on('secret-room-deleted', e => this._onSecretRoomDeleted(e.detail));
|
2023-03-01 21:35:00 +01:00
|
|
|
Events.on('display-name', e => this._onDisplayName(e.detail.message.displayName));
|
|
|
|
Events.on('self-display-name-changed', e => this._notifyPeersDisplayNameChanged(e.detail));
|
2023-03-06 11:24:19 +01:00
|
|
|
Events.on('peer-display-name-changed', e => this._notifyPeerDisplayNameChanged(e.detail.peerId));
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_onMessage(message) {
|
2023-01-23 19:53:14 +01:00
|
|
|
// if different roomType -> abort
|
2023-02-10 20:22:36 +01:00
|
|
|
if (this.peers[message.sender.id] && this.peers[message.sender.id]._roomType !== message.roomType) return;
|
|
|
|
if (!this.peers[message.sender.id]) {
|
|
|
|
this.peers[message.sender.id] = new RTCPeer(this._server, undefined, message.roomType, message.roomSecret);
|
2023-01-10 05:07:57 +01:00
|
|
|
}
|
2023-02-10 20:22:36 +01:00
|
|
|
this.peers[message.sender.id].onServerMessage(message);
|
2023-01-10 05:07:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_onPeers(msg) {
|
|
|
|
msg.peers.forEach(peer => {
|
2018-09-21 16:05:03 +02:00
|
|
|
if (this.peers[peer.id]) {
|
2023-01-23 19:53:14 +01:00
|
|
|
// if different roomType -> abort
|
2023-02-11 00:52:37 +01:00
|
|
|
if (this.peers[peer.id].roomType !== msg.roomType || this.peers[peer.id].roomSecret !== msg.roomSecret) return;
|
2023-01-23 19:53:14 +01:00
|
|
|
this.peers[peer.id].refresh();
|
2023-02-08 01:04:38 +01:00
|
|
|
return;
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
2023-02-04 17:03:25 +01:00
|
|
|
this.peers[peer.id] = new RTCPeer(this._server, peer.id, msg.roomType, msg.roomSecret);
|
2018-09-21 16:05:03 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-01-17 10:41:50 +01:00
|
|
|
_onRespondToFileTransferRequest(detail) {
|
2023-01-27 01:27:22 +01:00
|
|
|
this.peers[detail.to]._respondToFileTransferRequest(detail.accepted);
|
2023-01-17 10:41:50 +01:00
|
|
|
}
|
|
|
|
|
2018-09-21 16:05:03 +02:00
|
|
|
_onFilesSelected(message) {
|
2023-01-27 01:27:22 +01:00
|
|
|
let inputFiles = Array.from(message.files);
|
|
|
|
delete message.files;
|
2023-01-23 04:51:22 +01:00
|
|
|
let files = [];
|
2023-01-27 01:27:22 +01:00
|
|
|
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)});
|
2023-01-23 04:51:22 +01:00
|
|
|
files.push(file)
|
2023-01-19 06:01:23 +01:00
|
|
|
}
|
2023-01-23 04:51:22 +01:00
|
|
|
this.peers[message.to].requestFileTransfer(files);
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_onSendText(message) {
|
|
|
|
this.peers[message.to].sendText(message.text);
|
|
|
|
}
|
|
|
|
|
2023-02-10 23:47:39 +01:00
|
|
|
_onPeerLeft(msg) {
|
|
|
|
if (msg.disconnect === true) {
|
|
|
|
// if user actively disconnected from PairDrop disconnect all peer to peer connections immediately
|
|
|
|
Events.fire('peer-disconnected', msg.peerId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-01 21:35:00 +01:00
|
|
|
_onPeerConnected(peerId) {
|
|
|
|
this._notifyPeerDisplayNameChanged(peerId);
|
|
|
|
}
|
|
|
|
|
2023-02-10 18:56:13 +01:00
|
|
|
_onPeerDisconnected(peerId) {
|
2018-09-21 16:05:03 +02:00
|
|
|
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;
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
|
2023-01-10 05:07:57 +01:00
|
|
|
_onSecretRoomDeleted(roomSecret) {
|
|
|
|
for (const peerId in this.peers) {
|
|
|
|
const peer = this.peers[peerId];
|
|
|
|
if (peer._roomSecret === roomSecret) {
|
2023-02-10 23:41:04 +01:00
|
|
|
this._onPeerDisconnected(peerId);
|
2023-01-10 05:07:57 +01:00
|
|
|
}
|
|
|
|
}
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
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;
|
|
|
|
}
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class FileChunker {
|
|
|
|
|
|
|
|
constructor(file, onChunk, onPartitionEnd) {
|
2018-10-09 23:00:18 +02:00
|
|
|
this._chunkSize = 64000; // 64 KB
|
2018-10-09 15:45:07 +02:00
|
|
|
this._maxPartitionSize = 1e6; // 1 MB
|
2018-09-21 16:05:03 +02:00
|
|
|
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);
|
2022-10-21 02:54:14 +02:00
|
|
|
if (this.isFileEnd()) return;
|
|
|
|
if (this._isPartitionEnd()) {
|
2018-09-22 04:44:17 +02:00
|
|
|
this._onPartitionEnd(this._offset);
|
2018-09-21 16:05:03 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._readChunk();
|
|
|
|
}
|
|
|
|
|
|
|
|
repeatPartition() {
|
|
|
|
this._offset -= this._partitionSize;
|
2023-01-06 16:28:00 +01:00
|
|
|
this.nextPartition();
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_isPartitionEnd() {
|
|
|
|
return this._partitionSize >= this._maxPartitionSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
isFileEnd() {
|
2021-08-14 16:42:16 +02:00
|
|
|
return this._offset >= this._file.size;
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class FileDigester {
|
2018-09-22 08:47:40 +02:00
|
|
|
|
2023-01-27 01:27:22 +01:00
|
|
|
constructor(meta, totalSize, totalBytesReceived, callback) {
|
2018-09-21 16:05:03 +02:00
|
|
|
this._buffer = [];
|
|
|
|
this._bytesReceived = 0;
|
2023-01-27 01:27:22 +01:00
|
|
|
this._size = meta.size;
|
|
|
|
this._name = meta.name;
|
|
|
|
this._mime = meta.mime;
|
|
|
|
this._totalSize = totalSize;
|
|
|
|
this._totalBytesReceived = totalBytesReceived;
|
2018-09-21 16:05:03 +02:00
|
|
|
this._callback = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
unchunk(chunk) {
|
|
|
|
this._buffer.push(chunk);
|
|
|
|
this._bytesReceived += chunk.byteLength || chunk.size;
|
2023-01-27 01:27:22 +01:00
|
|
|
this.progress = (this._totalBytesReceived + this._bytesReceived) / this._totalSize;
|
2021-08-12 15:38:02 +02:00
|
|
|
if (isNaN(this.progress)) this.progress = 1
|
|
|
|
|
2018-09-21 16:05:03 +02:00
|
|
|
if (this._bytesReceived < this._size) return;
|
2018-09-22 08:47:40 +02:00
|
|
|
// we are done
|
2023-01-27 01:27:22 +01:00
|
|
|
const blob = new Blob(this._buffer)
|
|
|
|
this._buffer = null;
|
|
|
|
this._callback(new File([blob], this._name, {
|
|
|
|
type: this._mime,
|
|
|
|
lastModified: new Date().getTime()
|
|
|
|
}));
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
2018-09-22 04:44:17 +02:00
|
|
|
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class Events {
|
|
|
|
static fire(type, detail) {
|
|
|
|
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);
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
2022-09-13 14:33:03 +02:00
|
|
|
|
2023-03-06 11:59:56 +01:00
|
|
|
static off(type, callback, options = false) {
|
|
|
|
return window.removeEventListener(type, callback, options);
|
2022-09-13 14:33:03 +02:00
|
|
|
}
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|