This commit is contained in:
RobinLinus 2018-09-22 04:44:17 +02:00
parent 52bd7692e9
commit 04415ef28f
4 changed files with 73 additions and 68 deletions

View file

@ -2,7 +2,7 @@
[Snapdrop](https://snapdrop.net): local file sharing in your browser - inspired by Apple's Airdrop. [Snapdrop](https://snapdrop.net): local file sharing in your browser - inspired by Apple's Airdrop.
#### Snapdrop Version 2 is built with the following awesome technologies: #### Snapdrop (Version 2) is built with the following awesome technologies:
* Vanilla HTML5 / ES6 / CSS3 * Vanilla HTML5 / ES6 / CSS3
* Progressive Web App * Progressive Web App
* [WebRTC](http://webrtc.org/) * [WebRTC](http://webrtc.org/)
@ -18,12 +18,12 @@
* [idownloadblog](http://www.idownloadblog.com/2015/12/29/snapdrop/) * [idownloadblog](http://www.idownloadblog.com/2015/12/29/snapdrop/)
* [thenextweb](http://thenextweb.com/insider/2015/12/27/snapdrop-is-a-handy-web-based-replacement-for-apples-fiddly-airdrop-file-transfer-tool/) * [thenextweb](http://thenextweb.com/insider/2015/12/27/snapdrop-is-a-handy-web-based-replacement-for-apples-fiddly-airdrop-file-transfer-tool/)
* [winboard](http://www.winboard.org/artikel-ratgeber/6253-dateien-vom-desktop-pc-mit-anderen-plattformen-teilen-mit-snapdrop.html) * [winboard](http://www.winboard.org/artikel-ratgeber/6253-dateien-vom-desktop-pc-mit-anderen-plattformen-teilen-mit-snapdrop.html)
* [免費資源網路社群](https://free.com.tw/snapdrop/?utm_content=buffere6987&utm_medium=social&utm_source=twitter.com&utm_campaign=buffer) * [免費資源網路社群](https://free.com.tw/snapdrop/)
##### What about the connection? Is it a P2P-connection directly from device to device or is there any third-party-server? ##### What about the connection? Is it a P2P-connection directly from device to device or is there any third-party-server?
It uses a P2P connection if WebRTC is supported by the browser. (WebRTC needs a Signaling Server, but it is only used to establish a connection and is not involved in the file transfer). It uses a P2P connection if WebRTC is supported by the browser. (WebRTC needs a Signaling Server, but it is only used to establish a connection and is not involved in the file transfer).
If WebRTC isnt supported (Safari, IE) it uses a Web Sockets fallback for the file transfer. The server connects the clients with a stream. If WebRTC isnt supported (Safari, IE) it uses a Web Sockets fallback for the file transfer. The server connects the clients with each other.
##### What about privacy? Will files be saved on third-party-servers? ##### What about privacy? Will files be saved on third-party-servers?
None of your files are ever saved on any server. None of your files are ever saved on any server.
@ -33,15 +33,15 @@ But it does use Google Analytics.
##### Is SnapDrop a fork of ShareDrop? ##### Is SnapDrop a fork of ShareDrop?
No. ShareDrop is built with Ember. Snapdrop is built with vanilla ES6. No. ShareDrop is built with Ember. Snapdrop is built with vanilla ES6.
I wanted to play around with Progressive Web Apps and then I got the idea of a local file sharing app. By doing research on this idea I found and analysed ShareDrop. I liked it and thought about how to improve it. I wanted to play around with Progressive Web Apps and then I got the idea of a local file sharing app. By doing research on this idea I found and analysed ShareDrop. I liked it and thought about how to improve it.
ShareDrop uses WebRTC only and isn't compatible with Safari Browsers. Snapdrop uses a Websocket fallback and some hacks to make Snapdrop work due to the download restrictions on iDevices. ShareDrop uses WebRTC only and isn't compatible with Safari browsers. Snapdrop uses a Websocket fallback and some hacks to make Snapdrop work due to the download restrictions on iDevices.
### Snapdrop is awesome! How can I support it? ### Snapdrop is awesome! How can I support it?
* [File bugs, give feedback, submit suggestions](https://github.com/RobinLinus/snapdrop/issues) * [File bugs, give feedback, submit suggestions](https://github.com/RobinLinus/snapdrop/issues)
* Share Snapdrop on your social media. * Share Snapdrop on your social media.
* [Buy me a cup of coffee](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=R9C5E42UYEQCN) * [Buy me a cup of coffee](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=R9C5E42UYEQCN)
* Fix bugs and make a Pull Request. This is my first open source project, so I am not very used to the common workflow, but we'll figure it out! * Fix bugs and make a pull request.
* Do Security Analysis and suggestions * Do security analysis and suggestions
## Local Development ## Local Development
``` ```
@ -49,7 +49,9 @@ ShareDrop uses WebRTC only and isn't compatible with Safari Browsers. Snapdrop u
cd snapdrop/server cd snapdrop/server
npm install npm install
node index.js node index.js
cd ../client
# open a second shell:
cd snapdrop/client
python -m SimpleHTTPServer python -m SimpleHTTPServer
``` ```

View file

@ -20,14 +20,6 @@ class ServerConnection {
this._socket = ws; this._socket = ws;
} }
_isConnected() {
return this._socket && this._socket.readyState === this._socket.OPEN;
}
_isConnecting() {
return this._socket && this._socket.readyState === this._socket.CONNECTING;
}
_onMessage(msg) { _onMessage(msg) {
msg = JSON.parse(msg); msg = JSON.parse(msg);
console.log('WS:', msg); console.log('WS:', msg);
@ -82,6 +74,14 @@ class ServerConnection {
if (document.hidden) return; if (document.hidden) return;
this._connect(); this._connect();
} }
_isConnected() {
return this._socket && this._socket.readyState === this._socket.OPEN;
}
_isConnecting() {
return this._socket && this._socket.readyState === this._socket.CONNECTING;
}
} }
class Peer { class Peer {
@ -192,16 +192,12 @@ class Peer {
} }
_onDownloadProgress(progress) { _onDownloadProgress(progress) {
Events.fire('file-progress', { Events.fire('file-progress', { sender: this._peerId, progress: progress });
sender: this._peerId,
progress: progress
});
} }
_onFileReceived(proxyFile) { _onFileReceived(proxyFile) {
Events.fire('file-received', proxyFile); Events.fire('file-received', proxyFile);
this.sendJSON({ type: 'transfer-complete' }); this.sendJSON({ type: 'transfer-complete' });
// this._digester = null;
} }
_onTransferCompleted() { _onTransferCompleted() {
@ -213,17 +209,13 @@ class Peer {
} }
sendText(text) { sendText(text) {
this.sendJSON({ const unescaped = btoa(unescape(encodeURIComponent(text)));
type: 'text', this.sendJSON({ type: 'text', text: unescaped });
text: btoa(unescape(encodeURIComponent(text)))
});
} }
_onTextReceived(message) { _onTextReceived(message) {
Events.fire('text-received', { const escaped = decodeURIComponent(escape(atob(message.text)));
text: decodeURIComponent(escape(atob(message.text))), Events.fire('text-received', { text: escaped, sender: this._peerId });
sender: this._peerId
});
} }
} }
@ -232,35 +224,37 @@ class RTCPeer extends Peer {
constructor(serverConnection, peerId) { constructor(serverConnection, peerId) {
super(serverConnection, peerId); super(serverConnection, peerId);
if (!peerId) return; // we will listen for a caller if (!peerId) return; // we will listen for a caller
this._start(peerId, true); this._connect(peerId, true);
} }
_start(peerId, isCaller) { _connect(peerId, isCaller) {
if (!this._peer) { if (!this._conn) this._openConnection(peerId, isCaller);
this._isCaller = isCaller;
this._peerId = peerId;
this._peer = new RTCPeerConnection(RTCPeer.config);
this._peer.onicecandidate = e => this._onIceCandidate(e);
this._peer.onconnectionstatechange = e => this._onConnectionStateChange(e);
}
if (isCaller) { if (isCaller) {
this._createChannel(); this._openChannel();
} else { } else {
this._peer.ondatachannel = e => this._onChannelOpened(e); this._conn.ondatachannel = e => this._onChannelOpened(e);
} }
} }
_createChannel() { _openConnection(peerId, isCaller) {
const channel = this._peer.createDataChannel('data-channel', { reliable: true }); this._isCaller = isCaller;
this._peerId = peerId;
this._conn = new RTCPeerConnection(RTCPeer.config);
this._conn.onicecandidate = e => this._onIceCandidate(e);
this._conn.onconnectionstatechange = e => this._onConnectionStateChange(e);
}
_openChannel() {
const channel = this._conn.createDataChannel('data-channel', { reliable: true });
channel.binaryType = 'arraybuffer'; channel.binaryType = 'arraybuffer';
channel.onopen = e => this._onChannelOpened(e); channel.onopen = e => this._onChannelOpened(e);
this._peer.createOffer(d => this._onDescription(d), e => this._onError(e)); this._conn.createOffer(d => this._onDescription(d), e => this._onError(e));
} }
_onDescription(description) { _onDescription(description) {
// description.sdp = description.sdp.replace('b=AS:30', 'b=AS:1638400'); // description.sdp = description.sdp.replace('b=AS:30', 'b=AS:1638400');
this._peer.setLocalDescription(description, this._conn.setLocalDescription(description,
_ => this._sendSignal({ sdp: description }), _ => this._sendSignal({ sdp: description }),
e => this._onError(e)); e => this._onError(e));
} }
@ -270,23 +264,16 @@ class RTCPeer extends Peer {
this._sendSignal({ ice: event.candidate }); this._sendSignal({ ice: event.candidate });
} }
_sendSignal(signal) {
signal.type = 'signal';
signal.to = this._peerId;
this._server.send(signal);
}
onServerMessage(message) { onServerMessage(message) {
if (!this._peer) this._start(message.sender, false); if (!this._conn) this._connect(message.sender, false);
const conn = this._peer;
if (message.sdp) { if (message.sdp) {
this._peer.setRemoteDescription(new RTCSessionDescription(message.sdp), () => { this._conn.setRemoteDescription(new RTCSessionDescription(message.sdp), () => {
if (message.sdp.type !== 'offer') return; if (message.sdp.type !== 'offer') return;
this._peer.createAnswer(d => this._onDescription(d), e => this._onError(e)); this._conn.createAnswer(d => this._onDescription(d), e => this._onError(e));
}, e => this._onError(e)); }, e => this._onError(e));
} else if (message.ice) { } else if (message.ice) {
this._peer.addIceCandidate(new RTCIceCandidate(message.ice)); this._conn.addIceCandidate(new RTCIceCandidate(message.ice));
} }
} }
@ -301,34 +288,48 @@ class RTCPeer extends Peer {
_onChannelClosed() { _onChannelClosed() {
console.log('RTC: channel closed', this._peerId); console.log('RTC: channel closed', this._peerId);
if (!this.isCaller) return; if (!this.isCaller) return;
this._start(this._peerId, true); // reopen the channel this._connect(this._peerId, true); // reopen the channel
} }
_onConnectionStateChange(e) { _onConnectionStateChange(e) {
console.log('RTC: state changed:', this._peer.connectionState); console.log('RTC: state changed:', this._conn.connectionState);
switch (this._peer.connectionState) { switch (this._conn.connectionState) {
case 'disconnected': case 'disconnected':
this._onChannelClosed(); this._onChannelClosed();
break; break;
case 'failed': case 'failed':
this._peer = null; this._conn = null;
this._onChannelClosed(); this._onChannelClosed();
break; break;
} }
} }
_send(message) {
this._channel.send(message);
}
_onError(error) { _onError(error) {
console.error(error); console.error(error);
} }
_send(message) {
this._channel.send(message);
}
_sendSignal(signal) {
signal.type = 'signal';
signal.to = this._peerId;
this._server.send(signal);
}
refresh() { refresh() {
// check if channel open. otherwise create one // check if channel is open. otherwise create one
if (this._peer && this._channel && this._channel.readyState !== 'open') return; if (this._isConnected() || this._isConnecting()) return;
this._start(this._peerId, this._isCaller); this._connect(this._peerId, this._isCaller);
}
_isConnected() {
return this._channel && this._channel.readyState === 'open';
}
_isConnecting() {
return this._channel && this._channel.readyState === 'connecting';
} }
} }
@ -422,7 +423,7 @@ class FileChunker {
this._partitionSize += chunk.byteLength; this._partitionSize += chunk.byteLength;
this._onChunk(chunk); this._onChunk(chunk);
if (this._isPartitionEnd() || this.isFileEnd()) { if (this._isPartitionEnd() || this.isFileEnd()) {
this._onPartitionEnd(this._partitionSize); this._onPartitionEnd(this._offset);
return; return;
} }
this._readChunk(); this._readChunk();
@ -447,6 +448,7 @@ class FileChunker {
} }
class FileDigester { class FileDigester {
constructor(meta, callback) { constructor(meta, callback) {
this._buffer = []; this._buffer = [];
this._bytesReceived = 0; this._bytesReceived = 0;
@ -473,6 +475,7 @@ class FileDigester {
}); });
this._callback = null; this._callback = null;
} }
} }
class Events { class Events {
@ -485,7 +488,6 @@ class Events {
} }
} }
window.isRtcSupported = !!(window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection); window.isRtcSupported = !!(window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection);
RTCPeer.config = { RTCPeer.config = {

View file

@ -392,8 +392,8 @@ class Notifications {
} }
_copyText(message, notification) { _copyText(message, notification) {
document.copy(message);
notification.close(); notification.close();
if(!document.copy(message)) return;
this._notify('Copied text to clipboard'); this._notify('Copied text to clipboard');
} }

View file

@ -625,6 +625,7 @@ screen and (min-width: 1100px) {
x-instructions { x-instructions {
top: 24px; top: 24px;
} }
footer .logo { footer .logo {
--icon-size: 40px; --icon-size: 40px;
} }