revisions

This commit is contained in:
Emily Hou 2018-06-22 13:17:23 -07:00
parent 12ccce3016
commit dafe4884fc
9 changed files with 86 additions and 89 deletions

View File

@ -100,8 +100,30 @@ function asyncInitWebSocket(server) {
}); });
} }
function listenForResponse(ws, canceller) {
return new Promise((resolve, reject) => {
ws.addEventListener('message', function(msg) {
try {
const response = JSON.parse(msg.data);
if (response.error) {
throw new Error(response.error);
} else {
resolve({
url: response.url,
id: response.id,
ownerToken: response.owner
});
}
} catch (e) {
canceller.cancelled = true;
canceller.error = e;
reject(e);
}
});
});
}
async function upload( async function upload(
ws,
stream, stream,
streamInfo, streamInfo,
metadata, metadata,
@ -110,30 +132,21 @@ async function upload(
onprogress, onprogress,
canceller canceller
) { ) {
const host = window.location.hostname;
const port = window.location.port;
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
const error = { cancelled: false };
const ws = await asyncInitWebSocket(`${protocol}//${host}:${port}/api/ws`);
try {
const metadataHeader = arrayToB64(new Uint8Array(metadata)); const metadataHeader = arrayToB64(new Uint8Array(metadata));
const fileMeta = { const fileMeta = {
fileMetadata: metadataHeader, fileMetadata: metadataHeader,
authorization: `send-v1 ${verifierB64}` authorization: `send-v1 ${verifierB64}`
}; };
function listenForResponse() { const responsePromise = listenForResponse(ws, error);
return new Promise((resolve, reject) => {
ws.addEventListener('message', function(msg) {
const response = JSON.parse(msg.data);
if (response.error) {
reject(response.error);
} else {
resolve({
url: response.url,
id: response.id,
ownerToken: response.owner
});
}
});
});
}
const resPromise = listenForResponse();
ws.send(JSON.stringify(fileMeta)); ws.send(JSON.stringify(fileMeta));
const reader = stream.getReader(); const reader = stream.getReader();
@ -142,9 +155,11 @@ async function upload(
while (!state.done) { while (!state.done) {
const buf = state.value; const buf = state.value;
if (canceller.cancelled) { if (canceller.cancelled) {
ws.close(4000, 'upload cancelled');
throw new Error(0); throw new Error(0);
} }
if (error.cancelled) {
throw new Error(error.error);
}
ws.send(buf); ws.send(buf);
onprogress([Math.min(streamInfo.fileSize, size), streamInfo.fileSize]); onprogress([Math.min(streamInfo.fileSize, size), streamInfo.fileSize]);
@ -152,13 +167,16 @@ async function upload(
state = await reader.read(); state = await reader.read();
} }
const response = await resPromise; const response = await responsePromise; //promise only fufills if response is good
ws.close(); ws.close();
return response; return response;
} catch (e) {
ws.close(4000);
throw e;
}
} }
export async function uploadWs( export function uploadWs(
encrypted, encrypted,
info, info,
metadata, metadata,
@ -166,10 +184,6 @@ export async function uploadWs(
keychain, keychain,
onprogress onprogress
) { ) {
const host = window.location.hostname;
const port = window.location.port;
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
const ws = await asyncInitWebSocket(`${protocol}//${host}:${port}/api/ws`);
const canceller = { cancelled: false }; const canceller = { cancelled: false };
return { return {
@ -177,7 +191,6 @@ export async function uploadWs(
canceller.cancelled = true; canceller.cancelled = true;
}, },
result: upload( result: upload(
ws,
encrypted, encrypted,
info, info,
metadata, metadata,

View File

@ -282,11 +282,11 @@ export default class ECE {
this.streamInfo = { this.streamInfo = {
recordSize: rs, recordSize: rs,
fileSize: input.size + 16 * Math.floor(input.size / (rs - 17)) fileSize: 21 + input.size + 16 * Math.floor(input.size / (rs - 17))
}; };
input = new BlobSliceStream(input, rs, mode); const inputStream = new BlobSliceStream(input, rs, mode);
const ts = new TransformStream(new ECETransformer(mode, key, rs, salt)); const ts = new TransformStream(new ECETransformer(mode, key, rs, salt));
this.stream = input.pipeThrough(ts); this.stream = inputStream.pipeThrough(ts);
} }
} }

View File

@ -51,25 +51,18 @@ export default class FileReceiver extends Nanobus {
this.state = 'ready'; this.state = 'ready';
} }
async streamToArrayBuffer(stream) { async streamToArrayBuffer(stream, streamSize) {
const reader = stream.getReader(); const reader = stream.getReader();
const chunks = []; const result = new Int8Array(streamSize);
let length = 0; let offset = 0;
let state = await reader.read(); let state = await reader.read();
while (!state.done) { while (!state.done) {
chunks.push(state.value); result.set(state.value, offset);
length += state.value.length; offset += state.value.length;
state = await reader.read(); state = await reader.read();
} }
const result = new Int8Array(length);
let offset = 0;
for (let i = 0; i < chunks.length; i++) {
result.set(chunks[i], offset);
offset += chunks[i].length;
}
return result.buffer; return result.buffer;
} }
@ -93,8 +86,10 @@ export default class FileReceiver extends Nanobus {
const dec = await this.keychain.decryptStream(ciphertext); const dec = await this.keychain.decryptStream(ciphertext);
const plainstream = dec.stream; const plainstream = dec.stream;
const plaintext = await this.streamToArrayBuffer(
const plaintext = await this.streamToArrayBuffer(plainstream); plainstream,
dec.streamInfo.fileSize
);
if (!noSave) { if (!noSave) {
await saveFile({ await saveFile({

View File

@ -65,11 +65,11 @@ export default class FileSender extends Nanobus {
this.msg = 'encryptingFile'; this.msg = 'encryptingFile';
this.emit('encrypting'); this.emit('encrypting');
const enc = await this.keychain.encryptStream(this.file); const enc = this.keychain.encryptStream(this.file);
const metadata = await this.keychain.encryptMetadata(this.file); const metadata = await this.keychain.encryptMetadata(this.file);
const authKeyB64 = await this.keychain.authKeyB64(); const authKeyB64 = await this.keychain.authKeyB64();
this.uploadRequest = await uploadWs( this.uploadRequest = uploadWs(
enc.stream, enc.stream,
enc.streamInfo, enc.streamInfo,
metadata, metadata,

View File

@ -179,12 +179,12 @@ export default class Keychain {
return ciphertext; return ciphertext;
} }
async encryptStream(plaintext) { encryptStream(plaintext) {
const enc = new ECE(plaintext, this.rawSecret, 'encrypt'); const enc = new ECE(plaintext, this.rawSecret, 'encrypt');
return enc; return enc;
} }
async decryptStream(encstream) { decryptStream(encstream) {
const dec = new ECE(encstream, this.rawSecret, 'decrypt'); const dec = new ECE(encstream, this.rawSecret, 'decrypt');
return dec; return dec;
} }

10
package-lock.json generated
View File

@ -4373,16 +4373,6 @@
"integrity": "sha512-KEyUw8AwRET2iFjFsI1EJQrJ/fHeGiJtgpYgEWG3yDv4l/To/m3a2GaYfeGyB3lsWdvbesjF5XCMx+SVBgAAYw==", "integrity": "sha512-KEyUw8AwRET2iFjFsI1EJQrJ/fHeGiJtgpYgEWG3yDv4l/To/m3a2GaYfeGyB3lsWdvbesjF5XCMx+SVBgAAYw==",
"requires": { "requires": {
"ws": "5.2.0" "ws": "5.2.0"
},
"dependencies": {
"ws": {
"version": "5.2.0",
"resolved": "https://registry.npmjs.org/ws/-/ws-5.2.0.tgz",
"integrity": "sha512-c18dMeW+PEQdDFzkhDsnBAlS4Z8KGStBQQUcQ5mf7Nf689jyGk0594L+i9RaQuf4gog6SvWLJorz2NfSaqxZ7w==",
"requires": {
"async-limiter": "1.0.0"
}
}
} }
}, },
"extend": { "extend": {

View File

@ -132,8 +132,7 @@
"mozlog": "^2.2.0", "mozlog": "^2.2.0",
"raven": "^2.4.2", "raven": "^2.4.2",
"redis": "^2.8.0", "redis": "^2.8.0",
"websocket-stream": "^5.1.2", "websocket-stream": "^5.1.2"
"ws": "^5.2.0"
}, },
"availableLanguages": [ "availableLanguages": [
"en-US", "en-US",

View File

@ -11,15 +11,13 @@ module.exports = async function(ws, req) {
let fileStream; let fileStream;
ws.on('close', e => { ws.on('close', e => {
if (e !== 1000) { if (e !== 1000 && fileStream !== undefined) {
if (fileStream !== undefined) {
fileStream.destroy(); fileStream.destroy();
} }
}
}); });
let first = true; let first = true;
ws.on('message', function(message) { ws.on('message', async function(message) {
try { try {
if (first) { if (first) {
const newId = crypto.randomBytes(5).toString('hex'); const newId = crypto.randomBytes(5).toString('hex');
@ -35,6 +33,7 @@ module.exports = async function(ws, req) {
error: 400 error: 400
}) })
); );
ws.close();
} }
const meta = { const meta = {
@ -69,6 +68,7 @@ module.exports = async function(ws, req) {
error: 500 error: 500
}) })
); );
ws.close();
} }
}); });
}; };

View File

@ -13,11 +13,11 @@ describe('API', function() {
describe('websocket upload', function() { describe('websocket upload', function() {
it('returns file info on success', async function() { it('returns file info on success', async function() {
const keychain = new Keychain(); const keychain = new Keychain();
const enc = await keychain.encryptStream(plaintext); const enc = keychain.encryptStream(plaintext);
const meta = await keychain.encryptMetadata(metadata); const meta = await keychain.encryptMetadata(metadata);
const verifierB64 = await keychain.authKeyB64(); const verifierB64 = await keychain.authKeyB64();
const p = function() {}; const p = function() {};
const up = await api.uploadWs( const up = api.uploadWs(
enc.stream, enc.stream,
enc.streamInfo, enc.streamInfo,
meta, meta,
@ -34,11 +34,11 @@ describe('API', function() {
it('can be cancelled', async function() { it('can be cancelled', async function() {
const keychain = new Keychain(); const keychain = new Keychain();
const enc = await keychain.encryptStream(plaintext); const enc = keychain.encryptStream(plaintext);
const meta = await keychain.encryptMetadata(metadata); const meta = await keychain.encryptMetadata(metadata);
const verifierB64 = await keychain.authKeyB64(); const verifierB64 = await keychain.authKeyB64();
const p = function() {}; const p = function() {};
const up = await api.uploadWs( const up = api.uploadWs(
enc.stream, enc.stream,
enc.streamInfo, enc.streamInfo,
meta, meta,