This repository has been archived by the owner on Mar 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
cli.js
executable file
·341 lines (298 loc) · 7.08 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
#!/usr/bin/env node
'use strict';
/**
* remote-share-cli
* Quickly share files from command line with the world
* Author: Mario Nebl <https://github.com/marionebl>
* License: MIT
*/
const fs = require('fs');
const http = require('http');
const os = require('os');
const path = require('path');
const util = require('util');
const copyPaste = require('copy-paste');
const chalk = require('chalk');
const localtunnel = require('localtunnel');
const fp = require('lodash/fp');
const meow = require('meow');
const mime = require('mime');
const portscanner = require('portscanner');
const generate = require('project-name-generator');
const log = util.debuglog('remote-share-cli');
const cli = meow(`
Usage
$ remote-share [file]
Options
-n, --name Forced download name of the file
Examples
$ remote-share shared.png
https://mysteriouswomen.localtunnel.me
$ cat shared.png | remote-share --name=shared.png
https://humbleappliance.localtunnel.me
`, {
alias: {
n: 'name'
}
});
let __timer = null;
let connection;
const stdin = process.stdin;
// Kill process after 5 minutes of inactivity
function resetTimer() {
log('Reseting activitiy timer');
killTimer();
setTimer();
}
function setTimer() {
log('Setting activitiy timer, timing out after 5 minutes');
__timer = setTimeout(() => {
log('Timeout without after 5 minutes without activitiy, killing process');
process.exit(0);
}, 300000);
}
function killTimer() {
if (__timer) {
clearTimeout(__timer);
}
}
function tunnel(port, options) {
return new Promise((resolve, reject) => {
localtunnel(port, options, (error, connection) => {
if (error) {
return reject(error);
}
resolve(connection);
});
});
}
/**
* Copy input to clipboard
* @param {String} input
* @return {Promise<String>}
*/
function copy(input) {
return new Promise((resolve, reject) => {
copyPaste.copy(`${input}\r\n`, (error) => {
if (error) {
return reject(error);
}
resolve(input);
});
});
}
/**
* Check if an ip adress is external
*
* @return {Boolean}
*/
function isExternalAddress(networkInterface) {
return networkInterface.family === 'IPv4' &&
networkInterface.internal === false;
}
/**
* Get the local ip addresses
*
* @return {Object[]}
*/
function getAdresses() {
return fp.flatten(fp.values(os.networkInterfaces()));
}
/**
* Get the local ip address
*
* @return {String}
*/
function getLocalAddress() {
const found = fp.find(isExternalAddress)(getAdresses());
return found ? found.address : 'localhost';
}
/**
* Get an open port on localhost
*
* @return {Promise<Number>}
*/
function getOpenPort() {
return new Promise((resolve, reject) => {
portscanner.findAPortNotInUse(1337, 65535, '127.0.0.1', (error, port) => {
if (error) {
return reject(error);
}
resolve(port);
});
});
}
/**
* Get a file object
*
* @param {Object} options
* @param {Boolean} options.isStdin - If the input is given via stdin
* @param {String} [options.filePath] - Absolute path of file to read
* @param {String} [options.fileName] - Basename of file to read, defaults to path.basename(option.filePath)
*
* @return {Promise<File>}
*/
function getFile(options) {
return new Promise((resolve, reject) => {
const stream = options.isStdin ?
stdin : fs.createReadStream(options.filePath);
const name = options.isStdin ?
options.fileName : path.basename(options.filePath);
if (options.isStdin) {
return resolve({
stream,
name,
size: null,
ino: null,
mtime: null
});
}
fs.stat(options.filePath, (error, stat) => {
if (error) {
return reject(error);
}
resolve({
stream,
name,
size: stat.size,
ino: stat.ino,
mtime: Date.parse(stat.mtime)
});
});
});
}
/**
* Serve a File object on address with port on path id
*
* @param {Object} options
* @param {File} options.file
* @param {Number} options.port
* @param {String} options.address
* @param {String} options.id
* @return {Promise<Object>} - started server instance
*/
function serve(options) {
return new Promise((resolve, reject) => {
const file = options.file;
const downloadName = file.name || options.id;
const subdomain = options.id.split('-').join('').slice(0, 20);
const server = http.createServer((request, response) => {
// Only HEAD and GET are allowed
if (['GET', 'HEAD'].indexOf(request.method) === -1) {
response.writeHead(405);
return response.end('Method not Allowed.');
}
resetTimer();
response.setHeader('Content-Type', mime.lookup(downloadName));
response.setHeader('Content-Disposition', `attachment; filename=${downloadName}`);
if (file.size) {
response.setHeader('Content-Length', file.size);
}
// Do not send a body for HEAD requests
if (request.method === 'HEAD') {
response.setHeader('Connection', 'close');
return response.end();
}
const start = new Date();
file.stream.on('data', () => {
resetTimer();
});
file.stream.pipe(response)
.on('finish', () => {
const duration = new Date() - start;
const ttl = duration / 2;
// Give the downloader the half absolute download time
// to get remaining data from localtunnel.me
setTimeout(() => {
log(`Download completed after ${duration}ms, killing process in ${ttl}ms`);
process.exit(0);
}, ttl);
});
});
server.on('error', reject);
server.listen(options.port, () => {
tunnel(options.port, {
subdomain
})
.then(tunneled => {
connection = tunneled;
return connection;
})
.then(resolve)
.catch(reject);
});
});
}
/**
* Serve a File object on an open port
*
* @param {File} file
* @return {Promise<String>} - shareable address
*/
function serveFile(file) {
return getOpenPort()
.then(port => {
const address = getLocalAddress();
const id = generate().dashed;
const options = {
address,
port,
id,
file
};
return serve(options)
.then(connection => copy(connection.url));
});
}
/**
* Execute remote-share-cli main procedure
*
* @param {String[]} input - non-flag arguments
* @param {Object} args - flag arguments
* @return {Promise<String>} - shareable address
*/
function main(filePath, args) {
return new Promise((resolve, reject) => {
// Start the inactivity timer
setTimer();
// Sanitize input
if (stdin.isTTY && typeof filePath === 'undefined') {
const error = new Error('Either stdin or [file] have to be given');
error.cli = true;
return reject(error);
}
const isStdin = stdin.isTTY !== true && typeof filePath === 'undefined';
// Get a file object
const gettingFile = getFile({
isStdin,
filePath,
fileName: args.name
});
gettingFile
.then(serveFile)
.then(resolve)
.catch(reject);
});
}
main(cli.input[0], cli.flags)
.then(output => {
if (output) {
console.log(output);
}
})
.catch(error => {
if (error.cli) {
if (error.message) {
console.error(chalk.red(error.message));
}
cli.showHelp(1);
}
setTimeout(() => {
throw error;
});
});
/**
* @typedef {Object} File
* @property {Stream} File.stream - Readstream of the file
* @property {String} File.name - Basename of the file
*/