-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathonionfile.js
executable file
·28 lines (23 loc) · 962 Bytes
/
onionfile.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
#!/usr/bin/env node
const fs = require('fs')
, path = require('path')
, hsv3 = require('@deadcanaries/hsv3')
, app = require('express')()
, cli = require('meow')('Usage: onionfile <path>')
, tdir = cli.flags.tordir || path.join(require('os').tmpdir(), 'onionfile-' + Math.random().toString(36).substr(2))
cli.input[0] || cli.showHelp()
const file = path.resolve(cli.input[0])
, stat = fs.statSync(file)
app.disable('x-powered-by')
app.use(require('morgan')('dev'))
if (stat.isDirectory()) {
app.use(require('express').static(file))
app.use(require('serve-index')(file))
} else {
app.get('/', (req, res) => res.sendFile(file))
}
const server = app.listen(0, '127.0.0.1', _ =>
hsv3([ { dataDirectory: tdir, virtualPort: 80, localMapping: '127.0.0.1:' + server.address().port } ])
.on('error', console.error)
.on('ready', () => console.info('http://' + fs.readFileSync(path.join(tdir, 'hostname')).toString().trim()))
)