Fix bug with error when decoding

This commit is contained in:
Fabian Stamm 2020-07-10 18:54:32 +02:00
parent db5a363bff
commit 3c80ac3125
2 changed files with 718 additions and 651 deletions

View File

@ -4,7 +4,10 @@ import Refresh from "feather-icons/dist/icons/refresh-cw.svg";
import "./footer.scss";
import Notifications from "../notifications";
export class Footer extends Component<{}, { synced: boolean, syncing: boolean }> {
export class Footer extends Component<
{},
{ synced: boolean; syncing: boolean }
> {
constructor(props) {
super(props);
this.state = { synced: false, syncing: false };
@ -15,7 +18,7 @@ export class Footer extends Component<{}, { synced: boolean, syncing: boolean }>
async componentWillMount() {
Notes.syncedObservable.subscribe(this.onSyncedChange);
Notes.syncObservable.subscribe(this.onSyncChange);
this.setState({ synced: await Notes.isSynced() })
this.setState({ synced: await Notes.isSynced() });
}
componentWillUnmount() {
@ -25,18 +28,18 @@ export class Footer extends Component<{}, { synced: boolean, syncing: boolean }>
onSyncChange(state: boolean) {
console.log("sync", state);
this.setState({ syncing: state })
this.setState({ syncing: state });
}
onSyncedChange(state: boolean) {
console.log("synced", state);
this.setState({ synced: state })
this.setState({ synced: state });
}
onSyncClick() {
Notes.sync().then(() => {
Notifications.sendInfo("Finished Synchronisation");
})
});
}
render() {
@ -46,7 +49,7 @@ export class Footer extends Component<{}, { synced: boolean, syncing: boolean }>
if (this.state.syncing) {
color = "orange";
text = "syncing";
extrac = "reloading"
extrac = "reloading";
} else {
if (this.state.synced) {
color = "green";
@ -56,17 +59,21 @@ export class Footer extends Component<{}, { synced: boolean, syncing: boolean }>
text = "not synced";
}
}
return <footer class="elv-8">
return (
<footer class="elv-8">
<span>
<span class={extrac} ><a onClick={() => this.onSyncClick()} ><Refresh style="height: 1em; width: 1em;"></Refresh></a></span>
<span class={extrac}>
<a onClick={() => this.onSyncClick()}>
<Refresh style="height: 1em; width: 1em;"></Refresh>
</a>
</span>
<span style={"margin-left: 8px; color:" + color}>{text}</span>
</span>
<span>
Welcome <b>{Notes.name}</b>
</span>
<span style="color: lightgrey;">
v1.4
</span>
<span style="color: lightgrey;">v1.5</span>
</footer>
);
}
}

View File

@ -9,7 +9,6 @@ import * as b64 from "./helper/base64";
import IDB from "./helper/indexeddb";
import Notifications, { MessageType } from "./notifications";
export class HttpError extends Error {
constructor(public status: number, public statusText: string) {
super(statusText);
@ -46,7 +45,7 @@ const Decoder = new TextDecoder();
enum OpLogType {
CREATE,
CHANGE,
DELETE
DELETE,
}
interface OpLog {
@ -59,8 +58,8 @@ interface OpLog {
* The value
*/
values: {
value: Uint8Array,
preview: Uint8Array
value: Uint8Array;
preview: Uint8Array;
};
/**
@ -69,41 +68,43 @@ interface OpLog {
date: Date;
}
export type VaultList = { name: string, encrypted: boolean, id: string }[];
export type VaultList = { name: string; encrypted: boolean; id: string }[];
export interface IVault {
name: string;
id: string;
encrypted: boolean;
getAllNotes(): Promise<BaseNote[]>;
searchNote(term: string): Promise<BaseNote[]>
searchNote(term: string): Promise<BaseNote[]>;
newNote(): ViewNote;
saveNote(note: ViewNote, date?: Date): Promise<void>;
getNote(id: string): Promise<ViewNote>;
deleteNote(id: string): Promise<void>;
}
const awaitTimeout = (ms: number) => new Promise<void>(resolve => setTimeout(resolve, ms));
const awaitTimeout = (ms: number) =>
new Promise<void>((resolve) => setTimeout(resolve, ms));
class NotesProvider {
private notesObservableServer = new Observable<Note>()
public notesObservable = this.notesObservableServer.getPublicApi()
private notesObservableServer = new Observable<Note>();
public notesObservable = this.notesObservableServer.getPublicApi();
private syncObservableServer = new Observable<boolean>()
private syncObservableServer = new Observable<boolean>();
/**
* Will send false once finished and true on start
*/
public syncObservable = this.syncObservableServer.getPublicApi()
public syncObservable = this.syncObservableServer.getPublicApi();
private database = new IDB("notes", ["notes", "oplog"]);
private noteDB = this.database.getStore<DBNote>("notes");
private oplogDB = this.database.getStore<{ id: string, logs: OpLog[] }>("oplog");
private oplogDB = this.database.getStore<{ id: string; logs: OpLog[] }>(
"oplog"
);
private vaultKeys = new Map<string, Uint8Array>();
public apiLock = new Lock();
public apiLockRls = this.apiLock.getLock()
public apiLockRls = this.apiLock.getLock();
private syncLock = new Lock();
@ -115,7 +116,7 @@ class NotesProvider {
public syncedObservable = this.syncedObservableServer.getPublicApi();
public async isSynced() {
return (await this.oplogDB.getAll()).length <= 0
return (await this.oplogDB.getAll()).length <= 0;
}
private _name;
@ -128,11 +129,17 @@ class NotesProvider {
}
login() {
window.location.href = `${config.auth_server}/auth?client_id=${config.client_id}&scope=${config.permission}&redirect_uri=${encodeURIComponent(config.callback_url)}&response_type=code`
window.location.href = `${config.auth_server}/auth?client_id=${
config.client_id
}&scope=${config.permission}&redirect_uri=${encodeURIComponent(
config.callback_url
)}&response_type=code`;
}
async getToken(code: string) {
let req = await fetch(`${config.auth_server}/api/oauth/refresh?grant_type=authorization_code&client_id=${config.client_id}&code=${code}`);
let req = await fetch(
`${config.auth_server}/api/oauth/refresh?grant_type=authorization_code&client_id=${config.client_id}&code=${code}`
);
let res = await req.json();
if (!res.error) {
localStorage.setItem("refreshtoken", res.token);
@ -140,9 +147,9 @@ class NotesProvider {
this._name = res.profile.name;
let kb = this.passwordToKey(res.profile.enc_key);
localStorage.setItem("enc_key", b64.encode(kb));
this.generalEncryption = kb
this.generalEncryption = kb;
} else {
return "Invalid Code"
return "Invalid Code";
}
}
@ -155,11 +162,11 @@ class NotesProvider {
} catch (err) {
callback(err, null);
}
})
});
let key = localStorage.getItem("enc_key");
if (key) {
this.generalEncryption = b64.decode(key)
this.generalEncryption = b64.decode(key);
}
this._name = localStorage.getItem("name");
}
@ -168,34 +175,42 @@ class NotesProvider {
const next = () => {
setTimeout(() => {
this.sync().then(next);
}, 30000)
}
this.syncedObservableServer.send((await this.oplogDB.getAll()).length <= 0);
let prs = this.apiLockRls.then(lock => lock.release());
prs.then(() => awaitTimeout(2000)).then(() => this.sync()).then(() => next());
}, 30000);
};
this.syncedObservableServer.send(
(await this.oplogDB.getAll()).length <= 0
);
let prs = this.apiLockRls.then((lock) => lock.release());
prs.then(() => awaitTimeout(2000))
.then(() => this.sync())
.then(() => next());
return prs;
}
private async getJWT() {
let lock = await this.apiLock.getLock()
let lock = await this.apiLock.getLock();
try {
console.log("Getting JWT");
let req = await fetch(config.auth_server + "/api/oauth/jwt?refreshtoken=" + localStorage.getItem("refreshtoken"));
let req = await fetch(
config.auth_server +
"/api/oauth/jwt?refreshtoken=" +
localStorage.getItem("refreshtoken")
);
if (req.status !== 200) {
Notifications.sendNotification("offline", MessageType.INFO);
throw new Error("Offline")
throw new Error("Offline");
}
let res = await req.json();
if (res.error) {
console.log("Refresh token invalid, forward to login")
console.log("Refresh token invalid, forward to login");
localStorage.removeItem("refreshtoken");
this.login()
throw new Error("Need login!")
this.login();
throw new Error("Need login!");
} else {
return res.token
return res.token;
}
} finally {
lock.release()
lock.release();
}
}
@ -205,29 +220,38 @@ class NotesProvider {
}
async sync() {
const lock = await this.syncLock.getLock()
const lock = await this.syncLock.getLock();
const log = (...message: any[]) => {
console.log("[SYNC]: ", ...message)
}
console.log("[SYNC]: ", ...message);
};
this.syncObservableServer.send(true);
log("Start")
log("Start");
try {
log("Fetching");
let [remotes, locals, oplogs] = await Promise.all([this._secureFile.list(), this.noteDB.getAll(), this.oplogDB.getAll()]);
let [remotes, locals, oplogs] = await Promise.all([
this._secureFile.list(),
this.noteDB.getAll(),
this.oplogDB.getAll(),
]);
log("Fetched");
// Create sync pairs (remote & local)
log("Building pairs");
let pairs: { local: DBNote, remote: IFile, id: string, oplog: OpLog[] }[] = [];
remotes.map(r => {
let lIdx = locals.findIndex(e => e._id === r._id);
let pairs: {
local: DBNote;
remote: IFile;
id: string;
oplog: OpLog[];
}[] = [];
remotes.map((r) => {
let lIdx = locals.findIndex((e) => e._id === r._id);
let l: DBNote = undefined;
if (lIdx >= 0) {
l = locals[lIdx];
locals.splice(lIdx, 1);
}
let oIdx = oplogs.findIndex(e => e.id === r._id);
let oIdx = oplogs.findIndex((e) => e.id === r._id);
let oplog: OpLog[];
if (oIdx >= 0) {
oplog = oplogs[oIdx].logs;
@ -238,12 +262,12 @@ class NotesProvider {
remote: r,
local: l,
oplog,
id: r._id
})
})
id: r._id,
});
});
locals.forEach(l => {
let oIdx = oplogs.findIndex(e => e.id === l._id);
locals.forEach((l) => {
let oIdx = oplogs.findIndex((e) => e.id === l._id);
let oplog: OpLog[] = undefined;
if (oIdx >= 0) {
oplog = oplogs[oIdx].logs;
@ -255,20 +279,20 @@ class NotesProvider {
remote: undefined,
local: l,
oplog,
id: l._id
})
})
id: l._id,
});
});
oplogs.forEach(oplog => {
oplogs.forEach((oplog) => {
if (!oplog) return;
if (oplog.logs.length > 0)
pairs.push({
remote: undefined,
local: undefined,
oplog: oplog.logs,
id: oplog.id
})
})
id: oplog.id,
});
});
log("Pairs builded");
@ -279,8 +303,8 @@ class NotesProvider {
local_change: 0,
local_delete: 0,
do_nothing: 0,
error: 0
}
error: 0,
};
log("Start inspection");
for (let { local, remote, oplog, id } of pairs) {
@ -291,13 +315,19 @@ class NotesProvider {
case OpLogType.CHANGE:
log(id, "REMOTE CHANGE");
stats.remote_change++;
await this._secureFile.update(id, op.values.value, b64.encode(op.values.preview), op.date, old);
await this._secureFile.update(
id,
op.values.value,
b64.encode(op.values.preview),
op.date,
old
);
break;
case OpLogType.DELETE:
log(id, "REMOTE DELETE");
stats.remote_delete++;
if (old) break; // if the deletion is old, just ignore
await this._secureFile.delete(id)
await this._secureFile.delete(id);
break;
case OpLogType.CREATE:
log(id, "REMOTE CREATE");
@ -314,11 +344,11 @@ class NotesProvider {
break;
}
}
}
};
const localChange = (id: string) => {
//TODO implement
}
};
const create = async () => {
log(id, "LOCAL CREATAE/UPDATE");
@ -327,42 +357,43 @@ class NotesProvider {
let note: DBNote = {
_id: remote._id,
folder: remote.folder,
preview: b64.decode(remote.active.preview),
preview: b64.decode(remote.active.preview || ""),
time: remote.active.time,
__value: new Uint8Array(value)
}
__value: new Uint8Array(value),
};
await this.noteDB.set(id, note);
localChange(id);
}
};
try {
// log(id, "LRO: ", !!local, !!remote, !!oplog)
if (remote && !oplog) {
if (local) {
let old = remote.active.time.valueOf() > local.time.valueOf();
if (old)
await create()
let old =
remote.active.time.valueOf() > local.time.valueOf();
if (old) await create();
else {
stats.do_nothing++;
log(id, "DO NOTHING");
}
} else {
await create()
await create();
}
} else if (!remote && local && !oplog) { // No local changes, but remote deleted
} else if (!remote && local && !oplog) {
// No local changes, but remote deleted
log("LOCAL DELETE");
stats.local_delete++;
await this.noteDB.delete(id);
localChange(id);
} else if (!remote && oplog) { // Remote does not exist, but oplog, just apply all changes including possible delete
await apply()
} else if (!remote && oplog) {
// Remote does not exist, but oplog, just apply all changes including possible delete
await apply();
} else if (remote && oplog) {
let last = oplog[oplog.length - 1]
let last = oplog[oplog.length - 1];
let old = remote.active.time.valueOf() > last.date.valueOf();
if (old)
await create() // Will recreate local entry
await apply(old) // Will apply changes to remote
if (old) await create(); // Will recreate local entry
await apply(old); // Will apply changes to remote
} else {
log(id, "DO NOTHING");
stats.do_nothing++;
@ -370,15 +401,20 @@ class NotesProvider {
} catch (err) {
console.error(err);
stats.error++;
Notifications.sendNotification("Error syncing: " + id, MessageType.ERROR);
Notifications.sendNotification(
"Error syncing: " + id,
MessageType.ERROR
);
}
await this.oplogDB.delete(id);
}
log("Stats", stats);
this.syncedObservableServer.send((await this.oplogDB.getAll()).length <= 0)
this.syncedObservableServer.send(
(await this.oplogDB.getAll()).length <= 0
);
} finally {
log("Finished")
lock.release()
log("Finished");
lock.release();
this.syncObservableServer.send(false);
}
}
@ -408,15 +444,16 @@ class NotesProvider {
}
public getVaults(): Promise<VaultList> {
return this.noteDB.getAll()
.then(notes => notes
.filter(e => Decoder.decode(e.preview) === "__VAULT__")
.map(e => {
return this.noteDB.getAll().then((notes) =>
notes
.filter((e) => Decoder.decode(e.preview) === "__VAULT__")
.map((e) => {
let value = e.__value;
let encrypted = false;
if (this.decrypt(value) !== "__BASELINE__") encrypted = true;
return { name: e.folder, encrypted, id: e._id }
}));
return { name: e.folder, encrypted, id: e._id };
})
);
}
public async createVault(name: string, key?: Uint8Array) {
@ -425,35 +462,44 @@ class NotesProvider {
_id: uuidv4(),
folder: name,
preview: Encoder.encode("__VAULT__"),
time: new Date()
}
time: new Date(),
};
let tx = this.database.transaction();
await Promise.all([
this.addop(vault._id, OpLogType.CREATE, {
this.addop(
vault._id,
OpLogType.CREATE,
{
value: vault.__value,
preview: vault.preview
}, vault.time, tx),
this.noteDB.set(vault._id, vault)
preview: vault.preview,
},
vault.time,
tx
),
this.noteDB.set(vault._id, vault),
]);
}
public async getVault(vault_id: string, key?: Uint8Array): Promise<IVault> {
let vault = await this.noteDB.get(vault_id);
if (!vault) throw new Error("Vault not found!");
if (this.decrypt(vault.__value, key) !== "__BASELINE__") throw new Error("Invalid password!");
return new NotesProvider.Vault(vault, key)
if (this.decrypt(vault.__value, key) !== "__BASELINE__")
throw new Error("Invalid password!");
return new NotesProvider.Vault(vault, key);
}
public async deleteVault(vault_id: string) {
let vault = await this.noteDB.get(vault_id);
if (!vault) throw new Error("Vault not found!");
let v = new NotesProvider.Vault(vault);
await Promise.all((await v.getAllNotes()).map(note => this.delete(note._id)));
await Promise.all(
(await v.getAllNotes()).map((note) => this.delete(note._id))
);
await this.delete(v.id); // This can also delete a vault
}
public passwordToKey(password: string) {
return new Uint8Array(sha256.arrayBuffer(password + config.client_id))
return new Uint8Array(sha256.arrayBuffer(password + config.client_id));
}
private _encrypt(value: Uint8Array, key?: Uint8Array): Uint8Array {
@ -464,42 +510,48 @@ class NotesProvider {
}
private encrypt(value: string, key?: Uint8Array): Uint8Array {
let msg = this._encrypt(Encoder.encode(value), key)
return new Uint8Array(this._encrypt(msg, this.generalEncryption))
let msg = this._encrypt(Encoder.encode(value), key);
return new Uint8Array(this._encrypt(msg, this.generalEncryption));
}
private _decrypt(value: ArrayBuffer, key?: Uint8Array): Uint8Array {
if (!key) return new Uint8Array(value);
var aesCtr = new aesjs.ModeOfOperation.ctr(key);
var decryptedBytes = aesCtr.decrypt(value);
return new Uint8Array(decryptedBytes)
return new Uint8Array(decryptedBytes);
}
private decrypt(value: ArrayBuffer, key?: Uint8Array): string {
let msg = this._decrypt(value, key)
return Decoder.decode(this._decrypt(msg, this.generalEncryption))
let msg = this._decrypt(value, key);
return Decoder.decode(this._decrypt(msg, this.generalEncryption));
}
async addop(note_id: string, type: OpLogType, values: { value: Uint8Array, preview: Uint8Array }, date: Date, transaction?: IDBPTransaction<unknown, string[]>) {
async addop(
note_id: string,
type: OpLogType,
values: { value: Uint8Array; preview: Uint8Array },
date: Date,
transaction?: IDBPTransaction<unknown, string[]>
) {
let tx = transaction || this.oplogDB.transaction();
let oplog = await this.oplogDB.get(note_id, tx);
if (!oplog) oplog = { logs: [], id: note_id };
oplog.logs.push({
date: date,
type,
values
})
values,
});
this.syncedObservableServer.send(false);
await this.oplogDB.set(note_id, oplog, tx);
}
async delete(id: string) {
let lock = await this.syncLock.getLock();
let tx = this.database.transaction(this.oplogDB, this.noteDB)
let tx = this.database.transaction(this.oplogDB, this.noteDB);
await Promise.all([
this.addop(id, OpLogType.DELETE, null, new Date(), tx),
this.noteDB.delete(id, tx)
])
this.noteDB.delete(id, tx),
]);
lock.release();
}
@ -522,15 +574,17 @@ class NotesProvider {
}
async getAllNotes() {
return Notes.noteDB.getAll()
.then(all => all.filter(e => e.folder === this.vault._id)
return Notes.noteDB.getAll().then((all) =>
all
.filter((e) => e.folder === this.vault._id)
.sort(this.sort)
.map<BaseNote>(e => {
let new_note = clonedeep(<Note>e) as BaseNote
delete (<any>new_note).__value
new_note.preview = this.decrypt(e.preview)
.map<BaseNote>((e) => {
let new_note = clonedeep(<Note>e) as BaseNote;
delete (<any>new_note).__value;
new_note.preview = this.decrypt(e.preview);
return new_note;
}));
})
);
}
private sort(a: DBNote, b: DBNote) {
@ -539,7 +593,7 @@ class NotesProvider {
async searchNote(term: string) {
let all = await this.getAllNotes();
return all.filter(e => e.preview.indexOf(term) >= 0)
return all.filter((e) => e.preview.indexOf(term) >= 0);
}
newNote(): ViewNote {
@ -548,8 +602,8 @@ class NotesProvider {
folder: this.vault._id,
time: new Date(),
__value: "",
preview: ""
}
preview: "",
};
}
async saveNote(note: ViewNote, date = new Date()) {
@ -558,20 +612,26 @@ class NotesProvider {
let old_note = await Notes.noteDB.get(note._id, tx);
let new_note = clonedeep(<Note>note) as DBNote;
new_note.__value = this.encrypt(note.__value)
new_note.__value = this.encrypt(note.__value);
let [title, preview] = note.__value.split("\n");
if (preview) preview = "\n" + preview;
else preview = ""
new_note.preview = this.encrypt((title + preview).substr(0, 128))
else preview = "";
new_note.preview = this.encrypt((title + preview).substr(0, 128));
new_note.time = date;
await Promise.all([
Notes.addop(note._id, !old_note ? OpLogType.CREATE : OpLogType.CHANGE, {
Notes.addop(
note._id,
!old_note ? OpLogType.CREATE : OpLogType.CHANGE,
{
value: new_note.__value,
preview: new_note.preview
}, date, tx),
Notes.noteDB.set(note._id, new_note, tx)
])
preview: new_note.preview,
},
date,
tx
),
Notes.noteDB.set(note._id, new_note, tx),
]);
lock.release();
}
@ -586,10 +646,10 @@ class NotesProvider {
deleteNote(id: string) {
return Notes.delete(id);
}
}
};
}
const Notes = new NotesProvider()
const Notes = new NotesProvider();
export default Notes;
(<any>window).api = Notes
(<any>window).api = Notes;