sw
This commit is contained in:
parent
38ef52d3ba
commit
62ed0a411f
16 changed files with 245 additions and 133 deletions
58
app/api.js
58
app/api.js
|
@ -1,4 +1,7 @@
|
|||
import { arrayToB64, b64ToArray, delay } from './utils';
|
||||
import { ReadableStream as PolyRS} from 'web-streams-polyfill';
|
||||
import { createReadableStreamWrapper } from '@mattiasbuelens/web-streams-adapter';
|
||||
const RS = createReadableStreamWrapper(PolyRS);
|
||||
|
||||
function post(obj) {
|
||||
return {
|
||||
|
@ -201,38 +204,32 @@ export function uploadWs(encrypted, info, metadata, verifierB64, onprogress) {
|
|||
|
||||
async function downloadS(id, keychain, onprogress, signal) {
|
||||
const auth = await keychain.authHeader();
|
||||
try {
|
||||
const response = await fetch(`/api/download/${id}`, {
|
||||
signal: signal ,
|
||||
method: 'GET',
|
||||
headers: {'Authorization': auth}
|
||||
});
|
||||
|
||||
if (response.status !== 200) {
|
||||
throw new Error(response.status);
|
||||
}
|
||||
const response = await fetch(`/api/download/${id}`, {
|
||||
signal: signal,
|
||||
method: 'GET',
|
||||
headers: { Authorization: auth }
|
||||
});
|
||||
|
||||
const authHeader = response.headers.get('WWW-Authenticate');
|
||||
if (authHeader) {
|
||||
keychain.nonce = parseNonce(authHeader);
|
||||
}
|
||||
|
||||
const fileSize = response.headers.get('Content-Length');
|
||||
onprogress([0, fileSize]);
|
||||
|
||||
console.log(response.body);
|
||||
if (response.body) {
|
||||
return response.body;
|
||||
}
|
||||
return response.blob();
|
||||
|
||||
} catch (err) {
|
||||
if (err.name === 'AbortError') {
|
||||
throw new Error('0');
|
||||
} else {
|
||||
throw err;
|
||||
}
|
||||
if (response.status !== 200) {
|
||||
throw new Error(response.status);
|
||||
}
|
||||
|
||||
const authHeader = response.headers.get('WWW-Authenticate');
|
||||
if (authHeader) {
|
||||
keychain.nonce = parseNonce(authHeader);
|
||||
}
|
||||
|
||||
const fileSize = response.headers.get('Content-Length');
|
||||
|
||||
//right now only chrome allows obtaining a stream from fetch
|
||||
//for other browsers we fetch as a blob and convert to polyfill stream later
|
||||
if (response.body) {
|
||||
console.log("STREAM")
|
||||
return RS(response.body);
|
||||
}
|
||||
return response.blob();
|
||||
|
||||
}
|
||||
|
||||
async function tryDownloadStream(id, keychain, onprogress, signal, tries = 1) {
|
||||
|
@ -243,6 +240,9 @@ async function tryDownloadStream(id, keychain, onprogress, signal, tries = 1) {
|
|||
if (e.message === '401' && --tries > 0) {
|
||||
return tryDownloadStream(id, keychain, onprogress, signal, tries);
|
||||
}
|
||||
if (e.name === 'AbortError') {
|
||||
throw new Error('0');
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
|
85
app/ece.js
85
app/ece.js
|
@ -1,12 +1,15 @@
|
|||
require('buffer');
|
||||
import { TransformStream } from 'web-streams-polyfill';
|
||||
import { TransformStream as PolyTS, ReadableStream as PolyRS } from 'web-streams-polyfill';
|
||||
import { createReadableStreamWrapper, createTransformStreamWrapper } from '@mattiasbuelens/web-streams-adapter';
|
||||
const toTS = createTransformStreamWrapper(PolyTS);
|
||||
const toRS = createReadableStreamWrapper(PolyRS);
|
||||
|
||||
const NONCE_LENGTH = 12;
|
||||
const TAG_LENGTH = 16;
|
||||
const KEY_LENGTH = 16;
|
||||
const MODE_ENCRYPT = 'encrypt';
|
||||
const MODE_DECRYPT = 'decrypt';
|
||||
const RS = 1048576;
|
||||
const RS = 1024 * 1024;
|
||||
|
||||
const encoder = new TextEncoder();
|
||||
|
||||
|
@ -218,13 +221,14 @@ class ECETransformer {
|
|||
}
|
||||
|
||||
async flush(controller) {
|
||||
//console.log('ece stream ends')
|
||||
if (this.prevChunk) {
|
||||
await this.transformPrevChunk(true, controller);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class BlobSlicer {
|
||||
export class BlobSlicer {
|
||||
constructor(blob, rs, mode) {
|
||||
this.blob = blob;
|
||||
this.index = 0;
|
||||
|
@ -262,28 +266,27 @@ class StreamSlicer {
|
|||
constructor(rs, mode) {
|
||||
this.mode = mode;
|
||||
this.rs = rs;
|
||||
this.chunkSize = mode === MODE_ENCRYPT ? rs - 17 : 21;
|
||||
this.chunkSize = (mode === MODE_ENCRYPT) ? (rs - 17) : 21;
|
||||
this.partialChunk = new Uint8Array(this.chunkSize); //where partial chunks are saved
|
||||
this.offset = 0;
|
||||
this.offset = 0;
|
||||
}
|
||||
|
||||
send(buf, controller) {
|
||||
//console.log("sent a record")
|
||||
controller.enqueue(buf);
|
||||
if (this.chunkSize === 21) {
|
||||
if (this.chunkSize === 21 && this.mode === MODE_DECRYPT) {
|
||||
this.chunkSize = this.rs;
|
||||
this.partialChunk = new Uint8Array(this.chunkSize);
|
||||
}
|
||||
this.partialChunk = new Uint8Array(this.chunkSize);
|
||||
}
|
||||
|
||||
//reslice input uint8arrays into record sized chunks
|
||||
//reslice input into record sized chunks
|
||||
transform(chunk, controller) {
|
||||
//console.log('Received chunk') // with %d bytes.', chunk.byteLength)
|
||||
//console.log('Received chunk with %d bytes.', chunk.byteLength)
|
||||
let i = 0;
|
||||
|
||||
if (this.offset > 0) { //send off the partial chunk
|
||||
if (this.offset > 0) {
|
||||
const len = Math.min(chunk.byteLength, (this.chunkSize - this.offset));
|
||||
this.partialChunk.set((chunk.slice(0, len)), this.offset);
|
||||
this.partialChunk.set(chunk.slice(0, len), this.offset);
|
||||
this.offset += len;
|
||||
i += len;
|
||||
|
||||
|
@ -293,32 +296,41 @@ class StreamSlicer {
|
|||
}
|
||||
}
|
||||
|
||||
while (i < chunk.byteLength) { //send off whole records and stick last bit in partialChunk
|
||||
if ((chunk.byteLength - i) > this.chunkSize) {
|
||||
while (i < chunk.byteLength) {
|
||||
if ((chunk.byteLength - i) >= this.chunkSize) {
|
||||
const record = chunk.slice(i, i + this.chunkSize);
|
||||
i += this.chunkSize;
|
||||
this.send(record, controller);
|
||||
} else {
|
||||
const end = chunk.slice(i, end);
|
||||
const end = chunk.slice(i, this.chunkSize);
|
||||
i += end.length;
|
||||
this.partialChunk.set(end);
|
||||
this.offset = end.length;
|
||||
i += end.length;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
flush(controller) {
|
||||
//console.log('slice stream ends')
|
||||
if (this.offset > 0) {
|
||||
console.log("sent a partial record")
|
||||
controller.enqueue(this.partialChunk.slice(0, this.offset));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async function stream2blob(stream) {
|
||||
const chunks = [];
|
||||
const reader = stream.getReader();
|
||||
let state = await reader.read();
|
||||
while (!state.done) {
|
||||
chunks.push(state.value);
|
||||
state = await reader.read();
|
||||
}
|
||||
return new Blob(chunks);
|
||||
}
|
||||
|
||||
/*
|
||||
input: a blob or a readable stream containing data to be transformed
|
||||
input: a blob or a ReadableStream containing data to be transformed
|
||||
key: Uint8Array containing key of size KEY_LENGTH
|
||||
mode: string, either 'encrypt' or 'decrypt'
|
||||
rs: int containing record size, optional
|
||||
|
@ -326,26 +338,37 @@ salt: ArrayBuffer containing salt of KEY_LENGTH length, optional
|
|||
*/
|
||||
export default class ECE {
|
||||
constructor(input, key, mode, rs, salt) {
|
||||
this.input = input;
|
||||
this.key = key;
|
||||
this.mode = mode;
|
||||
this.rs = rs;
|
||||
this.salt = salt;
|
||||
if (rs === undefined) {
|
||||
rs = RS;
|
||||
this.rs = RS;
|
||||
}
|
||||
if (salt === undefined) {
|
||||
salt = generateSalt(KEY_LENGTH);
|
||||
this.salt = generateSalt(KEY_LENGTH);
|
||||
}
|
||||
}
|
||||
|
||||
info() {
|
||||
return {
|
||||
recordSize: this.rs,
|
||||
fileSize: 21 + this.input.size + 16 * Math.floor(this.input.size / (this.rs - 17))
|
||||
};
|
||||
}
|
||||
|
||||
transform() {
|
||||
let inputStream;
|
||||
if (input instanceof Blob) {
|
||||
this.streamInfo = {
|
||||
recordSize: rs,
|
||||
fileSize: 21 + input.size + 16 * Math.floor(input.size / (rs - 17))
|
||||
};
|
||||
inputStream = new ReadableStream(new BlobSlicer(input, rs, mode));
|
||||
|
||||
if (this.input instanceof Blob) {
|
||||
inputStream = toRS(new ReadableStream(new BlobSlicer(this.input, this.rs, this.mode)));
|
||||
} else {
|
||||
const sliceStream = new TransformStream(new StreamSlicer(rs, mode));
|
||||
inputStream = input.pipeThrough(sliceStream);
|
||||
const sliceStream = toTS(new TransformStream(new StreamSlicer(this.rs, this.mode)));
|
||||
inputStream = this.input.pipeThrough(sliceStream);
|
||||
}
|
||||
|
||||
const ts = new TransformStream(new ECETransformer(mode, key, rs, salt));
|
||||
this.stream = inputStream.pipeThrough(ts);
|
||||
const cryptoStream = toTS(new TransformStream(new ECETransformer(this.mode, this.key, this.rs, this.salt)));
|
||||
return inputStream.pipeThrough(cryptoStream);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,6 +36,12 @@ export default function(state, emitter) {
|
|||
}
|
||||
}
|
||||
|
||||
function register() {
|
||||
navigator.serviceWorker.register('/serviceWorker.js')
|
||||
.then( reg => console.log("registration successful or already installed"))
|
||||
.catch( e => console.log(e) );
|
||||
}
|
||||
|
||||
function updateProgress() {
|
||||
if (updateTitle) {
|
||||
emitter.emit('DOMTitleChange', percent(state.transfer.progressRatio));
|
||||
|
@ -162,6 +168,13 @@ export default function(state, emitter) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
const info = {
|
||||
key: file.secretKey,
|
||||
nonce: file.nonce
|
||||
}
|
||||
navigator.serviceWorker.controller.postMessage(info);
|
||||
|
||||
render();
|
||||
});
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import Nanobus from 'nanobus';
|
||||
import Keychain from './keychain';
|
||||
import { bytes } from './utils';
|
||||
import { metadata, downloadFile, downloadStream} from './api';
|
||||
import { metadata, downloadFile, downloadStream } from './api';
|
||||
|
||||
export default class FileReceiver extends Nanobus {
|
||||
constructor(fileInfo) {
|
||||
|
@ -51,89 +51,56 @@ export default class FileReceiver extends Nanobus {
|
|||
this.state = 'ready';
|
||||
}
|
||||
|
||||
/*
|
||||
async streamToArrayBuffer(stream, streamSize) {
|
||||
try {
|
||||
var finish;
|
||||
const promise = new Promise((resolve) => {
|
||||
finish = resolve;
|
||||
});
|
||||
const result = new Uint8Array(streamSize);
|
||||
let offset = 0;
|
||||
|
||||
|
||||
const writer = new WritableStream(
|
||||
{
|
||||
write(chunk) {
|
||||
result.set(state.value, offset);
|
||||
offset += state.value.length;
|
||||
},
|
||||
close() {
|
||||
//resolve a promise or something
|
||||
finish.resolve();
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
stream.pipeTo(writer);
|
||||
|
||||
await promise;
|
||||
return result.slice(0, offset).buffer;
|
||||
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
async streamToArrayBuffer(stream, streamSize) {
|
||||
async streamToArrayBuffer(stream, streamSize, onprogress) {
|
||||
try {
|
||||
const result = new Uint8Array(streamSize);
|
||||
let offset = 0;
|
||||
console.log("reading...")
|
||||
const reader = stream.getReader();
|
||||
let state = await reader.read();
|
||||
console.log("read done")
|
||||
while (!state.done) {
|
||||
result.set(state.value, offset);
|
||||
offset += state.value.length;
|
||||
state = await reader.read();
|
||||
onprogress([offset, streamSize]);
|
||||
}
|
||||
|
||||
onprogress([streamSize, streamSize]);
|
||||
return result.slice(0, offset).buffer;
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
console.log(e);
|
||||
throw (e);
|
||||
}
|
||||
}
|
||||
|
||||
async download(noSave = false) {
|
||||
this.state = 'downloading';
|
||||
this.downloadRequest = await downloadStream(
|
||||
this.fileInfo.id,
|
||||
this.keychain,
|
||||
p => {
|
||||
this.progress = p;
|
||||
this.emit('progress');
|
||||
}
|
||||
);
|
||||
const onprogress = p => {
|
||||
this.progress = p;
|
||||
this.emit('progress');
|
||||
}
|
||||
|
||||
try {
|
||||
this.state = 'downloading';
|
||||
this.downloadRequest = downloadStream(
|
||||
this.fileInfo.id,
|
||||
this.keychain
|
||||
);
|
||||
|
||||
const ciphertext = await this.downloadRequest.result;
|
||||
onprogress([0, this.fileInfo.size]);
|
||||
const download = await this.downloadRequest.result;
|
||||
const plainstream = this.keychain.decryptStream(download);
|
||||
|
||||
//temporary
|
||||
const plaintext = await this.streamToArrayBuffer(
|
||||
plainstream,
|
||||
this.fileInfo.size,
|
||||
onprogress
|
||||
);
|
||||
this.downloadRequest = null;
|
||||
|
||||
this.msg = 'decryptingFile';
|
||||
this.state = 'decrypting';
|
||||
this.emit('decrypting');
|
||||
|
||||
const dec = this.keychain.decryptStream(ciphertext);
|
||||
|
||||
let plaintext = await this.streamToArrayBuffer(
|
||||
dec.stream,
|
||||
this.fileInfo.size
|
||||
);
|
||||
|
||||
if (plaintext === undefined) { plaintext = (new Uint8Array(1)).buffer; }
|
||||
|
||||
if (!noSave) {
|
||||
await saveFile({
|
||||
plaintext,
|
||||
|
@ -144,7 +111,6 @@ export default class FileReceiver extends Nanobus {
|
|||
|
||||
this.msg = 'downloadFinish';
|
||||
this.state = 'complete';
|
||||
|
||||
} catch (e) {
|
||||
this.downloadRequest = null;
|
||||
throw e;
|
||||
|
|
|
@ -65,7 +65,7 @@ export default class FileSender extends Nanobus {
|
|||
this.msg = 'encryptingFile';
|
||||
this.emit('encrypting');
|
||||
|
||||
const enc = this.keychain.encryptStream(this.file);
|
||||
const enc = await this.keychain.encryptStream(this.file);
|
||||
const metadata = await this.keychain.encryptMetadata(this.file);
|
||||
const authKeyB64 = await this.keychain.authKeyB64();
|
||||
|
||||
|
|
|
@ -180,13 +180,16 @@ export default class Keychain {
|
|||
}
|
||||
|
||||
encryptStream(plaintext) {
|
||||
const enc = new ECE(plaintext, this.rawSecret, 'encrypt');
|
||||
return enc;
|
||||
const ece = new ECE(plaintext, this.rawSecret, 'encrypt');
|
||||
return {
|
||||
stream: ece.transform(),
|
||||
streamInfo: ece.info()
|
||||
};
|
||||
}
|
||||
|
||||
decryptStream(encstream) {
|
||||
const dec = new ECE(encstream, this.rawSecret, 'decrypt');
|
||||
return dec;
|
||||
decryptStream(cryptotext) {
|
||||
const ece = new ECE(cryptotext, this.rawSecret, 'decrypt');
|
||||
return ece.transform();
|
||||
}
|
||||
|
||||
async decryptFile(ciphertext) {
|
||||
|
|
|
@ -9,11 +9,18 @@ import storage from './storage';
|
|||
import metrics from './metrics';
|
||||
import experiments from './experiments';
|
||||
import Raven from 'raven-js';
|
||||
import assets from '../common/assets';
|
||||
|
||||
if (navigator.doNotTrack !== '1' && window.RAVEN_CONFIG) {
|
||||
Raven.config(window.SENTRY_ID, window.RAVEN_CONFIG).install();
|
||||
}
|
||||
|
||||
function register(state, emitter) {
|
||||
navigator.serviceWorker.register('serviceWorker.js')
|
||||
.then( reg => console.log("registration successful or already installed"))
|
||||
.catch( e => console.log(e) );
|
||||
}
|
||||
|
||||
app.use((state, emitter) => {
|
||||
state.transfer = null;
|
||||
state.fileInfo = null;
|
||||
|
@ -44,6 +51,7 @@ app.use((state, emitter) => {
|
|||
});
|
||||
});
|
||||
|
||||
app.use(register);
|
||||
app.use(metrics);
|
||||
app.use(fileManager);
|
||||
app.use(dragManager);
|
||||
|
|
38
app/serviceWorker.js
Normal file
38
app/serviceWorker.js
Normal file
|
@ -0,0 +1,38 @@
|
|||
import Keychain from './keychain';
|
||||
|
||||
self.addEventListener('install', (event) => {
|
||||
console.log("install event on sw")
|
||||
self.skipWaiting();
|
||||
});
|
||||
|
||||
async function decryptStream(request) {
|
||||
console.log("DOWNLOAD FETCH")
|
||||
//make actual request to server, get response back, decrypt it, send it
|
||||
const response = await fetch(req,
|
||||
{
|
||||
method: 'GET',
|
||||
headers: { Authorization: auth }
|
||||
}
|
||||
);
|
||||
|
||||
if (response.status !== 200) {
|
||||
console.log(response.status)
|
||||
throw new Error(response.status);
|
||||
}
|
||||
|
||||
const body = response.body;
|
||||
console.log(body);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
self.onfetch = (event) => {
|
||||
const req = event.request.clone();
|
||||
if (req.url.includes('/api/download')) {
|
||||
event.respondWith(decryptStream(req));
|
||||
}
|
||||
};
|
||||
|
||||
self.onmessage = (event) => {
|
||||
self.keychain = new Keychain(event.data.key, event.data.nonce);
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue