generated from sindresorhus/electron-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.js
executable file
·392 lines (358 loc) · 10.1 KB
/
backup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/**
* Backup
*
* This file contains all the logic related to backing up an SSB identity.
*
* The backup workflow is composed of multiple phases. Each phase is actually a
* [Mithril route](https://mithril.js.org/index.html#routing), so by changing
* routes, we move through phases.
*/
// dependencies needed...
const m = require("mithril")
const electron = require('electron')
const marked = require("marked")
const path = require("path")
const fs = require("fs")
const { dialog } = require("electron").remote
const Config = require("ssb-config/inject")
const ssbKeys = require("ssb-keys")
const ssbClient = require("ssb-client")
const ssbAvatar = require("ssb-avatar")
const {
MessageWithButton,
MessageWithIndeterminateProgress,
getDataUriFromURL
} = require("./common.js")
// get SSB configuration and data files...
const appName = process.env.ssb_appname || "ssb"
const config = Config(appName)
const gossipFile = path.join(config.path, "gossip.json")
const connFile = path.join(config.path, "conn.json")
const secretFile = path.join(config.path, "secret")
// script local variables used by multiple components...
let backup = {}
let sbot = false
let attemptedToStartBuiltinServer = false
/**
* start - phase 1 of backup
*
* Present a message to the user and wait for confirmation
* before moving along the automated backup phases.
*/
const start = {
view: function (vnode) {
return m(MessageWithButton, {
content: `
Backing up your identity generates a file on your computer with all the data required to
restore your identity in a future moment. This file is very important and you should take
good care of it. If someone else gets their hands on this file, they will be able to impersonate
you and even break your feed.
`,
cta: "Create Backup",
link: "/backup/checkssbfolder"
})
}
}
/**
* checkssbfolder - phase 2 of backup
*
* Get data from the files inside `~/.ssb` folder.
* The data retrieved in this phase is already enough to create
* a restorable backup. This means that to backup an SSB identity
* we don"t need a working ssb-server as long as the files on disk
* are correct.
*
* All the other phases gather extra data to make the backup stronger
* and more friendly.
*/
const checkssbfolder = {
oncreate: function (vnode) {
if (fs.existsSync(secretFile)) {
backup = {
exportDate: new Date().toISOString(),
secret: fs.readFileSync(secretFile, "utf8"),
feed: config.keys.public
}
} else {
m.route.set("/backup/nosecret")
}
if (fs.existsSync(gossipFile)) {
backup.gossip = require(gossipFile)
}
if (fs.existsSync(connFile)) {
backup.conn = require(connFile)
}
console.log("backup", backup)
m.route.set("/backup/connect")
},
view: function (vnode) {
return m(MessageWithIndeterminateProgress, {
content: `Getting data directly from the files in \`~/.ssb\` folder.`
})
}
}
/**
* connect - phase 3 of backup
*
* Connect to the running SSB server and make sbot available
* to the next phases.
*/
const connect = {
oncreate: function (vnode) {
config.keys = ssbKeys.loadOrCreateSync(path.join(config.path, "secret"))
config.remote = `net:127.0.0.1:${config.port}~shs:${config.keys.id.slice(1).replace(".ed25519", "")}`
ssbClient(config, (err, server) => {
if (err) {
console.error("err", err)
if (attemptedToStartBuiltinServer) {
m.route.set("/backup/servernotrunning")
} else {
m.route.set("/backup/startserver")
}
} else {
sbot = server
backup.feed = sbot.id
m.route.set("/backup/getdatafromserver")
}
})
},
view: function (vnode) {
return m(MessageWithIndeterminateProgress, {
content: `Attempting to connect to the running ssb-server...`
})
}
}
const startserver = {
oncreate: function (vnode) {
attemptedToStartBuiltinServer = true
electron.ipcRenderer.send("start-server", "/backup/connect")
},
view: function (vnode) {
return m(MessageWithIndeterminateProgress, {
content: `Attempting to start SSB server...`
})
}
}
/**
* getdatafromserver - phase 4 of backup
*
* Get extra data
* related to the user.
*
* The most important data is the `latestSequence` which will become
* a target for the _restore process_. Of course, if you keep using this
* identity after backing it up, the `latestSequence` inside the backup
* will not reflect the latest sequence in the real world but that is OK
* because the restore process will know that it need to wait at least until
* that number. It will try asking your peers for what they think is the latest
* sequence and compare it with this value.
*/
const getdatafromserver = {
getLatestSequence: function (vnode) {
sbot.latestSequence(sbot.id, function (err, seq) {
if (err) {
m.route.set("/backup/nolatestsequence")
} else {
backup.latestSequence = seq
console.log("backup", backup)
getdatafromserver.getAvatarData()
}
})
},
getAvatarData: () => {
ssbAvatar(sbot, sbot.id, sbot.id, async (err, data) => {
console.log(data)
backup.name = data.name
const imageBlob = await getDataUriFromURL(`http://localhost:8989/blobs/get/${encodeURIComponent(data.image)}`)
backup.avatarImage = imageBlob
console.log("backup", backup)
m.route.set("/backup/summary")
})
},
oncreate: function (vnode) {
return this.getLatestSequence()
},
view: function (vnode) {
m(MessageWithIndeterminateProgress, {
content: `Getting data from ssb-server...`
})
}
}
/**
* summary - phase 5 of backup
*
* Stop and present the user with their avatar and name and let them chose a file
* to use for the backup.
*/
const summary = {
save: function () {
let feedFragment = backup.feed.slice(1, 6)
dialog.showSaveDialog(
{
title: "Save Identity Backup",
buttonLabel: "Save",
defaultPath: `${feedFragment}.ssb-backup`
})
.then((data) => {
const canceled = data.canceled
const filename = data.filePath
if (canceled) {
m.route.set("/backup/cancelled")
} else {
fs.writeFileSync(filename, JSON.stringify(backup, null, 2), "utf8")
console.log("done saving")
m.route.set("/backup/saved")
}
})
.catch(err => {
console.log("error saving backup", err)
m.route.set("/backup/error")
})
},
avatarAndName: function (vnode) {
return m("div", [
m("figure.image.is-128x128", { style: { margin: "auto" } }, m("img.is-rounded", { src: backup.avatarImage })),
m("h3", backup.name || config.keys.public),
])
},
minimalBackup: function (vnode) {
return m.trust(marked(`This is a minimal backup for identity: \`${backup.feed}\``))
},
view: function (vnode) {
let identityCard = backup.avatarImage ? this.avatarAndName() : this.minimalBackup()
return m("div.content.has-text-centered", [
m("h1", "Ready To Save"),
identityCard,
m("button.button.is-primary.is-large", { onclick: summary.save }, "Save")
])
}
}
/**
* cancelled - phase 5.1 of backup
*
* This only happens if the user cancel the file selection in phase 5.
*/
const cancelled = {
view: function (vnode) {
return m(MessageWithButton, {
title: "Backup cancelled",
content: `You cancelled the backup.`,
cta: "Try Again?",
link: "/backup/start"
})
}
}
/**
* saved - phase 6 of backup
*
* Everything was saved and is fine.
*/
const saved = {
view: function (vnode) {
return m("div.content", [
m("h1", "Backup saved ❤"),
m("p", "Backup saved successfully.")
])
}
}
/**
* nosecret - phase 2.1 of backup
*
* This only happens if the SSB installation is broken and we can"t find a secret file.
*/
const nosecret = {
view: function (vnode) {
return m(MessageWithButton, {
title: "Can't backup!!! 🙀",
content: `
There is something deeply wrong here. We can"t find the \`secret\` file used by
SSB to store your identity. It usually is located inside a folder called \`.ssb\`
inside your home folder. Can you verify that the file is there?
If it is there and the backup tool can"t find it, then this is bug in the backup tool. Please,
use the _help_ menu to _submit an issue_ to us so that we can be aware of it and fix it.
`,
cta: "Try Again?",
link: "/backup/start"
})
}
}
/**
* nolatestsequence
*
* This only used if some unknown error happens.
*/
const nolatestsequence = {
view: function (vnode) {
return m(MessageWithButton, {
title: "Can't backup!!! 🙀",
content: `
There is something deeply wrong here. We were able to connect to the running SSB server
but we couldn't ask for the latest sequence. This should never happen. Please submit an issue
using the help menu.
`,
cta: "Try Again?",
link: "/backup/start"
})
}
}
/**
* error
*
* This only used if some unknown error happens.
*/
const error = {
view: function (vnode) {
return m(MessageWithButton, {
title: "Can't backup!!! 🙀",
content: `
There is something deeply wrong here. There was an unrecoverable error. Please submit an issue.
`,
cta: "Try Again?",
link: "/backup/start"
})
}
}
/**
* servernotrunning - phase 3.1 of backup
*
* We've failed to connect to the running SSB server. We still have enough data
* for a minimal backup so we present both options for the user.
*/
const servernotrunning = {
msg: `
Either we can't connect to the SSB server or it is not running. Please, make sure
SSB server is running and then try again.
You can backup with the current data we have but the restore process will be harder as we don't
yet have information about your last sequence number, your name, and your avatar.
`,
view: function (vnode) {
return m("div.content", [
m("h1", "SSB Server Not Running"),
m.trust(marked(this.msg)),
m(m.route.Link, { selector: "a.button.is-primary.is-pulled-right", style: { marginLeft: "10px" }, href: "/backup/connect" }, "Try to connect again"),
m(m.route.Link, { selector: "a.button.is-pulled-right", href: "/backup/summary" }, "Backup without the extra data"),
])
}
}
const BackupView = {
phases: {
start,
checkssbfolder,
connect,
startserver,
getdatafromserver,
summary,
saved,
cancelled,
nosecret,
servernotrunning,
error,
nolatestsequence
},
view: () => {
let phase = m.route.param("key") || "start"
let component = BackupView.phases[phase]
return m(component)
}
}
module.exports = BackupView