This repository has been archived by the owner on Nov 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·444 lines (410 loc) · 13.8 KB
/
cli.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#!/usr/bin/env node
const cli = require('commander')
const pkg = require('./package.json')
const Datagram = require('./src')
const { generateUser, fromB58 } = require('./src/utils')
const fs = require('fs-extra')
const path = require('path')
const chokidar = require('chokidar')
let dg = null
let watcher = null
function error(err) {
console.error(err)
process.exit()
}
process.on('uncaughtException', function(err) {
error(err)
})
// Cleaning up
async function cleanUp() {
watcher.close()
await dg.disconnect()
}
if (process.platform === 'win32') {
var rl = require('readline').createInterface({
input: process.stdin,
output: process.stdout,
})
rl.on('SIGINT', function() {
process.emit('SIGINT')
})
}
process.on('SIGINT', async () => {
cleanUp()
console.log('\nStopped sharing and disconnected all downloaders')
process.exit()
})
function openUserFile(filename) {
const ufile = fs.readFileSync(filename + '.user.dg', 'UTF-8')
if (ufile) {
const u = ufile.split('/')
if (u.length === 2 && u[0].length > 0 && u[1].length > 0) {
return {
id: u[0].trim(),
password: u[1].trim(),
}
} else return error('invalid file')
} else return error('no file found at ' + filename + '.user.dg')
}
function openCredsFile(filename) {
const ufile = fs.readFileSync(filename + '.creds.dg', 'UTF-8')
if (ufile) {
const u = ufile.split('/')
if (u.length === 2 && u[0].length > 0 && u[1].length > 0) {
return {
read: u[0].trim(),
encryption_password: u[1].trim(),
}
} else return error('invalid file')
} else return error('no file found at ' + filename + '.creds.dg')
}
function generateArgs(options) {
let args = {}
if (options.userfile) {
args = options.userfile
} else if (options.id && options.password) {
args = {
id: options.id,
password: options.password,
}
} else {
return error('insufficient user credentials provided')
}
if (options.datagram) {
args = { ...args, keys: { ...options.datagram } }
} else if (options.address && options.encryption) {
args = {
...args,
keys: {
read: options.address,
encryption_password: options.encryption,
},
}
} else if (options._name === 'share') {
return error('datagram address and encryption password required for sharing')
}
args.type = 'fs'
return args
}
cli.version(pkg.version)
// Generate user credentials
cli.command('user [filename]').description('Generate new user credentials to a file').action(async (filename) => {
if (!filename) return error('filename missing')
const user = await generateUser().catch(error)
fs.writeFileSync(filename + '.user.dg', `${user.id}/${user.password}`)
console.log(`User: ${user.id}\nPassword: ${user.password}\n\nStored at ${filename}.user.dg`)
process.exit()
})
// Create new
const create = async (filename, options) => {
if (!filename) return error('filename missing')
return new Promise(async (done, error) => {
const args = generateArgs(options)
const DG = new Datagram(args, args.keys || null)
dg = await DG.ready()
const keys = await dg.getKeys()
fs.writeFileSync(filename + '.creds.dg', `${keys.read}/${keys.encryption_password}`)
console.log(`Created new Datagram\nLocal address: ${keys.read}\nEncryption password: ${keys.encryption_password}`)
done(dg)
process.exit()
})
}
cli
.command('create [filename]')
.description('Generates new Datagram credentials to a file')
.option('-u --userfile [credentials_file]', 'User file', openUserFile)
.option('-i --id [id]', 'User id')
.option('-p --pass [password]', 'User password')
.action(create)
// Clone remote
const clone = async (options) => {
return new Promise(async (done, error) => {
if (!options.sharelink) return error('sharelink missing')
const args = {
...generateArgs(options),
sharelink: options.sharelink,
realtime: true,
full_sync: options.fullsync || false,
host: options.host || false,
}
try {
console.log('Connecting to the share...')
if(args.host) {
console.log('Host mode activated')
}
const DG = new Datagram(args || null)
dg = await DG.ready()
console.log('Real-time cloning started (stop by pressing CTRL+C)')
done(dg)
} catch (e) {
error(e)
}
})
}
cli
.command('clone')
.option('-u --userfile [credentials_file]', 'User file', openUserFile)
.option('-i --id [id]', 'User id')
.option('-p --pass [password]', 'User password')
.option('-l --sharelink [sharelink]', 'Sharelink')
.option('--fullsync [boolean]', 'Syncs everything')
.option('--host [boolean]', 'Partipates in the hosting')
.action(clone)
// Host
cli
.command('host')
.option('-u --userfile [credentials_file]', 'User file', openUserFile)
.option('-i --id [id]', 'User id')
.option('-p --pass [password]', 'User password')
.option('-l --sharelink [sharelink]', 'Sharelink')
.action(async (options) => {
options.fullsync = true
options.host = true
await clone(options)
})
// Share
async function createShare(dg) {
return new Promise(async (done, error) => {
const sharelink = await dg.share({ realtime: true })
console.log('Share started (stop by pressing CTRL+C)')
const settings = await dg.getSettings()
const keys = await dg.getKeys()
// Monitor for file adds
const db_path = `${settings.path}${fromB58(keys.read).toString('hex')}`
watcher = chokidar.watch(db_path, { persistent: true }).on('change', async (event, path) => {
console.log('New data added, restarting the share...')
cleanUp()
await createShare(dg)
})
done(sharelink)
})
}
const share = async (options) => {
const args = generateArgs(options)
if (!args.keys) {
const dg_keys = await dg.getKeys()
if (dg_keys) args.keys = dg_keys
else return error('datagram address is required for sharing')
}
const DG = new Datagram(args, args.keys)
dg = await DG.ready()
const sharelink = await createShare(dg)
console.log(`Sharelink: ${sharelink}`)
}
cli
.command('share')
.option('-u --userfile [credentials_file]', 'User file', openUserFile)
.option('-i --id [id]', 'User id')
.option('-p --pass [password]', 'User password')
.option('-a --address [address]', "Datagram's local address")
.option('-e --encryption [encryption_password]', "Datagram's encryption password")
.option('-d --datagram [dg_filename]', 'Datagram credentials file', openCredsFile)
.action(share)
// Add files
const add = async (filename, options) => {
if (!filename) return error('filename missing')
if (!await fs.exists(filename)) return error('non-existing file')
const args = generateArgs(options)
const DG = new Datagram(args, args.keys)
dg = await DG.ready()
const f = path.parse(filename)
const p = path.normalize(filename)
console.log(`Importing ${f.base}...`)
const file = await fs.readFile(p)
await dg.write(f.base, file)
console.log('...done')
process.exit()
}
cli
.command('add [filename]')
.description('Add new file to a datagram')
.option('-u --userfile [credentials_file]', 'User file', openUserFile)
.option('-i --id [id]', 'User id')
.option('-p --pass [password]', 'User password')
.option('-a --address [address]', "Datagram's local address")
.option('-e --encryption [encryption_password]', "Datagram's encryption password")
.option('-d --datagram [dg_filename]', 'Datagram credentials file', openCredsFile)
.action(add)
// List
const list = async (options) => {
const args = {
...generateArgs(options),
sharelink: options.sharelink,
realtime: false,
full_sync: options.fullsync || false,
host: options.host || false,
}
if (options.sharelink) {
console.log('Sharelink provided, connecting to remote datagrams...')
}
const DG = new Datagram(args, args.keys)
dg = await DG.ready()
const ls = await dg.ls()
ls.sort().forEach(f => console.log(f))
process.exit()
}
cli
.command('list')
.description('List all files')
.option('-u --userfile [credentials_file]', 'User file', openUserFile)
.option('-i --id [id]', 'User id')
.option('-p --pass [password]', 'User password')
.option('-a --address [address]', "Datagram's local address")
.option('-e --encryption [encryption_password]', "Datagram's encryption password")
.option('-d --datagram [dg_filename]', 'Datagram credentials file', openCredsFile)
.option('-l --sharelink [sharelink]', 'Sharelink')
.action(list)
// Search
// Export data
const exprt = async (data_name, target_file, options) => {
if (!data_name) return error('data_name missing')
if (!target_file) return error('target_file missing')
const args = {
...generateArgs(options),
sharelink: options.sharelink,
realtime: false,
full_sync: options.fullsync || false,
host: options.host || false,
}
if (options.sharelink) {
console.log('Sharelink provided, connecting to remote datagrams...')
}
const DG = new Datagram(args, args.keys)
dg = await DG.ready()
const data = await dg.read(data_name)
if (data) {
const f = path.parse(target_file)
const p = path.normalize(target_file)
console.log(`Data found, exporting to ${f.base}...`)
await fs.writeFile(p, data)
console.log('Export done')
process.exit()
} else {
console.log(`no data found with data name ${data_name}`)
process.exit()
}
}
cli
.command('export [data_name] [target_file]')
.description('Export data')
.option('-u --userfile [credentials_file]', 'User file', openUserFile)
.option('-i --id [id]', 'User id')
.option('-p --pass [password]', 'User password')
.option('-a --address [address]', "Datagram's local address")
.option('-e --encryption [encryption_password]', "Datagram's encryption password")
.option('-d --datagram [dg_filename]', 'Datagram credentials file', openCredsFile)
.option('-l --sharelink [sharelink]', 'Sharelink')
.action(exprt)
// Get stats
// Get authorization token
const authtoken = async (options) => {
const args = {
...generateArgs(options)
}
const DG = new Datagram(args, args.keys)
dg = await DG.ready()
const auth_token = await dg.getAuthToken()
console.log(`Authorization token: ${auth_token}`)
process.exit()
}
cli
.command('authtoken')
.description('Get authorization token')
.option('-u --userfile [credentials_file]', 'User file', openUserFile)
.option('-i --id [id]', 'User id')
.option('-p --pass [password]', 'User password')
.option('-a --address [address]', "Datagram's local address")
.option('-e --encryption [encryption_password]', "Datagram's encryption password")
.option('-d --datagram [dg_filename]', 'Datagram credentials file', openCredsFile)
.action(authtoken)
// Authorize another datagram
const authdevice = async (authorization_token, options) => {
const args = {
...generateArgs(options)
}
if (!authorization_token) {
console.log('Authorization token missing')
process.exit()
}
const DG = new Datagram(args, args.keys)
dg = await DG.ready()
const a = await dg.authorizeDevice({ auth_token: authorization_token })
console.log(`Authorization for ${authorization_token} done`)
process.exit()
}
cli
.command('authdevice [authorization_token]')
.description('Authorize another device')
.option('-u --userfile [credentials_file]', 'User file', openUserFile)
.option('-i --id [id]', 'User id')
.option('-p --pass [password]', 'User password')
.option('-a --address [address]', "Datagram's local address")
.option('-e --encryption [encryption_password]', "Datagram's encryption password")
.option('-d --datagram [dg_filename]', 'Datagram credentials file', openCredsFile)
.action(authdevice)
// const repl = async (options) => {
// let args = generateArgs(options)
// let dg = null
// if (options.sharelink) {
// console.log('Cloning...')
// options.realtime = true
// dg = await clone(options)
// } else if (!options.sharelink && options.address && options.encryption) {
// console.log('Opening...')
// console.log(args)
// dg = await create(options, args.keys)
// } else {
// console.log('Creating...')
// dg = await create(options)
// }
// const readline = require('readline')
// const rl = readline.createInterface({
// input: process.stdin,
// output: process.stdout,
// })
// function d(data) {
// rl.write(data)
// }
// async function run() {
// rl.question('λ ', async (code) => {
// if (code) {
// try {
// if (code === 'help') {
// console.log('Available commands', Object.keys(dg))
// await run()
// } else if (code.match(/share\(/)) {
// options = { realtime: true, ...options }
// const sharelink = await dg.share({ realtime: true })
// console.log(`Sharelink: ${sharelink}`)
// await run()
// } else if (code.match(/exit/)) {
// process.exit()
// } else if (code.match(/debug\(/)) {
// eval(`dg.${code}`)
// await run()
// } else {
// eval(
// `dg.${code}.then(async (d) => { console.log(d||'done'); await run(); }).catch(async (e) => { console.log(e); await run(); })`,
// ) // + '.then(d)')
// }
// } catch (e) {
// console.error(e)
// await run()
// }
// } else await run()
// })
// }
// run()
// }
// cli
// .command('repl')
// .option('-u --userfile [credentials_file]', 'User file', openUserFile)
// .option('-i --id [id]', 'User id')
// .option('-p --pass [password]', 'User password')
// .option('-l --sharelink [sharelink]', 'Sharelink')
// .option('-a --address [address]', "Datagram's local address")
// .option('-e --encryption [encryption_password]', "Datagram's encryption password")
// .option('--fullsync [boolean]', 'Syncs everything')
// .option('--host [boolean]', 'Partipates in the hosting')
// .action(repl)
cli.parse(process.argv)